@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.cjs
CHANGED
|
@@ -37,6 +37,7 @@ __export(index_exports, {
|
|
|
37
37
|
createResource: () => createResource,
|
|
38
38
|
createRoot: () => createRoot,
|
|
39
39
|
createSignal: () => createSignal,
|
|
40
|
+
createStore: () => createStore,
|
|
40
41
|
getRenderer: () => getRenderer,
|
|
41
42
|
h: () => h,
|
|
42
43
|
jsx: () => jsx,
|
|
@@ -49,6 +50,7 @@ __export(index_exports, {
|
|
|
49
50
|
render: () => render,
|
|
50
51
|
setRenderer: () => setRenderer,
|
|
51
52
|
untrack: () => untrack,
|
|
53
|
+
unwrap: () => unwrap,
|
|
52
54
|
useContext: () => useContext
|
|
53
55
|
});
|
|
54
56
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -77,16 +79,19 @@ function logError(message, error) {
|
|
|
77
79
|
}
|
|
78
80
|
|
|
79
81
|
// src/reactivity/signal.ts
|
|
82
|
+
var CLEAN = 0;
|
|
83
|
+
var CHECK = 1;
|
|
84
|
+
var DIRTY = 2;
|
|
80
85
|
var currentOwner = null;
|
|
81
|
-
var
|
|
86
|
+
var listener = null;
|
|
82
87
|
function clean(owner) {
|
|
83
88
|
const fns = owner.cleanups;
|
|
84
89
|
owner.cleanups = [];
|
|
85
90
|
for (const fn of fns) fn();
|
|
86
91
|
}
|
|
87
92
|
function cleanComputation(c) {
|
|
88
|
-
for (const
|
|
89
|
-
c.
|
|
93
|
+
for (const source of c.sources) source.observers.delete(c);
|
|
94
|
+
c.sources.clear();
|
|
90
95
|
clean(c);
|
|
91
96
|
}
|
|
92
97
|
function addCleanup(fn) {
|
|
@@ -102,11 +107,104 @@ function handleError(error, owner) {
|
|
|
102
107
|
throw error;
|
|
103
108
|
}
|
|
104
109
|
var batchDepth = 0;
|
|
105
|
-
var
|
|
110
|
+
var flushing = false;
|
|
111
|
+
var queued = /* @__PURE__ */ new Set();
|
|
112
|
+
var isComputation = (source) => "fn" in source;
|
|
113
|
+
function track(source) {
|
|
114
|
+
if (listener) {
|
|
115
|
+
source.observers.add(listener);
|
|
116
|
+
listener.sources.add(source);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function mark(node, state) {
|
|
120
|
+
if (node.disposed || node.state >= state) return;
|
|
121
|
+
const wasClean = node.state === CLEAN;
|
|
122
|
+
node.state = state;
|
|
123
|
+
if (!wasClean) return;
|
|
124
|
+
if (node.isEffect) {
|
|
125
|
+
queued.add(node);
|
|
126
|
+
} else {
|
|
127
|
+
for (const observer of node.observers) mark(observer, CHECK);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function notify(source) {
|
|
131
|
+
for (const observer of source.observers) mark(observer, DIRTY);
|
|
132
|
+
if (batchDepth === 0 && !flushing) flush();
|
|
133
|
+
}
|
|
134
|
+
function run(node) {
|
|
135
|
+
cleanComputation(node);
|
|
136
|
+
const prevOwner = currentOwner;
|
|
137
|
+
const prevListener = listener;
|
|
138
|
+
currentOwner = node;
|
|
139
|
+
listener = node;
|
|
140
|
+
node.state = CLEAN;
|
|
141
|
+
try {
|
|
142
|
+
const result = node.fn();
|
|
143
|
+
if (!node.isEffect) {
|
|
144
|
+
const changed = node.hasValue && !Object.is(node.value, result);
|
|
145
|
+
node.value = result;
|
|
146
|
+
node.hasValue = true;
|
|
147
|
+
if (changed) {
|
|
148
|
+
for (const observer of node.observers) {
|
|
149
|
+
if (observer.state === CHECK) observer.state = DIRTY;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
} catch (error) {
|
|
154
|
+
handleError(error, node);
|
|
155
|
+
} finally {
|
|
156
|
+
currentOwner = prevOwner;
|
|
157
|
+
listener = prevListener;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function update(node) {
|
|
161
|
+
if (node.disposed) {
|
|
162
|
+
node.state = CLEAN;
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (node.state === CHECK) {
|
|
166
|
+
for (const source of [...node.sources]) {
|
|
167
|
+
if (isComputation(source)) {
|
|
168
|
+
update(source);
|
|
169
|
+
if (node.state === DIRTY) break;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (node.state === DIRTY) run(node);
|
|
174
|
+
else node.state = CLEAN;
|
|
175
|
+
}
|
|
106
176
|
function flush() {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
177
|
+
flushing = true;
|
|
178
|
+
let failure = null;
|
|
179
|
+
let steps = 0;
|
|
180
|
+
try {
|
|
181
|
+
while (queued.size > 0) {
|
|
182
|
+
if (++steps > 1e5) {
|
|
183
|
+
queued.clear();
|
|
184
|
+
failure ??= {
|
|
185
|
+
error: new Error(
|
|
186
|
+
"Cajoa: updates never settled. An effect probably keeps changing a signal that it also reads."
|
|
187
|
+
)
|
|
188
|
+
};
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
const effect = queued.values().next().value;
|
|
192
|
+
queued.delete(effect);
|
|
193
|
+
try {
|
|
194
|
+
update(effect);
|
|
195
|
+
} catch (error) {
|
|
196
|
+
failure ??= { error };
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
} finally {
|
|
200
|
+
flushing = false;
|
|
201
|
+
}
|
|
202
|
+
if (failure) throw failure.error;
|
|
203
|
+
}
|
|
204
|
+
function dispose(node) {
|
|
205
|
+
node.disposed = true;
|
|
206
|
+
queued.delete(node);
|
|
207
|
+
cleanComputation(node);
|
|
110
208
|
}
|
|
111
209
|
function batch(fn) {
|
|
112
210
|
batchDepth++;
|
|
@@ -114,58 +212,51 @@ function batch(fn) {
|
|
|
114
212
|
return fn();
|
|
115
213
|
} finally {
|
|
116
214
|
batchDepth--;
|
|
117
|
-
if (batchDepth === 0) flush();
|
|
215
|
+
if (batchDepth === 0 && !flushing) flush();
|
|
118
216
|
}
|
|
119
217
|
}
|
|
120
218
|
function createSignal(value) {
|
|
121
|
-
const
|
|
219
|
+
const source = { observers: /* @__PURE__ */ new Set() };
|
|
122
220
|
const get = () => {
|
|
123
|
-
|
|
124
|
-
subs.add(currentEffect);
|
|
125
|
-
currentEffect.deps.add(subs);
|
|
126
|
-
}
|
|
221
|
+
track(source);
|
|
127
222
|
return value;
|
|
128
223
|
};
|
|
129
224
|
const set = (next) => {
|
|
130
225
|
const v = typeof next === "function" ? next(value) : next;
|
|
131
226
|
if (Object.is(v, value)) return;
|
|
132
227
|
value = v;
|
|
133
|
-
|
|
134
|
-
if (batchDepth > 0)
|
|
135
|
-
pending.add(effect);
|
|
136
|
-
else effect.run();
|
|
137
|
-
}
|
|
228
|
+
notify(source);
|
|
138
229
|
};
|
|
139
230
|
return [get, set];
|
|
140
231
|
}
|
|
141
|
-
function
|
|
142
|
-
|
|
232
|
+
function createComputation(fn, isEffect) {
|
|
233
|
+
return {
|
|
234
|
+
fn,
|
|
235
|
+
isEffect,
|
|
143
236
|
cleanups: [],
|
|
144
237
|
parent: currentOwner,
|
|
145
|
-
|
|
238
|
+
sources: /* @__PURE__ */ new Set(),
|
|
239
|
+
observers: /* @__PURE__ */ new Set(),
|
|
240
|
+
state: DIRTY,
|
|
146
241
|
disposed: false,
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
242
|
+
hasValue: false,
|
|
243
|
+
value: void 0
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
function createEffect(fn) {
|
|
247
|
+
const node = createComputation(fn, true);
|
|
248
|
+
addCleanup(() => dispose(node));
|
|
249
|
+
run(node);
|
|
250
|
+
}
|
|
251
|
+
function createMemo(fn) {
|
|
252
|
+
const node = createComputation(fn, false);
|
|
253
|
+
addCleanup(() => dispose(node));
|
|
254
|
+
run(node);
|
|
255
|
+
return () => {
|
|
256
|
+
if (node.state !== CLEAN) update(node);
|
|
257
|
+
track(node);
|
|
258
|
+
return node.value;
|
|
163
259
|
};
|
|
164
|
-
addCleanup(() => {
|
|
165
|
-
effect.disposed = true;
|
|
166
|
-
cleanComputation(effect);
|
|
167
|
-
});
|
|
168
|
-
effect.run();
|
|
169
260
|
}
|
|
170
261
|
function onCleanup(fn) {
|
|
171
262
|
if (!currentOwner) {
|
|
@@ -178,40 +269,40 @@ function onCleanup(fn) {
|
|
|
178
269
|
function createRoot(fn) {
|
|
179
270
|
const root = { cleanups: [], parent: currentOwner };
|
|
180
271
|
const prevOwner = currentOwner;
|
|
181
|
-
const
|
|
272
|
+
const prevListener = listener;
|
|
182
273
|
currentOwner = root;
|
|
183
|
-
|
|
274
|
+
listener = null;
|
|
184
275
|
try {
|
|
185
276
|
return fn(() => clean(root));
|
|
186
277
|
} finally {
|
|
187
278
|
currentOwner = prevOwner;
|
|
188
|
-
|
|
279
|
+
listener = prevListener;
|
|
189
280
|
}
|
|
190
281
|
}
|
|
191
282
|
function onMount(fn) {
|
|
192
283
|
const owner = currentOwner;
|
|
193
284
|
Promise.resolve().then(() => {
|
|
194
285
|
const prevOwner = currentOwner;
|
|
195
|
-
const
|
|
286
|
+
const prevListener = listener;
|
|
196
287
|
currentOwner = owner;
|
|
197
|
-
|
|
288
|
+
listener = null;
|
|
198
289
|
try {
|
|
199
290
|
fn();
|
|
200
291
|
} catch (error) {
|
|
201
292
|
handleError(error, owner);
|
|
202
293
|
} finally {
|
|
203
294
|
currentOwner = prevOwner;
|
|
204
|
-
|
|
295
|
+
listener = prevListener;
|
|
205
296
|
}
|
|
206
297
|
});
|
|
207
298
|
}
|
|
208
299
|
function untrack(fn) {
|
|
209
|
-
const prev =
|
|
210
|
-
|
|
300
|
+
const prev = listener;
|
|
301
|
+
listener = null;
|
|
211
302
|
try {
|
|
212
303
|
return fn();
|
|
213
304
|
} finally {
|
|
214
|
-
|
|
305
|
+
listener = prev;
|
|
215
306
|
}
|
|
216
307
|
}
|
|
217
308
|
function catchError(fn, handler) {
|
|
@@ -269,13 +360,6 @@ function useContext(ctx) {
|
|
|
269
360
|
return ctx.defaultValue;
|
|
270
361
|
}
|
|
271
362
|
|
|
272
|
-
// src/reactivity/memo.ts
|
|
273
|
-
function createMemo(fn) {
|
|
274
|
-
const [value, setValue] = createSignal(void 0);
|
|
275
|
-
createEffect(() => setValue(() => fn()));
|
|
276
|
-
return value;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
363
|
// src/reactivity/suspense.ts
|
|
280
364
|
var SuspenseContext = createContext(null);
|
|
281
365
|
function trackPending() {
|
|
@@ -295,12 +379,12 @@ function createResource(a, b) {
|
|
|
295
379
|
const [failure, setFailure] = createSignal(null);
|
|
296
380
|
const [loading, setLoading] = createSignal(false);
|
|
297
381
|
const [version, setVersion] = createSignal(0);
|
|
298
|
-
let
|
|
299
|
-
const
|
|
382
|
+
let run2 = 0;
|
|
383
|
+
const track2 = trackPending();
|
|
300
384
|
let release;
|
|
301
385
|
let settled = false;
|
|
302
386
|
const finish = (id, outcome) => {
|
|
303
|
-
if (id !==
|
|
387
|
+
if (id !== run2) return;
|
|
304
388
|
settled = true;
|
|
305
389
|
batch(() => {
|
|
306
390
|
setLoading(false);
|
|
@@ -317,10 +401,10 @@ function createResource(a, b) {
|
|
|
317
401
|
createEffect(() => {
|
|
318
402
|
const s = source();
|
|
319
403
|
version();
|
|
320
|
-
|
|
321
|
-
const id =
|
|
404
|
+
run2++;
|
|
405
|
+
const id = run2;
|
|
322
406
|
onCleanup(() => {
|
|
323
|
-
|
|
407
|
+
run2++;
|
|
324
408
|
release?.();
|
|
325
409
|
release = void 0;
|
|
326
410
|
});
|
|
@@ -332,7 +416,7 @@ function createResource(a, b) {
|
|
|
332
416
|
});
|
|
333
417
|
return;
|
|
334
418
|
}
|
|
335
|
-
if (!settled &&
|
|
419
|
+
if (!settled && track2 && !release) release = track2();
|
|
336
420
|
setLoading(true);
|
|
337
421
|
setFailure(null);
|
|
338
422
|
let result;
|
|
@@ -370,6 +454,239 @@ function createResource(a, b) {
|
|
|
370
454
|
];
|
|
371
455
|
}
|
|
372
456
|
|
|
457
|
+
// src/reactivity/store.ts
|
|
458
|
+
var RAW = /* @__PURE__ */ Symbol("cajoa-store-raw");
|
|
459
|
+
var MUTATE_MESSAGE = "Cannot change a store directly. Use its setter instead, for example setState('count', 1) or setState((draft) => { draft.count = 1; }).";
|
|
460
|
+
var nodes = /* @__PURE__ */ new WeakMap();
|
|
461
|
+
var readers = /* @__PURE__ */ new WeakMap();
|
|
462
|
+
var drafts = /* @__PURE__ */ new WeakMap();
|
|
463
|
+
function makeCell() {
|
|
464
|
+
const [read, write] = createSignal(0);
|
|
465
|
+
return {
|
|
466
|
+
track: () => {
|
|
467
|
+
read();
|
|
468
|
+
},
|
|
469
|
+
notify: () => write((n) => n + 1)
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
function nodeOf(raw) {
|
|
473
|
+
let node = nodes.get(raw);
|
|
474
|
+
if (!node) {
|
|
475
|
+
node = { cells: /* @__PURE__ */ new Map() };
|
|
476
|
+
nodes.set(raw, node);
|
|
477
|
+
}
|
|
478
|
+
return node;
|
|
479
|
+
}
|
|
480
|
+
function trackKey(raw, key) {
|
|
481
|
+
const node = nodeOf(raw);
|
|
482
|
+
let cell = node.cells.get(key);
|
|
483
|
+
if (!cell) {
|
|
484
|
+
cell = makeCell();
|
|
485
|
+
node.cells.set(key, cell);
|
|
486
|
+
}
|
|
487
|
+
cell.track();
|
|
488
|
+
}
|
|
489
|
+
function trackOwn(raw) {
|
|
490
|
+
const node = nodeOf(raw);
|
|
491
|
+
(node.own ??= makeCell()).track();
|
|
492
|
+
}
|
|
493
|
+
var notifyKey = (raw, key) => nodes.get(raw)?.cells.get(key)?.notify();
|
|
494
|
+
var notifyOwn = (raw) => nodes.get(raw)?.own?.notify();
|
|
495
|
+
function isWrappable(value) {
|
|
496
|
+
if (value === null || typeof value !== "object") return false;
|
|
497
|
+
if (Array.isArray(value)) return true;
|
|
498
|
+
const proto = Object.getPrototypeOf(value);
|
|
499
|
+
return proto === Object.prototype || proto === null;
|
|
500
|
+
}
|
|
501
|
+
function toRaw(value) {
|
|
502
|
+
if (value !== null && typeof value === "object") {
|
|
503
|
+
const raw = value[RAW];
|
|
504
|
+
if (raw) return raw;
|
|
505
|
+
}
|
|
506
|
+
return value;
|
|
507
|
+
}
|
|
508
|
+
function unwrapIncoming(value, seen = /* @__PURE__ */ new Set()) {
|
|
509
|
+
const raw = toRaw(value);
|
|
510
|
+
if (!isWrappable(raw) || seen.has(raw)) return raw;
|
|
511
|
+
seen.add(raw);
|
|
512
|
+
const record = raw;
|
|
513
|
+
for (const key of Object.keys(record)) {
|
|
514
|
+
const child = record[key];
|
|
515
|
+
const inner = unwrapIncoming(child, seen);
|
|
516
|
+
if (inner !== child) record[key] = inner;
|
|
517
|
+
}
|
|
518
|
+
return raw;
|
|
519
|
+
}
|
|
520
|
+
function removeProperty(raw, key) {
|
|
521
|
+
if (!Object.prototype.hasOwnProperty.call(raw, key)) return;
|
|
522
|
+
delete raw[key];
|
|
523
|
+
batch(() => {
|
|
524
|
+
notifyKey(raw, key);
|
|
525
|
+
notifyOwn(raw);
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
function writeProperty(raw, key, input) {
|
|
529
|
+
const value = toRaw(input);
|
|
530
|
+
const record = raw;
|
|
531
|
+
const isArray = Array.isArray(raw);
|
|
532
|
+
const had = Object.prototype.hasOwnProperty.call(raw, key);
|
|
533
|
+
if (value === void 0 && !isArray) {
|
|
534
|
+
removeProperty(raw, key);
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
if (had && Object.is(record[key], value)) return;
|
|
538
|
+
const next = unwrapIncoming(value);
|
|
539
|
+
if (isArray && key === "length") {
|
|
540
|
+
const array = raw;
|
|
541
|
+
const old = array.length;
|
|
542
|
+
const newLength = Number(next);
|
|
543
|
+
array.length = newLength;
|
|
544
|
+
batch(() => {
|
|
545
|
+
for (let i = newLength; i < old; i++) notifyKey(raw, String(i));
|
|
546
|
+
notifyKey(raw, "length");
|
|
547
|
+
notifyOwn(raw);
|
|
548
|
+
});
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
const oldLength = isArray ? raw.length : 0;
|
|
552
|
+
record[key] = next;
|
|
553
|
+
batch(() => {
|
|
554
|
+
notifyKey(raw, key);
|
|
555
|
+
if (!had) notifyOwn(raw);
|
|
556
|
+
if (isArray && raw.length !== oldLength) {
|
|
557
|
+
notifyKey(raw, "length");
|
|
558
|
+
notifyOwn(raw);
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
function merge(target, source) {
|
|
563
|
+
for (const key of Object.keys(source)) {
|
|
564
|
+
writeProperty(target, key, source[key]);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
var wrapRead = (value) => isWrappable(value) ? readerOf(toRaw(value)) : value;
|
|
568
|
+
var wrapDraft = (value) => isWrappable(value) ? draftOf(toRaw(value)) : value;
|
|
569
|
+
var readHandler = {
|
|
570
|
+
get(target, key, receiver) {
|
|
571
|
+
if (key === RAW) return target;
|
|
572
|
+
const value = Reflect.get(target, key, receiver);
|
|
573
|
+
if (typeof key === "symbol") return value;
|
|
574
|
+
if (typeof value === "function" && !Object.prototype.hasOwnProperty.call(target, key)) {
|
|
575
|
+
return value;
|
|
576
|
+
}
|
|
577
|
+
trackKey(target, key);
|
|
578
|
+
return wrapRead(value);
|
|
579
|
+
},
|
|
580
|
+
has(target, key) {
|
|
581
|
+
if (key === RAW) return true;
|
|
582
|
+
if (typeof key !== "symbol") trackKey(target, key);
|
|
583
|
+
return Reflect.has(target, key);
|
|
584
|
+
},
|
|
585
|
+
ownKeys(target) {
|
|
586
|
+
trackOwn(target);
|
|
587
|
+
return Reflect.ownKeys(target);
|
|
588
|
+
},
|
|
589
|
+
set() {
|
|
590
|
+
throw new Error(MUTATE_MESSAGE);
|
|
591
|
+
},
|
|
592
|
+
deleteProperty() {
|
|
593
|
+
throw new Error(MUTATE_MESSAGE);
|
|
594
|
+
},
|
|
595
|
+
defineProperty() {
|
|
596
|
+
throw new Error(MUTATE_MESSAGE);
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
var draftHandler = {
|
|
600
|
+
get(target, key, receiver) {
|
|
601
|
+
if (key === RAW) return target;
|
|
602
|
+
const value = Reflect.get(target, key, receiver);
|
|
603
|
+
if (typeof key === "symbol") return value;
|
|
604
|
+
return wrapDraft(value);
|
|
605
|
+
},
|
|
606
|
+
set(target, key, value) {
|
|
607
|
+
if (typeof key === "symbol") return Reflect.set(target, key, value);
|
|
608
|
+
writeProperty(target, key, value);
|
|
609
|
+
return true;
|
|
610
|
+
},
|
|
611
|
+
deleteProperty(target, key) {
|
|
612
|
+
if (typeof key === "symbol") return Reflect.deleteProperty(target, key);
|
|
613
|
+
removeProperty(target, key);
|
|
614
|
+
return true;
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
function readerOf(raw) {
|
|
618
|
+
let proxy = readers.get(raw);
|
|
619
|
+
if (!proxy) {
|
|
620
|
+
proxy = new Proxy(raw, readHandler);
|
|
621
|
+
readers.set(raw, proxy);
|
|
622
|
+
}
|
|
623
|
+
return proxy;
|
|
624
|
+
}
|
|
625
|
+
function draftOf(raw) {
|
|
626
|
+
let proxy = drafts.get(raw);
|
|
627
|
+
if (!proxy) {
|
|
628
|
+
proxy = new Proxy(raw, draftHandler);
|
|
629
|
+
drafts.set(raw, proxy);
|
|
630
|
+
}
|
|
631
|
+
return proxy;
|
|
632
|
+
}
|
|
633
|
+
function applyUpdate(root, args) {
|
|
634
|
+
if (args.length === 0) return;
|
|
635
|
+
if (args.length === 1) {
|
|
636
|
+
const arg = args[0];
|
|
637
|
+
if (typeof arg === "function") {
|
|
638
|
+
const result = untrack(
|
|
639
|
+
() => arg(draftOf(root))
|
|
640
|
+
);
|
|
641
|
+
if (isWrappable(result) && !Array.isArray(result))
|
|
642
|
+
merge(root, toRaw(result));
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
if (isWrappable(arg) && !Array.isArray(arg)) {
|
|
646
|
+
merge(root, toRaw(arg));
|
|
647
|
+
return;
|
|
648
|
+
}
|
|
649
|
+
throw new Error(
|
|
650
|
+
"setState(): pass an object to merge, a function that edits a draft, or a path followed by a value."
|
|
651
|
+
);
|
|
652
|
+
}
|
|
653
|
+
const path = args.slice(0, -1);
|
|
654
|
+
const last = args[args.length - 1];
|
|
655
|
+
let target = root;
|
|
656
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
657
|
+
const next = target[String(path[i])];
|
|
658
|
+
if (!isWrappable(next)) {
|
|
659
|
+
throw new Error(
|
|
660
|
+
`setState(): "${path.slice(0, i + 1).join(".")}" is not an object or an array, so it has no "${path[i + 1]}" to change.`
|
|
661
|
+
);
|
|
662
|
+
}
|
|
663
|
+
target = toRaw(next);
|
|
664
|
+
}
|
|
665
|
+
const key = String(path[path.length - 1]);
|
|
666
|
+
const prev = target[key];
|
|
667
|
+
if (typeof last === "function") {
|
|
668
|
+
const result = untrack(() => last(prev));
|
|
669
|
+
writeProperty(target, key, result);
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
const incoming = toRaw(last);
|
|
673
|
+
if (isWrappable(prev) && !Array.isArray(prev) && isWrappable(incoming) && !Array.isArray(incoming)) {
|
|
674
|
+
merge(toRaw(prev), incoming);
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
writeProperty(target, key, incoming);
|
|
678
|
+
}
|
|
679
|
+
function createStore(initial) {
|
|
680
|
+
const root = unwrapIncoming(initial);
|
|
681
|
+
const setState = (...args) => {
|
|
682
|
+
batch(() => applyUpdate(root, args));
|
|
683
|
+
};
|
|
684
|
+
return [readerOf(root), setState];
|
|
685
|
+
}
|
|
686
|
+
function unwrap(value) {
|
|
687
|
+
return toRaw(value);
|
|
688
|
+
}
|
|
689
|
+
|
|
373
690
|
// src/renderer/index.ts
|
|
374
691
|
var current = null;
|
|
375
692
|
function setRenderer(renderer) {
|
|
@@ -457,7 +774,7 @@ function appendChildren(parent, children) {
|
|
|
457
774
|
|
|
458
775
|
// src/runtime/render.ts
|
|
459
776
|
function render(component, container) {
|
|
460
|
-
return createRoot((
|
|
777
|
+
return createRoot((dispose2) => {
|
|
461
778
|
const r = getRenderer();
|
|
462
779
|
const node = component();
|
|
463
780
|
if (typeof node === "function") {
|
|
@@ -468,7 +785,7 @@ function render(component, container) {
|
|
|
468
785
|
r.clear(container);
|
|
469
786
|
r.insert(container, node);
|
|
470
787
|
return () => {
|
|
471
|
-
|
|
788
|
+
dispose2();
|
|
472
789
|
r.clear(container);
|
|
473
790
|
};
|
|
474
791
|
});
|
|
@@ -478,8 +795,12 @@ function render(component, container) {
|
|
|
478
795
|
function resolve(value) {
|
|
479
796
|
return typeof value === "function" ? value() : value;
|
|
480
797
|
}
|
|
798
|
+
function resolveCondition(value) {
|
|
799
|
+
while (typeof value === "function") value = value();
|
|
800
|
+
return value;
|
|
801
|
+
}
|
|
481
802
|
function Show(props) {
|
|
482
|
-
const visible = createMemo(() => !!
|
|
803
|
+
const visible = createMemo(() => !!resolveCondition(props.when));
|
|
483
804
|
return () => visible() ? untrack(() => resolve(props.children ?? null)) : untrack(() => resolve(props.fallback ?? null));
|
|
484
805
|
}
|
|
485
806
|
|
|
@@ -491,9 +812,11 @@ function For(props) {
|
|
|
491
812
|
cache.clear();
|
|
492
813
|
});
|
|
493
814
|
return () => {
|
|
494
|
-
|
|
815
|
+
let source = props.each;
|
|
816
|
+
while (typeof source === "function") source = source();
|
|
817
|
+
const items = source ?? [];
|
|
495
818
|
const next = /* @__PURE__ */ new Map();
|
|
496
|
-
const
|
|
819
|
+
const nodes2 = [];
|
|
497
820
|
items.forEach((item, i) => {
|
|
498
821
|
const k = props.key ? props.key(item, i) : item;
|
|
499
822
|
if (next.has(k)) {
|
|
@@ -503,19 +826,19 @@ function For(props) {
|
|
|
503
826
|
}
|
|
504
827
|
let entry = cache.get(k);
|
|
505
828
|
if (!entry) {
|
|
506
|
-
entry = createRoot((
|
|
829
|
+
entry = createRoot((dispose2) => ({
|
|
507
830
|
node: props.children(item, i),
|
|
508
|
-
dispose
|
|
831
|
+
dispose: dispose2
|
|
509
832
|
}));
|
|
510
833
|
}
|
|
511
834
|
next.set(k, entry);
|
|
512
|
-
|
|
835
|
+
nodes2.push(entry.node);
|
|
513
836
|
});
|
|
514
837
|
cache.forEach((entry, k) => {
|
|
515
838
|
if (!next.has(k)) entry.dispose();
|
|
516
839
|
});
|
|
517
840
|
cache = next;
|
|
518
|
-
return
|
|
841
|
+
return nodes2;
|
|
519
842
|
};
|
|
520
843
|
}
|
|
521
844
|
|
|
@@ -573,6 +896,10 @@ function ErrorBoundary(props) {
|
|
|
573
896
|
function resolve3(value) {
|
|
574
897
|
return typeof value === "function" ? value() : value;
|
|
575
898
|
}
|
|
899
|
+
function resolveCondition2(value) {
|
|
900
|
+
while (typeof value === "function") value = value();
|
|
901
|
+
return value;
|
|
902
|
+
}
|
|
576
903
|
function Match(props) {
|
|
577
904
|
return { __match: true, when: props.when, children: props.children };
|
|
578
905
|
}
|
|
@@ -581,7 +908,7 @@ function Switch(props) {
|
|
|
581
908
|
const matches = [props.children].flat(Infinity).filter(isMatch);
|
|
582
909
|
const selected = createMemo(() => {
|
|
583
910
|
for (let i = 0; i < matches.length; i++) {
|
|
584
|
-
if (
|
|
911
|
+
if (resolveCondition2(matches[i].when)) return i;
|
|
585
912
|
}
|
|
586
913
|
return -1;
|
|
587
914
|
});
|
|
@@ -664,7 +991,7 @@ function resolve5(value) {
|
|
|
664
991
|
return typeof value === "function" ? value() : value;
|
|
665
992
|
}
|
|
666
993
|
function Suspense(props) {
|
|
667
|
-
const [
|
|
994
|
+
const [pending, setPending] = createSignal(0);
|
|
668
995
|
const [ready, setReady] = createSignal(false);
|
|
669
996
|
const state = {
|
|
670
997
|
add() {
|
|
@@ -685,7 +1012,7 @@ function Suspense(props) {
|
|
|
685
1012
|
)
|
|
686
1013
|
);
|
|
687
1014
|
createEffect(() => {
|
|
688
|
-
if (
|
|
1015
|
+
if (pending() === 0) setReady(true);
|
|
689
1016
|
});
|
|
690
1017
|
return () => ready() ? content : untrack(() => resolve5(props.fallback ?? null));
|
|
691
1018
|
}
|
|
@@ -738,6 +1065,7 @@ function lazy(loader) {
|
|
|
738
1065
|
createResource,
|
|
739
1066
|
createRoot,
|
|
740
1067
|
createSignal,
|
|
1068
|
+
createStore,
|
|
741
1069
|
getRenderer,
|
|
742
1070
|
h,
|
|
743
1071
|
jsx,
|
|
@@ -750,5 +1078,6 @@ function lazy(loader) {
|
|
|
750
1078
|
render,
|
|
751
1079
|
setRenderer,
|
|
752
1080
|
untrack,
|
|
1081
|
+
unwrap,
|
|
753
1082
|
useContext
|
|
754
1083
|
});
|