Giter VIP home page Giter VIP logo

Comments (5)

tommedema avatar tommedema commented on June 7, 2024

I implemented this myself, maybe you can improve it?

eventemitter2.chains.js

if (!EventEmitter2) throw 'EventEmitter2 unknown.';

//unordered event chain (emitted once)
EventEmitter2.prototype.oncechain = function() {
    var emitter = this;

    //get arguments array
    var args = Array.prototype.slice.call(arguments);

    //get callback
    var cb = args.pop();

    //keep track of events
    var events = {};

    //checks if we are ready to go
    var checkPending = function() {
        for (var event in events) {
            if (events.hasOwnProperty(event) && events[event].pending) {
                return;
            }
        }

        //all events fired
        var evargs = [];
        for (var event in events) {
            if (events.hasOwnProperty(event)) {
                evargs = evargs.concat(event.args);
            }
        }

        //done
        cb.apply(this, evargs);
    };

    //listen to all events once
    args.forEach(function(event) {
        events[event] = {
            pending: true,
            args: null
        };
        emitter.once(event, function() {
            events[event].pending = false;
            events[event].args = Array.prototype.slice.call(arguments);
            checkPending();
        });
    });

};

Usage: emitter.oncechain('ui.body.hidden', 'document.ready', function() { });

from eventemitter2.

tommedema avatar tommedema commented on June 7, 2024

Improved by @Raynos with help of after module:

function onceChain() {
    var args = [].slice.call(arguments);

    var ee2 = args.unshift();
    var callback = args.pop();

    after.map(args, function (eventName, name, callback) {
        ee2.once(eventName, function (item) {
            callback(null, item);
        });
    }, function (err, data) {
        callback(err, data);
    });
}

onceChain(ee2, 'server.configured', 'socketio.configured', function (err, data) {
    data[0].listen();
});

from eventemitter2.

Raynos avatar Raynos commented on June 7, 2024

Note that an implementation of after.map should be in-lined if it ever went into EventEmitter2 core.

Personally I don't think this piece of "flow control" utility should go into eventemitter2 itself.

Also after.map isn't documented but it's implemented here

from eventemitter2.

tommedema avatar tommedema commented on June 7, 2024

Maybe a monkey patching module is an idea? All you'd need to do at process startup:

require('eventemitter2');
require('eventemitter2.flow');

from eventemitter2.

heapwolf avatar heapwolf commented on June 7, 2024

This is cool. I'd love to see a library on top of ee2 that did some fancy stuff like this. eventslib or something.

from eventemitter2.

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.