certiorem
An implementation of the observer pattern in node.js with event emitter. 0 dependencies and usage is very straight forward and declarative.
Usage
you have two functions in certiorem, one is called createObservable
and the other is called createObserver
.
createObservable
creates a new instance of a parent class that will hold all the observers and notify them on events emitted, the createObserver
creates a new instance of an observer that will talk directly to the observable instance.
you can have as many observables as you want and for each observable you can have as many observers as you want
1.0.32 features
added makeObservable and makeObserver decorators to extend existing classes
you can now extend your class to be an observable or an observer and even share listeners with other observables
let observableExample = // makeObservable can decorate classes in order to make them subjects that observers can subscribe to// excepts two args(optional observable, bool-share listeners if first arg passed)@ { console }// any instance of ToBeObservable will have emit now...let instance = // makeObserver decorates a class to be an observer// this decorator excepts two args(name, observableToAttachTo)// you can pass both an instance created by your decorated class or an instance created by the createObservable func@ { console }let decoratedObserver = decoratedObserver
First, install the package using npm:
npm install certiorem --save
Then, require it and use it like so:
Observable methods
emit
the emit method receives the event name as the first argument and all arguments that come after are passed to all listeners subscribed
let mainObservable = mainObservable
Observer methods
Observer receives two arguments, the first is the observers name, the second is the Observable to attach to, so that all subscribtions will listen to events emitted by that Observable
The example below shows how two listeners are added to the mainObservable instance, all emitted events will pass to them if they are subscribed
let mainObservable = let listener1 = let listener2 =
subscribe
subscribe receives two arguments, the first one is the event name to subscribe to and the second is a function that receives arguments
here you can see that listener1 is subscribed to the start event and its handler is a function that receives data and logs it to the console
let mainObservable = let listener1 = listener1mainObservable
unsubscribe
the unsubscribe method removes the current listener from a specific event
let mainObservable = let listener1 = let listener2 = listener1listener2listener1mainObservable// only listener2 will respond to this event
emit
the observers emit method allows an observer to dispatch an event to the parent he his attached to, the mainObservable in this example and the mainObservable automatically dispatches the event to all listeners attached
let mainObservable = let listener1 = let listener2 = listener1listener2// this emit is similar to the parent emit, except it emits to the parent, and the parent automatically emits the event to all listenerslistener1
First, install the package using npm:
npm install certiorem --save
Then, require it and use it like so:
Observable methods
emit
the emit method receives the event name as the first argument and all arguments that come after are passed to all listeners subscribed
let mainObservable = mainObservable
Observer methods
Observer receives two arguments, the first is the observers name, the second is the Observable to attach to, so that all subscribtions will listen to events emitted by that Observable
The example below shows how two listeners are added to the mainObservable instance, all emitted events will pass to them if they are subscribed
let mainObservable = let listener1 = let listener2 =
subscribe
subscribe receives two arguments, the first one is the event name to subscribe to and the second is a function that receives arguments
here you can see that listener1 is subscribed to the start event and its handler is a function that receives data and logs it to the console
let mainObservable = let listener1 = listener1mainObservable
unsubscribe
the unsubscribe method removes the current listener from a specific event
let mainObservable = let listener1 = let listener2 = listener1listener2listener1mainObservable// only listener2 will respond to this event
emit
the observers emit method allows an observer to dispatch an event to the parent he his attached to, the mainObservable in this example and the mainObservable automatically dispatches the event to all listeners attached
let mainObservable = let listener1 = let listener2 = listener1listener2// this emit is similar to the parent emit, except it emits to the parent, and the parent automatically emits the event to all listenerslistener1