@a-z.ren/event-hub 中文文档教程
Event Hub
简单事件中心
Table of Contents
Advantage
public subscriber: Map<string, Set<EventHandler>> = new Map();
EventHub
使用 Map并设置存储EventHandler
。 比使用Object
和Array
具有更好的性能和更简洁的代码来实现核心功能。
缺点是只支持
ES6
以上,编译成ES5
后不再有性能优势。
Install
npm i @a-z.ren/event-hub
Usage
JavaScript
import EventHub from "@a-z.ren/event-hub";
TypeScript
import EventHub from "@a-z.ren/event-hub/src/EventHub";
基本功能:
const eventHub = new EventHub();
function handlerA() {
console.log("I'm A!");
}
eventHub.on("1", handlerA);
eventHub.on("1", (a, b) => console.log(`I'm B, I like ${a} and ${b}!`));
eventHub.once("1", () => console.log("I'm C, I only be triggered once!"));
eventHub.emit("1", "????", "????");
// I'm A!
// I'm B, I like and ????!
// I'm C, I only be triggered once!
eventHub.off("1", handlerA);
eventHub.emit("1", "????", "????");
// I'm B, I like and ????!
Promise 和 Async/Await:
(async function () {
const value = await eventHub.promise("2");
console.log("Await " + value);
})();
eventHub
.promise("2")
.then((value) => "Then " + value)
.then(console.log);
eventHub.emit("2", "Hello");
// Await Hello
// Then Hello
Extends
class ChatCat extends EventHub {
sayHello() {
this.emit("say", "Hello!");
}
sayGood() {
this.emit("say", "Good!");
}
}
const cat = new ChatCat();
cat.on("say", console.log);
cat.sayHello();
// Hello!
cat.sayGood();
// Good!
Gather
eventHub.on("4", (value) => "Love " + value);
eventHub.once("4", (value) => "Good " + value);
eventHub.gather("4", "gather").forEach((result) => console.log(result));
// Love gather
// Good gather
Contribute
- Fork the repository
- Create Feat_xxx branch
- Commit your code
- Create Pull Request
License
MIT © AZ.REn
Event Hub
Simple Event Hub
Table of Contents
Advantage
public subscriber: Map<string, Set<EventHandler>> = new Map();
The EventHub
uses Map and Set to store EventHandler
. has better performance and more concise code to implement core functions than using Object
and Array
.
The disadvantage is that it only supports
ES6
or above, and no longer has performance advantage after compiling toES5
.
Install
npm i @a-z.ren/event-hub
Usage
JavaScript
import EventHub from "@a-z.ren/event-hub";
TypeScript
import EventHub from "@a-z.ren/event-hub/src/EventHub";
Basic functions:
const eventHub = new EventHub();
function handlerA() {
console.log("I'm A!");
}
eventHub.on("1", handlerA);
eventHub.on("1", (a, b) => console.log(`I'm B, I like ${a} and ${b}!`));
eventHub.once("1", () => console.log("I'm C, I only be triggered once!"));
eventHub.emit("1", "????", "????");
// I'm A!
// I'm B, I like ???? and ????!
// I'm C, I only be triggered once!
eventHub.off("1", handlerA);
eventHub.emit("1", "????", "????");
// I'm B, I like ???? and ????!
Promise and Async/Await:
(async function () {
const value = await eventHub.promise("2");
console.log("Await " + value);
})();
eventHub
.promise("2")
.then((value) => "Then " + value)
.then(console.log);
eventHub.emit("2", "Hello");
// Await Hello
// Then Hello
Extends
class ChatCat extends EventHub {
sayHello() {
this.emit("say", "Hello!");
}
sayGood() {
this.emit("say", "Good!");
}
}
const cat = new ChatCat();
cat.on("say", console.log);
cat.sayHello();
// Hello!
cat.sayGood();
// Good!
Gather
eventHub.on("4", (value) => "Love " + value);
eventHub.once("4", (value) => "Good " + value);
eventHub.gather("4", "gather").forEach((result) => console.log(result));
// Love gather
// Good gather
Contribute
- Fork the repository
- Create Feat_xxx branch
- Commit your code
- Create Pull Request
License
MIT © A-Z.REn
更多