-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.ts
More file actions
104 lines (100 loc) · 5.19 KB
/
index.ts
File metadata and controls
104 lines (100 loc) · 5.19 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
import { BLOCK_OVERHEAD, BLOCK } from "rt/common";
export abstract class EventEmitter {
public static EventMap: Map<i32, Map<string, u64>> = new Map<i32, Map<string, u64>>();
public static registerEventCallback<T, U>(event: string): void {
if (!isFunction<U>()) ERROR("Cannot register event callback of type U where U is not a function.");
if (!isVoid<ReturnType<U>>()) ERROR("Cannot register event callback of type U where ReturnType<U> is not void.");
if (!EventEmitter.EventMap.has(idof<T>())) EventEmitter.EventMap.set(idof<T>(), new Map<string, u64>());
let ClassEvents = EventEmitter.EventMap.get(idof<T>());
if (ClassEvents.has(event)) throw new Error("EventMap already contains a definition for event: " + event);
ClassEvents.set(event, <u64>idof<U>() | (<u64>lengthof<U>() << 32));
}
private _events: Map<string, u32[]> = new Map<string, u32[]>();
public on<T>(event: string, callback: T): EventEmitter {
if (!isFunction<T>()) ERROR("EventEmitter#on can only be called with a callback of type T where T is a static function.");
let rtId = changetype<BLOCK>(changetype<usize>(this) - BLOCK_OVERHEAD).rtId;
let EventMap = EventEmitter.EventMap;
if (!EventMap.has(rtId)) throw new Error("Cannot attach events to an EventEmitter with no EventMap definitions.");
let ClassEvents = EventMap.get(rtId);
if (!ClassEvents.has(event)) throw new Error("Event does not exist: " + event);
let classEventSignature = ClassEvents.get(event);
let classEventCallbackID = <u32>(classEventSignature & 0xFFFFFFFF);
assert(idof<T>() == classEventCallbackID);
if (!this._events.has(event)) this._events.set(event, new Array<u32>());
let eventList = this._events.get(event);
eventList.push(changetype<u32>(callback));
return this;
}
public emit<A = string | null, B = string | null, C = string | null, D = string | null, E = string | null, F = string | null, G = string | null, H = string | null, I = string | null, J = string | null>
(event: string, a: A = null, b: B = null, c: C = null, d: D = null, e: E = null, f: F = null, g: G = null, h: H = null, i: I = null, j: J = null): EventEmitter {
if (this._events.has(event)) {
let rtId = changetype<BLOCK>(changetype<usize>(this) - BLOCK_OVERHEAD).rtId;
let ClassEvents = EventEmitter.EventMap.get(rtId);
assert(ClassEvents.has(event), "No event definition.");
let classEventSignature = ClassEvents.get(event);
let classEventArgLength = <u32>(classEventSignature >> 32) & 0xFFFFFFFF;
let classEventCallbackID = <u32>(classEventSignature & 0xFFFFFFFF);
let list = this._events.get(event);
let length = list.length;
switch (classEventArgLength) {
case 0: {
assert(classEventCallbackID == idof<() => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]));
break;
}
case 1: {
assert(classEventCallbackID == idof<(a: A) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a);
break;
}
case 2: {
assert(classEventCallbackID == idof<(a: A, b: B) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a, b);
break;
}
case 3: {
assert(classEventCallbackID == idof<(a: A, b: B, c: C) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a, b, c);
break;
}
case 4: {
assert(classEventCallbackID == idof<(a: A, b: B, c: C, d: D) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a, b, c, d);
break;
}
case 5: {
assert(classEventCallbackID == idof<(a: A, b: B, c: C, d: D, e: E) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a, b, c, d, e);
break;
}
case 6: {
assert(classEventCallbackID == idof<(a: A, b: B, c: C, d: D, e: E, f: F) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a, b, c, d, e, f);
break;
}
case 7: {
assert(classEventCallbackID == idof<(a: A, b: B, c: C, d: D, e: E, f: F, g: G) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a, b, c, d, e, f, g);
break;
}
case 8: {
assert(classEventCallbackID == idof<(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a, b, c, d, e, f, g, h);
break;
}
case 9: {
assert(classEventCallbackID == idof<(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a, b, c, d, e, f, g, h, i);
break;
}
case 10: {
assert(classEventCallbackID == idof<(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J) => void>());
for (let z = 0; z < length; z++) call_indirect(unchecked(list[z]), a, b, c, d, e, f, g, h, i, j);
break;
}
default: assert(false);
}
}
return this;
}
}