A simple extendable event emitter class for Deno, Node, and Browser.
Before we start to use EventPine we need to import it first.
Put this on your first line
import 'https://x.nest.land/[email protected]/mod.js';
Add the module using package manager
$ npm i eventpine
Then import it to your script
let EventPine = require('eventpine');
Put this on HTML <head>
<script src="https://cdn.jsdelivr.net/npm/eventpine@latest"></script>
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'
Listen to an event.
obj.on('message' /* EventName */, function(data1, data2){
// Do something
});
Listen to an event then remove it after being called.
obj.once('message' /* EventName */, function(data1, data2){
// Do something once
});
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 to events that was registered.
obj.emit('message' /* EventName */, 'data1', 'data2', ...);
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.
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.
EventPine is under the MIT license.