Search code examples
node.jsrefluxjs

Not sure what .listen() in refluxjs does exactly


In refluxjs I'm not sure what .listen() does. From my understanding, it has the same concepts as nodejs eventemitter but reflux wraps in its own way. I can't seem to find documentation on this anywhere. Maybe I missed it. I would like to find .listen() in the source code or documentation so I know exactly how refluxjs uses it.


Solution

  • Did you try the README? There's a whole section on it: Listening to changes in data store.

    Listening to changes in data store

    In your component, register to listen to changes in your data store like this:

    // Fairly simple view component that outputs to console
    function ConsoleComponent() {
    
        // Registers a console logging callback to the statusStore updates
        statusStore.listen(function(status) {
            console.log('status: ', status);
        });
    };
    
    var consoleComponent = new ConsoleComponent();
    

    Invoke actions as if they were functions:

    statusUpdate(true);
    statusUpdate(false);
    

    With the setup above this will output the following in the console:

    status:  ONLINE
    status:  OFFLINE
    

    And yes, its semantics are pretty much like EventEmitter; it uses eventemitter3 under the hood. listen itself is defined in PublisherMethods.js.