@cajoa/core 0.14.0 → 0.16.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/dist/index.cjs +406 -77
- package/dist/index.d.cts +16 -4
- package/dist/index.d.ts +16 -4
- package/dist/index.js +404 -77
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -22,16 +22,19 @@ function logError(message, error) {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
// src/reactivity/signal.ts
|
|
25
|
+
var CLEAN = 0;
|
|
26
|
+
var CHECK = 1;
|
|
27
|
+
var DIRTY = 2;
|
|
25
28
|
var currentOwner = null;
|
|
26
|
-
var
|
|
29
|
+
var listener = null;
|
|
27
30
|
function clean(owner) {
|
|
28
31
|
const fns = owner.cleanups;
|
|
29
32
|
owner.cleanups = [];
|
|
30
33
|
for (const fn of fns) fn();
|
|
31
34
|
}
|
|
32
35
|
function cleanComputation(c) {
|
|
33
|
-
for (const
|
|
34
|
-
c.
|
|
36
|
+
for (const source of c.sources) source.observers.delete(c);
|
|
37
|
+
c.sources.clear();
|
|
35
38
|
clean(c);
|
|
36
39
|
}
|
|
37
40
|
function addCleanup(fn) {
|
|
@@ -47,11 +50,104 @@ function handleError(error, owner) {
|
|
|
47
50
|
throw error;
|
|
48
51
|
}
|
|
49
52
|
var batchDepth = 0;
|
|
50
|
-
var
|
|
53
|
+
var flushing = false;
|
|
54
|
+
var queued = /* @__PURE__ */ new Set();
|
|
55
|
+
var isComputation = (source) => "fn" in source;
|
|
56
|
+
function track(source) {
|
|
57
|
+
if (listener) {
|
|
58
|
+
source.observers.add(listener);
|
|
59
|
+
listener.sources.add(source);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function mark(node, state) {
|
|
63
|
+
if (node.disposed || node.state >= state) return;
|
|
64
|
+
const wasClean = node.state === CLEAN;
|
|
65
|
+
node.state = state;
|
|
66
|
+
if (!wasClean) return;
|
|
67
|
+
if (node.isEffect) {
|
|
68
|
+
queued.add(node);
|
|
69
|
+
} else {
|
|
70
|
+
for (const observer of node.observers) mark(observer, CHECK);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function notify(source) {
|
|
74
|
+
for (const observer of source.observers) mark(observer, DIRTY);
|
|
75
|
+
if (batchDepth === 0 && !flushing) flush();
|
|
76
|
+
}
|
|
77
|
+
function run(node) {
|
|
78
|
+
cleanComputation(node);
|
|
79
|
+
const prevOwner = currentOwner;
|
|
80
|
+
const prevListener = listener;
|
|
81
|
+
currentOwner = node;
|
|
82
|
+
listener = node;
|
|
83
|
+
node.state = CLEAN;
|
|
84
|
+
try {
|
|
85
|
+
const result = node.fn();
|
|
86
|
+
if (!node.isEffect) {
|
|
87
|
+
const changed = node.hasValue && !Object.is(node.value, result);
|
|
88
|
+
node.value = result;
|
|
89
|
+
node.hasValue = true;
|
|
90
|
+
if (changed) {
|
|
91
|
+
for (const observer of node.observers) {
|
|
92
|
+
if (observer.state === CHECK) observer.state = DIRTY;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
} catch (error) {
|
|
97
|
+
handleError(error, node);
|
|
98
|
+
} finally {
|
|
99
|
+
currentOwner = prevOwner;
|
|
100
|
+
listener = prevListener;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function update(node) {
|
|
104
|
+
if (node.disposed) {
|
|
105
|
+
node.state = CLEAN;
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (node.state === CHECK) {
|
|
109
|
+
for (const source of [...node.sources]) {
|
|
110
|
+
if (isComputation(source)) {
|
|
111
|
+
update(source);
|
|
112
|
+
if (node.state === DIRTY) break;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (node.state === DIRTY) run(node);
|
|
117
|
+
else node.state = CLEAN;
|
|
118
|
+
}
|
|
51
119
|
function flush() {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
120
|
+
flushing = true;
|
|
121
|
+
let failure = null;
|
|
122
|
+
let steps = 0;
|
|
123
|
+
try {
|
|
124
|
+
while (queued.size > 0) {
|
|
125
|
+
if (++steps > 1e5) {
|
|
126
|
+
queued.clear();
|
|
127
|
+
failure ??= {
|
|
128
|
+
error: new Error(
|
|
129
|
+
"Cajoa: updates never settled. An effect probably keeps changing a signal that it also reads."
|
|
130
|
+
)
|
|
131
|
+
};
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
const effect = queued.values().next().value;
|
|
135
|
+
queued.delete(effect);
|
|
136
|
+
try {
|
|
137
|
+
update(effect);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
failure ??= { error };
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
} finally {
|
|
143
|
+
flushing = false;
|
|
144
|
+
}
|
|
145
|
+
if (failure) throw failure.error;
|
|
146
|
+
}
|
|
147
|
+
function dispose(node) {
|
|
148
|
+
node.disposed = true;
|
|
149
|
+
queued.delete(node);
|
|
150
|
+
cleanComputation(node);
|
|
55
151
|
}
|
|
56
152
|
function batch(fn) {
|
|
57
153
|
batchDepth++;
|
|
@@ -59,58 +155,51 @@ function batch(fn) {
|
|
|
59
155
|
return fn();
|
|
60
156
|
} finally {
|
|
61
157
|
batchDepth--;
|
|
62
|
-
if (batchDepth === 0) flush();
|
|
158
|
+
if (batchDepth === 0 && !flushing) flush();
|
|
63
159
|
}
|
|
64
160
|
}
|
|
65
161
|
function createSignal(value) {
|
|
66
|
-
const
|
|
162
|
+
const source = { observers: /* @__PURE__ */ new Set() };
|
|
67
163
|
const get = () => {
|
|
68
|
-
|
|
69
|
-
subs.add(currentEffect);
|
|
70
|
-
currentEffect.deps.add(subs);
|
|
71
|
-
}
|
|
164
|
+
track(source);
|
|
72
165
|
return value;
|
|
73
166
|
};
|
|
74
167
|
const set = (next) => {
|
|
75
168
|
const v = typeof next === "function" ? next(value) : next;
|
|
76
169
|
if (Object.is(v, value)) return;
|
|
77
170
|
value = v;
|
|
78
|
-
|
|
79
|
-
if (batchDepth > 0)
|
|
80
|
-
pending.add(effect);
|
|
81
|
-
else effect.run();
|
|
82
|
-
}
|
|
171
|
+
notify(source);
|
|
83
172
|
};
|
|
84
173
|
return [get, set];
|
|
85
174
|
}
|
|
86
|
-
function
|
|
87
|
-
|
|
175
|
+
function createComputation(fn, isEffect) {
|
|
176
|
+
return {
|
|
177
|
+
fn,
|
|
178
|
+
isEffect,
|
|
88
179
|
cleanups: [],
|
|
89
180
|
parent: currentOwner,
|
|
90
|
-
|
|
181
|
+
sources: /* @__PURE__ */ new Set(),
|
|
182
|
+
observers: /* @__PURE__ */ new Set(),
|
|
183
|
+
state: DIRTY,
|
|
91
184
|
disposed: false,
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
185
|
+
hasValue: false,
|
|
186
|
+
value: void 0
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
function createEffect(fn) {
|
|
190
|
+
const node = createComputation(fn, true);
|
|
191
|
+
addCleanup(() => dispose(node));
|
|
192
|
+
run(node);
|
|
193
|
+
}
|
|
194
|
+
function createMemo(fn) {
|
|
195
|
+
const node = createComputation(fn, false);
|
|
196
|
+
addCleanup(() => dispose(node));
|
|
197
|
+
run(node);
|
|
198
|
+
return () => {
|
|
199
|
+
if (node.state !== CLEAN) update(node);
|
|
200
|
+
track(node);
|
|
201
|
+
return node.value;
|
|
108
202
|
};
|
|
109
|
-
addCleanup(() => {
|
|
110
|
-
effect.disposed = true;
|
|
111
|
-
cleanComputation(effect);
|
|
112
|
-
});
|
|
113
|
-
effect.run();
|
|
114
203
|
}
|
|
115
204
|
function onCleanup(fn) {
|
|
116
205
|
if (!currentOwner) {
|
|
@@ -123,40 +212,40 @@ function onCleanup(fn) {
|
|
|
123
212
|
function createRoot(fn) {
|
|
124
213
|
const root = { cleanups: [], parent: currentOwner };
|
|
125
214
|
const prevOwner = currentOwner;
|
|
126
|
-
const
|
|
215
|
+
const prevListener = listener;
|
|
127
216
|
currentOwner = root;
|
|
128
|
-
|
|
217
|
+
listener = null;
|
|
129
218
|
try {
|
|
130
219
|
return fn(() => clean(root));
|
|
131
220
|
} finally {
|
|
132
221
|
currentOwner = prevOwner;
|
|
133
|
-
|
|
222
|
+
listener = prevListener;
|
|
134
223
|
}
|
|
135
224
|
}
|
|
136
225
|
function onMount(fn) {
|
|
137
226
|
const owner = currentOwner;
|
|
138
227
|
Promise.resolve().then(() => {
|
|
139
228
|
const prevOwner = currentOwner;
|
|
140
|
-
const
|
|
229
|
+
const prevListener = listener;
|
|
141
230
|
currentOwner = owner;
|
|
142
|
-
|
|
231
|
+
listener = null;
|
|
143
232
|
try {
|
|
144
233
|
fn();
|
|
145
234
|
} catch (error) {
|
|
146
235
|
handleError(error, owner);
|
|
147
236
|
} finally {
|
|
148
237
|
currentOwner = prevOwner;
|
|
149
|
-
|
|
238
|
+
listener = prevListener;
|
|
150
239
|
}
|
|
151
240
|
});
|
|
152
241
|
}
|
|
153
242
|
function untrack(fn) {
|
|
154
|
-
const prev =
|
|
155
|
-
|
|
243
|
+
const prev = listener;
|
|
244
|
+
listener = null;
|
|
156
245
|
try {
|
|
157
246
|
return fn();
|
|
158
247
|
} finally {
|
|
159
|
-
|
|
248
|
+
listener = prev;
|
|
160
249
|
}
|
|
161
250
|
}
|
|
162
251
|
function catchError(fn, handler) {
|
|
@@ -214,13 +303,6 @@ function useContext(ctx) {
|
|
|
214
303
|
return ctx.defaultValue;
|
|
215
304
|
}
|
|
216
305
|
|
|
217
|
-
// src/reactivity/memo.ts
|
|
218
|
-
function createMemo(fn) {
|
|
219
|
-
const [value, setValue] = createSignal(void 0);
|
|
220
|
-
createEffect(() => setValue(() => fn()));
|
|
221
|
-
return value;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
306
|
// src/reactivity/suspense.ts
|
|
225
307
|
var SuspenseContext = createContext(null);
|
|
226
308
|
function trackPending() {
|
|
@@ -240,12 +322,12 @@ function createResource(a, b) {
|
|
|
240
322
|
const [failure, setFailure] = createSignal(null);
|
|
241
323
|
const [loading, setLoading] = createSignal(false);
|
|
242
324
|
const [version, setVersion] = createSignal(0);
|
|
243
|
-
let
|
|
244
|
-
const
|
|
325
|
+
let run2 = 0;
|
|
326
|
+
const track2 = trackPending();
|
|
245
327
|
let release;
|
|
246
328
|
let settled = false;
|
|
247
329
|
const finish = (id, outcome) => {
|
|
248
|
-
if (id !==
|
|
330
|
+
if (id !== run2) return;
|
|
249
331
|
settled = true;
|
|
250
332
|
batch(() => {
|
|
251
333
|
setLoading(false);
|
|
@@ -262,10 +344,10 @@ function createResource(a, b) {
|
|
|
262
344
|
createEffect(() => {
|
|
263
345
|
const s = source();
|
|
264
346
|
version();
|
|
265
|
-
|
|
266
|
-
const id =
|
|
347
|
+
run2++;
|
|
348
|
+
const id = run2;
|
|
267
349
|
onCleanup(() => {
|
|
268
|
-
|
|
350
|
+
run2++;
|
|
269
351
|
release?.();
|
|
270
352
|
release = void 0;
|
|
271
353
|
});
|
|
@@ -277,7 +359,7 @@ function createResource(a, b) {
|
|
|
277
359
|
});
|
|
278
360
|
return;
|
|
279
361
|
}
|
|
280
|
-
if (!settled &&
|
|
362
|
+
if (!settled && track2 && !release) release = track2();
|
|
281
363
|
setLoading(true);
|
|
282
364
|
setFailure(null);
|
|
283
365
|
let result;
|
|
@@ -315,6 +397,239 @@ function createResource(a, b) {
|
|
|
315
397
|
];
|
|
316
398
|
}
|
|
317
399
|
|
|
400
|
+
// src/reactivity/store.ts
|
|
401
|
+
var RAW = /* @__PURE__ */ Symbol("cajoa-store-raw");
|
|
402
|
+
var MUTATE_MESSAGE = "Cannot change a store directly. Use its setter instead, for example setState('count', 1) or setState((draft) => { draft.count = 1; }).";
|
|
403
|
+
var nodes = /* @__PURE__ */ new WeakMap();
|
|
404
|
+
var readers = /* @__PURE__ */ new WeakMap();
|
|
405
|
+
var drafts = /* @__PURE__ */ new WeakMap();
|
|
406
|
+
function makeCell() {
|
|
407
|
+
const [read, write] = createSignal(0);
|
|
408
|
+
return {
|
|
409
|
+
track: () => {
|
|
410
|
+
read();
|
|
411
|
+
},
|
|
412
|
+
notify: () => write((n) => n + 1)
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
function nodeOf(raw) {
|
|
416
|
+
let node = nodes.get(raw);
|
|
417
|
+
if (!node) {
|
|
418
|
+
node = { cells: /* @__PURE__ */ new Map() };
|
|
419
|
+
nodes.set(raw, node);
|
|
420
|
+
}
|
|
421
|
+
return node;
|
|
422
|
+
}
|
|
423
|
+
function trackKey(raw, key) {
|
|
424
|
+
const node = nodeOf(raw);
|
|
425
|
+
let cell = node.cells.get(key);
|
|
426
|
+
if (!cell) {
|
|
427
|
+
cell = makeCell();
|
|
428
|
+
node.cells.set(key, cell);
|
|
429
|
+
}
|
|
430
|
+
cell.track();
|
|
431
|
+
}
|
|
432
|
+
function trackOwn(raw) {
|
|
433
|
+
const node = nodeOf(raw);
|
|
434
|
+
(node.own ??= makeCell()).track();
|
|
435
|
+
}
|
|
436
|
+
var notifyKey = (raw, key) => nodes.get(raw)?.cells.get(key)?.notify();
|
|
437
|
+
var notifyOwn = (raw) => nodes.get(raw)?.own?.notify();
|
|
438
|
+
function isWrappable(value) {
|
|
439
|
+
if (value === null || typeof value !== "object") return false;
|
|
440
|
+
if (Array.isArray(value)) return true;
|
|
441
|
+
const proto = Object.getPrototypeOf(value);
|
|
442
|
+
return proto === Object.prototype || proto === null;
|
|
443
|
+
}
|
|
444
|
+
function toRaw(value) {
|
|
445
|
+
if (value !== null && typeof value === "object") {
|
|
446
|
+
const raw = value[RAW];
|
|
447
|
+
if (raw) return raw;
|
|
448
|
+
}
|
|
449
|
+
return value;
|
|
450
|
+
}
|
|
451
|
+
function unwrapIncoming(value, seen = /* @__PURE__ */ new Set()) {
|
|
452
|
+
const raw = toRaw(value);
|
|
453
|
+
if (!isWrappable(raw) || seen.has(raw)) return raw;
|
|
454
|
+
seen.add(raw);
|
|
455
|
+
const record = raw;
|
|
456
|
+
for (const key of Object.keys(record)) {
|
|
457
|
+
const child = record[key];
|
|
458
|
+
const inner = unwrapIncoming(child, seen);
|
|
459
|
+
if (inner !== child) record[key] = inner;
|
|
460
|
+
}
|
|
461
|
+
return raw;
|
|
462
|
+
}
|
|
463
|
+
function removeProperty(raw, key) {
|
|
464
|
+
if (!Object.prototype.hasOwnProperty.call(raw, key)) return;
|
|
465
|
+
delete raw[key];
|
|
466
|
+
batch(() => {
|
|
467
|
+
notifyKey(raw, key);
|
|
468
|
+
notifyOwn(raw);
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
function writeProperty(raw, key, input) {
|
|
472
|
+
const value = toRaw(input);
|
|
473
|
+
const record = raw;
|
|
474
|
+
const isArray = Array.isArray(raw);
|
|
475
|
+
const had = Object.prototype.hasOwnProperty.call(raw, key);
|
|
476
|
+
if (value === void 0 && !isArray) {
|
|
477
|
+
removeProperty(raw, key);
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
if (had && Object.is(record[key], value)) return;
|
|
481
|
+
const next = unwrapIncoming(value);
|
|
482
|
+
if (isArray && key === "length") {
|
|
483
|
+
const array = raw;
|
|
484
|
+
const old = array.length;
|
|
485
|
+
const newLength = Number(next);
|
|
486
|
+
array.length = newLength;
|
|
487
|
+
batch(() => {
|
|
488
|
+
for (let i = newLength; i < old; i++) notifyKey(raw, String(i));
|
|
489
|
+
notifyKey(raw, "length");
|
|
490
|
+
notifyOwn(raw);
|
|
491
|
+
});
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
const oldLength = isArray ? raw.length : 0;
|
|
495
|
+
record[key] = next;
|
|
496
|
+
batch(() => {
|
|
497
|
+
notifyKey(raw, key);
|
|
498
|
+
if (!had) notifyOwn(raw);
|
|
499
|
+
if (isArray && raw.length !== oldLength) {
|
|
500
|
+
notifyKey(raw, "length");
|
|
501
|
+
notifyOwn(raw);
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
function merge(target, source) {
|
|
506
|
+
for (const key of Object.keys(source)) {
|
|
507
|
+
writeProperty(target, key, source[key]);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
var wrapRead = (value) => isWrappable(value) ? readerOf(toRaw(value)) : value;
|
|
511
|
+
var wrapDraft = (value) => isWrappable(value) ? draftOf(toRaw(value)) : value;
|
|
512
|
+
var readHandler = {
|
|
513
|
+
get(target, key, receiver) {
|
|
514
|
+
if (key === RAW) return target;
|
|
515
|
+
const value = Reflect.get(target, key, receiver);
|
|
516
|
+
if (typeof key === "symbol") return value;
|
|
517
|
+
if (typeof value === "function" && !Object.prototype.hasOwnProperty.call(target, key)) {
|
|
518
|
+
return value;
|
|
519
|
+
}
|
|
520
|
+
trackKey(target, key);
|
|
521
|
+
return wrapRead(value);
|
|
522
|
+
},
|
|
523
|
+
has(target, key) {
|
|
524
|
+
if (key === RAW) return true;
|
|
525
|
+
if (typeof key !== "symbol") trackKey(target, key);
|
|
526
|
+
return Reflect.has(target, key);
|
|
527
|
+
},
|
|
528
|
+
ownKeys(target) {
|
|
529
|
+
trackOwn(target);
|
|
530
|
+
return Reflect.ownKeys(target);
|
|
531
|
+
},
|
|
532
|
+
set() {
|
|
533
|
+
throw new Error(MUTATE_MESSAGE);
|
|
534
|
+
},
|
|
535
|
+
deleteProperty() {
|
|
536
|
+
throw new Error(MUTATE_MESSAGE);
|
|
537
|
+
},
|
|
538
|
+
defineProperty() {
|
|
539
|
+
throw new Error(MUTATE_MESSAGE);
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
var draftHandler = {
|
|
543
|
+
get(target, key, receiver) {
|
|
544
|
+
if (key === RAW) return target;
|
|
545
|
+
const value = Reflect.get(target, key, receiver);
|
|
546
|
+
if (typeof key === "symbol") return value;
|
|
547
|
+
return wrapDraft(value);
|
|
548
|
+
},
|
|
549
|
+
set(target, key, value) {
|
|
550
|
+
if (typeof key === "symbol") return Reflect.set(target, key, value);
|
|
551
|
+
writeProperty(target, key, value);
|
|
552
|
+
return true;
|
|
553
|
+
},
|
|
554
|
+
deleteProperty(target, key) {
|
|
555
|
+
if (typeof key === "symbol") return Reflect.deleteProperty(target, key);
|
|
556
|
+
removeProperty(target, key);
|
|
557
|
+
return true;
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
function readerOf(raw) {
|
|
561
|
+
let proxy = readers.get(raw);
|
|
562
|
+
if (!proxy) {
|
|
563
|
+
proxy = new Proxy(raw, readHandler);
|
|
564
|
+
readers.set(raw, proxy);
|
|
565
|
+
}
|
|
566
|
+
return proxy;
|
|
567
|
+
}
|
|
568
|
+
function draftOf(raw) {
|
|
569
|
+
let proxy = drafts.get(raw);
|
|
570
|
+
if (!proxy) {
|
|
571
|
+
proxy = new Proxy(raw, draftHandler);
|
|
572
|
+
drafts.set(raw, proxy);
|
|
573
|
+
}
|
|
574
|
+
return proxy;
|
|
575
|
+
}
|
|
576
|
+
function applyUpdate(root, args) {
|
|
577
|
+
if (args.length === 0) return;
|
|
578
|
+
if (args.length === 1) {
|
|
579
|
+
const arg = args[0];
|
|
580
|
+
if (typeof arg === "function") {
|
|
581
|
+
const result = untrack(
|
|
582
|
+
() => arg(draftOf(root))
|
|
583
|
+
);
|
|
584
|
+
if (isWrappable(result) && !Array.isArray(result))
|
|
585
|
+
merge(root, toRaw(result));
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
if (isWrappable(arg) && !Array.isArray(arg)) {
|
|
589
|
+
merge(root, toRaw(arg));
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
throw new Error(
|
|
593
|
+
"setState(): pass an object to merge, a function that edits a draft, or a path followed by a value."
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
const path = args.slice(0, -1);
|
|
597
|
+
const last = args[args.length - 1];
|
|
598
|
+
let target = root;
|
|
599
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
600
|
+
const next = target[String(path[i])];
|
|
601
|
+
if (!isWrappable(next)) {
|
|
602
|
+
throw new Error(
|
|
603
|
+
`setState(): "${path.slice(0, i + 1).join(".")}" is not an object or an array, so it has no "${path[i + 1]}" to change.`
|
|
604
|
+
);
|
|
605
|
+
}
|
|
606
|
+
target = toRaw(next);
|
|
607
|
+
}
|
|
608
|
+
const key = String(path[path.length - 1]);
|
|
609
|
+
const prev = target[key];
|
|
610
|
+
if (typeof last === "function") {
|
|
611
|
+
const result = untrack(() => last(prev));
|
|
612
|
+
writeProperty(target, key, result);
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
const incoming = toRaw(last);
|
|
616
|
+
if (isWrappable(prev) && !Array.isArray(prev) && isWrappable(incoming) && !Array.isArray(incoming)) {
|
|
617
|
+
merge(toRaw(prev), incoming);
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
writeProperty(target, key, incoming);
|
|
621
|
+
}
|
|
622
|
+
function createStore(initial) {
|
|
623
|
+
const root = unwrapIncoming(initial);
|
|
624
|
+
const setState = (...args) => {
|
|
625
|
+
batch(() => applyUpdate(root, args));
|
|
626
|
+
};
|
|
627
|
+
return [readerOf(root), setState];
|
|
628
|
+
}
|
|
629
|
+
function unwrap(value) {
|
|
630
|
+
return toRaw(value);
|
|
631
|
+
}
|
|
632
|
+
|
|
318
633
|
// src/renderer/index.ts
|
|
319
634
|
var current = null;
|
|
320
635
|
function setRenderer(renderer) {
|
|
@@ -402,7 +717,7 @@ function appendChildren(parent, children) {
|
|
|
402
717
|
|
|
403
718
|
// src/runtime/render.ts
|
|
404
719
|
function render(component, container) {
|
|
405
|
-
return createRoot((
|
|
720
|
+
return createRoot((dispose2) => {
|
|
406
721
|
const r = getRenderer();
|
|
407
722
|
const node = component();
|
|
408
723
|
if (typeof node === "function") {
|
|
@@ -413,7 +728,7 @@ function render(component, container) {
|
|
|
413
728
|
r.clear(container);
|
|
414
729
|
r.insert(container, node);
|
|
415
730
|
return () => {
|
|
416
|
-
|
|
731
|
+
dispose2();
|
|
417
732
|
r.clear(container);
|
|
418
733
|
};
|
|
419
734
|
});
|
|
@@ -423,8 +738,12 @@ function render(component, container) {
|
|
|
423
738
|
function resolve(value) {
|
|
424
739
|
return typeof value === "function" ? value() : value;
|
|
425
740
|
}
|
|
741
|
+
function resolveCondition(value) {
|
|
742
|
+
while (typeof value === "function") value = value();
|
|
743
|
+
return value;
|
|
744
|
+
}
|
|
426
745
|
function Show(props) {
|
|
427
|
-
const visible = createMemo(() => !!
|
|
746
|
+
const visible = createMemo(() => !!resolveCondition(props.when));
|
|
428
747
|
return () => visible() ? untrack(() => resolve(props.children ?? null)) : untrack(() => resolve(props.fallback ?? null));
|
|
429
748
|
}
|
|
430
749
|
|
|
@@ -436,9 +755,11 @@ function For(props) {
|
|
|
436
755
|
cache.clear();
|
|
437
756
|
});
|
|
438
757
|
return () => {
|
|
439
|
-
|
|
758
|
+
let source = props.each;
|
|
759
|
+
while (typeof source === "function") source = source();
|
|
760
|
+
const items = source ?? [];
|
|
440
761
|
const next = /* @__PURE__ */ new Map();
|
|
441
|
-
const
|
|
762
|
+
const nodes2 = [];
|
|
442
763
|
items.forEach((item, i) => {
|
|
443
764
|
const k = props.key ? props.key(item, i) : item;
|
|
444
765
|
if (next.has(k)) {
|
|
@@ -448,19 +769,19 @@ function For(props) {
|
|
|
448
769
|
}
|
|
449
770
|
let entry = cache.get(k);
|
|
450
771
|
if (!entry) {
|
|
451
|
-
entry = createRoot((
|
|
772
|
+
entry = createRoot((dispose2) => ({
|
|
452
773
|
node: props.children(item, i),
|
|
453
|
-
dispose
|
|
774
|
+
dispose: dispose2
|
|
454
775
|
}));
|
|
455
776
|
}
|
|
456
777
|
next.set(k, entry);
|
|
457
|
-
|
|
778
|
+
nodes2.push(entry.node);
|
|
458
779
|
});
|
|
459
780
|
cache.forEach((entry, k) => {
|
|
460
781
|
if (!next.has(k)) entry.dispose();
|
|
461
782
|
});
|
|
462
783
|
cache = next;
|
|
463
|
-
return
|
|
784
|
+
return nodes2;
|
|
464
785
|
};
|
|
465
786
|
}
|
|
466
787
|
|
|
@@ -518,6 +839,10 @@ function ErrorBoundary(props) {
|
|
|
518
839
|
function resolve3(value) {
|
|
519
840
|
return typeof value === "function" ? value() : value;
|
|
520
841
|
}
|
|
842
|
+
function resolveCondition2(value) {
|
|
843
|
+
while (typeof value === "function") value = value();
|
|
844
|
+
return value;
|
|
845
|
+
}
|
|
521
846
|
function Match(props) {
|
|
522
847
|
return { __match: true, when: props.when, children: props.children };
|
|
523
848
|
}
|
|
@@ -526,7 +851,7 @@ function Switch(props) {
|
|
|
526
851
|
const matches = [props.children].flat(Infinity).filter(isMatch);
|
|
527
852
|
const selected = createMemo(() => {
|
|
528
853
|
for (let i = 0; i < matches.length; i++) {
|
|
529
|
-
if (
|
|
854
|
+
if (resolveCondition2(matches[i].when)) return i;
|
|
530
855
|
}
|
|
531
856
|
return -1;
|
|
532
857
|
});
|
|
@@ -609,7 +934,7 @@ function resolve5(value) {
|
|
|
609
934
|
return typeof value === "function" ? value() : value;
|
|
610
935
|
}
|
|
611
936
|
function Suspense(props) {
|
|
612
|
-
const [
|
|
937
|
+
const [pending, setPending] = createSignal(0);
|
|
613
938
|
const [ready, setReady] = createSignal(false);
|
|
614
939
|
const state = {
|
|
615
940
|
add() {
|
|
@@ -630,7 +955,7 @@ function Suspense(props) {
|
|
|
630
955
|
)
|
|
631
956
|
);
|
|
632
957
|
createEffect(() => {
|
|
633
|
-
if (
|
|
958
|
+
if (pending() === 0) setReady(true);
|
|
634
959
|
});
|
|
635
960
|
return () => ready() ? content : untrack(() => resolve5(props.fallback ?? null));
|
|
636
961
|
}
|
|
@@ -682,6 +1007,7 @@ export {
|
|
|
682
1007
|
createResource,
|
|
683
1008
|
createRoot,
|
|
684
1009
|
createSignal,
|
|
1010
|
+
createStore,
|
|
685
1011
|
getRenderer,
|
|
686
1012
|
h,
|
|
687
1013
|
jsx,
|
|
@@ -694,5 +1020,6 @@ export {
|
|
|
694
1020
|
render,
|
|
695
1021
|
setRenderer,
|
|
696
1022
|
untrack,
|
|
1023
|
+
unwrap,
|
|
697
1024
|
useContext
|
|
698
1025
|
};
|