-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniBus.ts
More file actions
110 lines (90 loc) · 3.29 KB
/
Copy pathMiniBus.ts
File metadata and controls
110 lines (90 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
module MiniBus {
class Subscription {
constructor(
public id: number,
public message_id: string,
public callback: (payload?: any, data?: any) => void) { }
public unSubscribe() {
new EventManager().unSubscribe(this);
}
}
interface IMessage {
subscribe(callback: (payload?: any) => void): any;
unSubscribe(subRef: Subscription): void;
notify(payload?: any, data?: any): void;
}
class Message implements IMessage {
private _subscriptions: Subscription[];
private _nextId: number;
private _lastValue: any;
private _id: string;
constructor(public message: string) {
this._id = message;
this._subscriptions = [];
this._nextId = 0;
}
public subscribe(callback: (payload?: any) => void) {
var subscription = new Subscription(this._nextId++, this._id, callback);
this._subscriptions[subscription.id] = subscription;
return this._subscriptions[subscription.id];
}
public unSubscribe(subRef: Subscription) {
var idx: number = 0,
len: number = this._subscriptions.length;
while (idx < len) {
if (this._subscriptions[idx] === subRef) {
this._subscriptions.splice(idx, 1);
this._subscriptions[idx] = undefined;
console.log(subRef)
console.log('subscription terminated');
}
break;
idx += 1;
}
}
public notify(payload?: any, data?: any) {
if (this._lastValue === payload) { }
else {
var index;
for (index = 0; index < this._subscriptions.length; index++) {
if (this._subscriptions[index]) {
this._subscriptions[index].callback(payload, data);
}
}
}
this._lastValue = payload;
}
}
export class EventManager {
private static _instance: EventManager = null;
private _messages: any;
constructor() {
if (EventManager._instance === null) {
this.init();
}
return EventManager._instance;
}
init() {
this._messages = {};
EventManager._instance = this;
}
subscribe(message: string, callback: (payload?: any) => void) {
var msg: IMessage;
msg = this._messages[message] ||
<IMessage>(this._messages[message] = new Message(message));
return msg.subscribe(callback);
}
unSubscribe(subRef: any) {
if (!(subRef instanceof Subscription)) {
console.log(subRef instanceof Subscription, subRef,'Error :: invalid argument! :: unSubscribe must be type of Subscription');
return false;
}
(<IMessage>(this._messages[subRef.message_id])).unSubscribe(subRef);
}
publish(message: string, payload?: any, data?:any) {
if (this._messages[message]) {
(<IMessage>(this._messages[message])).notify(payload, data);
}
}
}
}