millermedeiros / js-signals

Custom Event/Messaging system for JavaScript inspired by AS3-Signals
http://millermedeiros.github.com/js-signals/
1.97k stars 179 forks source link

Memory Size #55

Closed jeffrose closed 11 years ago

jeffrose commented 11 years ago

I was looking to migrate my current pub-sub system from a $.Callbacks implementation to one based on Signals in the hope of having a more robust solution. So far everything seems fine except for memorization.

$.Callbacks have a long-term memory...

// $.Callbacks implementation
Topic( 'foo' ).publish( 'bar' );
Topic( 'foo' ).publish( 'baz' );
Topic( 'foo' ).subscribe( console.log );
//--> "bar"
//--> "baz"

... whereas Signals seem to have a short-term memory...

// Signals implementation
// Setting memorize to true behind the scenes
Topic( 'foo' ).publish( 'bar' );
Topic( 'foo' ).publish( 'baz' );
Topic( 'foo' ).subscribe( console.log );
//--> "baz"

Have you considered adding a memory size option or similar to allow more dispatches to memorized?

millermedeiros commented 11 years ago

I followed the least astonishment principle. If we add something like memorySize what is the desired behavior with multiple listeners?

var initialized = new Signal();
iniltialized.memorize = true;
initialized.memorySize = -1; // lets suppose this feature exists
initialized.dispatch('lorem');
initialized.dispatch('ipsum');
initialized.add(console.log, console);
// > "lorem"
// > "ipsum"
// should `echo` be called once or twice?
initialized.add(echo);

I can see the use for a feature like that - specially if you want to implement something like a Stream (and don't want to lose any data) - but it would increase complexity a little bit. Any special use case for it that justifies the overhead?