-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.ts
289 lines (228 loc) · 8.01 KB
/
events.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import type { Fn } from './helpers';
const sort = (a: EventListener, b: EventListener) => a.priority - b.priority;
export const EventAsFunction = <This, Args extends any[]>(_this: This) => {
const listeners: EventListener<This, Args>[] = [];
type fn = Fn<This, Args>;
interface event {
(...args: Args): void;
on(fn: fn, priority?: number, once?: boolean, shift?: boolean): void;
once(fn: fn, priority?: number, once?: boolean, shift?: boolean): void;
off(fn?: fn): void;
}
const event: event = (...args: Args): void => {
for(let i = 0; i < listeners.length; ++i) {
const l = listeners[i];
l.fn.apply(l.ctx, args);
if(l.once) event.off(l.fn);
}
};
event.on = (fn: fn, priority: number = 0, once = false, shift = false): void => {
const listener = new EventListener(fn, _this, priority, once) as EventListener;
if(!shift) listeners.push(listener);
else listeners.unshift(listener);
listeners.sort(sort);
}
event.once = (fn: fn, priority: number = 0, once = true, shift = false): void => {
event.on(fn, priority, once, shift);
}
event.off = (fn?: fn) => {
if(fn) {
const i: number = listeners.findIndex(l => l.fn === fn);
if(!~i) return;
listeners.splice(i, 1);
}
else listeners.length = 0;
};
return event;
}
export class EventListener<This = any, Args extends any[] = any[]> {
constructor(
public fn: Fn<This, Args>,
public ctx: This,
public priority: number = 0,
public once: boolean = false
) {}
}
export declare namespace Event {
export type name<T extends string = string> = `@${T}`;
export type getKeysOf<T extends object> = ({
[K in keyof T]: K extends name ? T[K] extends Event ? K : never : never
})[keyof T];
export type KeysOf<T extends object> = ConvertDel<({
[K in keyof T]: K extends name ? T[K] extends Event ? K : never : never
})[keyof T]>;
export type getArgs<T extends object, K extends getKeysOf<T>> =
T[K] extends Event<any, infer A> ? A : never;
export type ConvertDel<U extends name> = U extends name<infer R> ? R : never;
export type ConvertAdd<U extends string> = name<U>;
export type Args<T extends object, Type extends ConvertDel<getKeysOf<T>>> =
getArgs<T, ConvertAdd<Type> extends getKeysOf<T> ? ConvertAdd<Type> : never>;
}
export class Event<This = any, Args extends any[] = any[]> {
private _listeners: EventListener<This, Args>[] = [];
protected readonly _this: This;
constructor(_this: This) { this._this = _this; }
public on<T extends This>(fn: Fn<T, Args>, priority: number = 0, once = false, shift = false): typeof fn {
const listener = new EventListener(fn, this._this as any, priority, once) as EventListener;
if(!shift) this._listeners.push(listener);
else this._listeners.unshift(listener);
this._listeners.sort(sort);
return fn;
}
public once<T extends This>(fn: Fn<T, Args>, priority: number = 0, once = true, shift = false): typeof fn {
return this.on(fn, priority, once, shift);
}
public off<T extends This>(fn?: Fn<T, Args>): void {
if(fn) {
const i: number = this._listeners.findIndex(l => l.fn === fn);
if(!~i) return;
this._listeners.splice(i, 1);
} else this._listeners.length = 0;
}
public emit(...args: Args): void {
for(let i = 0; i < this._listeners.length; ++i) {
const l = this._listeners[i];
l.fn.apply(l.ctx, args);
if(l.once) this.off(l.fn);
}
}
public *[Symbol.iterator]() {
for(let i = 0; i < this._listeners.length; i++) {
yield this._listeners[i];
}
}
}
export declare namespace Notification {
export type Args<T extends Notification> = T extends Notification<any, infer A> ? A : never;
export type static<T extends Notification> = (...args: Notification.Args<T>) => any;
export type KeysOf<T extends object> = ({
[K in keyof T]: T[K] extends Notification ? K : never;
})[keyof T];
export type ArgsOf<T extends object, K extends KeysOf<T>> =
//@ts-ignore
T[K] extends Notification<infer Class, infer Name> ? Fn.A<Class[Name]> : never;
}
export class Notification<
Class extends object = any,
Name extends string = any,
//@ts-ignore
This extends Fn.T<Class[Name]> = any
> {
constructor(
protected readonly Class: Class,
public readonly name: Name,
protected _this: This,
public reverse: boolean = false
) {}
//@ts-ignore
public notify(...args: Fn.A<Class[Name]>): void {
if(!this.name) return;
const reverse = this.reverse;
const arr: any[] = [this.constructor];
let c = this.constructor;
while(c !== this.Class && (c = Object.getPrototypeOf(c.prototype)?.constructor)) {
arr.push(Object.prototype.hasOwnProperty.call(c, this.name) ? c : null);
}
if(reverse) {
for(let i = 0; i < arr.length; i++) arr[i]?.[this.name].apply(this._this, args);
} else {
for(let i = arr.length-1; i >= 0; i--) arr[i]?.[this.name].apply(this._this, args);
}
}
}
export class EventDispatcher {
public notify<This extends EventDispatcher,
Name extends Notification.KeysOf<This>,
Args extends Notification.ArgsOf<This, Name>
//@ts-ignore
>(this: This, name: Event.ConvertDel<Name>, ...args: Args): void {
//@ts-ignore
this[`@${name}`].notify(...args);
}
public static on<This extends typeof EventDispatcher,
Type extends Event.KeysOf<This>,
//@ts-ignore
ThisFn extends This[Event.name<Type>]['_this'],
Args extends Event.Args<This, Type>
>(this: This, type: Type, fn: Fn<ThisFn, Args>, priority: number = 0, once = false, shift = false): typeof fn {
//@ts-ignore
return this[`@${type}`].on(fn, priority, once, shift);
}
public static once<This extends typeof EventDispatcher,
Type extends Event.KeysOf<This>,
//@ts-ignore
ThisFn extends This[Event.name<Type>]['_this'],
Args extends Event.Args<This, Type>
>(this: This, type: Type, fn: Fn<ThisFn, Args>, priority: number = 0): typeof fn {
//@ts-ignore
return this[`@${type}`].once(fn, priority);
}
public static off<This extends typeof EventDispatcher,
Type extends Event.KeysOf<This>,
//@ts-ignore
ThisFn extends This[Event.name<Type>]['_this'],
Args extends Event.Args<This, Type>
>(this: This, type: Type, fn?: Fn<ThisFn, Args>): void {
//@ts-ignore
return this[`@${type}`].off(fn);
}
public static emit<This extends typeof EventDispatcher,
Type extends Event.KeysOf<This>,
Args extends Event.Args<This, Type>
>(this: This, type: Type, ...args: Args): void {
//@ts-ignore
return this[`@${type}`].emit(...args);
}
public on<This extends EventDispatcher,
Type extends Event.KeysOf<This>,
//@ts-ignore
ThisFn extends This[Event.name<Type>]['_this'],
Args extends Event.Args<This, Type>
>(this: This, type: Type, fn: Fn<ThisFn, Args>, priority: number = 0, once = false, shift = false): typeof fn {
//@ts-ignore
return this[`@${type}`].on(fn, priority, once, shift);
}
public once<This extends EventDispatcher,
Type extends Event.KeysOf<This>,
//@ts-ignore
ThisFn extends This[Event.name<Type>]['_this'],
Args extends Event.Args<This, Type>
>(this: This, type: Type, fn: Fn<ThisFn, Args>, priority: number = 0): typeof fn {
//@ts-ignore
return this[`@${type}`].once(fn, priority);
}
public off<This extends EventDispatcher,
Type extends Event.KeysOf<This>,
//@ts-ignore
ThisFn extends This[Event.name<Type>]['_this'],
Args extends Event.Args<This, Type>
>(this: This, type: Type, fn?: Fn<ThisFn, Args>): void {
//@ts-ignore
return this[`@${type}`].off(fn);
}
public emit<This extends EventDispatcher,
Type extends Event.KeysOf<This>,
Args extends Event.Args<This, Type>
>(this: This, type: Type, ...args: Args): void {
//@ts-ignore
return this[`@${type}`].emit(...args);
}
public events_off(a: boolean = false): void {
for(const e of this.events(a)) e.off();
}
public static events_off(a: boolean = false): void {
for(const e of this.events(a)) e.off();
}
public *events(a: boolean = false): Generator<Event> {
for(const id in this) {
//@ts-ignore
if((a || id[0] === '@') && this[id] instanceof Event) yield this[id];
}
}
public static *events(a: boolean = false): Generator<Event> {
for(const id in this) {
//@ts-ignore
if((a || id[0] === '@') && this[id] instanceof Event) yield this[id];
}
}
}