@achrinza/event-pubsub

中文文档
5.0.11发布于 2024/05/28MITES Module近 30 天下载 3.6万

Super light and fast Extensible ES6+ events and EventEmitters for Node and the browser. Easy for any developer level, use the same exact code in node and the browser. No frills, just high speed events!

原始使用文档
文档目录13 节
  1. Local website
  2. Digital Ocean Static App
  3. Basic Examples
  4. Basic Event Emitter and/or Extending Event PubSub
  5. Strong Type Checking
  6. How Did I emulate a production install for the module insid…
  7. Testing done with vanilla-test
  8. See the c8 code coverage
  9. Node vanilla-test screenshot
  10. Chrome vanilla-test screenshot
  11. Chrome Example Screenshot
  12. Edge Example Screenshot
  13. FireFox Nightly Example Screenshot

Event PubSub

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.

Methods

MethodArgumentsDescription
ontype:string, handler:function, once:booleanwill 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.
oncetype:string, handler:functionwill 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
offtype/*:string, handler/*:functionwill 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.
emittype:string, ...data argumentswill 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
resetRemoves all events of any and all types including *

Members

MemberTypeDescription
.listObjectList representation of all the bound events, primarily used for visibility.

The * event type

The * 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.

Local website

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.

Digital Ocean Static App

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/

Basic Examples


//relative paths will let your code work in both node and the browser without transpiling unless you want to.
import EventPubSub from './node_modules/@achrinza/event-pubsub/index.js';

events=new EventPubSub

events.on(
    'hello',
    (data)=>{
        console.log('hello event recieved ', data);
    }
);

events.emit(
    'hello',
    'world'
);

Basic Chaining


events.on(
    'hello',
    someFunction
).on(
    'goodbye',
    anotherFunction
).emit(
    'hello',
    'world'
);

events.emit(
    'goodbye',
    'humans'
).off(
    'hello',
    '*'
);

Basic Event Emitter and/or Extending Event PubSub

//relative paths will let your code work in both node and the browser!
import EventPubSub from './node_modules/@achrinza/event-pubsub/index.js';


class Book extends EventPubSub{
    constructor(){
        super();
        //now Book has .on, .off, and .emit

        this.words=[];
    }

    add(...words){
        this.words.push(...words);
        this.emit(
            'added',
            ...words
        );
    }

    read(){
        this.emit(
            'reading'
        );
        console.log(this.words.join(' '));
    }
}

const book=new Book;

book.on(
    'added',
    function(...words){
        console.log('words added : ',words);
        this.read();
    }
);

book.add(
    'once','upon','a','time','in','a','cubicle'
);


Strong Type Checking

@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

For node

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

node @achrinza/event-pubsub basic example

For the browser

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.

How Did I emulate a production install for the module inside itself???

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!


 "scripts": {
    "test": "npm run emulate && node ./test/CI.js",
    "start": "npm run emulate && node-http-server port=8000 verbose=true",
    "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/\""
},

Testing done with vanilla-test

@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.

See the c8 code coverage

Node vanilla-test screenshot

node @achrinza/event-pubsub vanilla-test report

Chrome vanilla-test screenshot

Chrome @achrinza/event-pubsub vanilla-test report

Chrome Example Screenshot

Chrome @achrinza/event-pubsub basic example

Edge Example Screenshot

Edge @achrinza/event-pubsub basic example

FireFox Nightly Example Screenshot

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.

FireFox-nightly @achrinza/event-pubsub basic example

版本与发布记录

@achrinza/event-pubsub 共发布 8 个版本,首次发布于 2022/03/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.0.11latest2024/05/282 年前
5.0.102023/10/253 年前
5.0.92023/06/223 年前
5.0.82022/10/234 年前
5.0.72022/08/224 年前
5.0.62022/03/264 年前

关于 @achrinza/event-pubsub 的常见问题

@achrinza/event-pubsub 是什么?

Super light and fast Extensible ES6+ events and EventEmitters for Node and the browser. Easy for any developer level, use the same exact code in node and the browser. No frills, just high speed events!

如何安装 @achrinza/event-pubsub?

在项目目录运行 npm install @achrinza/event-pubsub,或使用 pnpm add / yarn add / bun add。

@achrinza/event-pubsub 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/achrinza__event-pubsub 是否存在。

@achrinza/event-pubsub 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@achrinza/event-pubsub 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js 13 || 14 || 16 || 17 || 18 || 19 || 20 || 21 || 22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@achrinza/event-pubsub 可以商用吗?

@achrinza/event-pubsub 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@achrinza/event-pubsub 的下载量有多少?

根据 npm 官方下载统计,@achrinza/event-pubsub 在 2026/10/05 – 2026/11/03 这 30 天内被下载 35,634 次,日均约 1,188 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@achrinza/event-pubsub 还在维护吗?

最新版本 5.0.11 发布于 2024/05/28(2 年前);至今共发布 8 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/03/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。