obso
v0.7.0
Published
Make an object observable.
Readme
obso
Make an object observable.
import Emitter from 'obso/index.mjs'
class Something extends Emitter {}
const something = new Something()
something.on('load', () => {
console.log('load event fired.')
})obso
Emitter ⏏
Kind: Exported class
emitter.addEventListener
Alias for on.
Kind: instance property of Emitter
emitter.emit(eventName)
Emit an event.
Kind: instance method of Emitter
| Param | Type | Description | | --- | --- | --- | | eventName | string | the event name to emit. | | ...args | * | args to pass to the event handler |
emitter.on([eventName], handler, [options])
Register an event listener.
Kind: instance method of Emitter
| Param | Type | Description |
| --- | --- | --- |
| [eventName] | string | The event name to watch. Omitting the name will catch all events. |
| handler | function | The function to be called when eventName is emitted. Invocated with this set to emitter. |
| [options] | object | |
| [options.once] | boolean | If true, the handler will be invoked once then removed. |
emitter.removeEventListener(eventName, handler)
Remove an event listener.
Kind: instance method of Emitter
| Param | Type | Description | | --- | --- | --- | | eventName | string | the event name | | handler | function | the event handler |
emitter.once(eventName, handler)
Once.
Kind: instance method of Emitter
| Param | Type | Description | | --- | --- | --- | | eventName | string | the event name to watch | | handler | function | the event handler |
Load anywhere
This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation.
Node.js:
const Emitter = require('obso')Within Node.js with ECMAScript Module support enabled:
import Emitter from 'obso/index.mjs'Within an modern browser ECMAScript Module:
import Emitter from './node_modules/obso/index.mjs'Old browser (adds window.Emitter):
<script nomodule src="./node_modules/obso/dist/index.js"></script>© 2018-21 Lloyd Brookes <[email protected]>. Documented by jsdoc-to-markdown.
