promise-observer
An observer implementation with promise support.
Its meant to behave similarly to typical synchronous Java / NET observers, where once you notify your subscribers you are able to wait for their update functions to execute before proceeding
Example
Given a blog post creator:
BlogPostCreator.prototype.create =
a categorizer can attach to its events
onPostCategorized = blogPostCreator.onCreatedcategorizepost.thensaveCategory.thenReturnpost;
an indexer can add search terms to the index for that post
onPostIndexed = blogPostCreator.onCreatedindexpost.thensaveIndex.thenReturnpost;
Then, the email notification system can wait for the post to be categorized and indexed before sending a notification to all subscribers:
onPostNotification = blogPostCreator.onCreated;
API
po.create(emit):Observable
po.create(emit: (val:T) => Promise<void>):Observable<T>
Creates a new observable. The observable exposes its emit function through the revealing constructor pattern. Use the emit function to notify all subscribers of new events.
The emit function returns a promise that resolves when all subscribers and their dependents finish processing the event.
Observable
observable(listener):LinkedObservable
Creates a listener for the observable. A listener is a mapping function that returns either a new value or a promise.
Returns a linked observable that emits whenever the returned promises or values resolve.
observable.next(predicate?):Promise
Waits for the next event that satisfies the specified predicate. Returns a promise for the value contained in that event.
The predicate is optional.
observable.remove(linkedObservable)
Removes a listener (linked observable).
linkedObservable.unlink()
Same as parentObservable.remove(linkedObservable)
Building
npm install
npm run build
License
MIT