@overmvp/kumi 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +14 -2
- package/animation/color-transition.ts +196 -0
- package/animation/cycle.ts +151 -0
- package/animation/ease.ts +29 -0
- package/animation/index.ts +13 -0
- package/animation/package.json +12 -0
- package/animation/presets.ts +376 -0
- package/animation/spec.ts +197 -0
- package/builder/Cargo.lock +192 -0
- package/builder/Cargo.toml +7 -0
- package/builder/css-v1.mjs +334 -0
- package/builder/css-v1.test.mjs +321 -0
- package/builder/generated-output.mjs +61 -0
- package/builder/generated-output.test.mjs +132 -0
- package/builder/jsx-form.mjs +93 -0
- package/builder/jsx-form.test.mjs +48 -0
- package/builder/jsx-lists.mjs +209 -0
- package/builder/link-addon-package.mjs +86 -0
- package/builder/lua-assets.mjs +74 -0
- package/builder/lua-builtin-values.mjs +57 -0
- package/builder/lua-builtin-values.test.mjs +155 -0
- package/builder/lua-loop-closures.mjs +183 -0
- package/builder/lua-loop-closures.test.mjs +89 -0
- package/builder/lua-truthiness.mjs +61 -0
- package/builder/lua-truthiness.test.mjs +113 -0
- package/builder/module-declarations.mjs +594 -0
- package/builder/module-declarations.test.mjs +444 -0
- package/builder/native-kernels.mjs +60 -0
- package/builder/native-lua.d.ts +57 -0
- package/builder/native-routes.mjs +51 -0
- package/builder/no-static-ref-read.mjs +93 -0
- package/builder/no-static-ref-read.test.mjs +181 -0
- package/builder/secure-snippet.mjs +317 -0
- package/builder/secure-snippet.test.mjs +101 -0
- package/builder/shared-lua-runtime.mjs +150 -0
- package/builder/src/addon-link.lua +29 -0
- package/builder/src/arrays.lua +260 -0
- package/builder/src/assets.rs +177 -0
- package/builder/src/classes.lua +75 -0
- package/builder/src/collections.lua +190 -0
- package/builder/src/compiler.rs +2164 -0
- package/builder/src/config.rs +42 -0
- package/builder/src/entries.rs +166 -0
- package/builder/src/errors.lua +15 -0
- package/builder/src/lua_data.rs +231 -0
- package/builder/src/main.rs +367 -0
- package/builder/src/native_ir.rs +65 -0
- package/builder/src/native_signal.rs +47 -0
- package/builder/src/numbers.lua +62 -0
- package/builder/src/project-front.mjs +3331 -0
- package/builder/src/records.lua +75 -0
- package/builder/src/routes.rs +127 -0
- package/builder/src/signal-kernel.fingerprint +1 -0
- package/builder/src/strings.lua +103 -0
- package/builder/src/ts-lower.mjs +68 -0
- package/builder/src/wow_client.rs +253 -0
- package/builder/src/wow_types.rs +564 -0
- package/builder/style-states.mjs +100 -0
- package/builder/style-states.test.mjs +103 -0
- package/builder/vite-assets.mjs +53 -0
- package/builder/vite-project.mjs +10 -0
- package/config.ts +17 -0
- package/form/index.ts +522 -0
- package/form/package.json +13 -0
- package/motion/index.ts +2 -0
- package/motion/package.json +10 -0
- package/package.json +22 -4
- package/router/index.ts +43 -0
- package/router/package.json +12 -0
- package/router/routes.ts +64 -0
- package/scripts/kumi-cli.mjs +70 -0
- package/scripts/link-addons.mjs +51 -0
- package/src/bindings.ts +35 -0
- package/src/context.ts +15 -0
- package/src/css-tokens.ts +234 -0
- package/src/css-v1.ts +58 -0
- package/src/cvars.ts +91 -0
- package/src/element-node.ts +32 -0
- package/src/elements.ts +479 -0
- package/src/feature-scope.ts +82 -0
- package/src/flex-layout.ts +807 -0
- package/src/frame-scheduler.ts +334 -0
- package/src/grid-tracks.ts +165 -0
- package/src/image-imports.d.ts +24 -0
- package/src/images.ts +79 -0
- package/src/index.ts +28 -0
- package/src/jsx-dev-runtime.ts +2 -0
- package/src/jsx-runtime.ts +22 -0
- package/src/jsx.d.ts +12 -0
- package/src/keyed-list.ts +363 -0
- package/src/media-query.ts +144 -0
- package/src/module.ts +277 -0
- package/src/node-selectors.ts +114 -0
- package/src/owned-collections.ts +44 -0
- package/src/providers.ts +235 -0
- package/src/reactive-scope.ts +34 -0
- package/src/reactivity.ts +254 -0
- package/src/result.ts +58 -0
- package/src/runtime.ts +52 -0
- package/src/scheduler.ts +185 -0
- package/src/signal-kernel.ts +30 -0
- package/src/style-classes.ts +51 -0
- package/src/style-states.ts +134 -0
- package/src/style-variables.ts +53 -0
- package/src/text-children.ts +42 -0
- package/src/theme.ts +50 -0
- package/src/transition.ts +18 -0
- package/state/index.ts +3 -0
- package/state/package.json +12 -0
- package/state/store-bindings.ts +25 -0
- package/state/store.ts +82 -0
- package/styles/_layers.scss +14 -0
- package/templates/addon/README.md +14 -0
- package/templates/addon/__ADDON_NAME__.toc +4 -0
- package/templates/addon/eslint.config.mjs +12 -0
- package/templates/addon/gitignore +8 -0
- package/templates/addon/package.json +21 -0
- package/templates/addon/scripts/build.mjs +25 -0
- package/templates/addon/src/app/main.tsx +13 -0
- package/templates/addon/tsconfig.json +24 -0
- package/translate/index.ts +14 -0
- package/translate/package.json +9 -0
- package/utils/colors.ts +76 -0
- package/utils/format.ts +37 -0
- package/utils/index.ts +4 -0
- package/utils/number.ts +17 -0
- package/utils/package.json +7 -0
- package/utils/text.ts +6 -0
- package/validate/index.ts +119 -0
- package/validate/package.json +9 -0
- package/wow/absolute-position.ts +142 -0
- package/wow/animation.ts +428 -0
- package/wow/aura-button.ts +76 -0
- package/wow/aura-container.ts +591 -0
- package/wow/aura-countdown.ts +201 -0
- package/wow/aura-slot.ts +153 -0
- package/wow/bar-shade.ts +93 -0
- package/wow/borrowed-slot.ts +216 -0
- package/wow/button-drag.ts +48 -0
- package/wow/cast-clock.ts +153 -0
- package/wow/cast-text.ts +83 -0
- package/wow/class-colors.ts +126 -0
- package/wow/clock.ts +7 -0
- package/wow/color-picker.ts +128 -0
- package/wow/commands.ts +17 -0
- package/wow/contained-frame.ts +128 -0
- package/wow/contract.ts +42 -0
- package/wow/css-box.ts +110 -0
- package/wow/css-color.ts +12 -0
- package/wow/css-style.ts +109 -0
- package/wow/css-surfaces.ts +398 -0
- package/wow/css-text.ts +399 -0
- package/wow/cvar-schema.ts +32 -0
- package/wow/cvars.ts +98 -0
- package/wow/dropdown-art.ts +39 -0
- package/wow/element-click.ts +20 -0
- package/wow/element-event-coordinator.ts +219 -0
- package/wow/element-events.ts +317 -0
- package/wow/event-dispatcher.ts +151 -0
- package/wow/events.ts +50 -0
- package/wow/font-cache.ts +92 -0
- package/wow/frame-access.ts +31 -0
- package/wow/frame-layer.ts +5 -0
- package/wow/frames.ts +236 -0
- package/wow/heal-prediction.ts +305 -0
- package/wow/image-layers.ts +55 -0
- package/wow/index.ts +97 -0
- package/wow/layout-view.ts +651 -0
- package/wow/lua-string.ts +18 -0
- package/wow/lua-table.ts +18 -0
- package/wow/macro-book.ts +84 -0
- package/wow/map-position.ts +27 -0
- package/wow/media-query.ts +65 -0
- package/wow/module-events.ts +11 -0
- package/wow/native-hooks.ts +168 -0
- package/wow/native-placement.ts +88 -0
- package/wow/native.ts +135 -0
- package/wow/number-format.ts +8 -0
- package/wow/number-input.ts +10 -0
- package/wow/protect.ts +24 -0
- package/wow/protected-layout.ts +133 -0
- package/wow/range-art.ts +74 -0
- package/wow/reparent-frame.ts +99 -0
- package/wow/rounded-texture.ts +156 -0
- package/wow/saved-variables.ts +60 -0
- package/wow/scroll-view.ts +64 -0
- package/wow/secret-ref.ts +14 -0
- package/wow/secure-bindings.ts +89 -0
- package/wow/secure-clicks.ts +432 -0
- package/wow/secure-snippet.ts +235 -0
- package/wow/secure-state.ts +130 -0
- package/wow/secure-target-visibility.ts +89 -0
- package/wow/secure-transaction.ts +331 -0
- package/wow/special-window.ts +269 -0
- package/wow/start-addon.ts +99 -0
- package/wow/state-paint.ts +256 -0
- package/wow/transition.ts +86 -0
- package/wow/unit-button.ts +39 -0
- package/wow/unit-cast.ts +92 -0
- package/wow/unit-health-text.ts +47 -0
- package/wow/view-nodes.ts +304 -0
- package/wow/view.ts +1746 -0
- package/wow/visible-frame-job.ts +34 -0
- package/wow/width-reserve.ts +108 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import type { ClickEvent } from "../src/elements";
|
|
2
|
+
import type { OwnedArray } from "../src/owned-collections";
|
|
3
|
+
import { fail, ok, type Result } from "../src/result";
|
|
4
|
+
import type { RuntimeScope } from "../src/runtime";
|
|
5
|
+
import type { Cancel } from "../src/scheduler";
|
|
6
|
+
import type { EventApi } from "./contract";
|
|
7
|
+
import { readClickEvent } from "./element-click";
|
|
8
|
+
import { api, reveal } from "./native";
|
|
9
|
+
import { protectNative } from "./protect";
|
|
10
|
+
import type { SpecialWindowProxy } from "./special-window";
|
|
11
|
+
|
|
12
|
+
export interface ElementOutsideEntry {
|
|
13
|
+
hit(this: void): boolean | undefined;
|
|
14
|
+
ignoreOutside?: boolean;
|
|
15
|
+
onClickOutside?(this: void, event: ClickEvent): void;
|
|
16
|
+
onDismiss?(this: void, reason: "outside" | "escape"): void;
|
|
17
|
+
layerKind?: "transient" | "window";
|
|
18
|
+
dismissOn?: { outside?: boolean; escape?: boolean };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ElementOutsideLease {
|
|
22
|
+
setShown(this: void, shown: boolean): Result<void>;
|
|
23
|
+
cancel(this: void): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ElementEventCoordinator {
|
|
27
|
+
readonly scope: RuntimeScope;
|
|
28
|
+
watch(this: void, owner: RuntimeScope, entry: ElementOutsideEntry): Result<ElementOutsideLease>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface Watch {
|
|
32
|
+
owner: RuntimeScope;
|
|
33
|
+
entry: ElementOutsideEntry;
|
|
34
|
+
shown: boolean;
|
|
35
|
+
generation: number;
|
|
36
|
+
detach?: (this: void) => boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface PressEntry {
|
|
40
|
+
watch: Watch;
|
|
41
|
+
generation: number;
|
|
42
|
+
hit?: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function report(this: void, problem: unknown) {
|
|
46
|
+
protectNative(() => api.geterrorhandler()(problem));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function attempt<Value>(run: (this: void) => Result<Value>, fallback: string): Result<Value> {
|
|
50
|
+
const [ran, result] = protectNative(run);
|
|
51
|
+
if (ran) return result;
|
|
52
|
+
const reason = reveal(result);
|
|
53
|
+
return fail(typeof reason === "string" ? reason : fallback);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function createElementEventCoordinator(this: void, scope: RuntimeScope, events: EventApi,
|
|
57
|
+
escape: Pick<SpecialWindowProxy, "setCloseHandler">): Result<ElementEventCoordinator> {
|
|
58
|
+
const watchers = new Set<Watch>();
|
|
59
|
+
const visible: Watch[] = [];
|
|
60
|
+
let stopClicks: Cancel | undefined;
|
|
61
|
+
let clickGeneration = 0;
|
|
62
|
+
let armed: boolean | undefined = false;
|
|
63
|
+
|
|
64
|
+
function current(watch: Watch) {
|
|
65
|
+
return watchers.has(watch) && watch.owner.isActive() && scope.isActive();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function eligible({ watch, generation }: PressEntry) {
|
|
69
|
+
return current(watch) && watch.shown && watch.generation === generation;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function topEscape() {
|
|
73
|
+
for (let index = visible.length - 1; index >= 0; index--) {
|
|
74
|
+
const watch = visible[index];
|
|
75
|
+
if (watch.entry.onDismiss !== undefined && watch.entry.dismissOn?.escape !== false && current(watch)) return watch;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function transient(entry: ElementOutsideEntry) {
|
|
80
|
+
return entry.onDismiss !== undefined && entry.layerKind !== "window";
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function readHit(this: void, entry: ElementOutsideEntry) {
|
|
84
|
+
const [ran, result] = protectNative(entry.hit);
|
|
85
|
+
if (!ran) {
|
|
86
|
+
report(result);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const hit = reveal(result);
|
|
90
|
+
if (typeof hit === "boolean") return hit;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function dismiss(this: void, watch: Watch, reason: "outside" | "escape") {
|
|
94
|
+
const handler = watch.entry.onDismiss;
|
|
95
|
+
if (handler === undefined) return;
|
|
96
|
+
const [ran, problem] = protectNative(handler, reason);
|
|
97
|
+
if (!ran) report(problem);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function press(this: void, raw: unknown) {
|
|
101
|
+
const event = readClickEvent(raw);
|
|
102
|
+
if (event === undefined || !scope.isActive()) return;
|
|
103
|
+
const entries = visible.filter(({ entry }) => entry.ignoreOutside === true || entry.onClickOutside !== undefined || transient(entry))
|
|
104
|
+
.map(watch => ({ watch, generation: watch.generation })) as OwnedArray<PressEntry>;
|
|
105
|
+
for (const entry of entries) {
|
|
106
|
+
if (eligible(entry)) entry.hit = readHit(entry.watch.entry);
|
|
107
|
+
}
|
|
108
|
+
if (entries.some(entry => entry.watch.entry.ignoreOutside === true && entry.hit !== false && eligible(entry))) return;
|
|
109
|
+
for (const entry of entries) {
|
|
110
|
+
const handler = entry.watch.entry.onClickOutside;
|
|
111
|
+
if (handler === undefined || entry.hit !== false || !eligible(entry)) continue;
|
|
112
|
+
const [ran, problem] = protectNative(handler, event);
|
|
113
|
+
if (!ran) report(problem);
|
|
114
|
+
}
|
|
115
|
+
for (let index = entries.length - 1; index >= 0; index--) {
|
|
116
|
+
const entry = entries[index];
|
|
117
|
+
if (!transient(entry.watch.entry) || !eligible(entry)) continue;
|
|
118
|
+
if (entry.hit !== false || entry.watch.entry.dismissOn?.outside === false) break;
|
|
119
|
+
dismiss(entry.watch, "outside");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function onEscape(this: void) {
|
|
124
|
+
if (armed !== true || !scope.isActive()) return;
|
|
125
|
+
// The native special-window proxy consumes its handler on Hide.
|
|
126
|
+
armed = false;
|
|
127
|
+
const top = topEscape();
|
|
128
|
+
if (top !== undefined) dismiss(top, "escape");
|
|
129
|
+
if (scope.isActive()) {
|
|
130
|
+
const result = synchronize();
|
|
131
|
+
if (!result.ok) report(result.reason);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function arm(this: void, desired: boolean): Result<void> {
|
|
136
|
+
if (armed === desired) return ok(undefined);
|
|
137
|
+
const result = attempt(() => escape.setCloseHandler(desired ? onEscape : undefined), "Cannot update element Escape handling");
|
|
138
|
+
armed = result.ok ? desired : undefined;
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function releaseClicks(this: void) {
|
|
143
|
+
++clickGeneration;
|
|
144
|
+
const cancel = stopClicks;
|
|
145
|
+
stopClicks = undefined;
|
|
146
|
+
if (cancel === undefined) return;
|
|
147
|
+
const [ran, problem] = protectNative(cancel);
|
|
148
|
+
if (!ran) report(problem);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function synchronize(this: void): Result<void> {
|
|
152
|
+
const listening = visible.some(({ entry }) => entry.onClickOutside !== undefined || (transient(entry) && entry.dismissOn?.outside !== false));
|
|
153
|
+
if (!listening) releaseClicks();
|
|
154
|
+
else if (stopClicks === undefined) {
|
|
155
|
+
const ticket = ++clickGeneration;
|
|
156
|
+
const subscribed = attempt(() => events.on(scope, "GLOBAL_MOUSE_DOWN", raw => {
|
|
157
|
+
if (ticket === clickGeneration) press(raw);
|
|
158
|
+
}, { label: "Element outside clicks" }), "Cannot listen for element outside clicks");
|
|
159
|
+
if (!subscribed.ok) return subscribed;
|
|
160
|
+
stopClicks = subscribed.value;
|
|
161
|
+
}
|
|
162
|
+
return arm(topEscape() !== undefined);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function changeShown(watch: Watch, shown: boolean) {
|
|
166
|
+
if (watch.shown === shown) return;
|
|
167
|
+
watch.shown = shown;
|
|
168
|
+
++watch.generation;
|
|
169
|
+
if (shown) visible.push(watch);
|
|
170
|
+
else {
|
|
171
|
+
const index = visible.indexOf(watch);
|
|
172
|
+
if (index >= 0) visible.splice(index, 1);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function retire(watch: Watch) {
|
|
177
|
+
if (!watchers.delete(watch)) return false;
|
|
178
|
+
changeShown(watch, false);
|
|
179
|
+
watch.detach?.();
|
|
180
|
+
watch.detach = undefined;
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function watch(this: void, owner: RuntimeScope, entry: ElementOutsideEntry): Result<ElementOutsideLease> {
|
|
185
|
+
if (!scope.isActive() || !owner.isActive()) return fail("Element outside watching requires active scopes");
|
|
186
|
+
const state: Watch = { owner, entry, shown: false, generation: 0 };
|
|
187
|
+
function cancel(this: void) {
|
|
188
|
+
if (!retire(state) || !scope.isActive()) return;
|
|
189
|
+
const result = synchronize();
|
|
190
|
+
if (!result.ok) report(result.reason);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function setShown(this: void, shown: boolean): Result<void> {
|
|
194
|
+
if (!current(state)) return fail("Element outside lease is no longer active");
|
|
195
|
+
const previous = state.shown;
|
|
196
|
+
changeShown(state, shown);
|
|
197
|
+
const result = synchronize();
|
|
198
|
+
if (!result.ok && shown && !previous) {
|
|
199
|
+
changeShown(state, false);
|
|
200
|
+
const restored = synchronize();
|
|
201
|
+
if (!restored.ok) report(restored.reason);
|
|
202
|
+
}
|
|
203
|
+
return result;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const registered = owner.onDispose(cancel);
|
|
207
|
+
if (!registered.ok) return registered;
|
|
208
|
+
state.detach = registered.value;
|
|
209
|
+
watchers.add(state);
|
|
210
|
+
return ok({ setShown, cancel });
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const cleanup = scope.onDispose(() => {
|
|
214
|
+
for (const state of [...watchers]) retire(state);
|
|
215
|
+
releaseClicks();
|
|
216
|
+
arm(false);
|
|
217
|
+
});
|
|
218
|
+
return cleanup.ok ? ok({ scope, watch }) : cleanup;
|
|
219
|
+
}
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import type { ElementEvents, ElementTooltip, MouseButton } from "../src/elements";
|
|
2
|
+
import { ownedArray, ownedSet, type OwnedArray, type OwnedReadonlyArray } from "../src/owned-collections";
|
|
3
|
+
import { fail, ok, type Failure, type Result } from "../src/result";
|
|
4
|
+
import type { RuntimeScope } from "../src/runtime";
|
|
5
|
+
import type { Cancel } from "../src/scheduler";
|
|
6
|
+
import type { StyleState } from "../src/style-states";
|
|
7
|
+
import { readClickEvent, readMouseButton } from "./element-click";
|
|
8
|
+
import type { ElementEventCoordinator } from "./element-event-coordinator";
|
|
9
|
+
import type { Button, CommonScripts, Frame } from "./frames";
|
|
10
|
+
import { hookButtonClick, hookScript } from "./native-hooks";
|
|
11
|
+
import { api, isSecret, reveal } from "./native";
|
|
12
|
+
import { createProtectedLayoutUpdate } from "./protected-layout";
|
|
13
|
+
import { protect } from "./protect";
|
|
14
|
+
import { hitViewNode, type LogicalNode } from "./view-nodes";
|
|
15
|
+
|
|
16
|
+
export interface ElementTooltipPort {
|
|
17
|
+
open(this: void, anchor: Frame, tooltip: ElementTooltip, scope: RuntimeScope): Result<Cancel>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface NativeElementEventHost {
|
|
21
|
+
coordinator?: ElementEventCoordinator;
|
|
22
|
+
tooltip?: ElementTooltipPort;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ElementStyleInput {
|
|
26
|
+
needs(this: void, state: StyleState): boolean;
|
|
27
|
+
set(this: void, state: StyleState, active: boolean): void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type ElementInput = ElementEvents & { onShow?(this: void): void; onHide?(this: void): void };
|
|
31
|
+
type HitRegion = Pick<Frame, "IsVisible" | "IsMouseOver" | "IsForbidden">;
|
|
32
|
+
type HitCandidate = Partial<HitRegion> & { mouseHit?(this: object): boolean | undefined; value?: HitCandidate };
|
|
33
|
+
type ScopeFactory = (this: void) => RuntimeScope;
|
|
34
|
+
|
|
35
|
+
export function hasElementInput(this: void, input: ElementInput) {
|
|
36
|
+
return input.onClick !== undefined || input.onPointerEnter !== undefined || input.onPointerLeave !== undefined
|
|
37
|
+
|| input.onShow !== undefined || input.onHide !== undefined || input.onClickOutside !== undefined
|
|
38
|
+
|| input.onDismiss !== undefined || input.ignoreOutside === true || input.tooltip !== undefined || input.propagateClicks === false;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function targetHit(this: void, raw: unknown): boolean | undefined {
|
|
42
|
+
if (isSecret(raw)) return;
|
|
43
|
+
const candidate = raw as HitCandidate | undefined;
|
|
44
|
+
if (candidate === undefined) return false;
|
|
45
|
+
const region = typeof candidate.IsVisible === "function" || typeof candidate.mouseHit === "function" ? candidate : candidate.value;
|
|
46
|
+
if (isSecret(region)) return;
|
|
47
|
+
if (region === undefined) return false;
|
|
48
|
+
if (typeof region.mouseHit === "function") return region.mouseHit();
|
|
49
|
+
if (typeof region.IsForbidden === "function" && reveal(region.IsForbidden()) !== false) return;
|
|
50
|
+
const visible = reveal(region.IsVisible?.());
|
|
51
|
+
if (visible !== true) return visible;
|
|
52
|
+
return reveal(region.IsMouseOver?.());
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function hit(this: void, frame: Frame, targets?: ElementEvents["inside"], logical?: LogicalNode) {
|
|
56
|
+
const main = logical === undefined ? targetHit(frame) : hitViewNode(logical);
|
|
57
|
+
if (main === true || targets === undefined) return main;
|
|
58
|
+
let known = main !== undefined;
|
|
59
|
+
for (const target of targets as OwnedReadonlyArray<(typeof targets)[number]>) {
|
|
60
|
+
const outside = protect(() => targetHit(target));
|
|
61
|
+
if (outside.ok && outside.value === true) return true;
|
|
62
|
+
if (!outside.ok || outside.value === undefined) known = false;
|
|
63
|
+
}
|
|
64
|
+
return known ? false : undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function installScript<Script>(read: (this: void) => Script | undefined, write: (this: void, value?: Script) => void,
|
|
68
|
+
hook: (this: void, callback: Script) => Result<Cancel>, callback: Script, hookOnly: boolean, restorers: OwnedArray<Cancel>) {
|
|
69
|
+
if (hookOnly || read() !== undefined) {
|
|
70
|
+
const registered = hook(callback);
|
|
71
|
+
if (!registered.ok) return registered;
|
|
72
|
+
restorers.push(registered.value);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
restorers.push(() => { if (read() === callback) write(); });
|
|
76
|
+
write(callback);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function changeInputFlag(read: (this: void) => boolean | undefined, write: (this: void, state: boolean) => void,
|
|
80
|
+
state: boolean, restorers: OwnedArray<Cancel>) {
|
|
81
|
+
const previous = read();
|
|
82
|
+
if (previous === state) return;
|
|
83
|
+
if (previous !== undefined) restorers.push(() => { if (read() === state) write(previous); });
|
|
84
|
+
write(state);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function canChange(frame: Frame) {
|
|
88
|
+
if (typeof frame.IsForbidden === "function" && reveal(frame.IsForbidden()) !== false) return false;
|
|
89
|
+
return typeof frame.IsAnchoringSecret !== "function" || reveal(frame.IsAnchoringSecret()) === false;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function protectedFrame(frame: Frame) {
|
|
93
|
+
if (typeof frame.IsProtected !== "function") return true;
|
|
94
|
+
const [state] = frame.IsProtected();
|
|
95
|
+
return reveal(state) !== false;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function bindElementEvents(this: void, frame: Frame, input: ElementInput, owner: RuntimeScope, newScope: ScopeFactory,
|
|
99
|
+
host?: NativeElementEventHost, button?: Button, borrowed = false, logical?: LogicalNode,
|
|
100
|
+
style?: ElementStyleInput, preserveNativeClicks = false): Failure | undefined {
|
|
101
|
+
const hover = style?.needs("hover") === true;
|
|
102
|
+
const activeStyle = style?.needs("active") === true;
|
|
103
|
+
const disabledStyle = style?.needs("disabled") === true ? button : undefined;
|
|
104
|
+
if (!hasElementInput(input) && !hover && !activeStyle && disabledStyle === undefined) return;
|
|
105
|
+
if (!canChange(frame)) return fail("Element input target is inaccessible or restricted");
|
|
106
|
+
if (input.tooltip !== undefined && host?.tooltip === undefined) return fail("Element tooltip requires the host tooltip port");
|
|
107
|
+
const outside = input.onClickOutside !== undefined || input.onDismiss !== undefined || input.ignoreOutside === true;
|
|
108
|
+
const coordinator = host?.coordinator;
|
|
109
|
+
if (outside && coordinator === undefined) return fail("Outside dismissal requires the shared element event coordinator");
|
|
110
|
+
|
|
111
|
+
const registered = outside ? coordinator?.watch(owner, {
|
|
112
|
+
hit: () => hit(frame, input.inside, logical),
|
|
113
|
+
onClickOutside: input.onClickOutside,
|
|
114
|
+
onDismiss: input.onDismiss,
|
|
115
|
+
layerKind: input.layerKind,
|
|
116
|
+
dismissOn: input.dismissOn,
|
|
117
|
+
ignoreOutside: input.ignoreOutside,
|
|
118
|
+
}) : undefined;
|
|
119
|
+
if (registered?.ok === false) return registered;
|
|
120
|
+
const lease = registered?.value;
|
|
121
|
+
|
|
122
|
+
const restorers = ownedArray<Cancel>();
|
|
123
|
+
const motion = hover || activeStyle || input.onPointerEnter !== undefined || input.onPointerLeave !== undefined || input.tooltip !== undefined;
|
|
124
|
+
const pressed = activeStyle || (button === undefined && input.onClick !== undefined) ? ownedSet<MouseButton>() : undefined;
|
|
125
|
+
const observesVisibility = pressed !== undefined || outside || input.onShow !== undefined || input.onHide !== undefined
|
|
126
|
+
|| input.tooltip !== undefined || hover;
|
|
127
|
+
let hoveredTip: Cancel | undefined;
|
|
128
|
+
let visible = false;
|
|
129
|
+
let active = true;
|
|
130
|
+
let ownCleanup: RuntimeScope | undefined;
|
|
131
|
+
let request: ((this: void) => Result<void>) | undefined;
|
|
132
|
+
|
|
133
|
+
function live() { return active && owner.isActive(); }
|
|
134
|
+
|
|
135
|
+
function report(result: Result<unknown> | undefined) {
|
|
136
|
+
if (result?.ok === false) api.geterrorhandler()(result.reason);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function closeTip() {
|
|
140
|
+
const close = hoveredTip;
|
|
141
|
+
hoveredTip = undefined;
|
|
142
|
+
close?.();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function synchronize(notify: boolean) {
|
|
146
|
+
if (!live()) return;
|
|
147
|
+
const visibleValue = typeof frame.IsVisible === "function" ? reveal(frame.IsVisible()) : reveal(frame.IsShown());
|
|
148
|
+
const shown = visibleValue === true;
|
|
149
|
+
if (!shown) {
|
|
150
|
+
if (pressed !== undefined) pressed.clear();
|
|
151
|
+
if (activeStyle) style?.set("active", false);
|
|
152
|
+
if (hover) style?.set("hover", false);
|
|
153
|
+
}
|
|
154
|
+
if (shown === visible) return;
|
|
155
|
+
visible = shown;
|
|
156
|
+
const synchronized = lease?.setShown(shown);
|
|
157
|
+
if (!notify && synchronized?.ok === false) return synchronized;
|
|
158
|
+
report(synchronized);
|
|
159
|
+
if (shown) {
|
|
160
|
+
if (hover) style?.set("hover", reveal(frame.IsMouseOver()) === true);
|
|
161
|
+
if (notify) input.onShow?.();
|
|
162
|
+
} else {
|
|
163
|
+
closeTip();
|
|
164
|
+
if (notify) input.onHide?.();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function enter() {
|
|
169
|
+
if (hover) style?.set("hover", true);
|
|
170
|
+
if (activeStyle) style?.set("active", pressed !== undefined && pressed.size > 0);
|
|
171
|
+
input.onPointerEnter?.();
|
|
172
|
+
if (!live() || !visible || input.tooltip === undefined || host?.tooltip === undefined) return;
|
|
173
|
+
closeTip();
|
|
174
|
+
const opened = host.tooltip.open(frame, input.tooltip, owner);
|
|
175
|
+
if (opened.ok) hoveredTip = opened.value;
|
|
176
|
+
else report(opened);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function restore() {
|
|
180
|
+
let failure: Failure | undefined;
|
|
181
|
+
for (let index = restorers.length - 1; index >= 0; index--) {
|
|
182
|
+
const restored = protect(restorers[index]);
|
|
183
|
+
if (!restored.ok && failure === undefined) failure = restored;
|
|
184
|
+
}
|
|
185
|
+
restorers.length = 0;
|
|
186
|
+
const temporary = ownCleanup;
|
|
187
|
+
ownCleanup = undefined;
|
|
188
|
+
request = undefined;
|
|
189
|
+
temporary?.dispose();
|
|
190
|
+
return failure;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function install() {
|
|
194
|
+
if (!canChange(frame)) return fail("Element input target became inaccessible or restricted");
|
|
195
|
+
const hookOnly = borrowed || protectedFrame(frame);
|
|
196
|
+
function listen<Script extends keyof CommonScripts>(script: Script,
|
|
197
|
+
callback: (this: void, self: Frame, ...args: CommonScripts[Script]) => void) {
|
|
198
|
+
return installScript(() => frame.GetScript?.(script), value => frame.SetScript(script, value),
|
|
199
|
+
handler => hookScript(frame, script, handler, owner), callback, hookOnly, restorers);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (disabledStyle !== undefined) {
|
|
203
|
+
function watchEnabled(native: Button, script: "OnEnable" | "OnDisable", disabled: boolean) {
|
|
204
|
+
return installScript(() => native.GetScript?.(script), value => native.SetScript(script, value),
|
|
205
|
+
handler => hookScript(native, script, handler, owner),
|
|
206
|
+
() => { if (live()) style?.set("disabled", disabled); }, hookOnly, restorers);
|
|
207
|
+
}
|
|
208
|
+
const failure = watchEnabled(disabledStyle, "OnEnable", false) ?? watchEnabled(disabledStyle, "OnDisable", true);
|
|
209
|
+
if (failure !== undefined) return failure;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (input.onClick !== undefined && button !== undefined) {
|
|
213
|
+
const click = input.onClick;
|
|
214
|
+
const failure = installScript(() => button.GetScript?.("OnClick"), value => button.SetScript("OnClick", value),
|
|
215
|
+
handler => hookButtonClick(button, handler, owner), (_self, raw) => {
|
|
216
|
+
if (!live() || isSecret(raw)) return;
|
|
217
|
+
const event = readClickEvent(raw === undefined ? "LeftButton" : raw);
|
|
218
|
+
if (event !== undefined) click(event);
|
|
219
|
+
}, hookOnly, restorers);
|
|
220
|
+
if (failure !== undefined) return failure;
|
|
221
|
+
}
|
|
222
|
+
if (pressed !== undefined) {
|
|
223
|
+
const failure = listen("OnMouseDown", (_self, raw) => {
|
|
224
|
+
if (!live() || reveal(frame.IsMouseOver()) !== true) return;
|
|
225
|
+
const key = readMouseButton(raw);
|
|
226
|
+
if (key === undefined) return;
|
|
227
|
+
pressed.add(key);
|
|
228
|
+
if (activeStyle) style?.set("active", true);
|
|
229
|
+
}) ?? listen("OnDragStart", () => {
|
|
230
|
+
if (!live()) return;
|
|
231
|
+
pressed.clear();
|
|
232
|
+
if (activeStyle) style?.set("active", false);
|
|
233
|
+
}) ?? listen("OnMouseUp", (_self, raw) => {
|
|
234
|
+
if (!live()) return;
|
|
235
|
+
const key = readMouseButton(raw);
|
|
236
|
+
if (key === undefined || !pressed.delete(key)) return;
|
|
237
|
+
const over = reveal(frame.IsMouseOver()) === true;
|
|
238
|
+
if (activeStyle) style?.set("active", over && pressed.size > 0);
|
|
239
|
+
if (button !== undefined || input.onClick === undefined || !over || reveal(frame.IsDragging?.()) === true) return;
|
|
240
|
+
const event = readClickEvent(key);
|
|
241
|
+
if (event !== undefined) input.onClick(event);
|
|
242
|
+
});
|
|
243
|
+
if (failure !== undefined) return failure;
|
|
244
|
+
}
|
|
245
|
+
if (motion) {
|
|
246
|
+
const failure = listen("OnEnter", () => { if (live()) enter(); })
|
|
247
|
+
?? listen("OnLeave", () => {
|
|
248
|
+
if (!live()) return;
|
|
249
|
+
closeTip();
|
|
250
|
+
if (hover) style?.set("hover", false);
|
|
251
|
+
if (activeStyle) style?.set("active", false);
|
|
252
|
+
input.onPointerLeave?.();
|
|
253
|
+
});
|
|
254
|
+
if (failure !== undefined) return failure;
|
|
255
|
+
}
|
|
256
|
+
if (observesVisibility) {
|
|
257
|
+
const failure = listen("OnShow", () => { synchronize(true); })
|
|
258
|
+
?? listen("OnHide", () => { synchronize(true); }) ?? synchronize(false);
|
|
259
|
+
if (failure !== undefined) return failure;
|
|
260
|
+
}
|
|
261
|
+
const click = input.onClick !== undefined || activeStyle || input.propagateClicks === false;
|
|
262
|
+
if (!preserveNativeClicks && (click || motion) && typeof frame.SetMouseClickEnabled === "function") {
|
|
263
|
+
const nativeClick = frame.GetScript?.("OnMouseDown") !== undefined || frame.GetScript?.("OnMouseUp") !== undefined
|
|
264
|
+
|| frame.GetScript?.("OnDragStart") !== undefined || button?.GetScript?.("OnClick") !== undefined;
|
|
265
|
+
if (click || !nativeClick) {
|
|
266
|
+
changeInputFlag(() => reveal(frame.IsMouseClickEnabled?.()), state => frame.SetMouseClickEnabled(state), click, restorers);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
if (motion && typeof frame.SetMouseMotionEnabled === "function") {
|
|
270
|
+
changeInputFlag(() => reveal(frame.IsMouseMotionEnabled?.()), state => frame.SetMouseMotionEnabled(state), true, restorers);
|
|
271
|
+
}
|
|
272
|
+
let propagate = input.propagateClicks;
|
|
273
|
+
if (!preserveNativeClicks && propagate === undefined && activeStyle && input.onClick === undefined
|
|
274
|
+
&& button === undefined && !borrowed) propagate = true;
|
|
275
|
+
if (click && propagate !== undefined && typeof frame.SetPropagateMouseClicks === "function") {
|
|
276
|
+
changeInputFlag(() => reveal(frame.CanPropagateMouseClicks?.()), state => frame.SetPropagateMouseClicks(state),
|
|
277
|
+
propagate, restorers);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function update() {
|
|
282
|
+
if (!live()) return restore();
|
|
283
|
+
const configured = protect(install);
|
|
284
|
+
const failure = configured.ok ? configured.value : configured;
|
|
285
|
+
if (failure === undefined) return;
|
|
286
|
+
active = false;
|
|
287
|
+
closeTip();
|
|
288
|
+
lease?.cancel();
|
|
289
|
+
report(restore());
|
|
290
|
+
return failure;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function reconcile(): Result<void> {
|
|
294
|
+
if (protectedFrame(frame)) {
|
|
295
|
+
if (request === undefined) {
|
|
296
|
+
const lifetime = coordinator?.scope ?? (ownCleanup = newScope());
|
|
297
|
+
const queue = createProtectedLayoutUpdate(update, { scope: lifetime, label: "Element input scripts" });
|
|
298
|
+
if (!queue.ok) { ownCleanup?.dispose(); return queue; }
|
|
299
|
+
request = queue.value.request;
|
|
300
|
+
}
|
|
301
|
+
return request();
|
|
302
|
+
}
|
|
303
|
+
const configured = protect(update);
|
|
304
|
+
return configured.ok ? configured.value ?? ok(undefined) : configured;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const cleanup = owner.onDispose(() => {
|
|
308
|
+
active = false;
|
|
309
|
+
closeTip();
|
|
310
|
+
lease?.cancel();
|
|
311
|
+
if (restorers.length === 0 && request === undefined) return;
|
|
312
|
+
report(request?.() ?? reconcile());
|
|
313
|
+
});
|
|
314
|
+
if (!cleanup.ok) return cleanup;
|
|
315
|
+
const result = reconcile();
|
|
316
|
+
return result.ok ? undefined : result;
|
|
317
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type { Cancel } from "../src";
|
|
2
|
+
import { ownedArray, type OwnedArray, type OwnedMap, type OwnedSet } from "../src/owned-collections";
|
|
3
|
+
import { fail, ok, type Result } from "../src/result";
|
|
4
|
+
import type { EventApi, EventOptions, EventScope, WoWEvent, WoWEventMap } from "./contract";
|
|
5
|
+
import { createScopedEvents } from "./events";
|
|
6
|
+
import { createFrame, type Frame } from "./frames";
|
|
7
|
+
import { api, reveal } from "./native";
|
|
8
|
+
import { protectNative } from "./protect";
|
|
9
|
+
|
|
10
|
+
type Dispatch = (this: void, event: WoWEvent, ...payload: unknown[]) => void;
|
|
11
|
+
|
|
12
|
+
export type EventDispatcherFrame = Pick<Frame, "IsEventRegistered" | "UnregisterEvent"> & {
|
|
13
|
+
RegisterEvent(this: object, event: WoWEvent): boolean;
|
|
14
|
+
RegisterUnitEvent(this: object, event: WoWEvent, ...units: string[]): boolean;
|
|
15
|
+
SetScript(this: object, script: "OnEvent", handler: (this: void, frame: EventDispatcherFrame,
|
|
16
|
+
event: WoWEvent, ...payload: unknown[]) => void): void;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
interface Listener {
|
|
20
|
+
dispatch?: Dispatch;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface Channel {
|
|
24
|
+
frame: EventDispatcherFrame;
|
|
25
|
+
listeners: OwnedMap<WoWEvent, OwnedSet<Listener>>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function nativeFailure(problem: unknown, fallback: string) {
|
|
29
|
+
const reason = reveal(problem);
|
|
30
|
+
return fail(typeof reason === "string" ? reason : fallback);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function report(problem: unknown) {
|
|
34
|
+
protectNative(() => api.geterrorhandler()(problem));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function createChannel(makeFrame: (this: void) => EventDispatcherFrame): Result<Channel> {
|
|
38
|
+
const listeners = new Map<WoWEvent, OwnedSet<Listener>>() as OwnedMap<WoWEvent, OwnedSet<Listener>>;
|
|
39
|
+
const [success, frame] = protectNative(() => {
|
|
40
|
+
const native = makeFrame();
|
|
41
|
+
native.SetScript("OnEvent", (_, event, ...payload) => {
|
|
42
|
+
const list = listeners.get(event);
|
|
43
|
+
if (list === undefined || list.size === 0) return;
|
|
44
|
+
const single = list.size === 1;
|
|
45
|
+
const delivery: Iterable<Listener> = single ? list : [...list];
|
|
46
|
+
for (const listener of delivery) {
|
|
47
|
+
const dispatch = listener.dispatch;
|
|
48
|
+
if (dispatch !== undefined) {
|
|
49
|
+
const [delivered, problem] = protectNative(dispatch, event, ...payload);
|
|
50
|
+
if (!delivered) report(problem);
|
|
51
|
+
}
|
|
52
|
+
// New listeners join the next native delivery, even when the first callback adds one.
|
|
53
|
+
if (single) break;
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
return native;
|
|
57
|
+
});
|
|
58
|
+
return success ? ok({ frame, listeners }) : nativeFailure(frame, "Could not create a native event frame");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function unregister(channel: Channel, event: WoWEvent) {
|
|
62
|
+
channel.listeners.delete(event);
|
|
63
|
+
const [success, result] = protectNative(() => channel.frame.UnregisterEvent(event));
|
|
64
|
+
if (!success) report(result);
|
|
65
|
+
else if (reveal(result) !== true) report(`Could not unregister native event ${event}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function ensureListeners(channel: Channel, event: WoWEvent, units: OwnedArray<string>): Result<OwnedSet<Listener>> {
|
|
69
|
+
const existing = channel.listeners.get(event);
|
|
70
|
+
if (existing !== undefined) return ok(existing);
|
|
71
|
+
const { frame } = channel;
|
|
72
|
+
const [success, result] = protectNative(() => {
|
|
73
|
+
if (units.length === 0) return frame.RegisterEvent(event);
|
|
74
|
+
return frame.RegisterUnitEvent(event, ...units);
|
|
75
|
+
});
|
|
76
|
+
if (!success || reveal(result) !== true) {
|
|
77
|
+
// A native call can fail after installing its registration. This frame is exclusively ours.
|
|
78
|
+
const [inspected, registered] = protectNative(() => {
|
|
79
|
+
const [active] = frame.IsEventRegistered(event);
|
|
80
|
+
return active;
|
|
81
|
+
});
|
|
82
|
+
if (inspected && reveal(registered) === true) unregister(channel, event);
|
|
83
|
+
const reason = `Could not register native event ${event}`;
|
|
84
|
+
return success ? fail(reason) : nativeFailure(result, reason);
|
|
85
|
+
}
|
|
86
|
+
const listeners = new Set<Listener>() as OwnedSet<Listener>;
|
|
87
|
+
channel.listeners.set(event, listeners);
|
|
88
|
+
return ok(listeners);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function groupUnits(options?: EventOptions): Result<OwnedArray<OwnedArray<string>>> {
|
|
92
|
+
const units = (options?.units ?? (options?.unit === undefined ? undefined : [options.unit])) as OwnedArray<string> | undefined;
|
|
93
|
+
if (units === undefined) return ok([ownedArray<string>()] as OwnedArray<OwnedArray<string>>);
|
|
94
|
+
const seen = new Set<string>();
|
|
95
|
+
for (const unit of units) {
|
|
96
|
+
const token = reveal(unit);
|
|
97
|
+
if (typeof token !== "string" || token === "") return fail("Event unit filters require nonempty readable unit tokens");
|
|
98
|
+
seen.add(token.toLowerCase());
|
|
99
|
+
}
|
|
100
|
+
const sorted = [...seen].sort();
|
|
101
|
+
const groups = ownedArray<OwnedArray<string>>();
|
|
102
|
+
for (let i = 0; i < sorted.length; i += 2) groups.push(sorted.slice(i, i + 2) as OwnedArray<string>);
|
|
103
|
+
return ok(groups);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Create once per host. Native unit delivery order is preserved within each frame.
|
|
107
|
+
* Host profiling labels and coalesced payloads remain features of the legacy adapter.
|
|
108
|
+
*/
|
|
109
|
+
export function createEventDispatcher(this: void,
|
|
110
|
+
makeFrame: (this: void) => EventDispatcherFrame = () => createFrame("Frame") as Frame & EventDispatcherFrame): EventApi {
|
|
111
|
+
const channels = new Map<string, Channel>();
|
|
112
|
+
return createScopedEvents(<E extends WoWEvent>(_owner: EventScope, event: E,
|
|
113
|
+
handler: (this: void, event: E, ...payload: WoWEventMap[E]) => void, options?: EventOptions): Result<Cancel> => {
|
|
114
|
+
if (options?.coalesce !== undefined) return fail("Native event subscriptions do not support coalescing");
|
|
115
|
+
const groups = groupUnits(options);
|
|
116
|
+
if (!groups.ok) return groups;
|
|
117
|
+
|
|
118
|
+
const listener: Listener = { dispatch: handler as Dispatch };
|
|
119
|
+
const subscriptions: { channel: Channel; list: OwnedSet<Listener> }[] = [];
|
|
120
|
+
function cancel() {
|
|
121
|
+
listener.dispatch = undefined;
|
|
122
|
+
for (const subscription of subscriptions) {
|
|
123
|
+
const { channel, list } = subscription;
|
|
124
|
+
list.delete(listener);
|
|
125
|
+
if (list.size === 0) unregister(channel, event);
|
|
126
|
+
}
|
|
127
|
+
subscriptions.length = 0;
|
|
128
|
+
}
|
|
129
|
+
for (const units of groups.value) {
|
|
130
|
+
const key = units.length === 0 ? "" : `${units[0].length}:${units.join("")}`;
|
|
131
|
+
let channel = channels.get(key);
|
|
132
|
+
if (channel === undefined) {
|
|
133
|
+
const created = createChannel(makeFrame);
|
|
134
|
+
if (!created.ok) {
|
|
135
|
+
cancel();
|
|
136
|
+
return created;
|
|
137
|
+
}
|
|
138
|
+
channel = created.value;
|
|
139
|
+
channels.set(key, channel);
|
|
140
|
+
}
|
|
141
|
+
const result = ensureListeners(channel, event, units);
|
|
142
|
+
if (!result.ok) {
|
|
143
|
+
cancel();
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
subscriptions.push({ channel, list: result.value });
|
|
147
|
+
}
|
|
148
|
+
for (const subscription of subscriptions) subscription.list.add(listener);
|
|
149
|
+
return ok(cancel);
|
|
150
|
+
});
|
|
151
|
+
}
|