-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevents.ts
27 lines (26 loc) · 1.04 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
//@On annotation, takes an event name. this is only valid for methods
export function On(eventName?: string) {
return function (target: any, key: string) {
//create the temp events holder if non existane
if (!target.$$events) target.$$events = {};
target.$$events[eventName || key] = target[key];
//make sure the function does not end up in methods
if (!target.$$methodsToRemove) target.$$methodsToRemove = [];
target.$$methodsToRemove.push(key);
};
}
//@Once annotation, same as @On but will remove the handler when fired
export function Once(eventName?: string) {
return function (target: any, key: string) {
//create the temp props holder if non existane
if (!target.$$events) target.$$events = {};
eventName = eventName || key;
target.$$events[eventName] = function (...args: any[]) {
target[key].call(this, ...args);
this.$off(eventName, target.$$events[eventName]);
};
//make sure the function does not end up in methods
if (!target.$$methodsToRemove) target.$$methodsToRemove = [];
target.$$methodsToRemove.push(key);
};
}