@a-z.ren/event-hub 中文文档教程

发布于 4年前 浏览 27 项目主页 更新于 3年前

Event Hub

简单事件中心

Table of Contents

Advantage

public subscriber: Map<string, Set<EventHandler>> = new Map();

EventHub 使用 Map设置存储EventHandler。 比使用ObjectArray具有更好的性能和更简洁的代码来实现核心功能。

缺点是只支持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

  1. Fork the repository
  2. Create Feat_xxx branch
  3. Commit your code
  4. 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 to 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";

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

  1. Fork the repository
  2. Create Feat_xxx branch
  3. Commit your code
  4. Create Pull Request

License

MIT © A-Z.REn

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文