emitter
You can use micell.emitter.create() to create an Emitter instance. And, every Emitter instance has the below methods:
emitgetListenersoffononce
The micell.emitter can be used as a global Emitter instance. That is, the below functions are the methods of the global emitter.
micell.emitter.emitmicell.emitter.getListenersmicell.emitter.offmicell.emitter.onmicell.emitter.once
Emitter
js
new Emitter()The Emitter constructor. Recommend to use the below create() method to create an Emitter instance.
Since
0.5.0
Return
(Emitter)
create
js
create()Create an Emitter instance.
Since
0.5.0
Return
(Emitter)
Example
js
const emitter = create()
emitter.on('foo', () => console.log('foo'))
emitter.emit('foo')
// => 'foo'emit
js
emit(type, [arg1], [arg2], ...)Since
0.5.0
Arguments
type (string)[argN] (any)
js
micell.emitter.on('foo', (...args) => console.log(args))
micell.emitter.emit('foo', 1, 2, 3)
// => [1, 2, 3]getListeners
js
getListeners(type)Return the given type listeners or all listeners.
Since
0.5.0
Arguments
[type (string)]
Return
(Array<Listener>)
Example
js
micell.emitter.on('foo', () => console.log('foo 1'))
micell.emitter.on('bar', () => console.log('bar 1'))
micell.emitter.on('foo', () => console.log('foo 2'))
// All listeners for 'foo'
micell.emitter.getListeners('foo')
// All listeners
micell.emitter.getListeners()off
js
off(type, listener)- If nothing is passed, all listeners will be removed.
- If only the
typeparameter is passed, the given type listeners will be removed. - If the
typeandlistenerare passed, then all thelisteners of thetypewill be removed.
Since
0.5.0
Arguments
[type (string)][listener (function)]
Example
js
const fn1 = () => console.log(1)
const fn2 = () => console.log(2)
micell.emitter.on('foo', fn1)
micell.emitter.on('bar', fn1)
micell.emitter.on('foo', fn2)
micell.emitter.off('foo', fn1)
micell.emitter.off('foo')
micell.emitter.off()on
js
on(type, listener)Attach a listener to the emitter.
Since
0.5.0
Arguments
[type (string)][listener (function)]
once
js
once(type, listener)Same as on, attach a listener to the emitter but the listener will be triggered once.
Since
0.5.0
Arguments
[type (string)][listener (function)]