Gathering detailed insights and metrics for @achrinza/event-pubsub
Gathering detailed insights and metrics for @achrinza/event-pubsub
Gathering detailed insights and metrics for @achrinza/event-pubsub
Gathering detailed insights and metrics for @achrinza/event-pubsub
npm install @achrinza/event-pubsub
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
126 Commits
2 Forks
2 Watching
2 Branches
5 Contributors
Updated on 28 May 2024
JavaScript (95.23%)
HTML (4.77%)
Cumulative downloads
Total Downloads
Last day
-52.7%
1,595
Compared to previous day
Last week
-17.2%
11,294
Compared to previous week
Last month
11%
50,194
Compared to previous month
Last year
-8.8%
622,470
Compared to previous year
1
4
npm install @achrinza/event-pubsub
Super light and fast Extensible ES6+ event system for Node and the browser the same files that work in node will work in the browser without any modifications. If you must support old browsers you can transpile the module.
Method | Arguments | Description |
---|---|---|
on | type:string , handler:function , once:boolean | will bind the handler function to the the type event. Just like addEventListener in the browser. If once is set to true the hander will be removed after being called once. |
once | type:string , handler:function | will bind the handler function to the the type event and unbind it after one execution. Just like addEventListener in the browser withe the once option set |
off | type/* :string , handler/* :function | will unbind the handler function from the the type event. If the handler is * , all handlers for the event type will be removed. Just like removeEventListener in the browser, but also can remove all event handlers for the type. |
emit | type:string , ...data arguments | will call all handler functions bound to the * event and the type event. It will pass all ...data arguments to those handlers, for * events, the first arg will be the type you can filter the events |
reset | Removes all events of any and all types including * |
Member | Type | Description |
---|---|---|
.list | Object | List representation of all the bound events, primarily used for visibility. |
*
event typeThe *
event type will be triggered by any emit
. These also run first. The handlers for *
should expect the first arg to be the type
and all args after that to be data arguments.
npm start
actually starts a node-http-server. So if you just want quick links to the example and test web pages, there is a page in the root of this module with links. You can access it by going to the local homepage : http://localhost:8000
Provided your router and firewall are not blocking your IP/ports, you can also go to http://[your-ip-here]:8000/
on any device including your mobile device provided it is on the same network.
We use the free Digital Ocean Static Apps to host a version of the local server. It is exactly the same as if you ran npm start on your machine. You can also use this like a CDN as it automatically rebuilds from main/master each time the branch is updated. @achrinza/event-pubsub CDN home : https://cdn-avsja.ondigitalocean.app/@achrinza/event-pubsub/
1 2//relative paths will let your code work in both node and the browser without transpiling unless you want to. 3import EventPubSub from './node_modules/@achrinza/event-pubsub/index.js'; 4 5events=new EventPubSub 6 7events.on( 8 'hello', 9 (data)=>{ 10 console.log('hello event recieved ', data); 11 } 12); 13 14events.emit( 15 'hello', 16 'world' 17); 18
1 2events.on( 3 'hello', 4 someFunction 5).on( 6 'goodbye', 7 anotherFunction 8).emit( 9 'hello', 10 'world' 11); 12 13events.emit( 14 'goodbye', 15 'humans' 16).off( 17 'hello', 18 '*' 19); 20
1//relative paths will let your code work in both node and the browser! 2import EventPubSub from './node_modules/@achrinza/event-pubsub/index.js'; 3 4 5class Book extends EventPubSub{ 6 constructor(){ 7 super(); 8 //now Book has .on, .off, and .emit 9 10 this.words=[]; 11 } 12 13 add(...words){ 14 this.words.push(...words); 15 this.emit( 16 'added', 17 ...words 18 ); 19 } 20 21 read(){ 22 this.emit( 23 'reading' 24 ); 25 console.log(this.words.join(' ')); 26 } 27} 28 29const book=new Book; 30 31book.on( 32 'added', 33 function(...words){ 34 console.log('words added : ',words); 35 this.read(); 36 } 37); 38 39book.add( 40 'once','upon','a','time','in','a','cubicle' 41); 42 43
@achrinza/event-pubsub
uses the strong-type
class which provides methods to test all the built in js primatives, objects, classes, and even fancy things like async functions and generators. This should help make sure your code doesn't do unexpected things.
full strong-type documentation
Since we use the same files for node and the browser, we need to emulate a production npm i @achrinza/event-pubsub
in the example folder, so be sure to :
first run npm run emulate
then run any of the following examples
node ./example/basic.js
node ./example/miltiple.js
node ./example/extending.js
node ./example/once.js
run npm start
this will automatically run npm run emulate
for you as well.
Then just go to the local server : http://localhost:8000 from here you can see both the examples and the tests. Or go directly to the local example : http://localhost:8000/example/. It actually imports the node example into the browser and runs it, same exact file, no transpiling or custom code for the browser. If you want to transpile though, you can.
I'm actually pretty pleased with how easy this was. Feel free to use the same type of scripts in your projects. You can even copy paste and just change the repo/module names if you want. Here is the code from my package.json using && is important otherwise your commands will run in parallel, and you really need them to run atomically.
This is needed because we use relative paths in our ES6+ modules to allow the same exact js to work in node and the browser. Its what we have all been waiting for!
1 2 "scripts": { 3 "test": "npm run emulate && node ./test/CI.js", 4 "start": "npm run emulate && node-http-server port=8000 verbose=true", 5 "emulate": "npm i && copyfiles -V \"./!(node_modules)/*\" \"./**!(node_modules)\" \"./example/node_modules/@achrinza/event-pubsub/\" && copyfiles -V \"./node_modules/**/*\" \"./example/\" && copyfiles -V \"./!(node_modules)/*\" \"./**!(node_modules)\" \"./test/node_modules/@achrinza/event-pubsub/\" && copyfiles -V \"./node_modules/**/*\" \"./test/\"" 6}, 7
@node-ipc/vanilla-test is a pretty sweet, And minimalist ES6+ testing suite for both the browser and node. You can run the tests with npm test
Also, the tests can be run in the browser if you run npm start
and then go to the local server : http://localhost:8000 and click the test link. Also, remember, you should be able to access them via http://[your-ip]:8000 provided your firwall and router are not blocking your ip or ports.
As of 11/22/2020 FF still does not support private fields or methods in js classes, however, the nightly build has it included behind a flag. With the private field and method flags set to true, FireFox nightly works like a charm.
No vulnerabilities found.
No security vulnerabilities found.