1

I want to create my own signal emitter or callback storage, call it whatever you want.

Here is what I come up so far:

var DetonationCallback = function detonationCallback() {
    detonationCallback.callbacks = [];
    detonationCallback.add = function(callback) {
        detonationCallback.callbacks.push(callback);
    };
    for(var i = 0; i < detonationCallback.callbacks.length; ++i) {
        callback[i](arguments);
    }
};

Basically I have two problems now which I can not tackle. The first one is how can I move

    detonationCallback.callbacks = [];
    detonationCallback.add = function(callback) {
        detonationCallback.callbacks.push(callback);
    };

outside of the function? And another question is how can I pass all the arguments which were passed into the detonationCallback just in the same order into callback[i]?

Will be grateful for any answer.

Additional info: when done with the implementation of the type I would like to use it as follows:

var callback = new DetonationCallback();

function f1() {

}

function f2(firstArg, secondArg) {

}

callback.add(f1);
callback.add(f2);

callback();
1
  • You can't really do this with the new keyword. The new keyword returns a new instance of DetonationCalback. You can't return anything if you're calling a constructor. Commented Mar 22, 2018 at 15:18

3 Answers 3

1

First, make detonationCallback an Object

var detonationCallbacks = {};
detonationCallbacks.callbacks = [];
detonationCallback.clear = function() {
    detonationCallback.callbacks = [];
}
detonationCallback.add = function(callback) {
    detonationCallback.callbacks.push(callback);
};

Second, make your detonate a function in detonationCallback

detonationCallback.detonate = function() {
    for(var i = 0; i < detonationCallback.callbacks.length; ++i) {
        this.callback[i](arguments);
    }
};

If you want/need to use new, just create an object prototype for this

Sign up to request clarification or add additional context in comments.

2 Comments

Sounds good. But I still have a problem with passing parameters into callback[i].
@yrslvrtfmv you can just use arguments: jtricks.com/javascript_tutorials/varargs.html
1

I'm not sure why you wan't to move the add function outside but how about using a class instead?

Also note the use of .apply() to send several arguments with an array.

class DetonationCallback {
    constructor() {
        this.callbacks = [];
    }
      
    add(callback) {
        this.callbacks.push(callback);
    }
      
    call(args) {
        this.callbacks.forEach(function(callback) {
            callback.apply(this, args);
        });
    }
}

var callback = function(arg1, arg2) {
    console.log(arg1, arg2);
}

var callback2 = function(arg1) {
    console.log(arg1);
}

var handler = new DetonationCallback();

handler.add(callback);
handler.add(callback2);
handler.call(['arg1', 'arg2']);

Comments

0

No need to write this yourself:

When using node you can use the built in EventEmitter lib.
https://nodejs.org/api/events.html

There is also a port for the Browser
https://github.com/Olical/EventEmitter

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.