eventpine
v1.0.3
Published
A simple extendable event emitter class
Downloads
10
Maintainers
Readme
EventPine
A simple extendable event emitter class for Deno, Node, and Browser.
Getting Started
Before we start to use EventPine we need to import it first.
Deno
Put this on your first line
import 'https://x.nest.land/[email protected]/mod.js';
Node
Add the module using package manager
$ npm i eventpine
Then import it to your script
let EventPine = require('eventpine');
Browser
Put this on HTML <head>
<script src="https://cdn.jsdelivr.net/npm/eventpine@latest"></script>
How to use
EventPine can be extended with your custom class.
// Custom class with EventPine (Optional)
class MyCustom extends EventPine{/* ... */}
var obj = new MyCustom();
// Or just create from the root
var obj = new EventPine();
// The next section also use this 'obj'
On
Listen to an event.
obj.on('message' /* EventName */, function(data1, data2){
// Do something
});
Once
Listen to an event then remove it after being called.
obj.once('message' /* EventName */, function(data1, data2){
// Do something once
});
Off
Remove event listener registered by on/once.
// Remove specific callback listener to this event
obj.off('message', myFunction);
// Remove all event listener to this event name
obj.off('message');
// Clear all event including the internal event
obj.off();
If the second argument was empty, every callback related to this EventName will be removed.
Emit
Emit to events that was registered.
obj.emit('message' /* EventName */, 'data1', 'data2', ...);
Internal Event
There are some internal event that may be useful.
|EventName|Arguments|Description|
|---|---|---|
|*
|EventName, ...|Wildcards event, everytime emit
was called|
obj.on('*', function(...){
// Do stuff
});
Instead of wildcard there are another experimental internal event, they're not documented yet because may be changed on the future.
Contribution
If you want to help in EventPine please fork this project and edit on your repository, then make a pull request to here. Otherwise, you can help with donation via patreon.
But don't forget to put a link to this repository, or share it maybe.
License
EventPine is under the MIT license.