evx
Handy dandy persistent-state pub/sub with multi, wildcard, and single-property subscriptions. 400 bytes gzipped.
Install
npm i evx --save
Usage
evx
is just a simple pub/sub bus:
But it also allows you to subscribe to multiple events at once:
// fires once
And emit multiple events at once:
// fires twice
It has wildcard support:
Additionally, you can subscribe to specific property values by passing the property key as the event name:
// someProperty updated // will fire // will also fire
State
Additionally, it has a concept of state. In evx
state is always an object.
Any object passed to emit
will be shallowly merged with global state:
And all subscribers are passed the full state object:
// true
To emit transient data that does not get merged into the global state, pass an object as the third argument to emit
:
And access via the second argument subscribers:
// Hello
If you need to add some state but don't want to emit any events, use hydrate
:
But for convenience, hydrate
also returns a function that, when called, will
emit a '*' event:
The current read-only state is accessible as well:
// { baz: true }
Cleanup
Subscribers return a function that will unsubscribe from that event:
const unsubscribe = // will fire // will not fire
Multiple instances
If you need to create a discrete instance of evx
, use create
:
const bus =
All methods above are now accessible on bus
.
You can also pass an optional initial state object to create
:
const bus =
License
MIT License © Eric Bailey