Update framework
This commit is contained in:
11
framework/event-bus/event-bus.d.ts
vendored
Normal file
11
framework/event-bus/event-bus.d.ts
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
export declare class EventBus {
|
||||
private events;
|
||||
constructor();
|
||||
emit(id: string, data: any): void;
|
||||
subscribe(id: string, subscriber: Function): void;
|
||||
}
|
||||
export declare class EventItem {
|
||||
id: string;
|
||||
subscribers: Function[];
|
||||
constructor(id: string);
|
||||
}
|
30
framework/event-bus/event-bus.js
Normal file
30
framework/event-bus/event-bus.js
Normal file
@@ -0,0 +1,30 @@
|
||||
export class EventBus {
|
||||
constructor() {
|
||||
this.events = new Map();
|
||||
}
|
||||
emit(id, data) {
|
||||
let ev = this.events.get(id);
|
||||
if (!ev) {
|
||||
let ev = new EventItem(id);
|
||||
this.events.set(id, ev);
|
||||
return;
|
||||
}
|
||||
ev.subscribers.forEach((subscriber) => {
|
||||
subscriber(data);
|
||||
});
|
||||
}
|
||||
subscribe(id, subscriber) {
|
||||
let ev = this.events.get(id);
|
||||
if (!ev) {
|
||||
ev = new EventItem(id);
|
||||
this.events.set(id, ev);
|
||||
}
|
||||
ev.subscribers.push(subscriber);
|
||||
}
|
||||
}
|
||||
export class EventItem {
|
||||
constructor(id) {
|
||||
this.id = id;
|
||||
this.subscribers = [];
|
||||
}
|
||||
}
|
13
framework/event-bus/index.d.ts
vendored
Normal file
13
framework/event-bus/index.d.ts
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
export declare class EventBus {
|
||||
private events;
|
||||
constructor();
|
||||
emit(id: string, data?: any): void;
|
||||
subscribe(id: string, subscriber: Function): void;
|
||||
unsubscribe(id: string, subscriber: Function): void;
|
||||
unsubscribeAll(id: string): void;
|
||||
}
|
||||
export declare class EventItem {
|
||||
id: string;
|
||||
subscribers: Function[];
|
||||
constructor(id: string);
|
||||
}
|
44
framework/event-bus/index.js
Normal file
44
framework/event-bus/index.js
Normal file
@@ -0,0 +1,44 @@
|
||||
export class EventBus {
|
||||
constructor() {
|
||||
this.events = new Map();
|
||||
}
|
||||
emit(id, data = {}) {
|
||||
let ev = this.events.get(id);
|
||||
if (!ev) {
|
||||
let ev = new EventItem(id);
|
||||
this.events.set(id, ev);
|
||||
return;
|
||||
}
|
||||
ev.subscribers.forEach((subscriber) => {
|
||||
subscriber(data);
|
||||
});
|
||||
}
|
||||
subscribe(id, subscriber) {
|
||||
let ev = this.events.get(id);
|
||||
if (!ev) {
|
||||
ev = new EventItem(id);
|
||||
this.events.set(id, ev);
|
||||
}
|
||||
ev.subscribers.push(subscriber);
|
||||
}
|
||||
unsubscribe(id, subscriber) {
|
||||
if (this.events.has(id)) {
|
||||
let ev = this.events.get(id);
|
||||
ev.subscribers = ev.subscribers.filter((fn) => fn !== subscriber);
|
||||
if (ev.subscribers.length < 1) {
|
||||
this.events.delete(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
unsubscribeAll(id) {
|
||||
if (this.events.has(id)) {
|
||||
this.events.delete(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
export class EventItem {
|
||||
constructor(id) {
|
||||
this.id = id;
|
||||
this.subscribers = [];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user