-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
170 lines (137 loc) · 3.8 KB
/
index.ts
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
export interface SubscribeToken {
unsubscribe: () => void;
}
export interface KeepToken {
release: () => void;
}
export interface CommonObject {
[key: string]: any[];
}
interface Callback<T extends CommonObject, K extends keyof T> {
(...args: T[K]): void;
}
type Listener<T extends CommonObject> = Partial<
Record<
keyof T,
{
token: string;
fn: Callback<T, any>;
}[]
>
>;
interface Keep<T extends CommonObject> {
shouldPublish: true | (() => boolean);
name: keyof T;
token: string;
args: any[];
}
const when = (deps: true | (() => boolean)): boolean => {
return typeof deps === 'boolean' ? deps : deps();
};
const aps = Array.prototype.slice;
let counter: number = 0;
export class Topic<T extends CommonObject> {
protected listeners: Listener<T> = {};
protected origins: Listener<T> = {};
protected keeps: Keep<T>[] = [];
publish<K extends keyof T>(name: K, ...args: T[K]): void;
publish<K extends keyof T>(): void {
const name: K = arguments[0];
const args = aps.call(arguments, 1) as T[K];
const listeners = (this.origins[name] = this.listeners[name]);
if (!listeners || !listeners.length) {
return;
}
for (let i = 0; i < listeners.length; ++i) {
listeners[i]!.fn.apply(null, args);
}
this.origins[name] = undefined;
}
subscribeOnce<K extends keyof T>(
name: K,
fn: Callback<T, K>,
): SubscribeToken {
let executed = false;
const ref: { token?: SubscribeToken } = {};
ref.token = this.subscribe(name, function () {
fn.apply(null, arguments as unknown as T[K]);
if (ref.token) {
ref.token.unsubscribe();
} else {
executed = true;
}
});
if (executed) {
ref.token.unsubscribe();
}
return ref.token;
}
subscribe<K extends keyof T>(name: K, fn: Callback<T, K>): SubscribeToken {
const token = 'Token_' + counter++;
let listeners = this.listeners[name];
if (listeners) {
const origin = this.origins[name];
if (origin && listeners === origin) {
listeners = this.listeners[name] = listeners.slice(0);
}
} else {
listeners = this.listeners[name] = [];
}
listeners.push({
token,
fn,
});
if (this.keeps.length) {
const keeps = this.keeps.filter(
(item) => item.name === name && when(item.shouldPublish),
);
for (let i = 0; i < keeps.length; ++i) {
fn.apply(null, keeps[i]!.args as T[K]);
}
}
return {
unsubscribe: this.unsubscribe.bind(this, name, token),
};
}
/**
* Keep publishing for every subscription.
* The deps can be `true` or a function `() => true/false`. For dynamic publishing, function is required.
* ```javascript
* let deps = true;
* topic.keep('test', () => deps, 'hello', 'world');
* ```
*/
keep<K extends keyof T>(
name: K,
shouldPublish: Keep<T>['shouldPublish'],
...args: T[K]
): KeepToken;
keep<K extends keyof T>(): KeepToken {
const name: K = arguments[0];
const shouldPublish: Keep<T>['shouldPublish'] = arguments[1];
const args = aps.call(arguments, 2) as T[K];
const token = 'Token_' + counter++;
when(shouldPublish) &&
this.publish.apply(this, [name].concat(args) as [name: K, ...args: T[K]]);
this.keeps.push({
shouldPublish,
token,
name,
args,
});
return {
release: this.release.bind(this, token),
};
}
protected unsubscribe(name: keyof T, token: string): void {
const listeners = this.listeners[name];
if (!listeners || !listeners.length) {
return;
}
this.listeners[name] = listeners.filter((item) => item.token !== token);
}
protected release(token: string) {
this.keeps = this.keeps.filter((item) => item.token !== token);
}
}
export const topic = new Topic();