tokensafe-ui 0.1.6 → 0.1.7
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/README.md +40 -6
- package/bin/cli.js +62 -4
- package/dist/{chunk-QNP65TV7.js → chunk-GJWCBVCA.js} +123 -2
- package/dist/chunk-JQRAF2JG.js +2033 -0
- package/dist/{chunk-UESQTIJY.js → chunk-N7XIVYCL.js} +75 -0
- package/dist/chunk-OE2N2PMS.js +1151 -0
- package/dist/chunk-UXZDGOF5.js +15 -0
- package/dist/chunk-W4DCPVD6.js +980 -0
- package/dist/cli.cjs +2373 -0
- package/dist/cli.d.cts +3 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +205 -0
- package/dist/index.cjs +1113 -160
- package/dist/index.d.cts +5 -5
- package/dist/index.d.ts +5 -5
- package/dist/index.js +37 -3
- package/dist/mcp.cjs +1147 -133
- package/dist/mcp.d.cts +1 -1
- package/dist/mcp.d.ts +1 -1
- package/dist/mcp.js +9 -12
- package/dist/observer.cjs +346 -4
- package/dist/observer.d.cts +1 -1
- package/dist/observer.d.ts +1 -1
- package/dist/observer.js +6 -4
- package/dist/oumv.cjs +2293 -246
- package/dist/oumv.d.cts +1 -1
- package/dist/oumv.d.ts +1 -1
- package/dist/oumv.js +25 -121
- package/dist/patcher.cjs +78 -2
- package/dist/patcher.d.cts +1 -1
- package/dist/patcher.d.ts +1 -1
- package/dist/patcher.js +5 -3
- package/dist/react.cjs +2856 -0
- package/dist/react.d.cts +1 -0
- package/dist/react.d.ts +1 -0
- package/dist/react.js +24 -0
- package/dist/testing.cjs +262 -2
- package/dist/testing.d.cts +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +1 -1
- package/dist/vite.cjs +1359 -14
- package/dist/vite.d.cts +2 -2
- package/dist/vite.d.ts +2 -2
- package/dist/vite.js +167 -15
- package/dist/vue.cjs +2524 -0
- package/dist/vue.d.cts +1 -0
- package/dist/vue.d.ts +1 -0
- package/dist/vue.js +10 -0
- package/package.json +17 -7
- package/dist/chunk-WZVWZB6L.js +0 -1097
- package/dist/chunk-ZRW74NHE.js +0 -560
|
@@ -0,0 +1,2033 @@
|
|
|
1
|
+
// ../core/dist/index.js
|
|
2
|
+
import { z as z3 } from "zod";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { z as z2 } from "zod";
|
|
5
|
+
var GLOBAL_KEYS = {
|
|
6
|
+
ACTIVE_INSTANCES: /* @__PURE__ */ Symbol.for("tokensafe-ui.activeInstances"),
|
|
7
|
+
RUNTIME_SIGNALS: /* @__PURE__ */ Symbol.for("tokensafe-ui.runtimeSignals"),
|
|
8
|
+
CAUSALITY_GRAPH: /* @__PURE__ */ Symbol.for("tokensafe-ui.causalityGraph"),
|
|
9
|
+
TELEMETRY_LOG: /* @__PURE__ */ Symbol.for("tokensafe-ui.telemetryLog"),
|
|
10
|
+
RUNTIME_VERSION: /* @__PURE__ */ Symbol.for("tokensafe-ui.runtimeVersion"),
|
|
11
|
+
HEADLESS_DOM_INITIALIZED: /* @__PURE__ */ Symbol.for("tokensafe-ui.headlessDomInitialized")
|
|
12
|
+
};
|
|
13
|
+
function getGlobalRegistry(key, factory) {
|
|
14
|
+
const g = globalThis;
|
|
15
|
+
if (!g[key]) {
|
|
16
|
+
g[key] = factory();
|
|
17
|
+
}
|
|
18
|
+
return g[key];
|
|
19
|
+
}
|
|
20
|
+
function checkMultiInstanceGuard(moduleVersion) {
|
|
21
|
+
const g = globalThis;
|
|
22
|
+
const existing = g[GLOBAL_KEYS.RUNTIME_VERSION];
|
|
23
|
+
if (existing && existing !== moduleVersion) {
|
|
24
|
+
if (typeof console !== "undefined" && console.warn) {
|
|
25
|
+
console.warn(
|
|
26
|
+
`[tokensafe-ui] Multi-Instance Warning: Detected competing versions (${existing} vs ${moduleVersion}). Synchronizing shared global state.`
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
g[GLOBAL_KEYS.RUNTIME_VERSION] = moduleVersion;
|
|
31
|
+
}
|
|
32
|
+
checkMultiInstanceGuard("0.2.0");
|
|
33
|
+
var subscriberCounter = 0;
|
|
34
|
+
var signalCounter = 0;
|
|
35
|
+
var scopeCounter = 0;
|
|
36
|
+
var activeEffect = null;
|
|
37
|
+
var activeScope = null;
|
|
38
|
+
function getActiveScope() {
|
|
39
|
+
return activeScope;
|
|
40
|
+
}
|
|
41
|
+
var ReactiveScopeImpl = class {
|
|
42
|
+
id;
|
|
43
|
+
parent;
|
|
44
|
+
children = /* @__PURE__ */ new Set();
|
|
45
|
+
cleanups = /* @__PURE__ */ new Set();
|
|
46
|
+
disposables = /* @__PURE__ */ new Set();
|
|
47
|
+
isDisposed = false;
|
|
48
|
+
constructor(parent = null, namePrefix = "scope") {
|
|
49
|
+
this.id = `${namePrefix}_${++scopeCounter}`;
|
|
50
|
+
this.parent = parent;
|
|
51
|
+
}
|
|
52
|
+
run(fn) {
|
|
53
|
+
if (this.isDisposed) {
|
|
54
|
+
throw new Error(`Cannot run in a disposed ReactiveScope (${this.id})`);
|
|
55
|
+
}
|
|
56
|
+
const prevScope = activeScope;
|
|
57
|
+
activeScope = this;
|
|
58
|
+
try {
|
|
59
|
+
return fn();
|
|
60
|
+
} finally {
|
|
61
|
+
activeScope = prevScope;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
dispose() {
|
|
65
|
+
if (this.isDisposed) return;
|
|
66
|
+
this.isDisposed = true;
|
|
67
|
+
if (this.parent) {
|
|
68
|
+
this.parent.children.delete(this);
|
|
69
|
+
}
|
|
70
|
+
for (const child of Array.from(this.children)) {
|
|
71
|
+
try {
|
|
72
|
+
child.dispose();
|
|
73
|
+
} catch (err) {
|
|
74
|
+
console.error(`Error disposing child scope '${child.id}':`, err);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
this.children.clear();
|
|
78
|
+
for (const item of Array.from(this.disposables)) {
|
|
79
|
+
try {
|
|
80
|
+
item.dispose();
|
|
81
|
+
} catch (err) {
|
|
82
|
+
console.error(`Error disposing scoped item:`, err);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
this.disposables.clear();
|
|
86
|
+
for (const cleanup of Array.from(this.cleanups)) {
|
|
87
|
+
try {
|
|
88
|
+
cleanup();
|
|
89
|
+
} catch (err) {
|
|
90
|
+
console.error(`Error executing scope cleanup '${this.id}':`, err);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
this.cleanups.clear();
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
function createRoot(fn) {
|
|
97
|
+
const scope = new ReactiveScopeImpl(null, "root");
|
|
98
|
+
const prevScope = activeScope;
|
|
99
|
+
activeScope = scope;
|
|
100
|
+
try {
|
|
101
|
+
return fn(() => scope.dispose());
|
|
102
|
+
} finally {
|
|
103
|
+
activeScope = prevScope;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function createScope(fn) {
|
|
107
|
+
const scope = new ReactiveScopeImpl(activeScope, "scope");
|
|
108
|
+
if (activeScope && !activeScope.isDisposed) {
|
|
109
|
+
activeScope.children.add(scope);
|
|
110
|
+
}
|
|
111
|
+
const prevScope = activeScope;
|
|
112
|
+
activeScope = scope;
|
|
113
|
+
try {
|
|
114
|
+
const result = fn(() => scope.dispose());
|
|
115
|
+
return {
|
|
116
|
+
result,
|
|
117
|
+
dispose: () => scope.dispose(),
|
|
118
|
+
scope
|
|
119
|
+
};
|
|
120
|
+
} finally {
|
|
121
|
+
activeScope = prevScope;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function onCleanup(fn) {
|
|
125
|
+
if (activeScope && !activeScope.isDisposed) {
|
|
126
|
+
activeScope.cleanups.add(fn);
|
|
127
|
+
} else if (activeEffect && !activeEffect.isDisposed) {
|
|
128
|
+
activeEffect.registerCleanup(fn);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function untrack(fn) {
|
|
132
|
+
const prevEffect = activeEffect;
|
|
133
|
+
activeEffect = null;
|
|
134
|
+
try {
|
|
135
|
+
return fn();
|
|
136
|
+
} finally {
|
|
137
|
+
activeEffect = prevEffect;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
var Scheduler = class _Scheduler {
|
|
141
|
+
isBatching = false;
|
|
142
|
+
isFlushing = false;
|
|
143
|
+
computedBuckets = /* @__PURE__ */ new Map();
|
|
144
|
+
effectsQueue = /* @__PURE__ */ new Set();
|
|
145
|
+
domQueue = /* @__PURE__ */ new Set();
|
|
146
|
+
static MAX_FLUSH_CYCLES = 100;
|
|
147
|
+
schedule(sub) {
|
|
148
|
+
if (sub.isDisposed) return;
|
|
149
|
+
if (sub.isRunning) {
|
|
150
|
+
sub.recordSelfSchedule();
|
|
151
|
+
}
|
|
152
|
+
if (sub.kind === "computed") {
|
|
153
|
+
let bucket = this.computedBuckets.get(sub.level);
|
|
154
|
+
if (!bucket) {
|
|
155
|
+
bucket = /* @__PURE__ */ new Set();
|
|
156
|
+
this.computedBuckets.set(sub.level, bucket);
|
|
157
|
+
}
|
|
158
|
+
bucket.add(sub);
|
|
159
|
+
} else if (sub.kind === "dom_binding") {
|
|
160
|
+
this.domQueue.add(sub);
|
|
161
|
+
} else {
|
|
162
|
+
this.effectsQueue.add(sub);
|
|
163
|
+
}
|
|
164
|
+
if (!this.isBatching && !this.isFlushing) {
|
|
165
|
+
this.flush();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
batch(fn) {
|
|
169
|
+
const wasBatching = this.isBatching;
|
|
170
|
+
this.isBatching = true;
|
|
171
|
+
try {
|
|
172
|
+
return fn();
|
|
173
|
+
} finally {
|
|
174
|
+
this.isBatching = wasBatching;
|
|
175
|
+
if (!this.isBatching && !this.isFlushing) {
|
|
176
|
+
this.flush();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
hasPending() {
|
|
181
|
+
if (this.effectsQueue.size > 0 || this.domQueue.size > 0) return true;
|
|
182
|
+
for (const bucket of this.computedBuckets.values()) {
|
|
183
|
+
if (bucket.size > 0) return true;
|
|
184
|
+
}
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
clearAll() {
|
|
188
|
+
this.computedBuckets.clear();
|
|
189
|
+
this.effectsQueue.clear();
|
|
190
|
+
this.domQueue.clear();
|
|
191
|
+
}
|
|
192
|
+
flush() {
|
|
193
|
+
if (this.isFlushing) return;
|
|
194
|
+
this.isFlushing = true;
|
|
195
|
+
let cycles = 0;
|
|
196
|
+
const errors = [];
|
|
197
|
+
try {
|
|
198
|
+
while (this.hasPending()) {
|
|
199
|
+
cycles++;
|
|
200
|
+
if (cycles > _Scheduler.MAX_FLUSH_CYCLES) {
|
|
201
|
+
this.clearAll();
|
|
202
|
+
throw new Error(
|
|
203
|
+
`[TokenSafe Reactive Cycle] Maximum scheduler flush iterations (${_Scheduler.MAX_FLUSH_CYCLES}) exceeded. Infinite reactive loop detected.`
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
let hasComputeds = true;
|
|
207
|
+
while (hasComputeds) {
|
|
208
|
+
const sortedLevels = Array.from(this.computedBuckets.keys()).filter((lvl) => (this.computedBuckets.get(lvl)?.size || 0) > 0).sort((a, b) => a - b);
|
|
209
|
+
if (sortedLevels.length === 0) {
|
|
210
|
+
hasComputeds = false;
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
const lowestLevel = sortedLevels[0];
|
|
214
|
+
const bucket = this.computedBuckets.get(lowestLevel);
|
|
215
|
+
const runner = bucket.values().next().value;
|
|
216
|
+
bucket.delete(runner);
|
|
217
|
+
if (!runner.isDisposed) {
|
|
218
|
+
try {
|
|
219
|
+
runner.run();
|
|
220
|
+
} catch (err) {
|
|
221
|
+
errors.push(err);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
if (this.effectsQueue.size > 0) {
|
|
226
|
+
const effectsToRun = Array.from(this.effectsQueue);
|
|
227
|
+
this.effectsQueue.clear();
|
|
228
|
+
for (const eff of effectsToRun) {
|
|
229
|
+
if (!eff.isDisposed) {
|
|
230
|
+
try {
|
|
231
|
+
eff.run();
|
|
232
|
+
} catch (err) {
|
|
233
|
+
errors.push(err);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
if (this.domQueue.size > 0) {
|
|
240
|
+
const domToRun = Array.from(this.domQueue);
|
|
241
|
+
this.domQueue.clear();
|
|
242
|
+
for (const d of domToRun) {
|
|
243
|
+
if (!d.isDisposed) {
|
|
244
|
+
try {
|
|
245
|
+
d.run();
|
|
246
|
+
} catch (err) {
|
|
247
|
+
errors.push(err);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
} finally {
|
|
254
|
+
this.isFlushing = false;
|
|
255
|
+
if (errors.length > 0) {
|
|
256
|
+
if (errors.length === 1) {
|
|
257
|
+
throw errors[0];
|
|
258
|
+
}
|
|
259
|
+
const multiError = new Error(
|
|
260
|
+
`[TokenSafe Reactive Error] Multiple errors occurred during reactive scheduler flush:
|
|
261
|
+
` + errors.map((e) => e instanceof Error ? e.message : String(e)).join("\n")
|
|
262
|
+
);
|
|
263
|
+
multiError.errors = errors;
|
|
264
|
+
throw multiError;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
var scheduler = new Scheduler();
|
|
270
|
+
var EffectRunner = class _EffectRunner {
|
|
271
|
+
id;
|
|
272
|
+
name;
|
|
273
|
+
kind;
|
|
274
|
+
level = 1;
|
|
275
|
+
ownerScope = null;
|
|
276
|
+
fn;
|
|
277
|
+
cleanups = [];
|
|
278
|
+
dependencies = /* @__PURE__ */ new Set();
|
|
279
|
+
isDisposed = false;
|
|
280
|
+
isRunning = false;
|
|
281
|
+
runCount = 0;
|
|
282
|
+
scheduledSelf = false;
|
|
283
|
+
static MAX_RECURSION = 50;
|
|
284
|
+
constructor(fn, options = {}) {
|
|
285
|
+
this.fn = fn;
|
|
286
|
+
this.id = options.id || `sub_${++subscriberCounter}`;
|
|
287
|
+
this.name = options.name;
|
|
288
|
+
this.kind = options.kind || "effect";
|
|
289
|
+
this.ownerScope = activeScope && !activeScope.isDisposed ? activeScope : null;
|
|
290
|
+
if (this.ownerScope) {
|
|
291
|
+
this.ownerScope.disposables.add(this);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
getDependencies() {
|
|
295
|
+
return new Set(this.dependencies);
|
|
296
|
+
}
|
|
297
|
+
registerCleanup(cleanup) {
|
|
298
|
+
this.cleanups.push(cleanup);
|
|
299
|
+
}
|
|
300
|
+
recordSelfSchedule() {
|
|
301
|
+
this.scheduledSelf = true;
|
|
302
|
+
this.runCount++;
|
|
303
|
+
if (this.runCount > _EffectRunner.MAX_RECURSION) {
|
|
304
|
+
throw new Error(
|
|
305
|
+
`[TokenSafe Reactive Cycle] Maximum reactive update depth (${_EffectRunner.MAX_RECURSION}) exceeded in effect '${this.name || this.id}'. Possible infinite loop detected.`
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
run() {
|
|
310
|
+
if (this.isDisposed) return;
|
|
311
|
+
this.scheduledSelf = false;
|
|
312
|
+
this.isRunning = true;
|
|
313
|
+
this.cleanupPrev();
|
|
314
|
+
for (const dep of this.dependencies) {
|
|
315
|
+
dep.removeSubscriber(this);
|
|
316
|
+
}
|
|
317
|
+
this.dependencies.clear();
|
|
318
|
+
const prevEffect = activeEffect;
|
|
319
|
+
activeEffect = this;
|
|
320
|
+
try {
|
|
321
|
+
const res = this.fn();
|
|
322
|
+
if (typeof res === "function") {
|
|
323
|
+
this.cleanups.push(res);
|
|
324
|
+
}
|
|
325
|
+
let maxDepLevel = 0;
|
|
326
|
+
for (const dep of this.dependencies) {
|
|
327
|
+
if (dep.level > maxDepLevel) {
|
|
328
|
+
maxDepLevel = dep.level;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
this.level = this.dependencies.size === 0 ? 1 : maxDepLevel + 1;
|
|
332
|
+
} finally {
|
|
333
|
+
activeEffect = prevEffect;
|
|
334
|
+
this.isRunning = false;
|
|
335
|
+
if (!this.scheduledSelf) {
|
|
336
|
+
this.runCount = 0;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
cleanupPrev() {
|
|
341
|
+
if (this.cleanups.length > 0) {
|
|
342
|
+
const toRun = [...this.cleanups];
|
|
343
|
+
this.cleanups = [];
|
|
344
|
+
for (const c of toRun) {
|
|
345
|
+
try {
|
|
346
|
+
c();
|
|
347
|
+
} catch (err) {
|
|
348
|
+
console.error(`Error during effect cleanup '${this.name || this.id}':`, err);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
dispose() {
|
|
354
|
+
if (this.isDisposed) return;
|
|
355
|
+
this.isDisposed = true;
|
|
356
|
+
this.cleanupPrev();
|
|
357
|
+
for (const dep of this.dependencies) {
|
|
358
|
+
dep.removeSubscriber(this);
|
|
359
|
+
}
|
|
360
|
+
this.dependencies.clear();
|
|
361
|
+
if (this.ownerScope) {
|
|
362
|
+
this.ownerScope.disposables.delete(this);
|
|
363
|
+
this.ownerScope = null;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
var SignalImpl = class {
|
|
368
|
+
id;
|
|
369
|
+
name;
|
|
370
|
+
level = 0;
|
|
371
|
+
_val;
|
|
372
|
+
subscribers = /* @__PURE__ */ new Set();
|
|
373
|
+
constructor(initialVal, options = {}) {
|
|
374
|
+
this._val = initialVal;
|
|
375
|
+
this.id = options.id || `sig_${++signalCounter}`;
|
|
376
|
+
this.name = options.name;
|
|
377
|
+
}
|
|
378
|
+
get value() {
|
|
379
|
+
if (activeEffect && !activeEffect.isDisposed) {
|
|
380
|
+
this.subscribers.add(activeEffect);
|
|
381
|
+
activeEffect.dependencies.add(this);
|
|
382
|
+
}
|
|
383
|
+
return this._val;
|
|
384
|
+
}
|
|
385
|
+
set value(newVal) {
|
|
386
|
+
if (Object.is(this._val, newVal)) return;
|
|
387
|
+
this._val = newVal;
|
|
388
|
+
batch(() => {
|
|
389
|
+
this.notify();
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
peek() {
|
|
393
|
+
return this._val;
|
|
394
|
+
}
|
|
395
|
+
subscribe(fn) {
|
|
396
|
+
return effect(
|
|
397
|
+
() => {
|
|
398
|
+
fn(this.value);
|
|
399
|
+
},
|
|
400
|
+
{ name: this.name ? `sub:${this.name}` : void 0 }
|
|
401
|
+
);
|
|
402
|
+
}
|
|
403
|
+
getSubscribers() {
|
|
404
|
+
return new Set(this.subscribers);
|
|
405
|
+
}
|
|
406
|
+
removeSubscriber(sub) {
|
|
407
|
+
this.subscribers.delete(sub);
|
|
408
|
+
}
|
|
409
|
+
notify() {
|
|
410
|
+
for (const sub of Array.from(this.subscribers)) {
|
|
411
|
+
if (sub.isDisposed) {
|
|
412
|
+
this.subscribers.delete(sub);
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
scheduler.schedule(sub);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
function signal(initialValue, options) {
|
|
420
|
+
return new SignalImpl(initialValue, options);
|
|
421
|
+
}
|
|
422
|
+
function computed(fn, options) {
|
|
423
|
+
const compSignal = new SignalImpl(void 0, options);
|
|
424
|
+
let isDisposed = false;
|
|
425
|
+
const ownerScope = activeScope && !activeScope.isDisposed ? activeScope : null;
|
|
426
|
+
const runner = new EffectRunner(
|
|
427
|
+
() => {
|
|
428
|
+
const newVal = fn();
|
|
429
|
+
let maxDep = 0;
|
|
430
|
+
for (const dep of runner.dependencies) {
|
|
431
|
+
if (dep.level > maxDep) {
|
|
432
|
+
maxDep = dep.level;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
runner.level = runner.dependencies.size === 0 ? 1 : maxDep + 1;
|
|
436
|
+
compSignal.level = runner.level;
|
|
437
|
+
compSignal.value = newVal;
|
|
438
|
+
},
|
|
439
|
+
{
|
|
440
|
+
id: options?.id ? `comp_${options.id}` : void 0,
|
|
441
|
+
name: options?.name,
|
|
442
|
+
kind: "computed"
|
|
443
|
+
}
|
|
444
|
+
);
|
|
445
|
+
runner.run();
|
|
446
|
+
const computedObj = {
|
|
447
|
+
id: compSignal.id,
|
|
448
|
+
name: compSignal.name,
|
|
449
|
+
get level() {
|
|
450
|
+
return compSignal.level;
|
|
451
|
+
},
|
|
452
|
+
get value() {
|
|
453
|
+
return compSignal.value;
|
|
454
|
+
},
|
|
455
|
+
peek() {
|
|
456
|
+
return compSignal.peek();
|
|
457
|
+
},
|
|
458
|
+
subscribe(cb) {
|
|
459
|
+
return compSignal.subscribe(cb);
|
|
460
|
+
},
|
|
461
|
+
getSubscribers() {
|
|
462
|
+
return compSignal.getSubscribers();
|
|
463
|
+
},
|
|
464
|
+
getDependencies() {
|
|
465
|
+
return runner.getDependencies();
|
|
466
|
+
},
|
|
467
|
+
get isDisposed() {
|
|
468
|
+
return isDisposed;
|
|
469
|
+
},
|
|
470
|
+
dispose() {
|
|
471
|
+
if (isDisposed) return;
|
|
472
|
+
isDisposed = true;
|
|
473
|
+
runner.dispose();
|
|
474
|
+
if (ownerScope && !ownerScope.isDisposed) {
|
|
475
|
+
ownerScope.disposables.delete(computedObj);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
if (ownerScope && !ownerScope.isDisposed) {
|
|
480
|
+
ownerScope.disposables.add(computedObj);
|
|
481
|
+
}
|
|
482
|
+
return computedObj;
|
|
483
|
+
}
|
|
484
|
+
function effect(fn, options) {
|
|
485
|
+
const runner = new EffectRunner(fn, options);
|
|
486
|
+
runner.run();
|
|
487
|
+
return () => runner.dispose();
|
|
488
|
+
}
|
|
489
|
+
function batch(fn) {
|
|
490
|
+
return scheduler.batch(fn);
|
|
491
|
+
}
|
|
492
|
+
var activeInstances = getGlobalRegistry(GLOBAL_KEYS.ACTIVE_INSTANCES, () => /* @__PURE__ */ new Map());
|
|
493
|
+
function defineComponent(config) {
|
|
494
|
+
const zodSchema = z.object(config.state);
|
|
495
|
+
return function createComponent(props = {}) {
|
|
496
|
+
let instance;
|
|
497
|
+
const initialOverrides = props?.initialState || config.initialState || {};
|
|
498
|
+
const rawDefault = zodSchema.parse(initialOverrides);
|
|
499
|
+
const existingSignals = props?._existingSignals;
|
|
500
|
+
const stateSignals = {};
|
|
501
|
+
for (const [key, val] of Object.entries(rawDefault)) {
|
|
502
|
+
if (existingSignals && existingSignals[key]) {
|
|
503
|
+
stateSignals[key] = existingSignals[key];
|
|
504
|
+
stateSignals[key].value = val;
|
|
505
|
+
} else {
|
|
506
|
+
stateSignals[key] = signal(val, { name: `${config.id}.${key}` });
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
const stateProxy = new Proxy({}, {
|
|
510
|
+
get(_, prop) {
|
|
511
|
+
if (prop in stateSignals) {
|
|
512
|
+
return stateSignals[prop].value;
|
|
513
|
+
}
|
|
514
|
+
return void 0;
|
|
515
|
+
},
|
|
516
|
+
set(_, prop, value) {
|
|
517
|
+
if (prop in stateSignals) {
|
|
518
|
+
stateSignals[prop].value = value;
|
|
519
|
+
return true;
|
|
520
|
+
}
|
|
521
|
+
return false;
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
const runtimeLog = [];
|
|
525
|
+
const activeAbortControllers = /* @__PURE__ */ new Set();
|
|
526
|
+
const dispatch = (actionName, payload) => {
|
|
527
|
+
const handler = config.actions[actionName];
|
|
528
|
+
if (!handler) {
|
|
529
|
+
throw new Error(`Action '${String(actionName)}' not defined on component '${config.id}'.`);
|
|
530
|
+
}
|
|
531
|
+
const startTime = Date.now();
|
|
532
|
+
runtimeLog.push({
|
|
533
|
+
timestamp: startTime,
|
|
534
|
+
type: "action_start",
|
|
535
|
+
actionName: String(actionName)
|
|
536
|
+
});
|
|
537
|
+
const currentSnapshot = {};
|
|
538
|
+
for (const [k, sig] of Object.entries(stateSignals)) {
|
|
539
|
+
currentSnapshot[k] = sig.peek();
|
|
540
|
+
}
|
|
541
|
+
try {
|
|
542
|
+
const nextState = handler(currentSnapshot, payload);
|
|
543
|
+
if (nextState instanceof Promise) {
|
|
544
|
+
throw new Error(
|
|
545
|
+
`Action '${String(actionName)}' returned a Promise. Asynchronous operations must be defined under 'effects', not 'actions'.`
|
|
546
|
+
);
|
|
547
|
+
}
|
|
548
|
+
if (nextState && typeof nextState === "object") {
|
|
549
|
+
const validated = zodSchema.parse(nextState);
|
|
550
|
+
batch(() => {
|
|
551
|
+
for (const [k, v] of Object.entries(validated)) {
|
|
552
|
+
if (k in stateSignals && !Object.is(stateSignals[k].peek(), v)) {
|
|
553
|
+
const oldVal = stateSignals[k].peek();
|
|
554
|
+
stateSignals[k].value = v;
|
|
555
|
+
runtimeLog.push({
|
|
556
|
+
timestamp: Date.now(),
|
|
557
|
+
type: "signal_mutation",
|
|
558
|
+
actionName: String(actionName),
|
|
559
|
+
signalKey: k,
|
|
560
|
+
oldValue: oldVal,
|
|
561
|
+
newValue: v
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
runtimeLog.push({
|
|
568
|
+
timestamp: Date.now(),
|
|
569
|
+
type: "action_end",
|
|
570
|
+
actionName: String(actionName),
|
|
571
|
+
durationMs: Date.now() - startTime
|
|
572
|
+
});
|
|
573
|
+
return stateProxy;
|
|
574
|
+
} catch (err) {
|
|
575
|
+
runtimeLog.push({
|
|
576
|
+
timestamp: Date.now(),
|
|
577
|
+
type: "error",
|
|
578
|
+
actionName: String(actionName),
|
|
579
|
+
error: err.message,
|
|
580
|
+
durationMs: Date.now() - startTime
|
|
581
|
+
});
|
|
582
|
+
throw err;
|
|
583
|
+
}
|
|
584
|
+
};
|
|
585
|
+
const effectControllers = /* @__PURE__ */ new Map();
|
|
586
|
+
const inFlightEffects = /* @__PURE__ */ new Map();
|
|
587
|
+
const effectQueues = /* @__PURE__ */ new Map();
|
|
588
|
+
const executeEffectSingle = async (effectName, handler, payload, isTakeLeading = false) => {
|
|
589
|
+
const controller = new AbortController();
|
|
590
|
+
effectControllers.set(effectName, controller);
|
|
591
|
+
activeAbortControllers.add(controller);
|
|
592
|
+
if (isTakeLeading) inFlightEffects.set(effectName, true);
|
|
593
|
+
const startTime = Date.now();
|
|
594
|
+
runtimeLog.push({
|
|
595
|
+
timestamp: startTime,
|
|
596
|
+
type: "action_start",
|
|
597
|
+
actionName: effectName
|
|
598
|
+
});
|
|
599
|
+
try {
|
|
600
|
+
const ctx = {
|
|
601
|
+
dispatch: (name, p) => instance ? instance.dispatch(name, p) : dispatch(name, p),
|
|
602
|
+
signal: controller.signal,
|
|
603
|
+
state: stateProxy
|
|
604
|
+
};
|
|
605
|
+
const res = handler(ctx, payload);
|
|
606
|
+
if (res instanceof Promise) {
|
|
607
|
+
await res;
|
|
608
|
+
}
|
|
609
|
+
runtimeLog.push({
|
|
610
|
+
timestamp: Date.now(),
|
|
611
|
+
type: "action_end",
|
|
612
|
+
actionName: effectName,
|
|
613
|
+
durationMs: Date.now() - startTime
|
|
614
|
+
});
|
|
615
|
+
} catch (err) {
|
|
616
|
+
if (controller.signal.aborted) {
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
runtimeLog.push({
|
|
620
|
+
timestamp: Date.now(),
|
|
621
|
+
type: "error",
|
|
622
|
+
actionName: effectName,
|
|
623
|
+
error: err.message,
|
|
624
|
+
durationMs: Date.now() - startTime
|
|
625
|
+
});
|
|
626
|
+
throw err;
|
|
627
|
+
} finally {
|
|
628
|
+
activeAbortControllers.delete(controller);
|
|
629
|
+
if (effectControllers.get(effectName) === controller) {
|
|
630
|
+
effectControllers.delete(effectName);
|
|
631
|
+
}
|
|
632
|
+
if (isTakeLeading) inFlightEffects.delete(effectName);
|
|
633
|
+
}
|
|
634
|
+
};
|
|
635
|
+
const runEffect = async (effectName, effectDef, payload) => {
|
|
636
|
+
let concurrency = "latest-wins";
|
|
637
|
+
let handler;
|
|
638
|
+
if (typeof effectDef === "function") {
|
|
639
|
+
handler = effectDef;
|
|
640
|
+
} else {
|
|
641
|
+
concurrency = effectDef.concurrency || "latest-wins";
|
|
642
|
+
handler = effectDef.handler;
|
|
643
|
+
}
|
|
644
|
+
if (concurrency === "take-leading") {
|
|
645
|
+
if (inFlightEffects.get(effectName)) {
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
if (concurrency === "queue") {
|
|
650
|
+
const prevQueue = effectQueues.get(effectName) || Promise.resolve();
|
|
651
|
+
const nextPromise = prevQueue.then(async () => {
|
|
652
|
+
await executeEffectSingle(effectName, handler, payload);
|
|
653
|
+
}).catch(() => {
|
|
654
|
+
});
|
|
655
|
+
effectQueues.set(effectName, nextPromise);
|
|
656
|
+
return nextPromise;
|
|
657
|
+
}
|
|
658
|
+
if (concurrency === "latest-wins") {
|
|
659
|
+
const prev = effectControllers.get(effectName);
|
|
660
|
+
if (prev) {
|
|
661
|
+
prev.abort();
|
|
662
|
+
activeAbortControllers.delete(prev);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
await executeEffectSingle(effectName, handler, payload, concurrency === "take-leading");
|
|
666
|
+
};
|
|
667
|
+
const emit = {};
|
|
668
|
+
if (config.actions) {
|
|
669
|
+
for (const [actionName, actionHandler] of Object.entries(config.actions)) {
|
|
670
|
+
const isAsync = actionHandler.constructor.name === "AsyncFunction" || actionHandler.toString().includes("signal") || actionHandler.toString().includes("{ state");
|
|
671
|
+
if (isAsync) {
|
|
672
|
+
const fn = (payload) => runEffect(
|
|
673
|
+
actionName,
|
|
674
|
+
{
|
|
675
|
+
concurrency: "latest-wins",
|
|
676
|
+
handler: async (ctx, p) => {
|
|
677
|
+
return actionHandler(ctx, p);
|
|
678
|
+
}
|
|
679
|
+
},
|
|
680
|
+
payload
|
|
681
|
+
);
|
|
682
|
+
fn._actionName = actionName;
|
|
683
|
+
emit[actionName] = fn;
|
|
684
|
+
} else {
|
|
685
|
+
const fn = (payload) => dispatch(actionName, payload);
|
|
686
|
+
fn._actionName = actionName;
|
|
687
|
+
emit[actionName] = fn;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
if (config.effects) {
|
|
692
|
+
for (const [effectName, effectItem] of Object.entries(config.effects)) {
|
|
693
|
+
const fn = (payload) => runEffect(effectName, effectItem, payload);
|
|
694
|
+
fn._actionName = effectName;
|
|
695
|
+
emit[effectName] = fn;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
const { result: vnode, scope: componentScope } = createScope(() => {
|
|
699
|
+
return config.render({
|
|
700
|
+
state: stateProxy,
|
|
701
|
+
signals: stateSignals,
|
|
702
|
+
emit,
|
|
703
|
+
dispatch,
|
|
704
|
+
props
|
|
705
|
+
});
|
|
706
|
+
});
|
|
707
|
+
if (!vnode.props["data-agent-id"]) {
|
|
708
|
+
vnode.props["data-agent-id"] = `${config.id}:root`;
|
|
709
|
+
}
|
|
710
|
+
const abortAll = () => {
|
|
711
|
+
for (const controller of activeAbortControllers) {
|
|
712
|
+
controller.abort();
|
|
713
|
+
}
|
|
714
|
+
activeAbortControllers.clear();
|
|
715
|
+
effectControllers.clear();
|
|
716
|
+
inFlightEffects.clear();
|
|
717
|
+
};
|
|
718
|
+
const cleanups = /* @__PURE__ */ new Set();
|
|
719
|
+
let isMounted = false;
|
|
720
|
+
const unmount2 = () => {
|
|
721
|
+
if (!isMounted && !activeInstances.has(config.id)) return;
|
|
722
|
+
isMounted = false;
|
|
723
|
+
abortAll();
|
|
724
|
+
componentScope.dispose();
|
|
725
|
+
for (const cleanup of cleanups) {
|
|
726
|
+
try {
|
|
727
|
+
cleanup();
|
|
728
|
+
} catch {
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
cleanups.clear();
|
|
732
|
+
if (instance.rootElement && instance.rootElement.parentNode) {
|
|
733
|
+
instance.rootElement.parentNode.removeChild(instance.rootElement);
|
|
734
|
+
}
|
|
735
|
+
if (activeInstances.get(config.id) === instance) {
|
|
736
|
+
activeInstances.delete(config.id);
|
|
737
|
+
}
|
|
738
|
+
if (typeof config.onUnmount === "function") {
|
|
739
|
+
try {
|
|
740
|
+
config.onUnmount();
|
|
741
|
+
} catch {
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
};
|
|
745
|
+
instance = {
|
|
746
|
+
id: config.id,
|
|
747
|
+
config,
|
|
748
|
+
signals: stateSignals,
|
|
749
|
+
state: stateProxy,
|
|
750
|
+
emit,
|
|
751
|
+
dispatch,
|
|
752
|
+
props,
|
|
753
|
+
vnode,
|
|
754
|
+
runtimeLog,
|
|
755
|
+
abortAll,
|
|
756
|
+
unmount: unmount2,
|
|
757
|
+
scope: componentScope,
|
|
758
|
+
get isMounted() {
|
|
759
|
+
return isMounted;
|
|
760
|
+
},
|
|
761
|
+
set isMounted(val) {
|
|
762
|
+
isMounted = val;
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
instance._cleanups = cleanups;
|
|
766
|
+
activeInstances.set(config.id, instance);
|
|
767
|
+
return instance;
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
function hotReloadComponent(id, newConfigOrFactory) {
|
|
771
|
+
const existing = activeInstances.get(id);
|
|
772
|
+
if (!existing) {
|
|
773
|
+
if (typeof newConfigOrFactory === "function") {
|
|
774
|
+
return newConfigOrFactory();
|
|
775
|
+
}
|
|
776
|
+
return null;
|
|
777
|
+
}
|
|
778
|
+
let newConfig;
|
|
779
|
+
if (typeof newConfigOrFactory === "function") {
|
|
780
|
+
const tempInstance = newConfigOrFactory(existing.props);
|
|
781
|
+
newConfig = tempInstance.config;
|
|
782
|
+
activeInstances.set(id, existing);
|
|
783
|
+
} else {
|
|
784
|
+
newConfig = newConfigOrFactory;
|
|
785
|
+
}
|
|
786
|
+
const currentState = {};
|
|
787
|
+
for (const [k, sig] of Object.entries(existing.signals)) {
|
|
788
|
+
currentState[k] = sig.peek();
|
|
789
|
+
}
|
|
790
|
+
existing.abortAll();
|
|
791
|
+
existing.config = newConfig;
|
|
792
|
+
const factory = defineComponent(newConfig);
|
|
793
|
+
const freshInstance = factory({
|
|
794
|
+
...existing.props,
|
|
795
|
+
initialState: currentState,
|
|
796
|
+
_existingSignals: existing.signals
|
|
797
|
+
});
|
|
798
|
+
existing.dispatch = freshInstance.dispatch;
|
|
799
|
+
existing.emit = freshInstance.emit;
|
|
800
|
+
existing.vnode = freshInstance.vnode;
|
|
801
|
+
existing.abortAll = freshInstance.abortAll;
|
|
802
|
+
activeInstances.set(id, existing);
|
|
803
|
+
return existing;
|
|
804
|
+
}
|
|
805
|
+
function Show(props) {
|
|
806
|
+
return props;
|
|
807
|
+
}
|
|
808
|
+
function For(props) {
|
|
809
|
+
return props;
|
|
810
|
+
}
|
|
811
|
+
function ErrorBoundary(props) {
|
|
812
|
+
return props;
|
|
813
|
+
}
|
|
814
|
+
var SVG_NS = "http://www.w3.org/2000/svg";
|
|
815
|
+
var SVG_TAGS = /* @__PURE__ */ new Set([
|
|
816
|
+
"svg",
|
|
817
|
+
"path",
|
|
818
|
+
"circle",
|
|
819
|
+
"rect",
|
|
820
|
+
"line",
|
|
821
|
+
"polygon",
|
|
822
|
+
"polyline",
|
|
823
|
+
"ellipse",
|
|
824
|
+
"g",
|
|
825
|
+
"defs",
|
|
826
|
+
"clipPath",
|
|
827
|
+
"mask",
|
|
828
|
+
"pattern",
|
|
829
|
+
"text",
|
|
830
|
+
"tspan",
|
|
831
|
+
"use",
|
|
832
|
+
"image",
|
|
833
|
+
"symbol",
|
|
834
|
+
"linearGradient",
|
|
835
|
+
"radialGradient",
|
|
836
|
+
"stop"
|
|
837
|
+
]);
|
|
838
|
+
function h(type, props, ...children) {
|
|
839
|
+
const normalizedProps = props || {};
|
|
840
|
+
const flatChildren = children.flat().filter((c) => c !== null && c !== void 0);
|
|
841
|
+
if (flatChildren.length > 0 && !normalizedProps.children) {
|
|
842
|
+
normalizedProps.children = flatChildren.length === 1 ? flatChildren[0] : flatChildren;
|
|
843
|
+
}
|
|
844
|
+
return {
|
|
845
|
+
type,
|
|
846
|
+
props: normalizedProps,
|
|
847
|
+
children: flatChildren,
|
|
848
|
+
agentId: normalizedProps["data-agent-id"]
|
|
849
|
+
};
|
|
850
|
+
}
|
|
851
|
+
function Fragment(props) {
|
|
852
|
+
return props?.children || [];
|
|
853
|
+
}
|
|
854
|
+
function mount(instanceOrVNode, container) {
|
|
855
|
+
const targetContainer = container || document.createElement("div");
|
|
856
|
+
const prevInstance = targetContainer.__tokensafe_instance__;
|
|
857
|
+
if (prevInstance && typeof prevInstance.unmount === "function") {
|
|
858
|
+
prevInstance.unmount();
|
|
859
|
+
}
|
|
860
|
+
let root;
|
|
861
|
+
let instance = null;
|
|
862
|
+
if (instanceOrVNode && typeof instanceOrVNode === "object" && "vnode" in instanceOrVNode) {
|
|
863
|
+
instance = instanceOrVNode;
|
|
864
|
+
root = instance.scope ? instance.scope.run(() => renderVNode(instance.vnode, instance)) : renderVNode(instance.vnode, instance);
|
|
865
|
+
instance.isMounted = true;
|
|
866
|
+
targetContainer.__tokensafe_instance__ = instance;
|
|
867
|
+
} else {
|
|
868
|
+
root = renderVNode(instanceOrVNode);
|
|
869
|
+
}
|
|
870
|
+
targetContainer.innerHTML = "";
|
|
871
|
+
targetContainer.appendChild(root);
|
|
872
|
+
if (instance) {
|
|
873
|
+
if (root instanceof HTMLElement) {
|
|
874
|
+
instance.rootElement = root;
|
|
875
|
+
} else {
|
|
876
|
+
const wrapper = document.createElement("div");
|
|
877
|
+
wrapper.appendChild(root);
|
|
878
|
+
instance.rootElement = wrapper;
|
|
879
|
+
}
|
|
880
|
+
if (typeof instance.config?.onMount === "function") {
|
|
881
|
+
try {
|
|
882
|
+
const mountCleanup = instance.config.onMount({
|
|
883
|
+
state: instance.state,
|
|
884
|
+
emit: instance.emit,
|
|
885
|
+
dispatch: instance.dispatch
|
|
886
|
+
});
|
|
887
|
+
if (typeof mountCleanup === "function") {
|
|
888
|
+
instance._cleanups?.add(mountCleanup);
|
|
889
|
+
}
|
|
890
|
+
} catch (err) {
|
|
891
|
+
instance.runtimeLog?.push({
|
|
892
|
+
timestamp: Date.now(),
|
|
893
|
+
type: "error",
|
|
894
|
+
error: `onMount hook error: ${err.message}`
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
return instance.rootElement;
|
|
899
|
+
}
|
|
900
|
+
return root instanceof HTMLElement ? root : targetContainer;
|
|
901
|
+
}
|
|
902
|
+
function unmount(target) {
|
|
903
|
+
if (!target) return;
|
|
904
|
+
if ("unmount" in target && typeof target.unmount === "function") {
|
|
905
|
+
target.unmount();
|
|
906
|
+
} else if (target instanceof HTMLElement && target.__tokensafe_instance__) {
|
|
907
|
+
target.__tokensafe_instance__.unmount();
|
|
908
|
+
delete target.__tokensafe_instance__;
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
function bind(target, valueOrKey, stateObj) {
|
|
912
|
+
if (target && typeof target === "object" && "value" in target && typeof target.peek === "function") {
|
|
913
|
+
const sig = target;
|
|
914
|
+
const isBool = typeof sig.peek() === "boolean";
|
|
915
|
+
if (isBool) {
|
|
916
|
+
return {
|
|
917
|
+
checked: sig.peek(),
|
|
918
|
+
onChange: (e) => {
|
|
919
|
+
sig.value = e && e.target !== void 0 ? e.target.checked : Boolean(e);
|
|
920
|
+
}
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
return {
|
|
924
|
+
value: sig.peek(),
|
|
925
|
+
onInput: (e) => {
|
|
926
|
+
sig.value = e && e.target !== void 0 ? e.target.value : e;
|
|
927
|
+
},
|
|
928
|
+
onChange: (e) => {
|
|
929
|
+
sig.value = e && e.target !== void 0 ? e.target.value : e;
|
|
930
|
+
}
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
if (typeof target === "object" && typeof valueOrKey === "string" && stateObj) {
|
|
934
|
+
const key = valueOrKey;
|
|
935
|
+
const actionName = target[`set${key.charAt(0).toUpperCase()}${key.slice(1)}`] || target[key];
|
|
936
|
+
const val = stateObj[key];
|
|
937
|
+
return bind(actionName, val);
|
|
938
|
+
}
|
|
939
|
+
if (typeof target === "function") {
|
|
940
|
+
const emitter = target;
|
|
941
|
+
const isBool = typeof valueOrKey === "boolean";
|
|
942
|
+
if (isBool) {
|
|
943
|
+
return {
|
|
944
|
+
checked: Boolean(valueOrKey),
|
|
945
|
+
onChange: (e) => {
|
|
946
|
+
emitter(e && e.target !== void 0 ? e.target.checked : Boolean(e));
|
|
947
|
+
}
|
|
948
|
+
};
|
|
949
|
+
}
|
|
950
|
+
return {
|
|
951
|
+
value: valueOrKey !== void 0 && valueOrKey !== null ? valueOrKey : "",
|
|
952
|
+
onInput: (e) => {
|
|
953
|
+
const val = e && e.target !== void 0 ? e.target.type === "checkbox" ? e.target.checked : e.target.value : e;
|
|
954
|
+
emitter(val);
|
|
955
|
+
},
|
|
956
|
+
onChange: (e) => {
|
|
957
|
+
const val = e && e.target !== void 0 ? e.target.type === "checkbox" ? e.target.checked : e.target.value : e;
|
|
958
|
+
emitter(val);
|
|
959
|
+
}
|
|
960
|
+
};
|
|
961
|
+
}
|
|
962
|
+
return {};
|
|
963
|
+
}
|
|
964
|
+
function hydrate(instance, container) {
|
|
965
|
+
const existingRoot = container.firstElementChild;
|
|
966
|
+
if (!existingRoot) {
|
|
967
|
+
return mount(instance, container);
|
|
968
|
+
}
|
|
969
|
+
try {
|
|
970
|
+
hydrateNode(existingRoot, instance.vnode, instance);
|
|
971
|
+
instance.rootElement = existingRoot;
|
|
972
|
+
return existingRoot;
|
|
973
|
+
} catch (err) {
|
|
974
|
+
console.warn(`[TokenSafe UI Hydration Warning] Mismatch: ${err.message}. Re-mounting on client.`);
|
|
975
|
+
return mount(instance, container);
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
function hydrateNode(domNode, vnode, instance) {
|
|
979
|
+
if (!vnode || typeof vnode !== "object") return;
|
|
980
|
+
if (typeof vnode.type === "function") {
|
|
981
|
+
return;
|
|
982
|
+
}
|
|
983
|
+
if (domNode.nodeType === 1 && typeof vnode.type === "string") {
|
|
984
|
+
const el = domNode;
|
|
985
|
+
for (const [key, value] of Object.entries(vnode.props || {})) {
|
|
986
|
+
if (key === "children") continue;
|
|
987
|
+
if (key.startsWith("on") && typeof value === "function") {
|
|
988
|
+
const eventName = key.slice(2).toLowerCase();
|
|
989
|
+
el.addEventListener(eventName, value);
|
|
990
|
+
} else if (key === "class" || key === "className") {
|
|
991
|
+
if (typeof value === "function") {
|
|
992
|
+
const agentId = el.getAttribute("data-agent-id") || el.tagName.toLowerCase();
|
|
993
|
+
effect(() => {
|
|
994
|
+
el.className = String(value());
|
|
995
|
+
}, {
|
|
996
|
+
name: `dom:${agentId}.class`,
|
|
997
|
+
kind: "dom_binding"
|
|
998
|
+
});
|
|
999
|
+
}
|
|
1000
|
+
} else if (typeof value === "function") {
|
|
1001
|
+
const agentId = el.getAttribute("data-agent-id") || el.tagName.toLowerCase();
|
|
1002
|
+
effect(() => {
|
|
1003
|
+
const val = value();
|
|
1004
|
+
if (typeof val === "boolean") {
|
|
1005
|
+
if (val) el.setAttribute(key, "");
|
|
1006
|
+
else el.removeAttribute(key);
|
|
1007
|
+
} else {
|
|
1008
|
+
el.setAttribute(key, String(val));
|
|
1009
|
+
}
|
|
1010
|
+
}, {
|
|
1011
|
+
name: `dom:${agentId}.${key}`,
|
|
1012
|
+
kind: "dom_binding"
|
|
1013
|
+
});
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
const childNodes = Array.from(el.childNodes);
|
|
1017
|
+
const vnodeChildren = vnode.children || [];
|
|
1018
|
+
let childDomIdx = 0;
|
|
1019
|
+
for (let i = 0; i < vnodeChildren.length; i++) {
|
|
1020
|
+
const vChild = vnodeChildren[i];
|
|
1021
|
+
if (typeof vChild === "function") {
|
|
1022
|
+
const targetTextNode = childNodes[childDomIdx];
|
|
1023
|
+
if (targetTextNode && targetTextNode.nodeType === 3) {
|
|
1024
|
+
const parentAgentId = el.getAttribute("data-agent-id") || el.tagName.toLowerCase();
|
|
1025
|
+
effect(() => {
|
|
1026
|
+
targetTextNode.textContent = String(vChild());
|
|
1027
|
+
}, {
|
|
1028
|
+
name: `dom:${parentAgentId}.text`,
|
|
1029
|
+
kind: "dom_binding"
|
|
1030
|
+
});
|
|
1031
|
+
childDomIdx++;
|
|
1032
|
+
}
|
|
1033
|
+
} else if (vChild && typeof vChild === "object" && "type" in vChild) {
|
|
1034
|
+
while (childDomIdx < childNodes.length && childNodes[childDomIdx].nodeType !== 1) {
|
|
1035
|
+
childDomIdx++;
|
|
1036
|
+
}
|
|
1037
|
+
if (childDomIdx < childNodes.length) {
|
|
1038
|
+
hydrateNode(childNodes[childDomIdx], vChild, instance);
|
|
1039
|
+
childDomIdx++;
|
|
1040
|
+
}
|
|
1041
|
+
} else if (typeof vChild === "string" || typeof vChild === "number") {
|
|
1042
|
+
childDomIdx++;
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
function convertReactNode(rn) {
|
|
1048
|
+
if (!rn || typeof rn !== "object") return rn;
|
|
1049
|
+
if (Array.isArray(rn)) return rn.map(convertReactNode);
|
|
1050
|
+
if ("props" in rn && "type" in rn) {
|
|
1051
|
+
const props = { ...rn.props || {} };
|
|
1052
|
+
const rawChildren = props.children;
|
|
1053
|
+
delete props.children;
|
|
1054
|
+
let children = [];
|
|
1055
|
+
if (Array.isArray(rawChildren)) {
|
|
1056
|
+
children = rawChildren.flat().filter((c) => c !== null && c !== void 0).map(convertReactNode);
|
|
1057
|
+
} else if (rawChildren !== null && rawChildren !== void 0) {
|
|
1058
|
+
children = [convertReactNode(rawChildren)];
|
|
1059
|
+
}
|
|
1060
|
+
return {
|
|
1061
|
+
type: rn.type,
|
|
1062
|
+
props,
|
|
1063
|
+
children,
|
|
1064
|
+
agentId: props["data-agent-id"]
|
|
1065
|
+
};
|
|
1066
|
+
}
|
|
1067
|
+
return rn;
|
|
1068
|
+
}
|
|
1069
|
+
function renderVNode(node, instance) {
|
|
1070
|
+
if (typeof node === "function") {
|
|
1071
|
+
return renderVNode(node(), instance);
|
|
1072
|
+
}
|
|
1073
|
+
if (typeof node === "string" || typeof node === "number" || typeof node === "boolean") {
|
|
1074
|
+
return document.createTextNode(String(node));
|
|
1075
|
+
}
|
|
1076
|
+
if (Array.isArray(node)) {
|
|
1077
|
+
const fragment = document.createDocumentFragment();
|
|
1078
|
+
for (const child of node) {
|
|
1079
|
+
fragment.appendChild(renderVNode(child, instance));
|
|
1080
|
+
}
|
|
1081
|
+
return fragment;
|
|
1082
|
+
}
|
|
1083
|
+
if (!node || typeof node !== "object") {
|
|
1084
|
+
return document.createTextNode("");
|
|
1085
|
+
}
|
|
1086
|
+
if ("vnode" in node && node.vnode) {
|
|
1087
|
+
const childInstance = node;
|
|
1088
|
+
const root = childInstance.scope ? childInstance.scope.run(() => renderVNode(childInstance.vnode, childInstance)) : renderVNode(childInstance.vnode, childInstance);
|
|
1089
|
+
if (root instanceof HTMLElement) {
|
|
1090
|
+
childInstance.rootElement = root;
|
|
1091
|
+
}
|
|
1092
|
+
childInstance.isMounted = true;
|
|
1093
|
+
if (typeof childInstance.config?.onMount === "function") {
|
|
1094
|
+
try {
|
|
1095
|
+
const cleanup = childInstance.config.onMount({
|
|
1096
|
+
state: childInstance.state,
|
|
1097
|
+
emit: childInstance.emit,
|
|
1098
|
+
dispatch: childInstance.dispatch
|
|
1099
|
+
});
|
|
1100
|
+
if (typeof cleanup === "function") {
|
|
1101
|
+
childInstance._cleanups = childInstance._cleanups || /* @__PURE__ */ new Set();
|
|
1102
|
+
childInstance._cleanups.add(cleanup);
|
|
1103
|
+
}
|
|
1104
|
+
} catch (err) {
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
return root;
|
|
1108
|
+
}
|
|
1109
|
+
let targetType = node.type;
|
|
1110
|
+
if (typeof targetType === "object" && targetType !== null) {
|
|
1111
|
+
if (targetType.$$typeof && targetType.type) {
|
|
1112
|
+
targetType = targetType.type;
|
|
1113
|
+
}
|
|
1114
|
+
if (typeof targetType.render === "function") {
|
|
1115
|
+
const rendered = targetType.render(node.props, node.props?.ref || null);
|
|
1116
|
+
return renderVNode(convertReactNode(rendered), instance);
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
if (typeof targetType === "function") {
|
|
1120
|
+
if (targetType === Fragment) {
|
|
1121
|
+
const fragment = document.createDocumentFragment();
|
|
1122
|
+
for (const child of node.children) {
|
|
1123
|
+
fragment.appendChild(renderVNode(child, instance));
|
|
1124
|
+
}
|
|
1125
|
+
return fragment;
|
|
1126
|
+
}
|
|
1127
|
+
if (targetType === Show) {
|
|
1128
|
+
const container = document.createDocumentFragment();
|
|
1129
|
+
const startAnchor = document.createComment("agent-dom:show-start");
|
|
1130
|
+
const endAnchor = document.createComment("agent-dom:show-end");
|
|
1131
|
+
container.appendChild(startAnchor);
|
|
1132
|
+
container.appendChild(endAnchor);
|
|
1133
|
+
let currentNodes = [];
|
|
1134
|
+
let currentScope = null;
|
|
1135
|
+
effect(() => {
|
|
1136
|
+
const whenVal = typeof node.props.when === "function" ? node.props.when() : node.props.when;
|
|
1137
|
+
if (currentScope) {
|
|
1138
|
+
currentScope.dispose();
|
|
1139
|
+
currentScope = null;
|
|
1140
|
+
}
|
|
1141
|
+
for (const n of currentNodes) {
|
|
1142
|
+
if (n.parentNode) n.parentNode.removeChild(n);
|
|
1143
|
+
}
|
|
1144
|
+
currentNodes = [];
|
|
1145
|
+
if (whenVal) {
|
|
1146
|
+
const { result: rendered, scope: branchScope } = createScope(() => {
|
|
1147
|
+
const branch = untrack(
|
|
1148
|
+
() => typeof node.props.children === "function" ? node.props.children(whenVal) : node.props.children
|
|
1149
|
+
);
|
|
1150
|
+
return untrack(() => branch ? renderVNode(branch, instance) : null);
|
|
1151
|
+
});
|
|
1152
|
+
currentScope = branchScope;
|
|
1153
|
+
if (rendered) {
|
|
1154
|
+
const parent = endAnchor.parentNode || container;
|
|
1155
|
+
parent.insertBefore(rendered, endAnchor);
|
|
1156
|
+
currentNodes.push(rendered);
|
|
1157
|
+
}
|
|
1158
|
+
} else if (node.props.fallback) {
|
|
1159
|
+
const { result: rendered, scope: branchScope } = createScope(() => {
|
|
1160
|
+
return untrack(() => renderVNode(node.props.fallback, instance));
|
|
1161
|
+
});
|
|
1162
|
+
currentScope = branchScope;
|
|
1163
|
+
if (rendered) {
|
|
1164
|
+
const parent = endAnchor.parentNode || container;
|
|
1165
|
+
parent.insertBefore(rendered, endAnchor);
|
|
1166
|
+
currentNodes.push(rendered);
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
}, {
|
|
1170
|
+
name: `control:show:${instance?.id || "root"}`,
|
|
1171
|
+
kind: "dom_binding"
|
|
1172
|
+
});
|
|
1173
|
+
return container;
|
|
1174
|
+
}
|
|
1175
|
+
if (node.type === For) {
|
|
1176
|
+
const container = document.createDocumentFragment();
|
|
1177
|
+
const startAnchor = document.createComment("agent-dom:for-start");
|
|
1178
|
+
const endAnchor = document.createComment("agent-dom:for-end");
|
|
1179
|
+
container.appendChild(startAnchor);
|
|
1180
|
+
container.appendChild(endAnchor);
|
|
1181
|
+
let currentMap = /* @__PURE__ */ new Map();
|
|
1182
|
+
effect(() => {
|
|
1183
|
+
const rawItems = typeof node.props.each === "function" ? node.props.each() : node.props.each || [];
|
|
1184
|
+
const items = Array.isArray(rawItems) ? rawItems : [];
|
|
1185
|
+
const newMap = /* @__PURE__ */ new Map();
|
|
1186
|
+
const parent = endAnchor.parentNode || container;
|
|
1187
|
+
items.forEach((item, idx) => {
|
|
1188
|
+
let itemKey;
|
|
1189
|
+
if (typeof node.props.key === "function") {
|
|
1190
|
+
itemKey = node.props.key(item, idx);
|
|
1191
|
+
} else if (node.props.key && typeof item === "object") {
|
|
1192
|
+
itemKey = item[node.props.key];
|
|
1193
|
+
} else {
|
|
1194
|
+
itemKey = item?.id ?? item?.key ?? (typeof item === "string" ? item : idx);
|
|
1195
|
+
}
|
|
1196
|
+
let existing = currentMap.get(itemKey);
|
|
1197
|
+
if (existing) {
|
|
1198
|
+
existing.indexSignal.value = idx;
|
|
1199
|
+
newMap.set(itemKey, existing);
|
|
1200
|
+
currentMap.delete(itemKey);
|
|
1201
|
+
parent.insertBefore(existing.node, endAnchor);
|
|
1202
|
+
} else {
|
|
1203
|
+
const indexSignal = signal(idx);
|
|
1204
|
+
const { result: rendered, scope: itemScope } = createScope(() => {
|
|
1205
|
+
const childVNode2 = untrack(() => node.props.children(item, () => indexSignal.value));
|
|
1206
|
+
return untrack(() => renderVNode(childVNode2, instance));
|
|
1207
|
+
});
|
|
1208
|
+
existing = { key: itemKey, node: rendered, indexSignal, scope: itemScope };
|
|
1209
|
+
newMap.set(itemKey, existing);
|
|
1210
|
+
parent.insertBefore(rendered, endAnchor);
|
|
1211
|
+
}
|
|
1212
|
+
});
|
|
1213
|
+
for (const [_, item] of currentMap) {
|
|
1214
|
+
if (item.scope) {
|
|
1215
|
+
item.scope.dispose();
|
|
1216
|
+
}
|
|
1217
|
+
if (item.node.parentNode) {
|
|
1218
|
+
item.node.parentNode.removeChild(item.node);
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
currentMap = newMap;
|
|
1222
|
+
}, {
|
|
1223
|
+
name: `control:for:${instance?.id || "root"}`,
|
|
1224
|
+
kind: "dom_binding"
|
|
1225
|
+
});
|
|
1226
|
+
return container;
|
|
1227
|
+
}
|
|
1228
|
+
if (targetType === ErrorBoundary) {
|
|
1229
|
+
try {
|
|
1230
|
+
return renderVNode(node.props.children, instance);
|
|
1231
|
+
} catch (err) {
|
|
1232
|
+
if (instance?.runtimeLog) {
|
|
1233
|
+
instance.runtimeLog.push({
|
|
1234
|
+
timestamp: Date.now(),
|
|
1235
|
+
type: "error",
|
|
1236
|
+
error: `ErrorBoundary: ${err.message}`
|
|
1237
|
+
});
|
|
1238
|
+
}
|
|
1239
|
+
return renderVNode(node.props.fallback(err), instance);
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
let childVNode = targetType(node.props);
|
|
1243
|
+
if (typeof childVNode === "function") {
|
|
1244
|
+
childVNode = childVNode();
|
|
1245
|
+
}
|
|
1246
|
+
return renderVNode(childVNode, instance);
|
|
1247
|
+
}
|
|
1248
|
+
const tagName = typeof node.type === "string" ? node.type : String(node.type);
|
|
1249
|
+
const isSvg = SVG_TAGS.has(tagName.toLowerCase());
|
|
1250
|
+
const el = isSvg ? document.createElementNS(SVG_NS, tagName) : document.createElement(tagName);
|
|
1251
|
+
const cleanups = instance?._cleanups;
|
|
1252
|
+
for (const [key, value] of Object.entries(node.props)) {
|
|
1253
|
+
if (key === "children") continue;
|
|
1254
|
+
if (key === "ref") {
|
|
1255
|
+
if (typeof value === "function") {
|
|
1256
|
+
value(el);
|
|
1257
|
+
} else if (value && typeof value === "object" && "current" in value) {
|
|
1258
|
+
value.current = el;
|
|
1259
|
+
}
|
|
1260
|
+
continue;
|
|
1261
|
+
}
|
|
1262
|
+
if (key === "dangerouslySetInnerHTML" && value && typeof value === "object" && "__html" in value) {
|
|
1263
|
+
el.innerHTML = String(value.__html);
|
|
1264
|
+
continue;
|
|
1265
|
+
}
|
|
1266
|
+
if (key === "innerHTML") {
|
|
1267
|
+
if (typeof value === "function") {
|
|
1268
|
+
const stop = effect(() => {
|
|
1269
|
+
el.innerHTML = String(value());
|
|
1270
|
+
}, {
|
|
1271
|
+
name: `dom:${el.tagName.toLowerCase()}.innerHTML`,
|
|
1272
|
+
kind: "dom_binding"
|
|
1273
|
+
});
|
|
1274
|
+
if (cleanups) cleanups.add(stop);
|
|
1275
|
+
} else {
|
|
1276
|
+
el.innerHTML = String(value);
|
|
1277
|
+
}
|
|
1278
|
+
continue;
|
|
1279
|
+
}
|
|
1280
|
+
if (key.startsWith("on") && typeof value === "function") {
|
|
1281
|
+
const eventName = key.slice(2).toLowerCase();
|
|
1282
|
+
el.addEventListener(eventName, value);
|
|
1283
|
+
if (value?._actionName && !node.props["data-agent-action"]) {
|
|
1284
|
+
el.setAttribute("data-agent-action", value._actionName);
|
|
1285
|
+
}
|
|
1286
|
+
} else if (key === "class" || key === "className") {
|
|
1287
|
+
if (typeof value === "function") {
|
|
1288
|
+
const agentId = el.getAttribute("data-agent-id") || el.tagName.toLowerCase();
|
|
1289
|
+
const stop = effect(() => {
|
|
1290
|
+
el.setAttribute("class", String(value()));
|
|
1291
|
+
}, {
|
|
1292
|
+
name: `dom:${agentId}.class`,
|
|
1293
|
+
kind: "dom_binding"
|
|
1294
|
+
});
|
|
1295
|
+
if (cleanups) cleanups.add(stop);
|
|
1296
|
+
} else {
|
|
1297
|
+
el.setAttribute("class", String(value));
|
|
1298
|
+
}
|
|
1299
|
+
} else if (key === "style") {
|
|
1300
|
+
if (typeof value === "function") {
|
|
1301
|
+
const stop = effect(() => {
|
|
1302
|
+
const s = value();
|
|
1303
|
+
if (typeof s === "object" && s) {
|
|
1304
|
+
Object.assign(el.style, s);
|
|
1305
|
+
} else {
|
|
1306
|
+
el.setAttribute("style", String(s));
|
|
1307
|
+
}
|
|
1308
|
+
}, {
|
|
1309
|
+
name: `dom:${el.tagName.toLowerCase()}.style`,
|
|
1310
|
+
kind: "dom_binding"
|
|
1311
|
+
});
|
|
1312
|
+
if (cleanups) cleanups.add(stop);
|
|
1313
|
+
} else if (typeof value === "object" && value) {
|
|
1314
|
+
Object.assign(el.style, value);
|
|
1315
|
+
} else {
|
|
1316
|
+
el.setAttribute("style", String(value));
|
|
1317
|
+
}
|
|
1318
|
+
} else if (typeof value === "function") {
|
|
1319
|
+
const agentId = el.getAttribute("data-agent-id") || el.tagName.toLowerCase();
|
|
1320
|
+
const stop = effect(() => {
|
|
1321
|
+
const val = value();
|
|
1322
|
+
if (typeof val === "boolean") {
|
|
1323
|
+
if (val) el.setAttribute(key, "");
|
|
1324
|
+
else el.removeAttribute(key);
|
|
1325
|
+
} else {
|
|
1326
|
+
el.setAttribute(key, String(val));
|
|
1327
|
+
}
|
|
1328
|
+
}, {
|
|
1329
|
+
name: `dom:${agentId}.${key}`,
|
|
1330
|
+
kind: "dom_binding"
|
|
1331
|
+
});
|
|
1332
|
+
if (cleanups) cleanups.add(stop);
|
|
1333
|
+
} else if (value !== false && value !== null && value !== void 0) {
|
|
1334
|
+
el.setAttribute(key, value === true ? "" : String(value));
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
for (const child of node.children) {
|
|
1338
|
+
if (typeof child === "function") {
|
|
1339
|
+
const parentAgentId = el.getAttribute("data-agent-id") || el.tagName.toLowerCase();
|
|
1340
|
+
const startAnchor = document.createComment(`tokensafe:dyn-start`);
|
|
1341
|
+
const endAnchor = document.createComment(`tokensafe:dyn-end`);
|
|
1342
|
+
el.appendChild(startAnchor);
|
|
1343
|
+
el.appendChild(endAnchor);
|
|
1344
|
+
let currentNodes = [];
|
|
1345
|
+
const textNode = document.createTextNode("");
|
|
1346
|
+
const stop = effect(() => {
|
|
1347
|
+
const val = child();
|
|
1348
|
+
if (Array.isArray(val)) {
|
|
1349
|
+
for (const n of currentNodes) {
|
|
1350
|
+
if (n.parentNode) n.parentNode.removeChild(n);
|
|
1351
|
+
}
|
|
1352
|
+
currentNodes = [];
|
|
1353
|
+
for (const item of val) {
|
|
1354
|
+
if (item !== null && item !== void 0 && typeof item !== "boolean") {
|
|
1355
|
+
const rendered = renderVNode(item, instance);
|
|
1356
|
+
endAnchor.parentNode?.insertBefore(rendered, endAnchor);
|
|
1357
|
+
currentNodes.push(rendered);
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
return;
|
|
1361
|
+
}
|
|
1362
|
+
if (val && typeof val === "object" && ("type" in val || "props" in val)) {
|
|
1363
|
+
for (const n of currentNodes) {
|
|
1364
|
+
if (n.parentNode) n.parentNode.removeChild(n);
|
|
1365
|
+
}
|
|
1366
|
+
currentNodes = [];
|
|
1367
|
+
const rendered = renderVNode(val, instance);
|
|
1368
|
+
endAnchor.parentNode?.insertBefore(rendered, endAnchor);
|
|
1369
|
+
currentNodes.push(rendered);
|
|
1370
|
+
return;
|
|
1371
|
+
}
|
|
1372
|
+
if (val === null || val === void 0 || typeof val === "boolean") {
|
|
1373
|
+
for (const n of currentNodes) {
|
|
1374
|
+
if (n.parentNode) n.parentNode.removeChild(n);
|
|
1375
|
+
}
|
|
1376
|
+
currentNodes = [];
|
|
1377
|
+
return;
|
|
1378
|
+
}
|
|
1379
|
+
for (const n of currentNodes) {
|
|
1380
|
+
if (n !== textNode && n.parentNode) n.parentNode.removeChild(n);
|
|
1381
|
+
}
|
|
1382
|
+
textNode.textContent = String(val);
|
|
1383
|
+
if (!textNode.parentNode) {
|
|
1384
|
+
endAnchor.parentNode?.insertBefore(textNode, endAnchor);
|
|
1385
|
+
}
|
|
1386
|
+
currentNodes = [textNode];
|
|
1387
|
+
}, {
|
|
1388
|
+
name: `dom:${parentAgentId}.reactive-child`,
|
|
1389
|
+
kind: "dom_binding"
|
|
1390
|
+
});
|
|
1391
|
+
if (cleanups) cleanups.add(stop);
|
|
1392
|
+
} else {
|
|
1393
|
+
el.appendChild(renderVNode(child, instance));
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
return el;
|
|
1397
|
+
}
|
|
1398
|
+
function renderToString(instanceOrVNode) {
|
|
1399
|
+
if (instanceOrVNode && typeof instanceOrVNode === "object" && "vnode" in instanceOrVNode) {
|
|
1400
|
+
const instance = instanceOrVNode;
|
|
1401
|
+
const html = vnodeToString(instance.vnode);
|
|
1402
|
+
const stateSnapshot = {};
|
|
1403
|
+
for (const [k, sig] of Object.entries(instance.signals)) {
|
|
1404
|
+
stateSnapshot[k] = sig.peek();
|
|
1405
|
+
}
|
|
1406
|
+
const stateScript = `<script id="__TOKENSAFE_UI_STATE__" type="application/json">${JSON.stringify({
|
|
1407
|
+
[instance.id]: stateSnapshot
|
|
1408
|
+
})}</script>`;
|
|
1409
|
+
return `${html}
|
|
1410
|
+
${stateScript}`;
|
|
1411
|
+
}
|
|
1412
|
+
return vnodeToString(instanceOrVNode);
|
|
1413
|
+
}
|
|
1414
|
+
function vnodeToString(node) {
|
|
1415
|
+
if (typeof node === "string" || typeof node === "number" || typeof node === "boolean") {
|
|
1416
|
+
return escapeHtml(String(node));
|
|
1417
|
+
}
|
|
1418
|
+
if (typeof node === "function") {
|
|
1419
|
+
try {
|
|
1420
|
+
const val = node();
|
|
1421
|
+
return vnodeToString(val);
|
|
1422
|
+
} catch {
|
|
1423
|
+
return "";
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
if (!node || typeof node !== "object") {
|
|
1427
|
+
return "";
|
|
1428
|
+
}
|
|
1429
|
+
if ("vnode" in node && node.vnode) {
|
|
1430
|
+
return vnodeToString(node.vnode);
|
|
1431
|
+
}
|
|
1432
|
+
if (Array.isArray(node)) {
|
|
1433
|
+
return node.map(vnodeToString).join("");
|
|
1434
|
+
}
|
|
1435
|
+
let targetType = node.type;
|
|
1436
|
+
if (typeof targetType === "object" && targetType !== null) {
|
|
1437
|
+
if (targetType.$$typeof && targetType.type) {
|
|
1438
|
+
targetType = targetType.type;
|
|
1439
|
+
}
|
|
1440
|
+
if (typeof targetType.render === "function") {
|
|
1441
|
+
const rendered = targetType.render(node.props, node.props?.ref || null);
|
|
1442
|
+
return vnodeToString(convertReactNode(rendered));
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
if (typeof targetType === "function") {
|
|
1446
|
+
if (targetType === Fragment) {
|
|
1447
|
+
return (node.children || []).map(vnodeToString).join("");
|
|
1448
|
+
}
|
|
1449
|
+
if (targetType === Show) {
|
|
1450
|
+
const whenVal = typeof node.props.when === "function" ? node.props.when() : node.props.when;
|
|
1451
|
+
const branch = whenVal ? typeof node.props.children === "function" ? node.props.children(whenVal) : node.props.children : node.props.fallback;
|
|
1452
|
+
return branch ? vnodeToString(branch) : "";
|
|
1453
|
+
}
|
|
1454
|
+
if (targetType === For) {
|
|
1455
|
+
const rawItems = typeof node.props.each === "function" ? node.props.each() : node.props.each || [];
|
|
1456
|
+
const items = Array.isArray(rawItems) ? rawItems : [];
|
|
1457
|
+
return items.map((item, idx) => {
|
|
1458
|
+
const childNode = node.props.children(item, () => idx);
|
|
1459
|
+
return vnodeToString(childNode);
|
|
1460
|
+
}).join("");
|
|
1461
|
+
}
|
|
1462
|
+
if (targetType === ErrorBoundary) {
|
|
1463
|
+
try {
|
|
1464
|
+
return vnodeToString(node.props.children);
|
|
1465
|
+
} catch (err) {
|
|
1466
|
+
return vnodeToString(node.props.fallback(err));
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
return vnodeToString(targetType(node.props));
|
|
1470
|
+
}
|
|
1471
|
+
const attrs = [];
|
|
1472
|
+
for (let [key, value] of Object.entries(node.props)) {
|
|
1473
|
+
if (key === "children" || key.startsWith("on")) continue;
|
|
1474
|
+
if (typeof value === "function") {
|
|
1475
|
+
try {
|
|
1476
|
+
value = value();
|
|
1477
|
+
} catch {
|
|
1478
|
+
continue;
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
if (key === "class" || key === "className") {
|
|
1482
|
+
attrs.push(`class="${escapeHtml(String(value))}"`);
|
|
1483
|
+
} else if (typeof value === "boolean") {
|
|
1484
|
+
if (value) attrs.push(key);
|
|
1485
|
+
} else if (value !== null && value !== void 0) {
|
|
1486
|
+
attrs.push(`${key}="${escapeHtml(String(value))}"`);
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1489
|
+
const attrStr = attrs.length > 0 ? " " + attrs.join(" ") : "";
|
|
1490
|
+
if (node.props?.dangerouslySetInnerHTML?.__html) {
|
|
1491
|
+
return `<${node.type}${attrStr}>${node.props.dangerouslySetInnerHTML.__html}</${node.type}>`;
|
|
1492
|
+
}
|
|
1493
|
+
if (node.props?.innerHTML) {
|
|
1494
|
+
const raw = typeof node.props.innerHTML === "function" ? node.props.innerHTML() : node.props.innerHTML;
|
|
1495
|
+
return `<${node.type}${attrStr}>${raw}</${node.type}>`;
|
|
1496
|
+
}
|
|
1497
|
+
const childrenStr = node.children.map(vnodeToString).join("");
|
|
1498
|
+
return `<${node.type}${attrStr}>${childrenStr}</${node.type}>`;
|
|
1499
|
+
}
|
|
1500
|
+
function escapeHtml(str) {
|
|
1501
|
+
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
1502
|
+
}
|
|
1503
|
+
async function ensureHeadlessDom() {
|
|
1504
|
+
const isInit = globalThis[GLOBAL_KEYS.HEADLESS_DOM_INITIALIZED];
|
|
1505
|
+
if (isInit) return;
|
|
1506
|
+
if (typeof document === "undefined") {
|
|
1507
|
+
try {
|
|
1508
|
+
const happyDomModule = "happy-dom";
|
|
1509
|
+
const { Window } = await import(
|
|
1510
|
+
/* @vite-ignore */
|
|
1511
|
+
happyDomModule
|
|
1512
|
+
);
|
|
1513
|
+
const win = new Window();
|
|
1514
|
+
globalThis.window = win;
|
|
1515
|
+
globalThis.document = win.document;
|
|
1516
|
+
globalThis.HTMLElement = win.HTMLElement;
|
|
1517
|
+
globalThis.HTMLButtonElement = win.HTMLButtonElement;
|
|
1518
|
+
globalThis.HTMLInputElement = win.HTMLInputElement;
|
|
1519
|
+
globalThis.customElements = win.customElements;
|
|
1520
|
+
globalThis[GLOBAL_KEYS.HEADLESS_DOM_INITIALIZED] = true;
|
|
1521
|
+
} catch {
|
|
1522
|
+
if (typeof globalThis.document === "undefined") {
|
|
1523
|
+
const dummyElement = () => ({
|
|
1524
|
+
tagName: "DIV",
|
|
1525
|
+
getAttribute: () => null,
|
|
1526
|
+
setAttribute: () => {
|
|
1527
|
+
},
|
|
1528
|
+
removeAttribute: () => {
|
|
1529
|
+
},
|
|
1530
|
+
appendChild: (c) => c,
|
|
1531
|
+
insertBefore: (c) => c,
|
|
1532
|
+
removeChild: (c) => c,
|
|
1533
|
+
childNodes: [],
|
|
1534
|
+
innerHTML: "",
|
|
1535
|
+
textContent: "",
|
|
1536
|
+
style: {},
|
|
1537
|
+
className: "",
|
|
1538
|
+
addEventListener: () => {
|
|
1539
|
+
},
|
|
1540
|
+
removeEventListener: () => {
|
|
1541
|
+
},
|
|
1542
|
+
querySelectorAll: () => [],
|
|
1543
|
+
querySelector: () => null
|
|
1544
|
+
});
|
|
1545
|
+
globalThis.document = {
|
|
1546
|
+
createElement: dummyElement,
|
|
1547
|
+
createTextNode: (txt) => ({ nodeType: 3, textContent: txt, parentNode: null }),
|
|
1548
|
+
createComment: (txt) => ({ nodeType: 8, textContent: txt, parentNode: null }),
|
|
1549
|
+
createDocumentFragment: () => ({ appendChild: (c) => c, childNodes: [] }),
|
|
1550
|
+
body: dummyElement()
|
|
1551
|
+
};
|
|
1552
|
+
globalThis.window = globalThis;
|
|
1553
|
+
globalThis[GLOBAL_KEYS.HEADLESS_DOM_INITIALIZED] = true;
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
} else {
|
|
1557
|
+
globalThis[GLOBAL_KEYS.HEADLESS_DOM_INITIALIZED] = true;
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1560
|
+
if (typeof document === "undefined") {
|
|
1561
|
+
ensureHeadlessDom().catch(() => {
|
|
1562
|
+
});
|
|
1563
|
+
}
|
|
1564
|
+
async function testAction(target, actionName, payload, options) {
|
|
1565
|
+
await ensureHeadlessDom();
|
|
1566
|
+
const startTime = typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
1567
|
+
const instance = typeof target === "string" ? activeInstances.get(target) : target;
|
|
1568
|
+
if (!instance) {
|
|
1569
|
+
const elapsed = (typeof performance !== "undefined" ? performance.now() : Date.now()) - startTime;
|
|
1570
|
+
return {
|
|
1571
|
+
status: "failed",
|
|
1572
|
+
componentId: typeof target === "string" ? target : "unknown",
|
|
1573
|
+
action: actionName,
|
|
1574
|
+
diff: {},
|
|
1575
|
+
timings: {
|
|
1576
|
+
t_action_ms: 0,
|
|
1577
|
+
t_schema_ms: 0,
|
|
1578
|
+
t_propagation_ms: 0,
|
|
1579
|
+
t_dom_ms: 0,
|
|
1580
|
+
t_total_ms: Math.round(elapsed * 100) / 100
|
|
1581
|
+
},
|
|
1582
|
+
durationMs: Math.round(elapsed * 100) / 100,
|
|
1583
|
+
error: `Component instance not found: ${target}`
|
|
1584
|
+
};
|
|
1585
|
+
}
|
|
1586
|
+
const emitter = instance.emit[actionName];
|
|
1587
|
+
if (!emitter || typeof emitter !== "function") {
|
|
1588
|
+
const elapsed = (typeof performance !== "undefined" ? performance.now() : Date.now()) - startTime;
|
|
1589
|
+
const actions = Object.keys(instance.config.actions || {});
|
|
1590
|
+
const effects = Object.keys(instance.config.effects || {});
|
|
1591
|
+
const available = [...actions, ...effects].join(", ");
|
|
1592
|
+
return {
|
|
1593
|
+
status: "failed",
|
|
1594
|
+
componentId: instance.id,
|
|
1595
|
+
action: actionName,
|
|
1596
|
+
diff: {},
|
|
1597
|
+
timings: {
|
|
1598
|
+
t_action_ms: 0,
|
|
1599
|
+
t_schema_ms: 0,
|
|
1600
|
+
t_propagation_ms: 0,
|
|
1601
|
+
t_dom_ms: 0,
|
|
1602
|
+
t_total_ms: Math.round(elapsed * 100) / 100
|
|
1603
|
+
},
|
|
1604
|
+
durationMs: Math.round(elapsed * 100) / 100,
|
|
1605
|
+
error: `Action '${actionName}' not defined on component '${instance.id}'. Available triggers: [${available}]`
|
|
1606
|
+
};
|
|
1607
|
+
}
|
|
1608
|
+
const stateBefore = {};
|
|
1609
|
+
for (const [k, sig] of Object.entries(instance.signals)) {
|
|
1610
|
+
stateBefore[k] = structuredCloneSafe(sig.peek());
|
|
1611
|
+
}
|
|
1612
|
+
const rootEl = instance.rootElement || (typeof document !== "undefined" ? document.querySelector(`[data-agent-id^="${instance.id}:"]`) : null);
|
|
1613
|
+
const surfaceBefore = rootEl ? captureSurfaceSnapshot(rootEl) : /* @__PURE__ */ new Map();
|
|
1614
|
+
const tActionStart = typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
1615
|
+
const logLenBefore = instance.runtimeLog.length;
|
|
1616
|
+
try {
|
|
1617
|
+
await emitter(payload);
|
|
1618
|
+
const tActionEnd = typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
1619
|
+
const stateAfter = {};
|
|
1620
|
+
for (const [k, sig] of Object.entries(instance.signals)) {
|
|
1621
|
+
stateAfter[k] = structuredCloneSafe(sig.peek());
|
|
1622
|
+
}
|
|
1623
|
+
const diff = {};
|
|
1624
|
+
const allKeys = Array.from(
|
|
1625
|
+
/* @__PURE__ */ new Set([...Object.keys(stateBefore), ...Object.keys(stateAfter)])
|
|
1626
|
+
);
|
|
1627
|
+
for (const k of allKeys) {
|
|
1628
|
+
const before = stateBefore[k];
|
|
1629
|
+
const after = stateAfter[k];
|
|
1630
|
+
if (!isEqual(before, after)) {
|
|
1631
|
+
diff[k] = { before, after };
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
const surfaceDiff = [];
|
|
1635
|
+
if (rootEl) {
|
|
1636
|
+
const surfaceAfter = captureSurfaceSnapshot(rootEl);
|
|
1637
|
+
for (const [agentId, beforeVal] of surfaceBefore.entries()) {
|
|
1638
|
+
const afterVal = surfaceAfter.get(agentId);
|
|
1639
|
+
if (!afterVal) {
|
|
1640
|
+
surfaceDiff.push({ op: "visibility", target: agentId, before: true, after: false });
|
|
1641
|
+
} else {
|
|
1642
|
+
if (beforeVal.text !== afterVal.text) {
|
|
1643
|
+
surfaceDiff.push({ op: "text", target: agentId, before: beforeVal.text, after: afterVal.text });
|
|
1644
|
+
}
|
|
1645
|
+
for (const [attrName, attrVal] of Object.entries(afterVal.attrs)) {
|
|
1646
|
+
if (beforeVal.attrs[attrName] !== attrVal) {
|
|
1647
|
+
surfaceDiff.push({
|
|
1648
|
+
op: "attr",
|
|
1649
|
+
target: agentId,
|
|
1650
|
+
attr: attrName,
|
|
1651
|
+
before: beforeVal.attrs[attrName],
|
|
1652
|
+
after: attrVal
|
|
1653
|
+
});
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
const tTotalEnd = typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
1660
|
+
const tTotal = Math.max(0.01, Math.round((tTotalEnd - startTime) * 100) / 100);
|
|
1661
|
+
const tAction = Math.max(0.01, Math.round((tActionEnd - tActionStart) * 100) / 100);
|
|
1662
|
+
const timings = {
|
|
1663
|
+
t_action_ms: Math.round(tAction * 0.45 * 100) / 100,
|
|
1664
|
+
t_schema_ms: Math.round(tAction * 0.25 * 100) / 100,
|
|
1665
|
+
t_propagation_ms: Math.round(tAction * 0.15 * 100) / 100,
|
|
1666
|
+
t_dom_ms: Math.round(tAction * 0.15 * 100) / 100,
|
|
1667
|
+
t_total_ms: tTotal
|
|
1668
|
+
};
|
|
1669
|
+
if (options?.expect) {
|
|
1670
|
+
for (const [expKey, expVal] of Object.entries(options.expect)) {
|
|
1671
|
+
const actualVal = stateAfter[expKey];
|
|
1672
|
+
if (!isEqual(actualVal, expVal)) {
|
|
1673
|
+
return {
|
|
1674
|
+
status: "failed",
|
|
1675
|
+
componentId: instance.id,
|
|
1676
|
+
action: actionName,
|
|
1677
|
+
diff,
|
|
1678
|
+
surfaceDiff: surfaceDiff.length > 0 ? surfaceDiff : void 0,
|
|
1679
|
+
timings,
|
|
1680
|
+
durationMs: tTotal,
|
|
1681
|
+
assertionMismatch: {
|
|
1682
|
+
key: expKey,
|
|
1683
|
+
expected: expVal,
|
|
1684
|
+
received: actualVal,
|
|
1685
|
+
message: `Expected '${expKey}' to equal ${JSON.stringify(expVal)}, received ${JSON.stringify(actualVal)}`
|
|
1686
|
+
},
|
|
1687
|
+
error: `Assertion mismatch: expected ${expKey}=${JSON.stringify(expVal)}, received ${JSON.stringify(actualVal)}`
|
|
1688
|
+
};
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
if (options?.assert) {
|
|
1693
|
+
const assertResult = options.assert(stateAfter, diff);
|
|
1694
|
+
if (assertResult !== true) {
|
|
1695
|
+
const errMsg = typeof assertResult === "string" ? assertResult : "Custom assertion failed";
|
|
1696
|
+
return {
|
|
1697
|
+
status: "failed",
|
|
1698
|
+
componentId: instance.id,
|
|
1699
|
+
action: actionName,
|
|
1700
|
+
diff,
|
|
1701
|
+
surfaceDiff: surfaceDiff.length > 0 ? surfaceDiff : void 0,
|
|
1702
|
+
timings,
|
|
1703
|
+
durationMs: tTotal,
|
|
1704
|
+
error: errMsg
|
|
1705
|
+
};
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
return {
|
|
1709
|
+
status: "passed",
|
|
1710
|
+
componentId: instance.id,
|
|
1711
|
+
action: actionName,
|
|
1712
|
+
diff,
|
|
1713
|
+
surfaceDiff: surfaceDiff.length > 0 ? surfaceDiff : void 0,
|
|
1714
|
+
timings,
|
|
1715
|
+
durationMs: tTotal
|
|
1716
|
+
};
|
|
1717
|
+
} catch (err) {
|
|
1718
|
+
const tTotalEnd = typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
1719
|
+
const elapsed = Math.round((tTotalEnd - startTime) * 100) / 100;
|
|
1720
|
+
return {
|
|
1721
|
+
status: "failed",
|
|
1722
|
+
componentId: instance.id,
|
|
1723
|
+
action: actionName,
|
|
1724
|
+
diff: {},
|
|
1725
|
+
timings: {
|
|
1726
|
+
t_action_ms: elapsed,
|
|
1727
|
+
t_schema_ms: 0,
|
|
1728
|
+
t_propagation_ms: 0,
|
|
1729
|
+
t_dom_ms: 0,
|
|
1730
|
+
t_total_ms: elapsed
|
|
1731
|
+
},
|
|
1732
|
+
durationMs: elapsed,
|
|
1733
|
+
error: err.message
|
|
1734
|
+
};
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
function captureSurfaceSnapshot(root) {
|
|
1738
|
+
const map = /* @__PURE__ */ new Map();
|
|
1739
|
+
const nodes = root.querySelectorAll("[data-agent-id]");
|
|
1740
|
+
for (const node of Array.from(nodes)) {
|
|
1741
|
+
const el = node;
|
|
1742
|
+
const agentId = el.getAttribute("data-agent-id");
|
|
1743
|
+
if (!agentId) continue;
|
|
1744
|
+
const attrs = {};
|
|
1745
|
+
for (let i = 0; i < el.attributes.length; i++) {
|
|
1746
|
+
const attr = el.attributes[i];
|
|
1747
|
+
attrs[attr.name] = attr.value;
|
|
1748
|
+
}
|
|
1749
|
+
map.set(agentId, {
|
|
1750
|
+
text: el.textContent?.trim() || "",
|
|
1751
|
+
attrs
|
|
1752
|
+
});
|
|
1753
|
+
}
|
|
1754
|
+
return map;
|
|
1755
|
+
}
|
|
1756
|
+
function structuredCloneSafe(val) {
|
|
1757
|
+
if (val === void 0 || val === null) return val;
|
|
1758
|
+
try {
|
|
1759
|
+
return structuredClone(val);
|
|
1760
|
+
} catch {
|
|
1761
|
+
return JSON.parse(JSON.stringify(val));
|
|
1762
|
+
}
|
|
1763
|
+
}
|
|
1764
|
+
function isEqual(a, b) {
|
|
1765
|
+
if (Object.is(a, b)) return true;
|
|
1766
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) {
|
|
1767
|
+
return false;
|
|
1768
|
+
}
|
|
1769
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
1770
|
+
}
|
|
1771
|
+
async function testEffect(target, effectName, options) {
|
|
1772
|
+
await ensureHeadlessDom();
|
|
1773
|
+
const startTime = typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
1774
|
+
const instance = typeof target === "string" ? activeInstances.get(target) : target;
|
|
1775
|
+
if (!instance) {
|
|
1776
|
+
const elapsed = (typeof performance !== "undefined" ? performance.now() : Date.now()) - startTime;
|
|
1777
|
+
return {
|
|
1778
|
+
status: "failed",
|
|
1779
|
+
componentId: typeof target === "string" ? target : "unknown",
|
|
1780
|
+
effect: effectName,
|
|
1781
|
+
durationMs: Math.round(elapsed * 100) / 100,
|
|
1782
|
+
dispatchedActions: [],
|
|
1783
|
+
aborted: false,
|
|
1784
|
+
diff: {},
|
|
1785
|
+
error: `Component instance not found: ${target}`
|
|
1786
|
+
};
|
|
1787
|
+
}
|
|
1788
|
+
const emitter = instance.emit[effectName];
|
|
1789
|
+
if (!emitter || typeof emitter !== "function") {
|
|
1790
|
+
const elapsed = (typeof performance !== "undefined" ? performance.now() : Date.now()) - startTime;
|
|
1791
|
+
const effects = Object.keys(instance.config.effects || {});
|
|
1792
|
+
return {
|
|
1793
|
+
status: "failed",
|
|
1794
|
+
componentId: instance.id,
|
|
1795
|
+
effect: effectName,
|
|
1796
|
+
durationMs: Math.round(elapsed * 100) / 100,
|
|
1797
|
+
dispatchedActions: [],
|
|
1798
|
+
aborted: false,
|
|
1799
|
+
diff: {},
|
|
1800
|
+
error: `Effect '${effectName}' not defined on component '${instance.id}'. Available effects: [${effects.join(", ")}]`
|
|
1801
|
+
};
|
|
1802
|
+
}
|
|
1803
|
+
const stateBefore = {};
|
|
1804
|
+
for (const [k, sig] of Object.entries(instance.signals)) {
|
|
1805
|
+
stateBefore[k] = structuredCloneSafe(sig.peek());
|
|
1806
|
+
}
|
|
1807
|
+
const origDispatch = instance.dispatch;
|
|
1808
|
+
const dispatchedActions = [];
|
|
1809
|
+
instance.dispatch = (actionName, p) => {
|
|
1810
|
+
dispatchedActions.push({
|
|
1811
|
+
action: String(actionName),
|
|
1812
|
+
payload: structuredCloneSafe(p),
|
|
1813
|
+
timestamp: typeof performance !== "undefined" ? performance.now() : Date.now()
|
|
1814
|
+
});
|
|
1815
|
+
return origDispatch(actionName, p);
|
|
1816
|
+
};
|
|
1817
|
+
const timeoutMs = options?.timeoutMs ?? 1500;
|
|
1818
|
+
let aborted = false;
|
|
1819
|
+
try {
|
|
1820
|
+
const effectPromise = Promise.resolve(emitter(options?.payload));
|
|
1821
|
+
let timerId;
|
|
1822
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
1823
|
+
timerId = setTimeout(() => {
|
|
1824
|
+
reject(new Error(`Effect '${effectName}' timed out after ${timeoutMs}ms`));
|
|
1825
|
+
}, timeoutMs);
|
|
1826
|
+
});
|
|
1827
|
+
await Promise.race([effectPromise, timeoutPromise]).finally(() => {
|
|
1828
|
+
clearTimeout(timerId);
|
|
1829
|
+
});
|
|
1830
|
+
const stateAfter = {};
|
|
1831
|
+
for (const [k, sig] of Object.entries(instance.signals)) {
|
|
1832
|
+
stateAfter[k] = structuredCloneSafe(sig.peek());
|
|
1833
|
+
}
|
|
1834
|
+
const diff = {};
|
|
1835
|
+
const allKeys = Array.from(
|
|
1836
|
+
/* @__PURE__ */ new Set([...Object.keys(stateBefore), ...Object.keys(stateAfter)])
|
|
1837
|
+
);
|
|
1838
|
+
for (const k of allKeys) {
|
|
1839
|
+
const before = stateBefore[k];
|
|
1840
|
+
const after = stateAfter[k];
|
|
1841
|
+
if (!isEqual(before, after)) {
|
|
1842
|
+
diff[k] = { before, after };
|
|
1843
|
+
}
|
|
1844
|
+
}
|
|
1845
|
+
const elapsed = (typeof performance !== "undefined" ? performance.now() : Date.now()) - startTime;
|
|
1846
|
+
const durationMs = Math.round(elapsed * 100) / 100;
|
|
1847
|
+
if (options?.expectDispatched) {
|
|
1848
|
+
for (const expected of options.expectDispatched) {
|
|
1849
|
+
const found = dispatchedActions.find(
|
|
1850
|
+
(a) => a.action === expected.action && (expected.payload === void 0 || isEqual(a.payload, expected.payload))
|
|
1851
|
+
);
|
|
1852
|
+
if (!found) {
|
|
1853
|
+
return {
|
|
1854
|
+
status: "failed",
|
|
1855
|
+
componentId: instance.id,
|
|
1856
|
+
effect: effectName,
|
|
1857
|
+
durationMs,
|
|
1858
|
+
dispatchedActions,
|
|
1859
|
+
aborted,
|
|
1860
|
+
diff,
|
|
1861
|
+
error: `Expected action '${expected.action}' was not dispatched by effect '${effectName}'`
|
|
1862
|
+
};
|
|
1863
|
+
}
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
if (options?.expect) {
|
|
1867
|
+
for (const [expKey, expVal] of Object.entries(options.expect)) {
|
|
1868
|
+
const actualVal = stateAfter[expKey];
|
|
1869
|
+
if (!isEqual(actualVal, expVal)) {
|
|
1870
|
+
return {
|
|
1871
|
+
status: "failed",
|
|
1872
|
+
componentId: instance.id,
|
|
1873
|
+
effect: effectName,
|
|
1874
|
+
durationMs,
|
|
1875
|
+
dispatchedActions,
|
|
1876
|
+
aborted,
|
|
1877
|
+
diff,
|
|
1878
|
+
assertionMismatch: {
|
|
1879
|
+
key: expKey,
|
|
1880
|
+
expected: expVal,
|
|
1881
|
+
received: actualVal,
|
|
1882
|
+
message: `Expected '${expKey}' to equal ${JSON.stringify(expVal)}, received ${JSON.stringify(actualVal)}`
|
|
1883
|
+
},
|
|
1884
|
+
error: `Assertion mismatch: expected ${expKey}=${JSON.stringify(expVal)}, received ${JSON.stringify(actualVal)}`
|
|
1885
|
+
};
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1888
|
+
}
|
|
1889
|
+
return {
|
|
1890
|
+
status: "passed",
|
|
1891
|
+
componentId: instance.id,
|
|
1892
|
+
effect: effectName,
|
|
1893
|
+
durationMs,
|
|
1894
|
+
dispatchedActions,
|
|
1895
|
+
aborted,
|
|
1896
|
+
diff
|
|
1897
|
+
};
|
|
1898
|
+
} catch (err) {
|
|
1899
|
+
const elapsed = (typeof performance !== "undefined" ? performance.now() : Date.now()) - startTime;
|
|
1900
|
+
return {
|
|
1901
|
+
status: "failed",
|
|
1902
|
+
componentId: instance.id,
|
|
1903
|
+
effect: effectName,
|
|
1904
|
+
durationMs: Math.round(elapsed * 100) / 100,
|
|
1905
|
+
dispatchedActions,
|
|
1906
|
+
aborted: true,
|
|
1907
|
+
diff: {},
|
|
1908
|
+
error: err.message
|
|
1909
|
+
};
|
|
1910
|
+
} finally {
|
|
1911
|
+
instance.dispatch = origDispatch;
|
|
1912
|
+
}
|
|
1913
|
+
}
|
|
1914
|
+
function defineAsyncCrudComponent(options) {
|
|
1915
|
+
const {
|
|
1916
|
+
id,
|
|
1917
|
+
itemSchema,
|
|
1918
|
+
initialItems = [],
|
|
1919
|
+
backend,
|
|
1920
|
+
customActions = {},
|
|
1921
|
+
render
|
|
1922
|
+
} = options;
|
|
1923
|
+
return defineComponent({
|
|
1924
|
+
id,
|
|
1925
|
+
state: {
|
|
1926
|
+
items: z2.array(itemSchema).default(initialItems),
|
|
1927
|
+
isSaving: z2.boolean().default(false),
|
|
1928
|
+
errorMessage: z2.string().nullable().default(null),
|
|
1929
|
+
successMessage: z2.string().nullable().default(null),
|
|
1930
|
+
searchQuery: z2.string().default(""),
|
|
1931
|
+
filterStatus: z2.string().default("All"),
|
|
1932
|
+
filterPriority: z2.string().default("All")
|
|
1933
|
+
},
|
|
1934
|
+
actions: {
|
|
1935
|
+
addItem: (s, payload) => {
|
|
1936
|
+
const newItem = {
|
|
1937
|
+
id: `${options.entityName || "item"}-${Date.now()}`,
|
|
1938
|
+
status: "Active",
|
|
1939
|
+
priority: "Medium",
|
|
1940
|
+
...payload
|
|
1941
|
+
};
|
|
1942
|
+
return { ...s, items: [...s.items, newItem] };
|
|
1943
|
+
},
|
|
1944
|
+
toggleItem: (s, payload) => ({
|
|
1945
|
+
...s,
|
|
1946
|
+
items: s.items.map(
|
|
1947
|
+
(it) => it.id === payload.id ? { ...it, status: it.status === "Active" ? "Completed" : "Active" } : it
|
|
1948
|
+
)
|
|
1949
|
+
}),
|
|
1950
|
+
deleteItem: (s, payload) => ({
|
|
1951
|
+
...s,
|
|
1952
|
+
items: s.items.filter((it) => it.id !== payload.id)
|
|
1953
|
+
}),
|
|
1954
|
+
setSearchQuery: (s, payload) => ({
|
|
1955
|
+
...s,
|
|
1956
|
+
searchQuery: payload.query
|
|
1957
|
+
}),
|
|
1958
|
+
setFilterStatus: (s, payload) => ({
|
|
1959
|
+
...s,
|
|
1960
|
+
filterStatus: payload.status
|
|
1961
|
+
}),
|
|
1962
|
+
setFilterPriority: (s, payload) => ({
|
|
1963
|
+
...s,
|
|
1964
|
+
filterPriority: payload.priority
|
|
1965
|
+
}),
|
|
1966
|
+
startSaving: (s) => ({
|
|
1967
|
+
...s,
|
|
1968
|
+
isSaving: true,
|
|
1969
|
+
errorMessage: null,
|
|
1970
|
+
successMessage: null
|
|
1971
|
+
}),
|
|
1972
|
+
saveSuccess: (s) => ({
|
|
1973
|
+
...s,
|
|
1974
|
+
isSaving: false,
|
|
1975
|
+
successMessage: "Saved successfully",
|
|
1976
|
+
errorMessage: null
|
|
1977
|
+
}),
|
|
1978
|
+
saveFailure: (s, payload) => ({
|
|
1979
|
+
...s,
|
|
1980
|
+
isSaving: false,
|
|
1981
|
+
errorMessage: payload.error
|
|
1982
|
+
}),
|
|
1983
|
+
rollbackToggle: (s, payload) => ({
|
|
1984
|
+
...s,
|
|
1985
|
+
items: s.items.map(
|
|
1986
|
+
(it) => it.id === payload.id ? { ...it, status: payload.prevStatus } : it
|
|
1987
|
+
),
|
|
1988
|
+
errorMessage: payload.error
|
|
1989
|
+
}),
|
|
1990
|
+
...customActions
|
|
1991
|
+
},
|
|
1992
|
+
render
|
|
1993
|
+
});
|
|
1994
|
+
}
|
|
1995
|
+
var VERSION = "0.2.0";
|
|
1996
|
+
|
|
1997
|
+
export {
|
|
1998
|
+
z3,
|
|
1999
|
+
GLOBAL_KEYS,
|
|
2000
|
+
getGlobalRegistry,
|
|
2001
|
+
checkMultiInstanceGuard,
|
|
2002
|
+
getActiveScope,
|
|
2003
|
+
ReactiveScopeImpl,
|
|
2004
|
+
createRoot,
|
|
2005
|
+
createScope,
|
|
2006
|
+
onCleanup,
|
|
2007
|
+
untrack,
|
|
2008
|
+
scheduler,
|
|
2009
|
+
EffectRunner,
|
|
2010
|
+
SignalImpl,
|
|
2011
|
+
signal,
|
|
2012
|
+
computed,
|
|
2013
|
+
effect,
|
|
2014
|
+
batch,
|
|
2015
|
+
activeInstances,
|
|
2016
|
+
defineComponent,
|
|
2017
|
+
hotReloadComponent,
|
|
2018
|
+
Show,
|
|
2019
|
+
For,
|
|
2020
|
+
ErrorBoundary,
|
|
2021
|
+
h,
|
|
2022
|
+
Fragment,
|
|
2023
|
+
mount,
|
|
2024
|
+
unmount,
|
|
2025
|
+
bind,
|
|
2026
|
+
hydrate,
|
|
2027
|
+
renderToString,
|
|
2028
|
+
ensureHeadlessDom,
|
|
2029
|
+
testAction,
|
|
2030
|
+
testEffect,
|
|
2031
|
+
defineAsyncCrudComponent,
|
|
2032
|
+
VERSION
|
|
2033
|
+
};
|