@cajoa/core 0.19.0 → 0.21.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 +157 -139
- package/dist/index.d.cts +72 -9
- package/dist/index.d.ts +72 -9
- package/dist/index.js +157 -139
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -165,10 +165,11 @@ function run(node) {
|
|
|
165
165
|
try {
|
|
166
166
|
const result = node.fn();
|
|
167
167
|
if (!node.isEffect) {
|
|
168
|
-
const
|
|
168
|
+
const had = node.hasValue;
|
|
169
|
+
const same = had && (node.equals === void 0 ? Object.is(node.value, result) : node.equals !== false && node.equals(node.value, result));
|
|
169
170
|
node.value = result;
|
|
170
171
|
node.hasValue = true;
|
|
171
|
-
if (
|
|
172
|
+
if (had && !same) {
|
|
172
173
|
for (const observer of node.observers) {
|
|
173
174
|
if (observer.state === CHECK) observer.state = DIRTY;
|
|
174
175
|
}
|
|
@@ -204,10 +205,11 @@ function flush() {
|
|
|
204
205
|
try {
|
|
205
206
|
while (queued.size > 0) {
|
|
206
207
|
if (++steps > 1e5) {
|
|
208
|
+
const names = [...queued].map((e) => e.name).filter(Boolean);
|
|
207
209
|
queued.clear();
|
|
208
210
|
failure ??= {
|
|
209
211
|
error: new Error(
|
|
210
|
-
"Cajoa: updates never settled. An effect probably keeps changing a signal that it also reads."
|
|
212
|
+
"Cajoa: updates never settled. An effect probably keeps changing a signal that it also reads." + (names.length > 0 ? " Effects waiting: " + names.join(", ") + "." : "")
|
|
211
213
|
)
|
|
212
214
|
};
|
|
213
215
|
break;
|
|
@@ -239,24 +241,27 @@ function batch(fn) {
|
|
|
239
241
|
if (batchDepth === 0 && !flushing) flush();
|
|
240
242
|
}
|
|
241
243
|
}
|
|
242
|
-
function createSignal(value) {
|
|
243
|
-
const source = { observers: /* @__PURE__ */ new Set() };
|
|
244
|
+
function createSignal(value, options) {
|
|
245
|
+
const source = { observers: /* @__PURE__ */ new Set(), name: options?.name };
|
|
246
|
+
const equals = options?.equals;
|
|
244
247
|
const get = () => {
|
|
245
248
|
track(source);
|
|
246
249
|
return value;
|
|
247
250
|
};
|
|
248
251
|
const set = (next) => {
|
|
249
252
|
const v = typeof next === "function" ? next(value) : next;
|
|
250
|
-
|
|
253
|
+
const same = equals === false ? false : equals ? equals(value, v) : Object.is(value, v);
|
|
254
|
+
if (same) return;
|
|
251
255
|
value = v;
|
|
252
256
|
notify(source);
|
|
253
257
|
};
|
|
254
258
|
return [get, set];
|
|
255
259
|
}
|
|
256
|
-
function createComputation(fn, isEffect) {
|
|
260
|
+
function createComputation(fn, isEffect, name) {
|
|
257
261
|
return {
|
|
258
262
|
fn,
|
|
259
263
|
isEffect,
|
|
264
|
+
name,
|
|
260
265
|
cleanups: [],
|
|
261
266
|
parent: currentOwner,
|
|
262
267
|
sources: /* @__PURE__ */ new Set(),
|
|
@@ -267,13 +272,14 @@ function createComputation(fn, isEffect) {
|
|
|
267
272
|
value: void 0
|
|
268
273
|
};
|
|
269
274
|
}
|
|
270
|
-
function createEffect(fn) {
|
|
271
|
-
const node = createComputation(fn, true);
|
|
275
|
+
function createEffect(fn, options) {
|
|
276
|
+
const node = createComputation(fn, true, options?.name);
|
|
272
277
|
addCleanup(() => dispose(node));
|
|
273
278
|
run(node);
|
|
274
279
|
}
|
|
275
|
-
function createMemo(fn) {
|
|
276
|
-
const node = createComputation(fn, false);
|
|
280
|
+
function createMemo(fn, options) {
|
|
281
|
+
const node = createComputation(fn, false, options?.name);
|
|
282
|
+
node.equals = options?.equals;
|
|
277
283
|
addCleanup(() => dispose(node));
|
|
278
284
|
run(node);
|
|
279
285
|
return () => {
|
|
@@ -388,9 +394,6 @@ function catchError(fn, handler) {
|
|
|
388
394
|
}
|
|
389
395
|
return result;
|
|
390
396
|
}
|
|
391
|
-
function createContext(defaultValue) {
|
|
392
|
-
return { id: /* @__PURE__ */ Symbol("cajoa-context"), defaultValue };
|
|
393
|
-
}
|
|
394
397
|
function provide(ctx, value, fn) {
|
|
395
398
|
const owner = {
|
|
396
399
|
cleanups: [],
|
|
@@ -418,6 +421,132 @@ function useContext(ctx) {
|
|
|
418
421
|
return ctx.defaultValue;
|
|
419
422
|
}
|
|
420
423
|
|
|
424
|
+
// src/runtime/reconcile.ts
|
|
425
|
+
function longestIncreasingSubsequence(values) {
|
|
426
|
+
const predecessor = new Array(values.length);
|
|
427
|
+
const tails = [];
|
|
428
|
+
for (let i = 0; i < values.length; i++) {
|
|
429
|
+
const v = values[i];
|
|
430
|
+
if (v < 0) continue;
|
|
431
|
+
let lo;
|
|
432
|
+
if (tails.length === 0 || values[tails[tails.length - 1]] < v) {
|
|
433
|
+
lo = tails.length;
|
|
434
|
+
} else {
|
|
435
|
+
lo = 0;
|
|
436
|
+
let hi = tails.length - 1;
|
|
437
|
+
while (lo < hi) {
|
|
438
|
+
const mid = lo + hi >> 1;
|
|
439
|
+
if (values[tails[mid]] < v) lo = mid + 1;
|
|
440
|
+
else hi = mid;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
predecessor[i] = lo > 0 ? tails[lo - 1] : -1;
|
|
444
|
+
tails[lo] = i;
|
|
445
|
+
}
|
|
446
|
+
const result = new Array(tails.length);
|
|
447
|
+
let k = tails.length > 0 ? tails[tails.length - 1] : -1;
|
|
448
|
+
for (let j = tails.length - 1; j >= 0; j--) {
|
|
449
|
+
result[j] = k;
|
|
450
|
+
k = predecessor[k];
|
|
451
|
+
}
|
|
452
|
+
return result;
|
|
453
|
+
}
|
|
454
|
+
function reconcile(start, end, next) {
|
|
455
|
+
const r = getRenderer();
|
|
456
|
+
const keep = new Set(next);
|
|
457
|
+
const oldIndex = /* @__PURE__ */ new Map();
|
|
458
|
+
let count = 0;
|
|
459
|
+
let cur = r.nextSibling(start);
|
|
460
|
+
while (cur && cur !== end) {
|
|
461
|
+
const after = r.nextSibling(cur);
|
|
462
|
+
if (keep.has(cur)) oldIndex.set(cur, count++);
|
|
463
|
+
else r.remove(cur);
|
|
464
|
+
cur = after;
|
|
465
|
+
}
|
|
466
|
+
const parent = r.parentNode(end);
|
|
467
|
+
const n = next.length;
|
|
468
|
+
const positions = new Array(n);
|
|
469
|
+
for (let i = 0; i < n; i++) positions[i] = oldIndex.get(next[i]) ?? -1;
|
|
470
|
+
const stable = new Uint8Array(n);
|
|
471
|
+
for (const i of longestIncreasingSubsequence(positions)) stable[i] = 1;
|
|
472
|
+
const anchors = new Array(n);
|
|
473
|
+
let anchor = end;
|
|
474
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
475
|
+
anchors[i] = anchor;
|
|
476
|
+
if (stable[i]) anchor = next[i];
|
|
477
|
+
}
|
|
478
|
+
for (let i = 0; i < n; i++) {
|
|
479
|
+
if (!stable[i]) r.insert(parent, next[i], anchors[i]);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// src/runtime/h.ts
|
|
484
|
+
function h(tag, props, ...children) {
|
|
485
|
+
const r = getRenderer();
|
|
486
|
+
const el = r.createElement(tag);
|
|
487
|
+
let ref;
|
|
488
|
+
for (const [key, value] of Object.entries(props ?? {})) {
|
|
489
|
+
if (key === "ref") {
|
|
490
|
+
if (typeof value === "function") ref = value;
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
if (typeof value === "function" && !key.startsWith("on")) {
|
|
494
|
+
createEffect(() => r.setProp(el, key, value()));
|
|
495
|
+
} else {
|
|
496
|
+
r.setProp(el, key, value);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
appendChildren(el, children);
|
|
500
|
+
const onRef = ref;
|
|
501
|
+
if (onRef && !r.isServer) untrack(() => onRef(el));
|
|
502
|
+
return el;
|
|
503
|
+
}
|
|
504
|
+
function materialize(value) {
|
|
505
|
+
if (typeof value !== "function") return value;
|
|
506
|
+
const holder = getRenderer().createFragment();
|
|
507
|
+
appendChildren(holder, [value]);
|
|
508
|
+
return holder;
|
|
509
|
+
}
|
|
510
|
+
function toNodes(value) {
|
|
511
|
+
const r = getRenderer();
|
|
512
|
+
const out = [];
|
|
513
|
+
for (const v of [value].flat(Infinity)) {
|
|
514
|
+
if (v == null || typeof v === "boolean") continue;
|
|
515
|
+
if (typeof v === "function") {
|
|
516
|
+
out.push(materialize(v));
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
out.push(r.isNode(v) ? v : r.createText(String(v)));
|
|
520
|
+
}
|
|
521
|
+
return out;
|
|
522
|
+
}
|
|
523
|
+
function appendChildren(parent, children) {
|
|
524
|
+
const r = getRenderer();
|
|
525
|
+
for (const child of children.flat(Infinity)) {
|
|
526
|
+
if (typeof child === "function") {
|
|
527
|
+
const start = r.createMarker();
|
|
528
|
+
const end = r.createMarker();
|
|
529
|
+
r.insert(parent, start);
|
|
530
|
+
r.insert(parent, end);
|
|
531
|
+
createEffect(() => reconcile(start, end, toNodes(child())));
|
|
532
|
+
} else {
|
|
533
|
+
for (const node of toNodes(child)) r.insert(parent, node);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// src/reactivity/context.ts
|
|
539
|
+
function createContext(defaultValue) {
|
|
540
|
+
const context = {
|
|
541
|
+
id: /* @__PURE__ */ Symbol("cajoa-context"),
|
|
542
|
+
defaultValue,
|
|
543
|
+
Provider(props) {
|
|
544
|
+
return provide(context, props.value, () => materialize(props.children));
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
return context;
|
|
548
|
+
}
|
|
549
|
+
|
|
421
550
|
// src/ssr.ts
|
|
422
551
|
var ServerContext = createContext(null);
|
|
423
552
|
|
|
@@ -750,132 +879,13 @@ function unwrap(value) {
|
|
|
750
879
|
return toRaw(value);
|
|
751
880
|
}
|
|
752
881
|
|
|
753
|
-
// src/runtime/reconcile.ts
|
|
754
|
-
function longestIncreasingSubsequence(values) {
|
|
755
|
-
const predecessor = new Array(values.length);
|
|
756
|
-
const tails = [];
|
|
757
|
-
for (let i = 0; i < values.length; i++) {
|
|
758
|
-
const v = values[i];
|
|
759
|
-
if (v < 0) continue;
|
|
760
|
-
let lo;
|
|
761
|
-
if (tails.length === 0 || values[tails[tails.length - 1]] < v) {
|
|
762
|
-
lo = tails.length;
|
|
763
|
-
} else {
|
|
764
|
-
lo = 0;
|
|
765
|
-
let hi = tails.length - 1;
|
|
766
|
-
while (lo < hi) {
|
|
767
|
-
const mid = lo + hi >> 1;
|
|
768
|
-
if (values[tails[mid]] < v) lo = mid + 1;
|
|
769
|
-
else hi = mid;
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
predecessor[i] = lo > 0 ? tails[lo - 1] : -1;
|
|
773
|
-
tails[lo] = i;
|
|
774
|
-
}
|
|
775
|
-
const result = new Array(tails.length);
|
|
776
|
-
let k = tails.length > 0 ? tails[tails.length - 1] : -1;
|
|
777
|
-
for (let j = tails.length - 1; j >= 0; j--) {
|
|
778
|
-
result[j] = k;
|
|
779
|
-
k = predecessor[k];
|
|
780
|
-
}
|
|
781
|
-
return result;
|
|
782
|
-
}
|
|
783
|
-
function reconcile(start, end, next) {
|
|
784
|
-
const r = getRenderer();
|
|
785
|
-
const keep = new Set(next);
|
|
786
|
-
const oldIndex = /* @__PURE__ */ new Map();
|
|
787
|
-
let count = 0;
|
|
788
|
-
let cur = r.nextSibling(start);
|
|
789
|
-
while (cur && cur !== end) {
|
|
790
|
-
const after = r.nextSibling(cur);
|
|
791
|
-
if (keep.has(cur)) oldIndex.set(cur, count++);
|
|
792
|
-
else r.remove(cur);
|
|
793
|
-
cur = after;
|
|
794
|
-
}
|
|
795
|
-
const parent = r.parentNode(end);
|
|
796
|
-
const n = next.length;
|
|
797
|
-
const positions = new Array(n);
|
|
798
|
-
for (let i = 0; i < n; i++) positions[i] = oldIndex.get(next[i]) ?? -1;
|
|
799
|
-
const stable = new Uint8Array(n);
|
|
800
|
-
for (const i of longestIncreasingSubsequence(positions)) stable[i] = 1;
|
|
801
|
-
const anchors = new Array(n);
|
|
802
|
-
let anchor = end;
|
|
803
|
-
for (let i = n - 1; i >= 0; i--) {
|
|
804
|
-
anchors[i] = anchor;
|
|
805
|
-
if (stable[i]) anchor = next[i];
|
|
806
|
-
}
|
|
807
|
-
for (let i = 0; i < n; i++) {
|
|
808
|
-
if (!stable[i]) r.insert(parent, next[i], anchors[i]);
|
|
809
|
-
}
|
|
810
|
-
}
|
|
811
|
-
|
|
812
|
-
// src/runtime/h.ts
|
|
813
|
-
function h(tag, props, ...children) {
|
|
814
|
-
const r = getRenderer();
|
|
815
|
-
const el = r.createElement(tag);
|
|
816
|
-
let ref;
|
|
817
|
-
for (const [key, value] of Object.entries(props ?? {})) {
|
|
818
|
-
if (key === "ref") {
|
|
819
|
-
if (typeof value === "function") ref = value;
|
|
820
|
-
continue;
|
|
821
|
-
}
|
|
822
|
-
if (typeof value === "function" && !key.startsWith("on")) {
|
|
823
|
-
createEffect(() => r.setProp(el, key, value()));
|
|
824
|
-
} else {
|
|
825
|
-
r.setProp(el, key, value);
|
|
826
|
-
}
|
|
827
|
-
}
|
|
828
|
-
appendChildren(el, children);
|
|
829
|
-
const onRef = ref;
|
|
830
|
-
if (onRef && !r.isServer) untrack(() => onRef(el));
|
|
831
|
-
return el;
|
|
832
|
-
}
|
|
833
|
-
function materialize(value) {
|
|
834
|
-
if (typeof value !== "function") return value;
|
|
835
|
-
const holder = getRenderer().createFragment();
|
|
836
|
-
appendChildren(holder, [value]);
|
|
837
|
-
return holder;
|
|
838
|
-
}
|
|
839
|
-
function toNodes(value) {
|
|
840
|
-
const r = getRenderer();
|
|
841
|
-
const out = [];
|
|
842
|
-
for (const v of [value].flat(Infinity)) {
|
|
843
|
-
if (v == null || typeof v === "boolean") continue;
|
|
844
|
-
if (typeof v === "function") {
|
|
845
|
-
out.push(materialize(v));
|
|
846
|
-
continue;
|
|
847
|
-
}
|
|
848
|
-
out.push(r.isNode(v) ? v : r.createText(String(v)));
|
|
849
|
-
}
|
|
850
|
-
return out;
|
|
851
|
-
}
|
|
852
|
-
function appendChildren(parent, children) {
|
|
853
|
-
const r = getRenderer();
|
|
854
|
-
for (const child of children.flat(Infinity)) {
|
|
855
|
-
if (typeof child === "function") {
|
|
856
|
-
const start = r.createMarker();
|
|
857
|
-
const end = r.createMarker();
|
|
858
|
-
r.insert(parent, start);
|
|
859
|
-
r.insert(parent, end);
|
|
860
|
-
createEffect(() => reconcile(start, end, toNodes(child())));
|
|
861
|
-
} else {
|
|
862
|
-
for (const node of toNodes(child)) r.insert(parent, node);
|
|
863
|
-
}
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
|
|
867
882
|
// src/runtime/render.ts
|
|
868
883
|
function render(component, container) {
|
|
869
884
|
return createRoot((dispose2) => {
|
|
870
885
|
const r = getRenderer();
|
|
871
|
-
const
|
|
872
|
-
if (typeof node === "function") {
|
|
873
|
-
throw new Error(
|
|
874
|
-
"render(): the component returned a function (for example <Show>, <For>, <Router> or <ErrorBoundary>). Wrap it in an element, such as <div>\u2026</div>."
|
|
875
|
-
);
|
|
876
|
-
}
|
|
886
|
+
const result = component();
|
|
877
887
|
r.clear(container);
|
|
878
|
-
|
|
888
|
+
appendChildren(container, [result]);
|
|
879
889
|
return () => {
|
|
880
890
|
dispose2();
|
|
881
891
|
r.clear(container);
|
|
@@ -918,10 +928,12 @@ function For(props) {
|
|
|
918
928
|
}
|
|
919
929
|
let entry = cache.get(k);
|
|
920
930
|
if (!entry) {
|
|
921
|
-
entry = createRoot((dispose2) =>
|
|
922
|
-
|
|
923
|
-
dispose: dispose2
|
|
924
|
-
})
|
|
931
|
+
entry = createRoot((dispose2) => {
|
|
932
|
+
const [index, setIndex] = createSignal(i);
|
|
933
|
+
return { node: props.children(item, index), dispose: dispose2, setIndex };
|
|
934
|
+
});
|
|
935
|
+
} else {
|
|
936
|
+
entry.setIndex(i);
|
|
925
937
|
}
|
|
926
938
|
next.set(k, entry);
|
|
927
939
|
nodes2.push(entry.node);
|
|
@@ -930,6 +942,12 @@ function For(props) {
|
|
|
930
942
|
if (!next.has(k)) entry.dispose();
|
|
931
943
|
});
|
|
932
944
|
cache = next;
|
|
945
|
+
if (nodes2.length === 0 && props.fallback !== void 0) {
|
|
946
|
+
const fallback = props.fallback;
|
|
947
|
+
return untrack(
|
|
948
|
+
() => typeof fallback === "function" ? fallback() : fallback
|
|
949
|
+
);
|
|
950
|
+
}
|
|
933
951
|
return nodes2;
|
|
934
952
|
};
|
|
935
953
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,21 +1,62 @@
|
|
|
1
1
|
type Cleanup = () => void;
|
|
2
|
+
interface SignalOptions<T> {
|
|
3
|
+
/**
|
|
4
|
+
* How to tell that a new value is the same as the old one. The default is Object.is.
|
|
5
|
+
* Use `false` to notify on every write, even with the same value (for example after
|
|
6
|
+
* changing an object in place). Or pass your own function.
|
|
7
|
+
*/
|
|
8
|
+
equals?: false | ((previous: T, next: T) => boolean);
|
|
9
|
+
/** A name for error messages (and, later, for the debugger). */
|
|
10
|
+
name?: string;
|
|
11
|
+
}
|
|
12
|
+
interface EffectOptions {
|
|
13
|
+
/** A name for error messages (and, later, for the debugger). */
|
|
14
|
+
name?: string;
|
|
15
|
+
}
|
|
2
16
|
declare function batch<T>(fn: () => T): T;
|
|
3
|
-
declare function createSignal<T>(value: T): readonly [() => T, (next: T | ((prev: T) => T)) => void];
|
|
4
|
-
declare function createEffect(fn: () => void): void;
|
|
5
|
-
|
|
17
|
+
declare function createSignal<T>(value: T, options?: SignalOptions<T>): readonly [() => T, (next: T | ((prev: T) => T)) => void];
|
|
18
|
+
declare function createEffect(fn: () => void, options?: EffectOptions): void;
|
|
19
|
+
/**
|
|
20
|
+
* A cached computed value. It is computed when it is read (or when an effect needs it),
|
|
21
|
+
* and it only tells the listeners when the result changes.
|
|
22
|
+
*/
|
|
23
|
+
declare function createMemo<T>(fn: () => T, options?: SignalOptions<T>): () => T;
|
|
24
|
+
/**
|
|
25
|
+
* For "which one is selected?". `isSelected(key)` only wakes the two keys that changed
|
|
26
|
+
* (the old and the new selection), even if thousands are listening.
|
|
27
|
+
*/
|
|
6
28
|
declare function createSelector<T>(source: () => T): (key: T) => boolean;
|
|
7
29
|
declare function onCleanup(fn: Cleanup): void;
|
|
8
30
|
declare function createRoot<T>(fn: (dispose: () => void) => T): T;
|
|
9
31
|
declare function onMount(fn: () => void): void;
|
|
10
32
|
declare function untrack<T>(fn: () => T): T;
|
|
33
|
+
/**
|
|
34
|
+
* Runs `fn`. An error that it throws (or that the effects and onMount created inside it throw, even later)
|
|
35
|
+
* goes to `handler` instead of being thrown.
|
|
36
|
+
*/
|
|
11
37
|
declare function catchError<T>(fn: () => T, handler: (error: unknown) => void): T | undefined;
|
|
38
|
+
declare function provide<T, R>(ctx: {
|
|
39
|
+
readonly id: symbol;
|
|
40
|
+
readonly defaultValue: T;
|
|
41
|
+
}, value: T, fn: () => R): R;
|
|
42
|
+
declare function useContext<T>(ctx: {
|
|
43
|
+
readonly id: symbol;
|
|
44
|
+
readonly defaultValue: T;
|
|
45
|
+
}): T;
|
|
46
|
+
|
|
12
47
|
interface Context<T> {
|
|
13
48
|
readonly id: symbol;
|
|
14
49
|
readonly defaultValue: T;
|
|
50
|
+
/**
|
|
51
|
+
* Everything created inside can read `value` with useContext().
|
|
52
|
+
* `value` is read once: pass a signal (or a function) if you need it to stay live.
|
|
53
|
+
*/
|
|
54
|
+
Provider(props: {
|
|
55
|
+
value: T;
|
|
56
|
+
children?: unknown;
|
|
57
|
+
}): unknown;
|
|
15
58
|
}
|
|
16
59
|
declare function createContext<T>(defaultValue: T): Context<T>;
|
|
17
|
-
declare function provide<T, R>(ctx: Context<T>, value: T, fn: () => R): R;
|
|
18
|
-
declare function useContext<T>(ctx: Context<T>): T;
|
|
19
60
|
|
|
20
61
|
interface Resource<T> {
|
|
21
62
|
(): T | undefined;
|
|
@@ -63,7 +104,7 @@ interface Renderer<N = any> {
|
|
|
63
104
|
type Child = string | number | boolean | null | undefined | HostNode | (() => unknown) | Child[];
|
|
64
105
|
declare function h(tag: string, props: Record<string, any> | null, ...children: Child[]): HostNode;
|
|
65
106
|
|
|
66
|
-
declare function render(component: () =>
|
|
107
|
+
declare function render(component: () => unknown, container: HostNode): () => void;
|
|
67
108
|
|
|
68
109
|
type Lazy$4<T> = T | (() => T);
|
|
69
110
|
declare function Show<T>(props: {
|
|
@@ -74,9 +115,12 @@ declare function Show<T>(props: {
|
|
|
74
115
|
|
|
75
116
|
declare function For<T>(props: {
|
|
76
117
|
each: T[] | (() => T[]);
|
|
77
|
-
|
|
118
|
+
/** `index` is a function. It gives the current position, and it follows the item when the list changes. */
|
|
119
|
+
children: (item: T, index: () => number) => HostNode;
|
|
78
120
|
key?: (item: T, index: number) => unknown;
|
|
79
|
-
|
|
121
|
+
/** Shown when the list is empty. */
|
|
122
|
+
fallback?: Child | (() => Child);
|
|
123
|
+
}): () => Child;
|
|
80
124
|
|
|
81
125
|
type Lazy$3<T> = T | (() => T);
|
|
82
126
|
declare function ErrorBoundary(props: {
|
|
@@ -135,12 +179,31 @@ declare function getRenderer(): Renderer;
|
|
|
135
179
|
declare function currentRenderer(): Renderer | null;
|
|
136
180
|
declare function isServer(): boolean;
|
|
137
181
|
|
|
182
|
+
interface HeadMeta {
|
|
183
|
+
name?: string;
|
|
184
|
+
property?: string;
|
|
185
|
+
content: string;
|
|
186
|
+
}
|
|
187
|
+
/** The page title and meta tags that <Title> and <Meta> collect while rendering on the server. */
|
|
188
|
+
interface HeadData {
|
|
189
|
+
title?: string;
|
|
190
|
+
meta?: HeadMeta[];
|
|
191
|
+
}
|
|
192
|
+
/** What components can learn about the request that is being rendered on the server. */
|
|
138
193
|
interface ServerRequest {
|
|
194
|
+
/** The URL of the request (path and query), for example "/users/42?tab=posts". */
|
|
139
195
|
url: string;
|
|
196
|
+
/** Filled by <Title> and <Meta> while rendering. */
|
|
197
|
+
head: HeadData;
|
|
198
|
+
/** <Navigate> asks the server to redirect. */
|
|
140
199
|
redirect(to: string, status: number): void;
|
|
200
|
+
/**
|
|
201
|
+
* Tell renderToStringAsync that there is async work (for example a resource) to wait for.
|
|
202
|
+
* Returns the function to call when the work is done.
|
|
203
|
+
*/
|
|
141
204
|
track(): () => void;
|
|
142
205
|
}
|
|
143
206
|
declare const ServerContext: Context<ServerRequest | null>;
|
|
144
207
|
|
|
145
208
|
export { Dynamic, ErrorBoundary, For, Fragment, Match, Portal, ServerContext, Show, Suspense, Switch, batch, catchError, createContext, createEffect, createMemo, createResource, createRoot, createSelector, createSignal, createStore, currentRenderer, getRenderer, h, isServer, jsx, jsxDEV, jsxs, lazy, onCleanup, onMount, provide, render, setRenderer, untrack, unwrap, useContext };
|
|
146
|
-
export type { Child, Context, HostNode, MatchDef, Renderer, Resource, ResourceActions, ServerRequest, SetStoreFunction };
|
|
209
|
+
export type { Child, Context, EffectOptions, HeadData, HeadMeta, HostNode, MatchDef, Renderer, Resource, ResourceActions, ServerRequest, SetStoreFunction, SignalOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,21 +1,62 @@
|
|
|
1
1
|
type Cleanup = () => void;
|
|
2
|
+
interface SignalOptions<T> {
|
|
3
|
+
/**
|
|
4
|
+
* How to tell that a new value is the same as the old one. The default is Object.is.
|
|
5
|
+
* Use `false` to notify on every write, even with the same value (for example after
|
|
6
|
+
* changing an object in place). Or pass your own function.
|
|
7
|
+
*/
|
|
8
|
+
equals?: false | ((previous: T, next: T) => boolean);
|
|
9
|
+
/** A name for error messages (and, later, for the debugger). */
|
|
10
|
+
name?: string;
|
|
11
|
+
}
|
|
12
|
+
interface EffectOptions {
|
|
13
|
+
/** A name for error messages (and, later, for the debugger). */
|
|
14
|
+
name?: string;
|
|
15
|
+
}
|
|
2
16
|
declare function batch<T>(fn: () => T): T;
|
|
3
|
-
declare function createSignal<T>(value: T): readonly [() => T, (next: T | ((prev: T) => T)) => void];
|
|
4
|
-
declare function createEffect(fn: () => void): void;
|
|
5
|
-
|
|
17
|
+
declare function createSignal<T>(value: T, options?: SignalOptions<T>): readonly [() => T, (next: T | ((prev: T) => T)) => void];
|
|
18
|
+
declare function createEffect(fn: () => void, options?: EffectOptions): void;
|
|
19
|
+
/**
|
|
20
|
+
* A cached computed value. It is computed when it is read (or when an effect needs it),
|
|
21
|
+
* and it only tells the listeners when the result changes.
|
|
22
|
+
*/
|
|
23
|
+
declare function createMemo<T>(fn: () => T, options?: SignalOptions<T>): () => T;
|
|
24
|
+
/**
|
|
25
|
+
* For "which one is selected?". `isSelected(key)` only wakes the two keys that changed
|
|
26
|
+
* (the old and the new selection), even if thousands are listening.
|
|
27
|
+
*/
|
|
6
28
|
declare function createSelector<T>(source: () => T): (key: T) => boolean;
|
|
7
29
|
declare function onCleanup(fn: Cleanup): void;
|
|
8
30
|
declare function createRoot<T>(fn: (dispose: () => void) => T): T;
|
|
9
31
|
declare function onMount(fn: () => void): void;
|
|
10
32
|
declare function untrack<T>(fn: () => T): T;
|
|
33
|
+
/**
|
|
34
|
+
* Runs `fn`. An error that it throws (or that the effects and onMount created inside it throw, even later)
|
|
35
|
+
* goes to `handler` instead of being thrown.
|
|
36
|
+
*/
|
|
11
37
|
declare function catchError<T>(fn: () => T, handler: (error: unknown) => void): T | undefined;
|
|
38
|
+
declare function provide<T, R>(ctx: {
|
|
39
|
+
readonly id: symbol;
|
|
40
|
+
readonly defaultValue: T;
|
|
41
|
+
}, value: T, fn: () => R): R;
|
|
42
|
+
declare function useContext<T>(ctx: {
|
|
43
|
+
readonly id: symbol;
|
|
44
|
+
readonly defaultValue: T;
|
|
45
|
+
}): T;
|
|
46
|
+
|
|
12
47
|
interface Context<T> {
|
|
13
48
|
readonly id: symbol;
|
|
14
49
|
readonly defaultValue: T;
|
|
50
|
+
/**
|
|
51
|
+
* Everything created inside can read `value` with useContext().
|
|
52
|
+
* `value` is read once: pass a signal (or a function) if you need it to stay live.
|
|
53
|
+
*/
|
|
54
|
+
Provider(props: {
|
|
55
|
+
value: T;
|
|
56
|
+
children?: unknown;
|
|
57
|
+
}): unknown;
|
|
15
58
|
}
|
|
16
59
|
declare function createContext<T>(defaultValue: T): Context<T>;
|
|
17
|
-
declare function provide<T, R>(ctx: Context<T>, value: T, fn: () => R): R;
|
|
18
|
-
declare function useContext<T>(ctx: Context<T>): T;
|
|
19
60
|
|
|
20
61
|
interface Resource<T> {
|
|
21
62
|
(): T | undefined;
|
|
@@ -63,7 +104,7 @@ interface Renderer<N = any> {
|
|
|
63
104
|
type Child = string | number | boolean | null | undefined | HostNode | (() => unknown) | Child[];
|
|
64
105
|
declare function h(tag: string, props: Record<string, any> | null, ...children: Child[]): HostNode;
|
|
65
106
|
|
|
66
|
-
declare function render(component: () =>
|
|
107
|
+
declare function render(component: () => unknown, container: HostNode): () => void;
|
|
67
108
|
|
|
68
109
|
type Lazy$4<T> = T | (() => T);
|
|
69
110
|
declare function Show<T>(props: {
|
|
@@ -74,9 +115,12 @@ declare function Show<T>(props: {
|
|
|
74
115
|
|
|
75
116
|
declare function For<T>(props: {
|
|
76
117
|
each: T[] | (() => T[]);
|
|
77
|
-
|
|
118
|
+
/** `index` is a function. It gives the current position, and it follows the item when the list changes. */
|
|
119
|
+
children: (item: T, index: () => number) => HostNode;
|
|
78
120
|
key?: (item: T, index: number) => unknown;
|
|
79
|
-
|
|
121
|
+
/** Shown when the list is empty. */
|
|
122
|
+
fallback?: Child | (() => Child);
|
|
123
|
+
}): () => Child;
|
|
80
124
|
|
|
81
125
|
type Lazy$3<T> = T | (() => T);
|
|
82
126
|
declare function ErrorBoundary(props: {
|
|
@@ -135,12 +179,31 @@ declare function getRenderer(): Renderer;
|
|
|
135
179
|
declare function currentRenderer(): Renderer | null;
|
|
136
180
|
declare function isServer(): boolean;
|
|
137
181
|
|
|
182
|
+
interface HeadMeta {
|
|
183
|
+
name?: string;
|
|
184
|
+
property?: string;
|
|
185
|
+
content: string;
|
|
186
|
+
}
|
|
187
|
+
/** The page title and meta tags that <Title> and <Meta> collect while rendering on the server. */
|
|
188
|
+
interface HeadData {
|
|
189
|
+
title?: string;
|
|
190
|
+
meta?: HeadMeta[];
|
|
191
|
+
}
|
|
192
|
+
/** What components can learn about the request that is being rendered on the server. */
|
|
138
193
|
interface ServerRequest {
|
|
194
|
+
/** The URL of the request (path and query), for example "/users/42?tab=posts". */
|
|
139
195
|
url: string;
|
|
196
|
+
/** Filled by <Title> and <Meta> while rendering. */
|
|
197
|
+
head: HeadData;
|
|
198
|
+
/** <Navigate> asks the server to redirect. */
|
|
140
199
|
redirect(to: string, status: number): void;
|
|
200
|
+
/**
|
|
201
|
+
* Tell renderToStringAsync that there is async work (for example a resource) to wait for.
|
|
202
|
+
* Returns the function to call when the work is done.
|
|
203
|
+
*/
|
|
141
204
|
track(): () => void;
|
|
142
205
|
}
|
|
143
206
|
declare const ServerContext: Context<ServerRequest | null>;
|
|
144
207
|
|
|
145
208
|
export { Dynamic, ErrorBoundary, For, Fragment, Match, Portal, ServerContext, Show, Suspense, Switch, batch, catchError, createContext, createEffect, createMemo, createResource, createRoot, createSelector, createSignal, createStore, currentRenderer, getRenderer, h, isServer, jsx, jsxDEV, jsxs, lazy, onCleanup, onMount, provide, render, setRenderer, untrack, unwrap, useContext };
|
|
146
|
-
export type { Child, Context, HostNode, MatchDef, Renderer, Resource, ResourceActions, ServerRequest, SetStoreFunction };
|
|
209
|
+
export type { Child, Context, EffectOptions, HeadData, HeadMeta, HostNode, MatchDef, Renderer, Resource, ResourceActions, ServerRequest, SetStoreFunction, SignalOptions };
|
package/dist/index.js
CHANGED
|
@@ -104,10 +104,11 @@ function run(node) {
|
|
|
104
104
|
try {
|
|
105
105
|
const result = node.fn();
|
|
106
106
|
if (!node.isEffect) {
|
|
107
|
-
const
|
|
107
|
+
const had = node.hasValue;
|
|
108
|
+
const same = had && (node.equals === void 0 ? Object.is(node.value, result) : node.equals !== false && node.equals(node.value, result));
|
|
108
109
|
node.value = result;
|
|
109
110
|
node.hasValue = true;
|
|
110
|
-
if (
|
|
111
|
+
if (had && !same) {
|
|
111
112
|
for (const observer of node.observers) {
|
|
112
113
|
if (observer.state === CHECK) observer.state = DIRTY;
|
|
113
114
|
}
|
|
@@ -143,10 +144,11 @@ function flush() {
|
|
|
143
144
|
try {
|
|
144
145
|
while (queued.size > 0) {
|
|
145
146
|
if (++steps > 1e5) {
|
|
147
|
+
const names = [...queued].map((e) => e.name).filter(Boolean);
|
|
146
148
|
queued.clear();
|
|
147
149
|
failure ??= {
|
|
148
150
|
error: new Error(
|
|
149
|
-
"Cajoa: updates never settled. An effect probably keeps changing a signal that it also reads."
|
|
151
|
+
"Cajoa: updates never settled. An effect probably keeps changing a signal that it also reads." + (names.length > 0 ? " Effects waiting: " + names.join(", ") + "." : "")
|
|
150
152
|
)
|
|
151
153
|
};
|
|
152
154
|
break;
|
|
@@ -178,24 +180,27 @@ function batch(fn) {
|
|
|
178
180
|
if (batchDepth === 0 && !flushing) flush();
|
|
179
181
|
}
|
|
180
182
|
}
|
|
181
|
-
function createSignal(value) {
|
|
182
|
-
const source = { observers: /* @__PURE__ */ new Set() };
|
|
183
|
+
function createSignal(value, options) {
|
|
184
|
+
const source = { observers: /* @__PURE__ */ new Set(), name: options?.name };
|
|
185
|
+
const equals = options?.equals;
|
|
183
186
|
const get = () => {
|
|
184
187
|
track(source);
|
|
185
188
|
return value;
|
|
186
189
|
};
|
|
187
190
|
const set = (next) => {
|
|
188
191
|
const v = typeof next === "function" ? next(value) : next;
|
|
189
|
-
|
|
192
|
+
const same = equals === false ? false : equals ? equals(value, v) : Object.is(value, v);
|
|
193
|
+
if (same) return;
|
|
190
194
|
value = v;
|
|
191
195
|
notify(source);
|
|
192
196
|
};
|
|
193
197
|
return [get, set];
|
|
194
198
|
}
|
|
195
|
-
function createComputation(fn, isEffect) {
|
|
199
|
+
function createComputation(fn, isEffect, name) {
|
|
196
200
|
return {
|
|
197
201
|
fn,
|
|
198
202
|
isEffect,
|
|
203
|
+
name,
|
|
199
204
|
cleanups: [],
|
|
200
205
|
parent: currentOwner,
|
|
201
206
|
sources: /* @__PURE__ */ new Set(),
|
|
@@ -206,13 +211,14 @@ function createComputation(fn, isEffect) {
|
|
|
206
211
|
value: void 0
|
|
207
212
|
};
|
|
208
213
|
}
|
|
209
|
-
function createEffect(fn) {
|
|
210
|
-
const node = createComputation(fn, true);
|
|
214
|
+
function createEffect(fn, options) {
|
|
215
|
+
const node = createComputation(fn, true, options?.name);
|
|
211
216
|
addCleanup(() => dispose(node));
|
|
212
217
|
run(node);
|
|
213
218
|
}
|
|
214
|
-
function createMemo(fn) {
|
|
215
|
-
const node = createComputation(fn, false);
|
|
219
|
+
function createMemo(fn, options) {
|
|
220
|
+
const node = createComputation(fn, false, options?.name);
|
|
221
|
+
node.equals = options?.equals;
|
|
216
222
|
addCleanup(() => dispose(node));
|
|
217
223
|
run(node);
|
|
218
224
|
return () => {
|
|
@@ -327,9 +333,6 @@ function catchError(fn, handler) {
|
|
|
327
333
|
}
|
|
328
334
|
return result;
|
|
329
335
|
}
|
|
330
|
-
function createContext(defaultValue) {
|
|
331
|
-
return { id: /* @__PURE__ */ Symbol("cajoa-context"), defaultValue };
|
|
332
|
-
}
|
|
333
336
|
function provide(ctx, value, fn) {
|
|
334
337
|
const owner = {
|
|
335
338
|
cleanups: [],
|
|
@@ -357,6 +360,132 @@ function useContext(ctx) {
|
|
|
357
360
|
return ctx.defaultValue;
|
|
358
361
|
}
|
|
359
362
|
|
|
363
|
+
// src/runtime/reconcile.ts
|
|
364
|
+
function longestIncreasingSubsequence(values) {
|
|
365
|
+
const predecessor = new Array(values.length);
|
|
366
|
+
const tails = [];
|
|
367
|
+
for (let i = 0; i < values.length; i++) {
|
|
368
|
+
const v = values[i];
|
|
369
|
+
if (v < 0) continue;
|
|
370
|
+
let lo;
|
|
371
|
+
if (tails.length === 0 || values[tails[tails.length - 1]] < v) {
|
|
372
|
+
lo = tails.length;
|
|
373
|
+
} else {
|
|
374
|
+
lo = 0;
|
|
375
|
+
let hi = tails.length - 1;
|
|
376
|
+
while (lo < hi) {
|
|
377
|
+
const mid = lo + hi >> 1;
|
|
378
|
+
if (values[tails[mid]] < v) lo = mid + 1;
|
|
379
|
+
else hi = mid;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
predecessor[i] = lo > 0 ? tails[lo - 1] : -1;
|
|
383
|
+
tails[lo] = i;
|
|
384
|
+
}
|
|
385
|
+
const result = new Array(tails.length);
|
|
386
|
+
let k = tails.length > 0 ? tails[tails.length - 1] : -1;
|
|
387
|
+
for (let j = tails.length - 1; j >= 0; j--) {
|
|
388
|
+
result[j] = k;
|
|
389
|
+
k = predecessor[k];
|
|
390
|
+
}
|
|
391
|
+
return result;
|
|
392
|
+
}
|
|
393
|
+
function reconcile(start, end, next) {
|
|
394
|
+
const r = getRenderer();
|
|
395
|
+
const keep = new Set(next);
|
|
396
|
+
const oldIndex = /* @__PURE__ */ new Map();
|
|
397
|
+
let count = 0;
|
|
398
|
+
let cur = r.nextSibling(start);
|
|
399
|
+
while (cur && cur !== end) {
|
|
400
|
+
const after = r.nextSibling(cur);
|
|
401
|
+
if (keep.has(cur)) oldIndex.set(cur, count++);
|
|
402
|
+
else r.remove(cur);
|
|
403
|
+
cur = after;
|
|
404
|
+
}
|
|
405
|
+
const parent = r.parentNode(end);
|
|
406
|
+
const n = next.length;
|
|
407
|
+
const positions = new Array(n);
|
|
408
|
+
for (let i = 0; i < n; i++) positions[i] = oldIndex.get(next[i]) ?? -1;
|
|
409
|
+
const stable = new Uint8Array(n);
|
|
410
|
+
for (const i of longestIncreasingSubsequence(positions)) stable[i] = 1;
|
|
411
|
+
const anchors = new Array(n);
|
|
412
|
+
let anchor = end;
|
|
413
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
414
|
+
anchors[i] = anchor;
|
|
415
|
+
if (stable[i]) anchor = next[i];
|
|
416
|
+
}
|
|
417
|
+
for (let i = 0; i < n; i++) {
|
|
418
|
+
if (!stable[i]) r.insert(parent, next[i], anchors[i]);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// src/runtime/h.ts
|
|
423
|
+
function h(tag, props, ...children) {
|
|
424
|
+
const r = getRenderer();
|
|
425
|
+
const el = r.createElement(tag);
|
|
426
|
+
let ref;
|
|
427
|
+
for (const [key, value] of Object.entries(props ?? {})) {
|
|
428
|
+
if (key === "ref") {
|
|
429
|
+
if (typeof value === "function") ref = value;
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
if (typeof value === "function" && !key.startsWith("on")) {
|
|
433
|
+
createEffect(() => r.setProp(el, key, value()));
|
|
434
|
+
} else {
|
|
435
|
+
r.setProp(el, key, value);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
appendChildren(el, children);
|
|
439
|
+
const onRef = ref;
|
|
440
|
+
if (onRef && !r.isServer) untrack(() => onRef(el));
|
|
441
|
+
return el;
|
|
442
|
+
}
|
|
443
|
+
function materialize(value) {
|
|
444
|
+
if (typeof value !== "function") return value;
|
|
445
|
+
const holder = getRenderer().createFragment();
|
|
446
|
+
appendChildren(holder, [value]);
|
|
447
|
+
return holder;
|
|
448
|
+
}
|
|
449
|
+
function toNodes(value) {
|
|
450
|
+
const r = getRenderer();
|
|
451
|
+
const out = [];
|
|
452
|
+
for (const v of [value].flat(Infinity)) {
|
|
453
|
+
if (v == null || typeof v === "boolean") continue;
|
|
454
|
+
if (typeof v === "function") {
|
|
455
|
+
out.push(materialize(v));
|
|
456
|
+
continue;
|
|
457
|
+
}
|
|
458
|
+
out.push(r.isNode(v) ? v : r.createText(String(v)));
|
|
459
|
+
}
|
|
460
|
+
return out;
|
|
461
|
+
}
|
|
462
|
+
function appendChildren(parent, children) {
|
|
463
|
+
const r = getRenderer();
|
|
464
|
+
for (const child of children.flat(Infinity)) {
|
|
465
|
+
if (typeof child === "function") {
|
|
466
|
+
const start = r.createMarker();
|
|
467
|
+
const end = r.createMarker();
|
|
468
|
+
r.insert(parent, start);
|
|
469
|
+
r.insert(parent, end);
|
|
470
|
+
createEffect(() => reconcile(start, end, toNodes(child())));
|
|
471
|
+
} else {
|
|
472
|
+
for (const node of toNodes(child)) r.insert(parent, node);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// src/reactivity/context.ts
|
|
478
|
+
function createContext(defaultValue) {
|
|
479
|
+
const context = {
|
|
480
|
+
id: /* @__PURE__ */ Symbol("cajoa-context"),
|
|
481
|
+
defaultValue,
|
|
482
|
+
Provider(props) {
|
|
483
|
+
return provide(context, props.value, () => materialize(props.children));
|
|
484
|
+
}
|
|
485
|
+
};
|
|
486
|
+
return context;
|
|
487
|
+
}
|
|
488
|
+
|
|
360
489
|
// src/ssr.ts
|
|
361
490
|
var ServerContext = createContext(null);
|
|
362
491
|
|
|
@@ -689,132 +818,13 @@ function unwrap(value) {
|
|
|
689
818
|
return toRaw(value);
|
|
690
819
|
}
|
|
691
820
|
|
|
692
|
-
// src/runtime/reconcile.ts
|
|
693
|
-
function longestIncreasingSubsequence(values) {
|
|
694
|
-
const predecessor = new Array(values.length);
|
|
695
|
-
const tails = [];
|
|
696
|
-
for (let i = 0; i < values.length; i++) {
|
|
697
|
-
const v = values[i];
|
|
698
|
-
if (v < 0) continue;
|
|
699
|
-
let lo;
|
|
700
|
-
if (tails.length === 0 || values[tails[tails.length - 1]] < v) {
|
|
701
|
-
lo = tails.length;
|
|
702
|
-
} else {
|
|
703
|
-
lo = 0;
|
|
704
|
-
let hi = tails.length - 1;
|
|
705
|
-
while (lo < hi) {
|
|
706
|
-
const mid = lo + hi >> 1;
|
|
707
|
-
if (values[tails[mid]] < v) lo = mid + 1;
|
|
708
|
-
else hi = mid;
|
|
709
|
-
}
|
|
710
|
-
}
|
|
711
|
-
predecessor[i] = lo > 0 ? tails[lo - 1] : -1;
|
|
712
|
-
tails[lo] = i;
|
|
713
|
-
}
|
|
714
|
-
const result = new Array(tails.length);
|
|
715
|
-
let k = tails.length > 0 ? tails[tails.length - 1] : -1;
|
|
716
|
-
for (let j = tails.length - 1; j >= 0; j--) {
|
|
717
|
-
result[j] = k;
|
|
718
|
-
k = predecessor[k];
|
|
719
|
-
}
|
|
720
|
-
return result;
|
|
721
|
-
}
|
|
722
|
-
function reconcile(start, end, next) {
|
|
723
|
-
const r = getRenderer();
|
|
724
|
-
const keep = new Set(next);
|
|
725
|
-
const oldIndex = /* @__PURE__ */ new Map();
|
|
726
|
-
let count = 0;
|
|
727
|
-
let cur = r.nextSibling(start);
|
|
728
|
-
while (cur && cur !== end) {
|
|
729
|
-
const after = r.nextSibling(cur);
|
|
730
|
-
if (keep.has(cur)) oldIndex.set(cur, count++);
|
|
731
|
-
else r.remove(cur);
|
|
732
|
-
cur = after;
|
|
733
|
-
}
|
|
734
|
-
const parent = r.parentNode(end);
|
|
735
|
-
const n = next.length;
|
|
736
|
-
const positions = new Array(n);
|
|
737
|
-
for (let i = 0; i < n; i++) positions[i] = oldIndex.get(next[i]) ?? -1;
|
|
738
|
-
const stable = new Uint8Array(n);
|
|
739
|
-
for (const i of longestIncreasingSubsequence(positions)) stable[i] = 1;
|
|
740
|
-
const anchors = new Array(n);
|
|
741
|
-
let anchor = end;
|
|
742
|
-
for (let i = n - 1; i >= 0; i--) {
|
|
743
|
-
anchors[i] = anchor;
|
|
744
|
-
if (stable[i]) anchor = next[i];
|
|
745
|
-
}
|
|
746
|
-
for (let i = 0; i < n; i++) {
|
|
747
|
-
if (!stable[i]) r.insert(parent, next[i], anchors[i]);
|
|
748
|
-
}
|
|
749
|
-
}
|
|
750
|
-
|
|
751
|
-
// src/runtime/h.ts
|
|
752
|
-
function h(tag, props, ...children) {
|
|
753
|
-
const r = getRenderer();
|
|
754
|
-
const el = r.createElement(tag);
|
|
755
|
-
let ref;
|
|
756
|
-
for (const [key, value] of Object.entries(props ?? {})) {
|
|
757
|
-
if (key === "ref") {
|
|
758
|
-
if (typeof value === "function") ref = value;
|
|
759
|
-
continue;
|
|
760
|
-
}
|
|
761
|
-
if (typeof value === "function" && !key.startsWith("on")) {
|
|
762
|
-
createEffect(() => r.setProp(el, key, value()));
|
|
763
|
-
} else {
|
|
764
|
-
r.setProp(el, key, value);
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
appendChildren(el, children);
|
|
768
|
-
const onRef = ref;
|
|
769
|
-
if (onRef && !r.isServer) untrack(() => onRef(el));
|
|
770
|
-
return el;
|
|
771
|
-
}
|
|
772
|
-
function materialize(value) {
|
|
773
|
-
if (typeof value !== "function") return value;
|
|
774
|
-
const holder = getRenderer().createFragment();
|
|
775
|
-
appendChildren(holder, [value]);
|
|
776
|
-
return holder;
|
|
777
|
-
}
|
|
778
|
-
function toNodes(value) {
|
|
779
|
-
const r = getRenderer();
|
|
780
|
-
const out = [];
|
|
781
|
-
for (const v of [value].flat(Infinity)) {
|
|
782
|
-
if (v == null || typeof v === "boolean") continue;
|
|
783
|
-
if (typeof v === "function") {
|
|
784
|
-
out.push(materialize(v));
|
|
785
|
-
continue;
|
|
786
|
-
}
|
|
787
|
-
out.push(r.isNode(v) ? v : r.createText(String(v)));
|
|
788
|
-
}
|
|
789
|
-
return out;
|
|
790
|
-
}
|
|
791
|
-
function appendChildren(parent, children) {
|
|
792
|
-
const r = getRenderer();
|
|
793
|
-
for (const child of children.flat(Infinity)) {
|
|
794
|
-
if (typeof child === "function") {
|
|
795
|
-
const start = r.createMarker();
|
|
796
|
-
const end = r.createMarker();
|
|
797
|
-
r.insert(parent, start);
|
|
798
|
-
r.insert(parent, end);
|
|
799
|
-
createEffect(() => reconcile(start, end, toNodes(child())));
|
|
800
|
-
} else {
|
|
801
|
-
for (const node of toNodes(child)) r.insert(parent, node);
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
}
|
|
805
|
-
|
|
806
821
|
// src/runtime/render.ts
|
|
807
822
|
function render(component, container) {
|
|
808
823
|
return createRoot((dispose2) => {
|
|
809
824
|
const r = getRenderer();
|
|
810
|
-
const
|
|
811
|
-
if (typeof node === "function") {
|
|
812
|
-
throw new Error(
|
|
813
|
-
"render(): the component returned a function (for example <Show>, <For>, <Router> or <ErrorBoundary>). Wrap it in an element, such as <div>\u2026</div>."
|
|
814
|
-
);
|
|
815
|
-
}
|
|
825
|
+
const result = component();
|
|
816
826
|
r.clear(container);
|
|
817
|
-
|
|
827
|
+
appendChildren(container, [result]);
|
|
818
828
|
return () => {
|
|
819
829
|
dispose2();
|
|
820
830
|
r.clear(container);
|
|
@@ -857,10 +867,12 @@ function For(props) {
|
|
|
857
867
|
}
|
|
858
868
|
let entry = cache.get(k);
|
|
859
869
|
if (!entry) {
|
|
860
|
-
entry = createRoot((dispose2) =>
|
|
861
|
-
|
|
862
|
-
dispose: dispose2
|
|
863
|
-
})
|
|
870
|
+
entry = createRoot((dispose2) => {
|
|
871
|
+
const [index, setIndex] = createSignal(i);
|
|
872
|
+
return { node: props.children(item, index), dispose: dispose2, setIndex };
|
|
873
|
+
});
|
|
874
|
+
} else {
|
|
875
|
+
entry.setIndex(i);
|
|
864
876
|
}
|
|
865
877
|
next.set(k, entry);
|
|
866
878
|
nodes2.push(entry.node);
|
|
@@ -869,6 +881,12 @@ function For(props) {
|
|
|
869
881
|
if (!next.has(k)) entry.dispose();
|
|
870
882
|
});
|
|
871
883
|
cache = next;
|
|
884
|
+
if (nodes2.length === 0 && props.fallback !== void 0) {
|
|
885
|
+
const fallback = props.fallback;
|
|
886
|
+
return untrack(
|
|
887
|
+
() => typeof fallback === "function" ? fallback() : fallback
|
|
888
|
+
);
|
|
889
|
+
}
|
|
872
890
|
return nodes2;
|
|
873
891
|
};
|
|
874
892
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cajoa/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
4
4
|
"description": "Cajoa core: signals, JSX runtime and the renderer interface.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
}
|
|
50
50
|
},
|
|
51
51
|
"engines": {
|
|
52
|
-
"node": ">=
|
|
52
|
+
"node": ">=20"
|
|
53
53
|
},
|
|
54
54
|
"files": [
|
|
55
55
|
"dist"
|