@cajoa/core 0.13.0 → 0.15.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 CHANGED
@@ -20,10 +20,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ Dynamic: () => Dynamic,
23
24
  ErrorBoundary: () => ErrorBoundary,
24
25
  For: () => For,
25
26
  Fragment: () => Fragment,
27
+ Match: () => Match,
28
+ Portal: () => Portal,
26
29
  Show: () => Show,
30
+ Suspense: () => Suspense,
31
+ Switch: () => Switch,
27
32
  batch: () => batch,
28
33
  catchError: () => catchError,
29
34
  createContext: () => createContext,
@@ -32,28 +37,41 @@ __export(index_exports, {
32
37
  createResource: () => createResource,
33
38
  createRoot: () => createRoot,
34
39
  createSignal: () => createSignal,
40
+ createStore: () => createStore,
35
41
  getRenderer: () => getRenderer,
36
42
  h: () => h,
37
43
  jsx: () => jsx,
38
44
  jsxDEV: () => jsxDEV,
39
45
  jsxs: () => jsxs,
46
+ lazy: () => lazy,
40
47
  onCleanup: () => onCleanup,
41
48
  onMount: () => onMount,
42
49
  provide: () => provide,
43
50
  render: () => render,
44
51
  setRenderer: () => setRenderer,
45
52
  untrack: () => untrack,
53
+ unwrap: () => unwrap,
46
54
  useContext: () => useContext
47
55
  });
48
56
  module.exports = __toCommonJS(index_exports);
49
57
 
50
58
  // src/dev.ts
59
+ var muted = 0;
51
60
  function isDev() {
52
61
  return globalThis.__CAJOA_DEV__ === true;
53
62
  }
63
+ function quietly(fn) {
64
+ muted++;
65
+ try {
66
+ return fn();
67
+ } finally {
68
+ muted--;
69
+ }
70
+ }
54
71
  function warn(message) {
55
- if (isDev() && typeof console !== "undefined")
72
+ if (muted === 0 && isDev() && typeof console !== "undefined") {
56
73
  console.warn(`[cajoa] ${message}`);
74
+ }
57
75
  }
58
76
  function logError(message, error) {
59
77
  if (isDev() && typeof console !== "undefined")
@@ -260,6 +278,13 @@ function createMemo(fn) {
260
278
  return value;
261
279
  }
262
280
 
281
+ // src/reactivity/suspense.ts
282
+ var SuspenseContext = createContext(null);
283
+ function trackPending() {
284
+ const state = quietly(() => useContext(SuspenseContext));
285
+ return state ? () => state.add() : void 0;
286
+ }
287
+
263
288
  // src/reactivity/resource.ts
264
289
  function isPromiseLike(value) {
265
290
  return typeof value?.then === "function";
@@ -273,8 +298,12 @@ function createResource(a, b) {
273
298
  const [loading, setLoading] = createSignal(false);
274
299
  const [version, setVersion] = createSignal(0);
275
300
  let run = 0;
301
+ const track = trackPending();
302
+ let release;
303
+ let settled = false;
276
304
  const finish = (id, outcome) => {
277
305
  if (id !== run) return;
306
+ settled = true;
278
307
  batch(() => {
279
308
  setLoading(false);
280
309
  if ("error" in outcome) {
@@ -284,6 +313,8 @@ function createResource(a, b) {
284
313
  setData(() => outcome.value);
285
314
  }
286
315
  });
316
+ release?.();
317
+ release = void 0;
287
318
  };
288
319
  createEffect(() => {
289
320
  const s = source();
@@ -292,6 +323,8 @@ function createResource(a, b) {
292
323
  const id = run;
293
324
  onCleanup(() => {
294
325
  run++;
326
+ release?.();
327
+ release = void 0;
295
328
  });
296
329
  if (s === false || s === null || s === void 0) {
297
330
  batch(() => {
@@ -301,6 +334,7 @@ function createResource(a, b) {
301
334
  });
302
335
  return;
303
336
  }
337
+ if (!settled && track && !release) release = track();
304
338
  setLoading(true);
305
339
  setFailure(null);
306
340
  let result;
@@ -338,6 +372,239 @@ function createResource(a, b) {
338
372
  ];
339
373
  }
340
374
 
375
+ // src/reactivity/store.ts
376
+ var RAW = /* @__PURE__ */ Symbol("cajoa-store-raw");
377
+ var MUTATE_MESSAGE = "Cannot change a store directly. Use its setter instead, for example setState('count', 1) or setState((draft) => { draft.count = 1; }).";
378
+ var nodes = /* @__PURE__ */ new WeakMap();
379
+ var readers = /* @__PURE__ */ new WeakMap();
380
+ var drafts = /* @__PURE__ */ new WeakMap();
381
+ function makeCell() {
382
+ const [read, write] = createSignal(0);
383
+ return {
384
+ track: () => {
385
+ read();
386
+ },
387
+ notify: () => write((n) => n + 1)
388
+ };
389
+ }
390
+ function nodeOf(raw) {
391
+ let node = nodes.get(raw);
392
+ if (!node) {
393
+ node = { cells: /* @__PURE__ */ new Map() };
394
+ nodes.set(raw, node);
395
+ }
396
+ return node;
397
+ }
398
+ function trackKey(raw, key) {
399
+ const node = nodeOf(raw);
400
+ let cell = node.cells.get(key);
401
+ if (!cell) {
402
+ cell = makeCell();
403
+ node.cells.set(key, cell);
404
+ }
405
+ cell.track();
406
+ }
407
+ function trackOwn(raw) {
408
+ const node = nodeOf(raw);
409
+ (node.own ??= makeCell()).track();
410
+ }
411
+ var notifyKey = (raw, key) => nodes.get(raw)?.cells.get(key)?.notify();
412
+ var notifyOwn = (raw) => nodes.get(raw)?.own?.notify();
413
+ function isWrappable(value) {
414
+ if (value === null || typeof value !== "object") return false;
415
+ if (Array.isArray(value)) return true;
416
+ const proto = Object.getPrototypeOf(value);
417
+ return proto === Object.prototype || proto === null;
418
+ }
419
+ function toRaw(value) {
420
+ if (value !== null && typeof value === "object") {
421
+ const raw = value[RAW];
422
+ if (raw) return raw;
423
+ }
424
+ return value;
425
+ }
426
+ function unwrapIncoming(value, seen = /* @__PURE__ */ new Set()) {
427
+ const raw = toRaw(value);
428
+ if (!isWrappable(raw) || seen.has(raw)) return raw;
429
+ seen.add(raw);
430
+ const record = raw;
431
+ for (const key of Object.keys(record)) {
432
+ const child = record[key];
433
+ const inner = unwrapIncoming(child, seen);
434
+ if (inner !== child) record[key] = inner;
435
+ }
436
+ return raw;
437
+ }
438
+ function removeProperty(raw, key) {
439
+ if (!Object.prototype.hasOwnProperty.call(raw, key)) return;
440
+ delete raw[key];
441
+ batch(() => {
442
+ notifyKey(raw, key);
443
+ notifyOwn(raw);
444
+ });
445
+ }
446
+ function writeProperty(raw, key, input) {
447
+ const value = toRaw(input);
448
+ const record = raw;
449
+ const isArray = Array.isArray(raw);
450
+ const had = Object.prototype.hasOwnProperty.call(raw, key);
451
+ if (value === void 0 && !isArray) {
452
+ removeProperty(raw, key);
453
+ return;
454
+ }
455
+ if (had && Object.is(record[key], value)) return;
456
+ const next = unwrapIncoming(value);
457
+ if (isArray && key === "length") {
458
+ const array = raw;
459
+ const old = array.length;
460
+ const newLength = Number(next);
461
+ array.length = newLength;
462
+ batch(() => {
463
+ for (let i = newLength; i < old; i++) notifyKey(raw, String(i));
464
+ notifyKey(raw, "length");
465
+ notifyOwn(raw);
466
+ });
467
+ return;
468
+ }
469
+ const oldLength = isArray ? raw.length : 0;
470
+ record[key] = next;
471
+ batch(() => {
472
+ notifyKey(raw, key);
473
+ if (!had) notifyOwn(raw);
474
+ if (isArray && raw.length !== oldLength) {
475
+ notifyKey(raw, "length");
476
+ notifyOwn(raw);
477
+ }
478
+ });
479
+ }
480
+ function merge(target, source) {
481
+ for (const key of Object.keys(source)) {
482
+ writeProperty(target, key, source[key]);
483
+ }
484
+ }
485
+ var wrapRead = (value) => isWrappable(value) ? readerOf(toRaw(value)) : value;
486
+ var wrapDraft = (value) => isWrappable(value) ? draftOf(toRaw(value)) : value;
487
+ var readHandler = {
488
+ get(target, key, receiver) {
489
+ if (key === RAW) return target;
490
+ const value = Reflect.get(target, key, receiver);
491
+ if (typeof key === "symbol") return value;
492
+ if (typeof value === "function" && !Object.prototype.hasOwnProperty.call(target, key)) {
493
+ return value;
494
+ }
495
+ trackKey(target, key);
496
+ return wrapRead(value);
497
+ },
498
+ has(target, key) {
499
+ if (key === RAW) return true;
500
+ if (typeof key !== "symbol") trackKey(target, key);
501
+ return Reflect.has(target, key);
502
+ },
503
+ ownKeys(target) {
504
+ trackOwn(target);
505
+ return Reflect.ownKeys(target);
506
+ },
507
+ set() {
508
+ throw new Error(MUTATE_MESSAGE);
509
+ },
510
+ deleteProperty() {
511
+ throw new Error(MUTATE_MESSAGE);
512
+ },
513
+ defineProperty() {
514
+ throw new Error(MUTATE_MESSAGE);
515
+ }
516
+ };
517
+ var draftHandler = {
518
+ get(target, key, receiver) {
519
+ if (key === RAW) return target;
520
+ const value = Reflect.get(target, key, receiver);
521
+ if (typeof key === "symbol") return value;
522
+ return wrapDraft(value);
523
+ },
524
+ set(target, key, value) {
525
+ if (typeof key === "symbol") return Reflect.set(target, key, value);
526
+ writeProperty(target, key, value);
527
+ return true;
528
+ },
529
+ deleteProperty(target, key) {
530
+ if (typeof key === "symbol") return Reflect.deleteProperty(target, key);
531
+ removeProperty(target, key);
532
+ return true;
533
+ }
534
+ };
535
+ function readerOf(raw) {
536
+ let proxy = readers.get(raw);
537
+ if (!proxy) {
538
+ proxy = new Proxy(raw, readHandler);
539
+ readers.set(raw, proxy);
540
+ }
541
+ return proxy;
542
+ }
543
+ function draftOf(raw) {
544
+ let proxy = drafts.get(raw);
545
+ if (!proxy) {
546
+ proxy = new Proxy(raw, draftHandler);
547
+ drafts.set(raw, proxy);
548
+ }
549
+ return proxy;
550
+ }
551
+ function applyUpdate(root, args) {
552
+ if (args.length === 0) return;
553
+ if (args.length === 1) {
554
+ const arg = args[0];
555
+ if (typeof arg === "function") {
556
+ const result = untrack(
557
+ () => arg(draftOf(root))
558
+ );
559
+ if (isWrappable(result) && !Array.isArray(result))
560
+ merge(root, toRaw(result));
561
+ return;
562
+ }
563
+ if (isWrappable(arg) && !Array.isArray(arg)) {
564
+ merge(root, toRaw(arg));
565
+ return;
566
+ }
567
+ throw new Error(
568
+ "setState(): pass an object to merge, a function that edits a draft, or a path followed by a value."
569
+ );
570
+ }
571
+ const path = args.slice(0, -1);
572
+ const last = args[args.length - 1];
573
+ let target = root;
574
+ for (let i = 0; i < path.length - 1; i++) {
575
+ const next = target[String(path[i])];
576
+ if (!isWrappable(next)) {
577
+ throw new Error(
578
+ `setState(): "${path.slice(0, i + 1).join(".")}" is not an object or an array, so it has no "${path[i + 1]}" to change.`
579
+ );
580
+ }
581
+ target = toRaw(next);
582
+ }
583
+ const key = String(path[path.length - 1]);
584
+ const prev = target[key];
585
+ if (typeof last === "function") {
586
+ const result = untrack(() => last(prev));
587
+ writeProperty(target, key, result);
588
+ return;
589
+ }
590
+ const incoming = toRaw(last);
591
+ if (isWrappable(prev) && !Array.isArray(prev) && isWrappable(incoming) && !Array.isArray(incoming)) {
592
+ merge(toRaw(prev), incoming);
593
+ return;
594
+ }
595
+ writeProperty(target, key, incoming);
596
+ }
597
+ function createStore(initial) {
598
+ const root = unwrapIncoming(initial);
599
+ const setState = (...args) => {
600
+ batch(() => applyUpdate(root, args));
601
+ };
602
+ return [readerOf(root), setState];
603
+ }
604
+ function unwrap(value) {
605
+ return toRaw(value);
606
+ }
607
+
341
608
  // src/renderer/index.ts
342
609
  var current = null;
343
610
  function setRenderer(renderer) {
@@ -446,8 +713,12 @@ function render(component, container) {
446
713
  function resolve(value) {
447
714
  return typeof value === "function" ? value() : value;
448
715
  }
716
+ function resolveCondition(value) {
717
+ while (typeof value === "function") value = value();
718
+ return value;
719
+ }
449
720
  function Show(props) {
450
- const visible = createMemo(() => !!resolve(props.when));
721
+ const visible = createMemo(() => !!resolveCondition(props.when));
451
722
  return () => visible() ? untrack(() => resolve(props.children ?? null)) : untrack(() => resolve(props.fallback ?? null));
452
723
  }
453
724
 
@@ -459,9 +730,11 @@ function For(props) {
459
730
  cache.clear();
460
731
  });
461
732
  return () => {
462
- const items = typeof props.each === "function" ? props.each() : props.each;
733
+ let source = props.each;
734
+ while (typeof source === "function") source = source();
735
+ const items = source ?? [];
463
736
  const next = /* @__PURE__ */ new Map();
464
- const nodes = [];
737
+ const nodes2 = [];
465
738
  items.forEach((item, i) => {
466
739
  const k = props.key ? props.key(item, i) : item;
467
740
  if (next.has(k)) {
@@ -477,13 +750,13 @@ function For(props) {
477
750
  }));
478
751
  }
479
752
  next.set(k, entry);
480
- nodes.push(entry.node);
753
+ nodes2.push(entry.node);
481
754
  });
482
755
  cache.forEach((entry, k) => {
483
756
  if (!next.has(k)) entry.dispose();
484
757
  });
485
758
  cache = next;
486
- return nodes;
759
+ return nodes2;
487
760
  };
488
761
  }
489
762
 
@@ -537,6 +810,36 @@ function ErrorBoundary(props) {
537
810
  };
538
811
  }
539
812
 
813
+ // src/components/switch.ts
814
+ function resolve3(value) {
815
+ return typeof value === "function" ? value() : value;
816
+ }
817
+ function resolveCondition2(value) {
818
+ while (typeof value === "function") value = value();
819
+ return value;
820
+ }
821
+ function Match(props) {
822
+ return { __match: true, when: props.when, children: props.children };
823
+ }
824
+ var isMatch = (value) => typeof value === "object" && value !== null && value.__match === true;
825
+ function Switch(props) {
826
+ const matches = [props.children].flat(Infinity).filter(isMatch);
827
+ const selected = createMemo(() => {
828
+ for (let i = 0; i < matches.length; i++) {
829
+ if (resolveCondition2(matches[i].when)) return i;
830
+ }
831
+ return -1;
832
+ });
833
+ return () => {
834
+ const i = selected();
835
+ return untrack(
836
+ () => resolve3(
837
+ i === -1 ? props.fallback ?? null : matches[i].children ?? null
838
+ )
839
+ );
840
+ };
841
+ }
842
+
540
843
  // src/jsx-runtime.ts
541
844
  function jsx(type, props) {
542
845
  if (typeof type === "function") return type(props ?? {});
@@ -551,12 +854,127 @@ function Fragment(props) {
551
854
  appendChildren(frag, props.children === void 0 ? [] : [props.children]);
552
855
  return frag;
553
856
  }
857
+
858
+ // src/components/dynamic.ts
859
+ function Dynamic(props) {
860
+ const { is, ...rest } = props;
861
+ const read = typeof is === "function" ? is : () => is;
862
+ return () => {
863
+ const target = read();
864
+ if (target == null || target === false) return null;
865
+ if (typeof target !== "string" && typeof target !== "function") {
866
+ throw new Error(
867
+ `<Dynamic>: \`is\` must be a tag name, or a function that returns a tag name or a component (got ${typeof target}). To render a component directly, write is={() => MyComponent}.`
868
+ );
869
+ }
870
+ return untrack(() => jsx(target, rest));
871
+ };
872
+ }
873
+
874
+ // src/components/portal.ts
875
+ function resolve4(value) {
876
+ return typeof value === "function" ? value() : value;
877
+ }
878
+ function Portal(props) {
879
+ const r = getRenderer();
880
+ const mount = props.mount ?? r.portalRoot?.();
881
+ if (!mount) {
882
+ throw new Error(
883
+ "<Portal>: nowhere to render into. Pass a `mount` element (this renderer has no default)."
884
+ );
885
+ }
886
+ const start = r.createMarker();
887
+ const end = r.createMarker();
888
+ r.insert(mount, start);
889
+ r.insert(
890
+ mount,
891
+ materialize(() => resolve4(props.children ?? null))
892
+ );
893
+ r.insert(mount, end);
894
+ onCleanup(() => {
895
+ let node = r.nextSibling(start);
896
+ while (node && node !== end) {
897
+ const next = r.nextSibling(node);
898
+ r.remove(node);
899
+ node = next;
900
+ }
901
+ r.remove(start);
902
+ r.remove(end);
903
+ });
904
+ return null;
905
+ }
906
+
907
+ // src/components/suspense.ts
908
+ function resolve5(value) {
909
+ return typeof value === "function" ? value() : value;
910
+ }
911
+ function Suspense(props) {
912
+ const [pending2, setPending] = createSignal(0);
913
+ const [ready, setReady] = createSignal(false);
914
+ const state = {
915
+ add() {
916
+ setPending((n) => n + 1);
917
+ let released = false;
918
+ return () => {
919
+ if (released) return;
920
+ released = true;
921
+ setPending((n) => n - 1);
922
+ };
923
+ }
924
+ };
925
+ const content = untrack(
926
+ () => provide(
927
+ SuspenseContext,
928
+ state,
929
+ () => materialize(resolve5(props.children ?? null))
930
+ )
931
+ );
932
+ createEffect(() => {
933
+ if (pending2() === 0) setReady(true);
934
+ });
935
+ return () => ready() ? content : untrack(() => resolve5(props.fallback ?? null));
936
+ }
937
+
938
+ // src/components/lazy.ts
939
+ function lazy(loader) {
940
+ let promise;
941
+ let component;
942
+ let failure = null;
943
+ const load = () => promise ??= loader().then(
944
+ (module2) => {
945
+ component = typeof module2 === "function" ? module2 : module2.default;
946
+ },
947
+ (error) => {
948
+ failure = { error };
949
+ }
950
+ );
951
+ return function LazyComponent(props) {
952
+ if (component) return component(props);
953
+ if (failure) throw failure.error;
954
+ const release = trackPending()?.();
955
+ const [done, setDone] = createSignal(false);
956
+ load().then(() => {
957
+ setDone(true);
958
+ release?.();
959
+ });
960
+ return () => {
961
+ done();
962
+ if (failure) throw failure.error;
963
+ return component ? untrack(() => component(props)) : null;
964
+ };
965
+ };
966
+ }
554
967
  // Annotate the CommonJS export names for ESM import in node:
555
968
  0 && (module.exports = {
969
+ Dynamic,
556
970
  ErrorBoundary,
557
971
  For,
558
972
  Fragment,
973
+ Match,
974
+ Portal,
559
975
  Show,
976
+ Suspense,
977
+ Switch,
560
978
  batch,
561
979
  catchError,
562
980
  createContext,
@@ -565,16 +983,19 @@ function Fragment(props) {
565
983
  createResource,
566
984
  createRoot,
567
985
  createSignal,
986
+ createStore,
568
987
  getRenderer,
569
988
  h,
570
989
  jsx,
571
990
  jsxDEV,
572
991
  jsxs,
992
+ lazy,
573
993
  onCleanup,
574
994
  onMount,
575
995
  provide,
576
996
  render,
577
997
  setRenderer,
578
998
  untrack,
999
+ unwrap,
579
1000
  useContext
580
1001
  });
package/dist/index.d.cts CHANGED
@@ -30,6 +30,19 @@ type ResourceTuple<T> = [Resource<T>, ResourceActions<T>];
30
30
  declare function createResource<T>(fetcher: () => T | Promise<T>): ResourceTuple<T>;
31
31
  declare function createResource<T, S>(source: () => S | false | null | undefined, fetcher: (source: S) => T | Promise<T>): ResourceTuple<T>;
32
32
 
33
+ type Update<V> = V extends readonly unknown[] ? V : V extends object ? Partial<V> : V;
34
+ type ValueOrUpdate<V> = Update<V> | ((prev: V) => V);
35
+ interface SetStoreFunction<T> {
36
+ (partial: Partial<T>): void;
37
+ (edit: (draft: T) => Partial<T> | void): void;
38
+ <K1 extends keyof T>(k1: K1, value: ValueOrUpdate<T[K1]>): void;
39
+ <K1 extends keyof T, K2 extends keyof T[K1]>(k1: K1, k2: K2, value: ValueOrUpdate<T[K1][K2]>): void;
40
+ <K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>(k1: K1, k2: K2, k3: K3, value: ValueOrUpdate<T[K1][K2][K3]>): void;
41
+ <K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3]>(k1: K1, k2: K2, k3: K3, k4: K4, value: ValueOrUpdate<T[K1][K2][K3][K4]>): void;
42
+ }
43
+ declare function createStore<T extends object>(initial: T): [T, SetStoreFunction<T>];
44
+ declare function unwrap<T>(value: T): T;
45
+
33
46
  type HostNode = object;
34
47
  interface Renderer<N = any> {
35
48
  createElement(tag: string): N;
@@ -41,6 +54,7 @@ interface Renderer<N = any> {
41
54
  remove(node: N): void;
42
55
  nextSibling(node: N): N | null;
43
56
  parentNode(node: N): N | null;
57
+ portalRoot?(): N;
44
58
  clear(parent: N): void;
45
59
  setProp(el: N, name: string, value: unknown): void;
46
60
  }
@@ -50,11 +64,11 @@ declare function h(tag: string, props: Record<string, any> | null, ...children:
50
64
 
51
65
  declare function render(component: () => HostNode, container: HostNode): () => void;
52
66
 
53
- type Lazy$1<T> = T | (() => T);
67
+ type Lazy$4<T> = T | (() => T);
54
68
  declare function Show<T>(props: {
55
- when: Lazy$1<T>;
56
- children?: Lazy$1<Child>;
57
- fallback?: Lazy$1<Child>;
69
+ when: Lazy$4<T>;
70
+ children?: Lazy$4<Child>;
71
+ fallback?: Lazy$4<Child>;
58
72
  }): () => string | number | boolean | object | null;
59
73
 
60
74
  declare function For<T>(props: {
@@ -63,13 +77,51 @@ declare function For<T>(props: {
63
77
  key?: (item: T, index: number) => unknown;
64
78
  }): () => object[];
65
79
 
66
- type Lazy<T> = T | (() => T);
80
+ type Lazy$3<T> = T | (() => T);
67
81
  declare function ErrorBoundary(props: {
68
82
  fallback: Child | ((error: unknown, reset: () => void) => Child);
69
- children?: Lazy<Child>;
83
+ children?: Lazy$3<Child>;
70
84
  onError?: (error: unknown) => void;
71
85
  }): () => unknown;
72
86
 
87
+ type Lazy$2<T> = T | (() => T);
88
+ interface MatchDef {
89
+ __match: true;
90
+ when: Lazy$2<unknown>;
91
+ children?: Lazy$2<Child>;
92
+ }
93
+ declare function Match(props: {
94
+ when: Lazy$2<unknown>;
95
+ children?: Lazy$2<Child>;
96
+ }): MatchDef;
97
+ declare function Switch(props: {
98
+ fallback?: Lazy$2<Child>;
99
+ children?: unknown;
100
+ }): () => string | number | boolean | object | null;
101
+
102
+ type Target = string | ((props: any) => unknown) | null | undefined | false;
103
+ declare function Dynamic(props: {
104
+ is: Target | (() => Target);
105
+ [key: string]: unknown;
106
+ }): () => any;
107
+
108
+ type Lazy$1<T> = T | (() => T);
109
+ declare function Portal(props: {
110
+ mount?: HostNode;
111
+ children?: Lazy$1<Child>;
112
+ }): null;
113
+
114
+ type Lazy<T> = T | (() => T);
115
+ declare function Suspense(props: {
116
+ fallback?: Lazy<Child>;
117
+ children?: Lazy<Child>;
118
+ }): () => unknown;
119
+
120
+ type Component<P> = (props: P) => unknown;
121
+ declare function lazy<P = Record<string, unknown>>(loader: () => Promise<{
122
+ default: Component<P>;
123
+ } | Component<P>>): Component<P>;
124
+
73
125
  declare function jsx(type: any, props: Record<string, any> | null): any;
74
126
  declare const jsxs: typeof jsx;
75
127
  declare const jsxDEV: typeof jsx;
@@ -89,5 +141,5 @@ declare function Fragment(props: {
89
141
  declare function setRenderer(renderer: Renderer): void;
90
142
  declare function getRenderer(): Renderer;
91
143
 
92
- export { ErrorBoundary, For, Fragment, JSX, Show, batch, catchError, createContext, createEffect, createMemo, createResource, createRoot, createSignal, getRenderer, h, jsx, jsxDEV, jsxs, onCleanup, onMount, provide, render, setRenderer, untrack, useContext };
93
- export type { Child, Context, HostNode, Renderer, Resource, ResourceActions };
144
+ export { Dynamic, ErrorBoundary, For, Fragment, JSX, Match, Portal, Show, Suspense, Switch, batch, catchError, createContext, createEffect, createMemo, createResource, createRoot, createSignal, createStore, getRenderer, h, jsx, jsxDEV, jsxs, lazy, onCleanup, onMount, provide, render, setRenderer, untrack, unwrap, useContext };
145
+ export type { Child, Context, HostNode, MatchDef, Renderer, Resource, ResourceActions, SetStoreFunction };
package/dist/index.d.ts CHANGED
@@ -30,6 +30,19 @@ type ResourceTuple<T> = [Resource<T>, ResourceActions<T>];
30
30
  declare function createResource<T>(fetcher: () => T | Promise<T>): ResourceTuple<T>;
31
31
  declare function createResource<T, S>(source: () => S | false | null | undefined, fetcher: (source: S) => T | Promise<T>): ResourceTuple<T>;
32
32
 
33
+ type Update<V> = V extends readonly unknown[] ? V : V extends object ? Partial<V> : V;
34
+ type ValueOrUpdate<V> = Update<V> | ((prev: V) => V);
35
+ interface SetStoreFunction<T> {
36
+ (partial: Partial<T>): void;
37
+ (edit: (draft: T) => Partial<T> | void): void;
38
+ <K1 extends keyof T>(k1: K1, value: ValueOrUpdate<T[K1]>): void;
39
+ <K1 extends keyof T, K2 extends keyof T[K1]>(k1: K1, k2: K2, value: ValueOrUpdate<T[K1][K2]>): void;
40
+ <K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>(k1: K1, k2: K2, k3: K3, value: ValueOrUpdate<T[K1][K2][K3]>): void;
41
+ <K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3]>(k1: K1, k2: K2, k3: K3, k4: K4, value: ValueOrUpdate<T[K1][K2][K3][K4]>): void;
42
+ }
43
+ declare function createStore<T extends object>(initial: T): [T, SetStoreFunction<T>];
44
+ declare function unwrap<T>(value: T): T;
45
+
33
46
  type HostNode = object;
34
47
  interface Renderer<N = any> {
35
48
  createElement(tag: string): N;
@@ -41,6 +54,7 @@ interface Renderer<N = any> {
41
54
  remove(node: N): void;
42
55
  nextSibling(node: N): N | null;
43
56
  parentNode(node: N): N | null;
57
+ portalRoot?(): N;
44
58
  clear(parent: N): void;
45
59
  setProp(el: N, name: string, value: unknown): void;
46
60
  }
@@ -50,11 +64,11 @@ declare function h(tag: string, props: Record<string, any> | null, ...children:
50
64
 
51
65
  declare function render(component: () => HostNode, container: HostNode): () => void;
52
66
 
53
- type Lazy$1<T> = T | (() => T);
67
+ type Lazy$4<T> = T | (() => T);
54
68
  declare function Show<T>(props: {
55
- when: Lazy$1<T>;
56
- children?: Lazy$1<Child>;
57
- fallback?: Lazy$1<Child>;
69
+ when: Lazy$4<T>;
70
+ children?: Lazy$4<Child>;
71
+ fallback?: Lazy$4<Child>;
58
72
  }): () => string | number | boolean | object | null;
59
73
 
60
74
  declare function For<T>(props: {
@@ -63,13 +77,51 @@ declare function For<T>(props: {
63
77
  key?: (item: T, index: number) => unknown;
64
78
  }): () => object[];
65
79
 
66
- type Lazy<T> = T | (() => T);
80
+ type Lazy$3<T> = T | (() => T);
67
81
  declare function ErrorBoundary(props: {
68
82
  fallback: Child | ((error: unknown, reset: () => void) => Child);
69
- children?: Lazy<Child>;
83
+ children?: Lazy$3<Child>;
70
84
  onError?: (error: unknown) => void;
71
85
  }): () => unknown;
72
86
 
87
+ type Lazy$2<T> = T | (() => T);
88
+ interface MatchDef {
89
+ __match: true;
90
+ when: Lazy$2<unknown>;
91
+ children?: Lazy$2<Child>;
92
+ }
93
+ declare function Match(props: {
94
+ when: Lazy$2<unknown>;
95
+ children?: Lazy$2<Child>;
96
+ }): MatchDef;
97
+ declare function Switch(props: {
98
+ fallback?: Lazy$2<Child>;
99
+ children?: unknown;
100
+ }): () => string | number | boolean | object | null;
101
+
102
+ type Target = string | ((props: any) => unknown) | null | undefined | false;
103
+ declare function Dynamic(props: {
104
+ is: Target | (() => Target);
105
+ [key: string]: unknown;
106
+ }): () => any;
107
+
108
+ type Lazy$1<T> = T | (() => T);
109
+ declare function Portal(props: {
110
+ mount?: HostNode;
111
+ children?: Lazy$1<Child>;
112
+ }): null;
113
+
114
+ type Lazy<T> = T | (() => T);
115
+ declare function Suspense(props: {
116
+ fallback?: Lazy<Child>;
117
+ children?: Lazy<Child>;
118
+ }): () => unknown;
119
+
120
+ type Component<P> = (props: P) => unknown;
121
+ declare function lazy<P = Record<string, unknown>>(loader: () => Promise<{
122
+ default: Component<P>;
123
+ } | Component<P>>): Component<P>;
124
+
73
125
  declare function jsx(type: any, props: Record<string, any> | null): any;
74
126
  declare const jsxs: typeof jsx;
75
127
  declare const jsxDEV: typeof jsx;
@@ -89,5 +141,5 @@ declare function Fragment(props: {
89
141
  declare function setRenderer(renderer: Renderer): void;
90
142
  declare function getRenderer(): Renderer;
91
143
 
92
- export { ErrorBoundary, For, Fragment, JSX, Show, batch, catchError, createContext, createEffect, createMemo, createResource, createRoot, createSignal, getRenderer, h, jsx, jsxDEV, jsxs, onCleanup, onMount, provide, render, setRenderer, untrack, useContext };
93
- export type { Child, Context, HostNode, Renderer, Resource, ResourceActions };
144
+ export { Dynamic, ErrorBoundary, For, Fragment, JSX, Match, Portal, Show, Suspense, Switch, batch, catchError, createContext, createEffect, createMemo, createResource, createRoot, createSignal, createStore, getRenderer, h, jsx, jsxDEV, jsxs, lazy, onCleanup, onMount, provide, render, setRenderer, untrack, unwrap, useContext };
145
+ export type { Child, Context, HostNode, MatchDef, Renderer, Resource, ResourceActions, SetStoreFunction };
package/dist/index.js CHANGED
@@ -1,10 +1,20 @@
1
1
  // src/dev.ts
2
+ var muted = 0;
2
3
  function isDev() {
3
4
  return globalThis.__CAJOA_DEV__ === true;
4
5
  }
6
+ function quietly(fn) {
7
+ muted++;
8
+ try {
9
+ return fn();
10
+ } finally {
11
+ muted--;
12
+ }
13
+ }
5
14
  function warn(message) {
6
- if (isDev() && typeof console !== "undefined")
15
+ if (muted === 0 && isDev() && typeof console !== "undefined") {
7
16
  console.warn(`[cajoa] ${message}`);
17
+ }
8
18
  }
9
19
  function logError(message, error) {
10
20
  if (isDev() && typeof console !== "undefined")
@@ -211,6 +221,13 @@ function createMemo(fn) {
211
221
  return value;
212
222
  }
213
223
 
224
+ // src/reactivity/suspense.ts
225
+ var SuspenseContext = createContext(null);
226
+ function trackPending() {
227
+ const state = quietly(() => useContext(SuspenseContext));
228
+ return state ? () => state.add() : void 0;
229
+ }
230
+
214
231
  // src/reactivity/resource.ts
215
232
  function isPromiseLike(value) {
216
233
  return typeof value?.then === "function";
@@ -224,8 +241,12 @@ function createResource(a, b) {
224
241
  const [loading, setLoading] = createSignal(false);
225
242
  const [version, setVersion] = createSignal(0);
226
243
  let run = 0;
244
+ const track = trackPending();
245
+ let release;
246
+ let settled = false;
227
247
  const finish = (id, outcome) => {
228
248
  if (id !== run) return;
249
+ settled = true;
229
250
  batch(() => {
230
251
  setLoading(false);
231
252
  if ("error" in outcome) {
@@ -235,6 +256,8 @@ function createResource(a, b) {
235
256
  setData(() => outcome.value);
236
257
  }
237
258
  });
259
+ release?.();
260
+ release = void 0;
238
261
  };
239
262
  createEffect(() => {
240
263
  const s = source();
@@ -243,6 +266,8 @@ function createResource(a, b) {
243
266
  const id = run;
244
267
  onCleanup(() => {
245
268
  run++;
269
+ release?.();
270
+ release = void 0;
246
271
  });
247
272
  if (s === false || s === null || s === void 0) {
248
273
  batch(() => {
@@ -252,6 +277,7 @@ function createResource(a, b) {
252
277
  });
253
278
  return;
254
279
  }
280
+ if (!settled && track && !release) release = track();
255
281
  setLoading(true);
256
282
  setFailure(null);
257
283
  let result;
@@ -289,6 +315,239 @@ function createResource(a, b) {
289
315
  ];
290
316
  }
291
317
 
318
+ // src/reactivity/store.ts
319
+ var RAW = /* @__PURE__ */ Symbol("cajoa-store-raw");
320
+ var MUTATE_MESSAGE = "Cannot change a store directly. Use its setter instead, for example setState('count', 1) or setState((draft) => { draft.count = 1; }).";
321
+ var nodes = /* @__PURE__ */ new WeakMap();
322
+ var readers = /* @__PURE__ */ new WeakMap();
323
+ var drafts = /* @__PURE__ */ new WeakMap();
324
+ function makeCell() {
325
+ const [read, write] = createSignal(0);
326
+ return {
327
+ track: () => {
328
+ read();
329
+ },
330
+ notify: () => write((n) => n + 1)
331
+ };
332
+ }
333
+ function nodeOf(raw) {
334
+ let node = nodes.get(raw);
335
+ if (!node) {
336
+ node = { cells: /* @__PURE__ */ new Map() };
337
+ nodes.set(raw, node);
338
+ }
339
+ return node;
340
+ }
341
+ function trackKey(raw, key) {
342
+ const node = nodeOf(raw);
343
+ let cell = node.cells.get(key);
344
+ if (!cell) {
345
+ cell = makeCell();
346
+ node.cells.set(key, cell);
347
+ }
348
+ cell.track();
349
+ }
350
+ function trackOwn(raw) {
351
+ const node = nodeOf(raw);
352
+ (node.own ??= makeCell()).track();
353
+ }
354
+ var notifyKey = (raw, key) => nodes.get(raw)?.cells.get(key)?.notify();
355
+ var notifyOwn = (raw) => nodes.get(raw)?.own?.notify();
356
+ function isWrappable(value) {
357
+ if (value === null || typeof value !== "object") return false;
358
+ if (Array.isArray(value)) return true;
359
+ const proto = Object.getPrototypeOf(value);
360
+ return proto === Object.prototype || proto === null;
361
+ }
362
+ function toRaw(value) {
363
+ if (value !== null && typeof value === "object") {
364
+ const raw = value[RAW];
365
+ if (raw) return raw;
366
+ }
367
+ return value;
368
+ }
369
+ function unwrapIncoming(value, seen = /* @__PURE__ */ new Set()) {
370
+ const raw = toRaw(value);
371
+ if (!isWrappable(raw) || seen.has(raw)) return raw;
372
+ seen.add(raw);
373
+ const record = raw;
374
+ for (const key of Object.keys(record)) {
375
+ const child = record[key];
376
+ const inner = unwrapIncoming(child, seen);
377
+ if (inner !== child) record[key] = inner;
378
+ }
379
+ return raw;
380
+ }
381
+ function removeProperty(raw, key) {
382
+ if (!Object.prototype.hasOwnProperty.call(raw, key)) return;
383
+ delete raw[key];
384
+ batch(() => {
385
+ notifyKey(raw, key);
386
+ notifyOwn(raw);
387
+ });
388
+ }
389
+ function writeProperty(raw, key, input) {
390
+ const value = toRaw(input);
391
+ const record = raw;
392
+ const isArray = Array.isArray(raw);
393
+ const had = Object.prototype.hasOwnProperty.call(raw, key);
394
+ if (value === void 0 && !isArray) {
395
+ removeProperty(raw, key);
396
+ return;
397
+ }
398
+ if (had && Object.is(record[key], value)) return;
399
+ const next = unwrapIncoming(value);
400
+ if (isArray && key === "length") {
401
+ const array = raw;
402
+ const old = array.length;
403
+ const newLength = Number(next);
404
+ array.length = newLength;
405
+ batch(() => {
406
+ for (let i = newLength; i < old; i++) notifyKey(raw, String(i));
407
+ notifyKey(raw, "length");
408
+ notifyOwn(raw);
409
+ });
410
+ return;
411
+ }
412
+ const oldLength = isArray ? raw.length : 0;
413
+ record[key] = next;
414
+ batch(() => {
415
+ notifyKey(raw, key);
416
+ if (!had) notifyOwn(raw);
417
+ if (isArray && raw.length !== oldLength) {
418
+ notifyKey(raw, "length");
419
+ notifyOwn(raw);
420
+ }
421
+ });
422
+ }
423
+ function merge(target, source) {
424
+ for (const key of Object.keys(source)) {
425
+ writeProperty(target, key, source[key]);
426
+ }
427
+ }
428
+ var wrapRead = (value) => isWrappable(value) ? readerOf(toRaw(value)) : value;
429
+ var wrapDraft = (value) => isWrappable(value) ? draftOf(toRaw(value)) : value;
430
+ var readHandler = {
431
+ get(target, key, receiver) {
432
+ if (key === RAW) return target;
433
+ const value = Reflect.get(target, key, receiver);
434
+ if (typeof key === "symbol") return value;
435
+ if (typeof value === "function" && !Object.prototype.hasOwnProperty.call(target, key)) {
436
+ return value;
437
+ }
438
+ trackKey(target, key);
439
+ return wrapRead(value);
440
+ },
441
+ has(target, key) {
442
+ if (key === RAW) return true;
443
+ if (typeof key !== "symbol") trackKey(target, key);
444
+ return Reflect.has(target, key);
445
+ },
446
+ ownKeys(target) {
447
+ trackOwn(target);
448
+ return Reflect.ownKeys(target);
449
+ },
450
+ set() {
451
+ throw new Error(MUTATE_MESSAGE);
452
+ },
453
+ deleteProperty() {
454
+ throw new Error(MUTATE_MESSAGE);
455
+ },
456
+ defineProperty() {
457
+ throw new Error(MUTATE_MESSAGE);
458
+ }
459
+ };
460
+ var draftHandler = {
461
+ get(target, key, receiver) {
462
+ if (key === RAW) return target;
463
+ const value = Reflect.get(target, key, receiver);
464
+ if (typeof key === "symbol") return value;
465
+ return wrapDraft(value);
466
+ },
467
+ set(target, key, value) {
468
+ if (typeof key === "symbol") return Reflect.set(target, key, value);
469
+ writeProperty(target, key, value);
470
+ return true;
471
+ },
472
+ deleteProperty(target, key) {
473
+ if (typeof key === "symbol") return Reflect.deleteProperty(target, key);
474
+ removeProperty(target, key);
475
+ return true;
476
+ }
477
+ };
478
+ function readerOf(raw) {
479
+ let proxy = readers.get(raw);
480
+ if (!proxy) {
481
+ proxy = new Proxy(raw, readHandler);
482
+ readers.set(raw, proxy);
483
+ }
484
+ return proxy;
485
+ }
486
+ function draftOf(raw) {
487
+ let proxy = drafts.get(raw);
488
+ if (!proxy) {
489
+ proxy = new Proxy(raw, draftHandler);
490
+ drafts.set(raw, proxy);
491
+ }
492
+ return proxy;
493
+ }
494
+ function applyUpdate(root, args) {
495
+ if (args.length === 0) return;
496
+ if (args.length === 1) {
497
+ const arg = args[0];
498
+ if (typeof arg === "function") {
499
+ const result = untrack(
500
+ () => arg(draftOf(root))
501
+ );
502
+ if (isWrappable(result) && !Array.isArray(result))
503
+ merge(root, toRaw(result));
504
+ return;
505
+ }
506
+ if (isWrappable(arg) && !Array.isArray(arg)) {
507
+ merge(root, toRaw(arg));
508
+ return;
509
+ }
510
+ throw new Error(
511
+ "setState(): pass an object to merge, a function that edits a draft, or a path followed by a value."
512
+ );
513
+ }
514
+ const path = args.slice(0, -1);
515
+ const last = args[args.length - 1];
516
+ let target = root;
517
+ for (let i = 0; i < path.length - 1; i++) {
518
+ const next = target[String(path[i])];
519
+ if (!isWrappable(next)) {
520
+ throw new Error(
521
+ `setState(): "${path.slice(0, i + 1).join(".")}" is not an object or an array, so it has no "${path[i + 1]}" to change.`
522
+ );
523
+ }
524
+ target = toRaw(next);
525
+ }
526
+ const key = String(path[path.length - 1]);
527
+ const prev = target[key];
528
+ if (typeof last === "function") {
529
+ const result = untrack(() => last(prev));
530
+ writeProperty(target, key, result);
531
+ return;
532
+ }
533
+ const incoming = toRaw(last);
534
+ if (isWrappable(prev) && !Array.isArray(prev) && isWrappable(incoming) && !Array.isArray(incoming)) {
535
+ merge(toRaw(prev), incoming);
536
+ return;
537
+ }
538
+ writeProperty(target, key, incoming);
539
+ }
540
+ function createStore(initial) {
541
+ const root = unwrapIncoming(initial);
542
+ const setState = (...args) => {
543
+ batch(() => applyUpdate(root, args));
544
+ };
545
+ return [readerOf(root), setState];
546
+ }
547
+ function unwrap(value) {
548
+ return toRaw(value);
549
+ }
550
+
292
551
  // src/renderer/index.ts
293
552
  var current = null;
294
553
  function setRenderer(renderer) {
@@ -397,8 +656,12 @@ function render(component, container) {
397
656
  function resolve(value) {
398
657
  return typeof value === "function" ? value() : value;
399
658
  }
659
+ function resolveCondition(value) {
660
+ while (typeof value === "function") value = value();
661
+ return value;
662
+ }
400
663
  function Show(props) {
401
- const visible = createMemo(() => !!resolve(props.when));
664
+ const visible = createMemo(() => !!resolveCondition(props.when));
402
665
  return () => visible() ? untrack(() => resolve(props.children ?? null)) : untrack(() => resolve(props.fallback ?? null));
403
666
  }
404
667
 
@@ -410,9 +673,11 @@ function For(props) {
410
673
  cache.clear();
411
674
  });
412
675
  return () => {
413
- const items = typeof props.each === "function" ? props.each() : props.each;
676
+ let source = props.each;
677
+ while (typeof source === "function") source = source();
678
+ const items = source ?? [];
414
679
  const next = /* @__PURE__ */ new Map();
415
- const nodes = [];
680
+ const nodes2 = [];
416
681
  items.forEach((item, i) => {
417
682
  const k = props.key ? props.key(item, i) : item;
418
683
  if (next.has(k)) {
@@ -428,13 +693,13 @@ function For(props) {
428
693
  }));
429
694
  }
430
695
  next.set(k, entry);
431
- nodes.push(entry.node);
696
+ nodes2.push(entry.node);
432
697
  });
433
698
  cache.forEach((entry, k) => {
434
699
  if (!next.has(k)) entry.dispose();
435
700
  });
436
701
  cache = next;
437
- return nodes;
702
+ return nodes2;
438
703
  };
439
704
  }
440
705
 
@@ -488,6 +753,36 @@ function ErrorBoundary(props) {
488
753
  };
489
754
  }
490
755
 
756
+ // src/components/switch.ts
757
+ function resolve3(value) {
758
+ return typeof value === "function" ? value() : value;
759
+ }
760
+ function resolveCondition2(value) {
761
+ while (typeof value === "function") value = value();
762
+ return value;
763
+ }
764
+ function Match(props) {
765
+ return { __match: true, when: props.when, children: props.children };
766
+ }
767
+ var isMatch = (value) => typeof value === "object" && value !== null && value.__match === true;
768
+ function Switch(props) {
769
+ const matches = [props.children].flat(Infinity).filter(isMatch);
770
+ const selected = createMemo(() => {
771
+ for (let i = 0; i < matches.length; i++) {
772
+ if (resolveCondition2(matches[i].when)) return i;
773
+ }
774
+ return -1;
775
+ });
776
+ return () => {
777
+ const i = selected();
778
+ return untrack(
779
+ () => resolve3(
780
+ i === -1 ? props.fallback ?? null : matches[i].children ?? null
781
+ )
782
+ );
783
+ };
784
+ }
785
+
491
786
  // src/jsx-runtime.ts
492
787
  function jsx(type, props) {
493
788
  if (typeof type === "function") return type(props ?? {});
@@ -502,11 +797,126 @@ function Fragment(props) {
502
797
  appendChildren(frag, props.children === void 0 ? [] : [props.children]);
503
798
  return frag;
504
799
  }
800
+
801
+ // src/components/dynamic.ts
802
+ function Dynamic(props) {
803
+ const { is, ...rest } = props;
804
+ const read = typeof is === "function" ? is : () => is;
805
+ return () => {
806
+ const target = read();
807
+ if (target == null || target === false) return null;
808
+ if (typeof target !== "string" && typeof target !== "function") {
809
+ throw new Error(
810
+ `<Dynamic>: \`is\` must be a tag name, or a function that returns a tag name or a component (got ${typeof target}). To render a component directly, write is={() => MyComponent}.`
811
+ );
812
+ }
813
+ return untrack(() => jsx(target, rest));
814
+ };
815
+ }
816
+
817
+ // src/components/portal.ts
818
+ function resolve4(value) {
819
+ return typeof value === "function" ? value() : value;
820
+ }
821
+ function Portal(props) {
822
+ const r = getRenderer();
823
+ const mount = props.mount ?? r.portalRoot?.();
824
+ if (!mount) {
825
+ throw new Error(
826
+ "<Portal>: nowhere to render into. Pass a `mount` element (this renderer has no default)."
827
+ );
828
+ }
829
+ const start = r.createMarker();
830
+ const end = r.createMarker();
831
+ r.insert(mount, start);
832
+ r.insert(
833
+ mount,
834
+ materialize(() => resolve4(props.children ?? null))
835
+ );
836
+ r.insert(mount, end);
837
+ onCleanup(() => {
838
+ let node = r.nextSibling(start);
839
+ while (node && node !== end) {
840
+ const next = r.nextSibling(node);
841
+ r.remove(node);
842
+ node = next;
843
+ }
844
+ r.remove(start);
845
+ r.remove(end);
846
+ });
847
+ return null;
848
+ }
849
+
850
+ // src/components/suspense.ts
851
+ function resolve5(value) {
852
+ return typeof value === "function" ? value() : value;
853
+ }
854
+ function Suspense(props) {
855
+ const [pending2, setPending] = createSignal(0);
856
+ const [ready, setReady] = createSignal(false);
857
+ const state = {
858
+ add() {
859
+ setPending((n) => n + 1);
860
+ let released = false;
861
+ return () => {
862
+ if (released) return;
863
+ released = true;
864
+ setPending((n) => n - 1);
865
+ };
866
+ }
867
+ };
868
+ const content = untrack(
869
+ () => provide(
870
+ SuspenseContext,
871
+ state,
872
+ () => materialize(resolve5(props.children ?? null))
873
+ )
874
+ );
875
+ createEffect(() => {
876
+ if (pending2() === 0) setReady(true);
877
+ });
878
+ return () => ready() ? content : untrack(() => resolve5(props.fallback ?? null));
879
+ }
880
+
881
+ // src/components/lazy.ts
882
+ function lazy(loader) {
883
+ let promise;
884
+ let component;
885
+ let failure = null;
886
+ const load = () => promise ??= loader().then(
887
+ (module) => {
888
+ component = typeof module === "function" ? module : module.default;
889
+ },
890
+ (error) => {
891
+ failure = { error };
892
+ }
893
+ );
894
+ return function LazyComponent(props) {
895
+ if (component) return component(props);
896
+ if (failure) throw failure.error;
897
+ const release = trackPending()?.();
898
+ const [done, setDone] = createSignal(false);
899
+ load().then(() => {
900
+ setDone(true);
901
+ release?.();
902
+ });
903
+ return () => {
904
+ done();
905
+ if (failure) throw failure.error;
906
+ return component ? untrack(() => component(props)) : null;
907
+ };
908
+ };
909
+ }
505
910
  export {
911
+ Dynamic,
506
912
  ErrorBoundary,
507
913
  For,
508
914
  Fragment,
915
+ Match,
916
+ Portal,
509
917
  Show,
918
+ Suspense,
919
+ Switch,
510
920
  batch,
511
921
  catchError,
512
922
  createContext,
@@ -515,16 +925,19 @@ export {
515
925
  createResource,
516
926
  createRoot,
517
927
  createSignal,
928
+ createStore,
518
929
  getRenderer,
519
930
  h,
520
931
  jsx,
521
932
  jsxDEV,
522
933
  jsxs,
934
+ lazy,
523
935
  onCleanup,
524
936
  onMount,
525
937
  provide,
526
938
  render,
527
939
  setRenderer,
528
940
  untrack,
941
+ unwrap,
529
942
  useContext
530
943
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cajoa/core",
3
- "version": "0.13.0",
3
+ "version": "0.15.0",
4
4
  "description": "Cajoa core: signals, JSX runtime and the renderer interface.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",