metano-runtime 0.3.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/system/delegates.d.ts +40 -0
- package/dist/system/delegates.js +74 -0
- package/dist/system/index.d.ts +1 -0
- package/dist/system/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multicast delegate support for C# event/delegate lowering.
|
|
3
|
+
*
|
|
4
|
+
* A delegate is a plain function tagged with a hidden listener list. When `+=`
|
|
5
|
+
* is used on a function, it's promoted to a multicast delegate via
|
|
6
|
+
* {@link delegateAdd}. The promoted delegate IS still a function with the
|
|
7
|
+
* same signature — callers can't tell the difference.
|
|
8
|
+
*
|
|
9
|
+
* Uses an array (not Set) because C# multicast delegates allow duplicate
|
|
10
|
+
* subscriptions — adding the same handler twice invokes it twice, and
|
|
11
|
+
* `-=` removes only the last matching occurrence.
|
|
12
|
+
*
|
|
13
|
+
* Zero overhead when `+=` is never used: the function stays a plain function.
|
|
14
|
+
*/
|
|
15
|
+
declare const DELEGATE_LISTENERS: unique symbol;
|
|
16
|
+
type AnyFunc = (...args: any[]) => any;
|
|
17
|
+
type DelegateFunc<T extends AnyFunc> = T & {
|
|
18
|
+
[DELEGATE_LISTENERS]: T[];
|
|
19
|
+
};
|
|
20
|
+
/** Returns true if the function has been promoted to a multicast delegate. */
|
|
21
|
+
export declare function isDelegate<T extends AnyFunc>(fn: T | null): fn is DelegateFunc<T>;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a multicast delegate from one or more handlers. The returned
|
|
24
|
+
* function calls every handler in insertion order and returns the last
|
|
25
|
+
* handler's return value (matching C# multicast delegate semantics).
|
|
26
|
+
*/
|
|
27
|
+
export declare function createDelegate<T extends AnyFunc>(...handlers: T[]): T;
|
|
28
|
+
/**
|
|
29
|
+
* Adds a handler to a delegate. If the target is a plain function, it's
|
|
30
|
+
* promoted to a multicast delegate first. If the target is null, the handler
|
|
31
|
+
* itself is returned (first subscription on a nullable event).
|
|
32
|
+
*/
|
|
33
|
+
export declare function delegateAdd<T extends AnyFunc>(target: T | null, handler: T): T;
|
|
34
|
+
/**
|
|
35
|
+
* Removes a handler from a delegate. Removes the LAST matching occurrence
|
|
36
|
+
* (matching C# `-=` semantics). Returns null when the last handler is
|
|
37
|
+
* removed, or the target unchanged if the handler wasn't found.
|
|
38
|
+
*/
|
|
39
|
+
export declare function delegateRemove<T extends AnyFunc>(target: T | null, handler: T): T | null;
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multicast delegate support for C# event/delegate lowering.
|
|
3
|
+
*
|
|
4
|
+
* A delegate is a plain function tagged with a hidden listener list. When `+=`
|
|
5
|
+
* is used on a function, it's promoted to a multicast delegate via
|
|
6
|
+
* {@link delegateAdd}. The promoted delegate IS still a function with the
|
|
7
|
+
* same signature — callers can't tell the difference.
|
|
8
|
+
*
|
|
9
|
+
* Uses an array (not Set) because C# multicast delegates allow duplicate
|
|
10
|
+
* subscriptions — adding the same handler twice invokes it twice, and
|
|
11
|
+
* `-=` removes only the last matching occurrence.
|
|
12
|
+
*
|
|
13
|
+
* Zero overhead when `+=` is never used: the function stays a plain function.
|
|
14
|
+
*/
|
|
15
|
+
const DELEGATE_LISTENERS = Symbol("delegate_listeners");
|
|
16
|
+
/** Returns true if the function has been promoted to a multicast delegate. */
|
|
17
|
+
export function isDelegate(fn) {
|
|
18
|
+
return fn !== null && DELEGATE_LISTENERS in fn;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates a multicast delegate from one or more handlers. The returned
|
|
22
|
+
* function calls every handler in insertion order and returns the last
|
|
23
|
+
* handler's return value (matching C# multicast delegate semantics).
|
|
24
|
+
*/
|
|
25
|
+
export function createDelegate(...handlers) {
|
|
26
|
+
const listeners = [...handlers];
|
|
27
|
+
const delegate = ((...args) => {
|
|
28
|
+
let result;
|
|
29
|
+
for (const listener of listeners) {
|
|
30
|
+
result = listener(...args);
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
});
|
|
34
|
+
Object.defineProperty(delegate, DELEGATE_LISTENERS, {
|
|
35
|
+
value: listeners,
|
|
36
|
+
enumerable: false,
|
|
37
|
+
});
|
|
38
|
+
return delegate;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Adds a handler to a delegate. If the target is a plain function, it's
|
|
42
|
+
* promoted to a multicast delegate first. If the target is null, the handler
|
|
43
|
+
* itself is returned (first subscription on a nullable event).
|
|
44
|
+
*/
|
|
45
|
+
export function delegateAdd(target, handler) {
|
|
46
|
+
if (target === null)
|
|
47
|
+
return handler;
|
|
48
|
+
if (isDelegate(target)) {
|
|
49
|
+
target[DELEGATE_LISTENERS].push(handler);
|
|
50
|
+
return target;
|
|
51
|
+
}
|
|
52
|
+
return createDelegate(target, handler);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Removes a handler from a delegate. Removes the LAST matching occurrence
|
|
56
|
+
* (matching C# `-=` semantics). Returns null when the last handler is
|
|
57
|
+
* removed, or the target unchanged if the handler wasn't found.
|
|
58
|
+
*/
|
|
59
|
+
export function delegateRemove(target, handler) {
|
|
60
|
+
if (target === null)
|
|
61
|
+
return null;
|
|
62
|
+
if (isDelegate(target)) {
|
|
63
|
+
const list = target[DELEGATE_LISTENERS];
|
|
64
|
+
const idx = list.lastIndexOf(handler);
|
|
65
|
+
if (idx >= 0)
|
|
66
|
+
list.splice(idx, 1);
|
|
67
|
+
if (list.length === 0)
|
|
68
|
+
return null;
|
|
69
|
+
if (list.length === 1)
|
|
70
|
+
return list[0];
|
|
71
|
+
return target;
|
|
72
|
+
}
|
|
73
|
+
return target === handler ? null : target;
|
|
74
|
+
}
|
package/dist/system/index.d.ts
CHANGED
package/dist/system/index.js
CHANGED