Skip to content

Instantly share code, notes, and snippets.

@codeBelt
Created September 19, 2013 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeBelt/6618777 to your computer and use it in GitHub Desktop.
Save codeBelt/6618777 to your computer and use it in GitHub Desktop.
TypeScript Signal Test
///<reference path='com/millermedeiros/signals/Signal.ts'/>
import Signal = millermedeiros.Signal;
/**
* YUIDoc_comment
*
* @class SignalTest
* @constructor
**/
class SignalTest {
public CLASS_NAME:string = 'SignalTest';
myObject:any = {};
foo = null;
constructor() {
this.myObject = {
started : new Signal(), //past tense is the recommended signal naming convention
stopped : new Signal()
};
//https://github.com/millermedeiros/js-signals/wiki/Examples
// Single Listener
/*this.myObject.started.add(this.onStarted); //add listener
this.myObject.started.dispatch('foo', 'bar'); //dispatch signal passing custom parameters
this.myObject.started.remove(this.onStarted); //remove a single listener*/
// Multiple Listeners
/*this.myObject.stopped.add(this.onStopped);
this.myObject.stopped.add(this.onStopped2);
this.myObject.stopped.dispatch();
this.myObject.stopped.removeAll(); //remove all listeners of the `stopped` signal*/
// Multiple Dispatches
/*var i = 0;
this.myObject.started.add(function(){
i += 1;
console.log("anonymous", i);
});
this.myObject.started.dispatch(); //will alert 1
this.myObject.started.dispatch(); //will alert 2*/
// Multiple Dispatches + addOnce()
/*var i = 0;
this.myObject.started.addOnce(function(){
i += 1;
console.log("addOnce", i);
});
this.myObject.started.dispatch(); //will alert 1
this.myObject.started.dispatch(); //nothing happens*/
// Enable/Disable Signal
/*var i = 0;
this.myObject.started.add(function(){
i += 1;
console.log("Enable/Disable Signal", i);
});
this.myObject.started.dispatch(); //will alert 1
this.myObject.started.active = false;
this.myObject.started.dispatch(); //nothing happens
this.myObject.started.active = true;
this.myObject.started.dispatch(); //will alert 2*/
// Stop/Halt Propagation (method 1)
/*this.myObject.started.add(function(){
this.myObject.started.halt(); //prevent next listeners on the queue from being executed
}.bind(this));
this.myObject.started.add(function(){
console.log('second listener'); //won't be called since first listener stops propagation
});
this.myObject.started.dispatch();*/
// Stop/Halt Propagation (method 2)
/*this.myObject.started.add(function(){
return false; //if handler returns `false` will also stop propagation
});
this.myObject.started.add(function(){
console.log('second listener'); //won't be called since first listener stops propagation
});
this.myObject.started.dispatch();*/
// Set execution context of the listener handler
/*this.foo = 'bar';
var obj = {
foo : 10
};
//note that you cannot add the same handler twice to the same signal without removing it first
this.myObject.started.add(this.handler1.bind(this)); //default execution context
this.myObject.started.add(this.handler2, obj); //set a different execution context
this.myObject.started.dispatch(); //first handler will alert "bar", second will alert "10".*/
// Set listener priority/order (v0.5.3+)
/*var handler1 = function(){
console.log('handler1');
};
var handler2 = function(){
console.log('handler2');
};
this.myObject.started.add(handler1); //default priority is 0
this.myObject.started.add(handler2, null, 1); //setting priority to 1 will make `handler2` execute before `handler1`
this.myObject.started.dispatch(); //will alert "bar" than "foo"*/
// Enable/Disable a single SignalBinding
/*var handler1 = function(){
console.log('foo bar');
};
var handler2 = function(){
console.log('lorem ipsum');
};
var binding1 = this.myObject.started.add(handler1); //methods `add()` and `addOnce()` returns a SignalBinding object
this.myObject.started.add(handler2);
this.myObject.started.dispatch(); //will alert "foo bar" than "lorem ipsum"
binding1.active = false; //disable a single binding
this.myObject.started.dispatch(); //will alert "lorem ipsum"
binding1.active = true;
this.myObject.started.dispatch(); //will alert "foo bar" than "lorem ipsum"*/
// Manually execute a signal handler
/*var handler = function(){
console.log('foo bar');
};
var binding = this.myObject.started.add(handler); //methods `add()` and `addOnce()` returns a SignalBinding object
binding.execute(); //will alert "foo bar"*/
// Retrieve anonymous listener
/*var binding = this.myObject.started.add(function(){
console.log('foo bar');
});
var handler = binding.getListener(); //reference to the anonymous function
console.log(handler);*/
// Remove / Detach anonymous listener
/*var binding = this.myObject.started.add(function(){
console.log('foo bar');
});
this.myObject.started.dispatch(); //will console.log "foo bar"
binding.detach();
console.log(binding.isBound()); //will console.log `false`
this.myObject.started.dispatch(); //nothing happens*/
// Check if binding will execute only once
/*var binding1 = this.myObject.started.add(function(){
console.log('foo bar');
});
var binding2 = this.myObject.started.addOnce(function(){
console.log('foo bar');
});
console.log(binding1.isOnce()); //console.log "false"
console.log(binding2.isOnce()); //console.log "true"*/
// Change listener execution context on-the-fly
/*var foo = 'bar';
var obj = {
foo : "it's over 9000!"
};
var binding = this.myObject.started.add(function(){
console.log(this.foo);
});
this.myObject.started.dispatch(); //will console.log "bar"
binding.context = obj;
this.myObject.started.dispatch(); //will console.log "it's over 9000!"*/
// Add default parameters to Signal dispatch (v0.6.3+)
/*var binding = this.myObject.started.add(function(a, b, c){
console.log(a +' '+ b +' '+ c);
});
binding.params = ['lorem', 'ipsum']; //set default parameters of the binding
this.myObject.started.dispatch('dolor'); //will console.log "lorem ipsum dolor"
//Check if Signal has specific listener (v0.7.0+)
function onStart(a){
console.log(a);
}
this.myObject.started.add(onStart);
this.myObject.started.has(onStart); // true*/
//Memorize previously dispatched values / forget values (v0.7.0+)
/* this.myObject.started.memorize = true; // default is false
this.myObject.started.dispatch('foo');
// add()/addOnce() will automatically fire listener if signal was dispatched before
// will log "foo" since it keeps record of previously dispatched values
this.myObject.started.addOnce(console.log, console);
// dispatching a new value will overwrite the "memory"
this.myObject.started.dispatch('lorem');
// will log "lorem"
this.myObject.started.addOnce(console.log, console);
this.myObject.started.forget(); // forget previously dispatched values (reset signal state)
this.myObject.started.addOnce(console.log, console); // won't log till next dispatch (since it "forgot")
this.myObject.started.dispatch('bar'); // log "bar"*/
}
onStarted(param1, param2){
console.log(param1 + param2);
}
onStopped(){
console.log('stopped');
}
onStopped2(){
console.log('stopped listener 2');
}
handler1(){
console.log(this.foo);
}
handler2(){
console.log(this.foo);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment