observ-emitter
This is a wrapper around atomic emitter, similar to Raynos' geval, with the following changes:
- proxies all arguments of the emit method to listeners
- allows you to register listeners for change events of the emitter (e.g. when a listener is added/removed)
Usage
var emitter = var remove = emitter emitter emitter
This will output:
listeners changed!
emitted 42
listeners changed!
Example
You get an atomic value that you can pass around to add listeners and emit events. Render functions only get the emitter function, clearly cementing the unidirectional data flow -- you can't add listeners in your render functions.
The following example demonstrates the main purpose of observ-emitter: The Counter component is extensible and allows the CounterIncrement plugin to add a button that increments the counter value.
var ObservStruct = ObservValue = ObservEmitter = var vdom = h = vdomh { return } Counter { apphooksincrease} Counter { return } { return children} // This is a plugin that extends the Counter component with a button for the user to increase// the counter value. { statehooksrender} CounterIncrement { return } // Main loop { var state = component hooks var tree = component node = vdom return node}
API
Emitter(): Emitter
Create an event emitter.
emitter(onchange:Function)
Listen to changes in listeners (i.e. onchange
gets called when a listener is added or removed).
emitter.listen(listener:Function)
Add an event listener.
emitter.emit(args...)
Emit the event.
Meta
This is raw meat! It's a first implementation of an idea. If this doesn't work for you, or you would like to report a problem (be it philosophical or technical) please file an issue!
Legal
(c) 2015 by Marcel Klehr
MIT License