Giter VIP home page Giter VIP logo

Comments (5)

goatslacker avatar goatslacker commented on May 16, 2024

This is a good point. One pattern that I've adopted is to have a store that waits for every store, I then register a listener on that store and do snapshots there.

class SnapshotStore {
  constructor() {
    this.bindActions(whateverActions)
  }

  onAction() {
    this.waitFor(['Store1', 'Store2', 'Store3', 'Store4'])
  }
}

var snapshotStore = alt.createStore(SnapshotStore)

snapshotStore.listen(() => alt.takeSnapshot())

Funny you mention this, I have some code sitting in a local branch that does something like this. Here's the proposed change which would register a listener on the alt instance for those actions called:

alt.addActionListener(whateverActions.action, () => {
  alt.takeSnapshot()
})

from alt.

irium avatar irium commented on May 16, 2024

I've seen that branch listen-to-actions. But that's not a solution, because it uses internally dispatcher.register(...) and this doesn't guarantee calling order of listeners - such listener is just one more between others.

You solution with SnapshotStore looks interesting, but bug-prone - I have to remember to include every new store there.

For now, I'm using something like this:

class Flux extends Alt {

    constructor() {
        super();
        this.dispatcher.dispatch = _.wrap(this.dispatcher.dispatch, (dispatch, payload) => {
            dispatch.call(this.dispatcher, payload);
            this.onAction(payload);
        });

        this.storage = new Storage('state');
    }

    onAction(payload) {
        var state = this.takeSnapshot();
        this.storage.writeRaw(state);
    }
}

But this feels somewhat hacky, henсe my proposal. I guess it could be usefull to have official public API for this.

from alt.

goatslacker avatar goatslacker commented on May 16, 2024

Ah, I see what you're looking for. I also wish I had the same thing, essentially a "done" handler for when all dispatches have been completed.

This needs to be provided by flux's dispatcher.

For now your workaround, although hacky, is fine.

from alt.

goatslacker avatar goatslacker commented on May 16, 2024

A similar discussion: facebookarchive/flux#138 (comment)

from alt.

goatslacker avatar goatslacker commented on May 16, 2024

@irium

I went ahead and added a helper function for the SnapshotStore discussed above. You mentioned you didn't want to keep track of including every store in there so I made sure it's automatically done.

The commit is here: c104fb7

Usage is:

var makeFinalStore = require('alt/utils/makeFinalStore');
var FinalStore = makeFinalStore(yourAltInstance)

FinalStore.listen(function () {
  console.log('All dispatches have been completed')
})

from alt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.