sinwan 1.1.0 → 1.1.2

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.
Files changed (43) hide show
  1. package/README.md +34 -3
  2. package/dist/cjs/index.development.js +357 -221
  3. package/dist/cjs/index.development.js.map +12 -11
  4. package/dist/cjs/index.production.min.js +2 -2
  5. package/dist/cjs/index.production.min.js.map +12 -11
  6. package/dist/cjs/renderer/index.development.js +300 -116
  7. package/dist/cjs/renderer/index.development.js.map +11 -10
  8. package/dist/cjs/renderer/index.production.min.js +2 -2
  9. package/dist/cjs/renderer/index.production.min.js.map +11 -10
  10. package/dist/cjs/server/index.development.js +146 -68
  11. package/dist/cjs/server/index.development.js.map +8 -7
  12. package/dist/cjs/server/index.production.min.js +2 -2
  13. package/dist/cjs/server/index.production.min.js.map +8 -7
  14. package/dist/component/control-flow.d.ts +6 -1
  15. package/dist/component/control-flow.d.ts.map +1 -1
  16. package/dist/component/instance.d.ts.map +1 -1
  17. package/dist/esm/index.development.js +357 -221
  18. package/dist/esm/index.development.js.map +12 -11
  19. package/dist/esm/index.production.min.js +2 -2
  20. package/dist/esm/index.production.min.js.map +12 -11
  21. package/dist/esm/renderer/index.development.js +300 -116
  22. package/dist/esm/renderer/index.development.js.map +11 -10
  23. package/dist/esm/renderer/index.production.min.js +2 -2
  24. package/dist/esm/renderer/index.production.min.js.map +11 -10
  25. package/dist/esm/server/index.development.js +146 -68
  26. package/dist/esm/server/index.development.js.map +8 -7
  27. package/dist/esm/server/index.production.min.js +2 -2
  28. package/dist/esm/server/index.production.min.js.map +8 -7
  29. package/dist/reactivity/index.d.ts +2 -1
  30. package/dist/reactivity/index.d.ts.map +1 -1
  31. package/dist/reactivity/normalization.d.ts +19 -0
  32. package/dist/reactivity/normalization.d.ts.map +1 -0
  33. package/dist/renderer/attributes.d.ts.map +1 -1
  34. package/dist/renderer/events.d.ts.map +1 -1
  35. package/dist/renderer/render-children.d.ts.map +1 -1
  36. package/dist/renderer/render-control-flow.d.ts.map +1 -1
  37. package/dist/renderer/render-element.d.ts.map +1 -1
  38. package/dist/renderer/types.d.ts +2 -0
  39. package/dist/renderer/types.d.ts.map +1 -1
  40. package/dist/server/renderer.d.ts.map +1 -1
  41. package/dist/types.d.ts +2 -2
  42. package/dist/types.d.ts.map +1 -1
  43. package/package.json +6 -3
@@ -436,7 +436,7 @@ function resetDOMOps() {
436
436
 
437
437
  // src/renderer/events.ts
438
438
  function isEventProp(key) {
439
- return key.length > 2 && key[0] === "o" && key[1] === "n" && key[2] >= "A" && key[2] <= "Z";
439
+ return key.length > 2 && key.startsWith("on");
440
440
  }
441
441
  function toEventName(key) {
442
442
  return key.slice(2).toLowerCase();
@@ -473,6 +473,19 @@ function batch(fn) {
473
473
  }
474
474
  }
475
475
  }
476
+ // src/reactivity/normalization.ts
477
+ function isReactive(value) {
478
+ return isSignal(value) || isComputed(value) || typeof value === "function";
479
+ }
480
+ function resolve(value) {
481
+ if (isSignal(value) || isComputed(value)) {
482
+ return value.value;
483
+ }
484
+ if (typeof value === "function") {
485
+ return value();
486
+ }
487
+ return value;
488
+ }
476
489
  // src/component/control-flow.ts
477
490
  var SHOW_TYPE = Symbol.for("Sinwan.Show");
478
491
  var FOR_TYPE = Symbol.for("Sinwan.For");
@@ -566,6 +579,9 @@ function Portal(props) {
566
579
  children: []
567
580
  };
568
581
  }
582
+ function isElementLike(value) {
583
+ return value != null && typeof value === "object" && "tag" in value;
584
+ }
569
585
  function isShowElement(element) {
570
586
  return element.tag === SHOW_TYPE;
571
587
  }
@@ -590,6 +606,107 @@ function isDynamicElement(element) {
590
606
  function isPortalElement(element) {
591
607
  return element.tag === PORTAL_TYPE;
592
608
  }
609
+ function resolveSwitchContent(element) {
610
+ const props = element.props;
611
+ const children = normalizeContent(props.children ?? element.children);
612
+ const match = findTruthyMatch(children);
613
+ return match !== undefined ? match : props.fallback;
614
+ }
615
+ function findTruthyMatch(nodes) {
616
+ for (const node of nodes) {
617
+ if (node == null || typeof node === "boolean")
618
+ continue;
619
+ if (Array.isArray(node)) {
620
+ const match = findTruthyMatch(node);
621
+ if (match !== undefined)
622
+ return match;
623
+ continue;
624
+ }
625
+ if (isElementLike(node)) {
626
+ let element = node;
627
+ if (typeof element.tag === "function") {
628
+ const tag = element.tag;
629
+ if (tag === Match || tag === Show || tag === For || tag === Index || tag === Key || tag === Switch) {
630
+ element = tag(element.props);
631
+ }
632
+ }
633
+ if (isMatchElement(element)) {
634
+ const when = readReactive(element.props.when);
635
+ if (when) {
636
+ return resolveMatchChildren(element, when);
637
+ }
638
+ } else if (isShowElement(element)) {
639
+ const when = readReactive(element.props.when);
640
+ if (when) {
641
+ const content = resolveShowChildren(element, when);
642
+ const match = findTruthyMatch(normalizeContent(content));
643
+ if (match !== undefined)
644
+ return match;
645
+ } else if (element.props.fallback) {
646
+ const match = findTruthyMatch(normalizeContent(element.props.fallback));
647
+ if (match !== undefined)
648
+ return match;
649
+ }
650
+ } else if (isForElement(element)) {
651
+ const props = element.props;
652
+ const items = readReactive(props.each);
653
+ if (Array.isArray(items)) {
654
+ for (let i = 0;i < items.length; i++) {
655
+ const child = props.children(items[i], () => i);
656
+ const match = findTruthyMatch(normalizeContent(child));
657
+ if (match !== undefined)
658
+ return match;
659
+ }
660
+ }
661
+ } else if (isIndexElement(element)) {
662
+ const props = element.props;
663
+ const items = readReactive(props.each);
664
+ if (Array.isArray(items)) {
665
+ for (let i = 0;i < items.length; i++) {
666
+ const child = props.children(() => items[i], i);
667
+ const match = findTruthyMatch(normalizeContent(child));
668
+ if (match !== undefined)
669
+ return match;
670
+ }
671
+ }
672
+ } else if (isKeyElement(element)) {
673
+ const key = readReactive(element.props.when);
674
+ const child = resolveKeyChildren(element, key);
675
+ const match = findTruthyMatch(normalizeContent(child));
676
+ if (match !== undefined)
677
+ return match;
678
+ }
679
+ }
680
+ }
681
+ return;
682
+ }
683
+ function resolveMatchChildren(element, value) {
684
+ const children = element.props.children ?? element.children;
685
+ if (typeof children === "function") {
686
+ return children(value);
687
+ }
688
+ return children;
689
+ }
690
+ function resolveShowChildren(element, value) {
691
+ const children = element.props.children ?? element.children;
692
+ if (typeof children === "function") {
693
+ return children(value);
694
+ }
695
+ return children;
696
+ }
697
+ function resolveKeyChildren(element, value) {
698
+ const children = element.props.children ?? element.children;
699
+ if (typeof children === "function") {
700
+ return children(value);
701
+ }
702
+ return children;
703
+ }
704
+ function normalizeContent(content) {
705
+ if (content == null || typeof content === "boolean") {
706
+ return [];
707
+ }
708
+ return Array.isArray(content) ? content : [content];
709
+ }
593
710
  function normalizeChildren2(children) {
594
711
  if (children == null || typeof children === "boolean") {
595
712
  return [];
@@ -597,7 +714,7 @@ function normalizeChildren2(children) {
597
714
  return Array.isArray(children) ? children : [children];
598
715
  }
599
716
  function readReactive(value) {
600
- return isSignal(value) || isComputed(value) ? value.value : value;
717
+ return resolve(value);
601
718
  }
602
719
  function appendHiddenDisplay(style) {
603
720
  const trimmed = style.trim();
@@ -767,75 +884,27 @@ async function renderIndexElement(element) {
767
884
  const rendered = await Promise.all(each.map((item, index) => renderToString(props.children(() => item, index))));
768
885
  return rendered.join("");
769
886
  }
770
- function resolveSwitchContent(element) {
771
- const props = element.props;
772
- const children = normalizeContent(props.children ?? element.children);
773
- for (const child of children) {
774
- const match = getMatchElement(child);
775
- if (!match) {
776
- continue;
777
- }
778
- const when = readReactive2(match.props.when);
779
- if (when) {
780
- return resolveMatchChildren(match, when);
781
- }
782
- }
783
- return props.fallback;
784
- }
785
- function resolveMatchChildren(element, value) {
786
- const children = element.props.children ?? element.children;
787
- if (typeof children === "function") {
788
- return children(value);
789
- }
790
- return children;
791
- }
792
- function resolveKeyChildren(element, value) {
793
- const children = element.props.children ?? element.children;
794
- if (typeof children === "function") {
795
- return children(value);
796
- }
797
- return children;
798
- }
799
887
  function createDynamicElement(element, tag) {
800
888
  if (typeof tag !== "string" && typeof tag !== "function") {
801
889
  return null;
802
890
  }
803
891
  const { component, ...props } = element.props;
804
- const children = normalizeContent(props.children ?? element.children);
892
+ const children = normalizeContent2(props.children ?? element.children);
805
893
  return {
806
894
  tag,
807
895
  props,
808
896
  children
809
897
  };
810
898
  }
811
- function resolveShowChildren(element, value) {
812
- const children = element.props.children ?? element.children;
813
- if (typeof children === "function") {
814
- return children(value);
815
- }
816
- return children;
817
- }
818
899
  function readReactive2(value) {
819
900
  return isSignal(value) || isComputed(value) ? value.value : value;
820
901
  }
821
- function normalizeContent(content) {
902
+ function normalizeContent2(content) {
822
903
  if (content == null || typeof content === "boolean") {
823
904
  return [];
824
905
  }
825
906
  return Array.isArray(content) ? content : [content];
826
907
  }
827
- function isElementLike(value) {
828
- return value != null && typeof value === "object" && "tag" in value;
829
- }
830
- function getMatchElement(value) {
831
- if (!isElementLike(value)) {
832
- return null;
833
- }
834
- if (isMatchElement(value)) {
835
- return value;
836
- }
837
- return value.tag === Match ? Match(value.props) : null;
838
- }
839
908
  // src/component/instance.ts
840
909
  var uidCounter = 0;
841
910
  function createComponentInstance(component, props, parent) {
@@ -888,19 +957,28 @@ function fireMountedHooks(instance) {
888
957
  }
889
958
  }
890
959
  function fireUnmountedHooks(instance) {
891
- for (const child of instance.children) {
960
+ const children = [...instance.children];
961
+ for (const child of children) {
892
962
  fireUnmountedHooks(child);
893
963
  }
894
- if (instance.isMounted && !instance.isUnmounted) {
964
+ if (!instance.isUnmounted) {
895
965
  instance.isUnmounted = true;
896
- instance.isMounted = false;
897
- for (const hook of instance._unmountedHooks) {
898
- hook();
966
+ if (instance.isMounted) {
967
+ instance.isMounted = false;
968
+ for (const hook of instance._unmountedHooks) {
969
+ hook();
970
+ }
899
971
  }
900
972
  for (const dispose of instance.effects) {
901
973
  dispose();
902
974
  }
903
975
  instance.effects.length = 0;
976
+ if (instance.parent) {
977
+ const idx = instance.parent.children.indexOf(instance);
978
+ if (idx !== -1) {
979
+ instance.parent.children.splice(idx, 1);
980
+ }
981
+ }
904
982
  }
905
983
  }
906
984
  function fireUpdatedHooks(instance) {
@@ -1385,9 +1463,9 @@ function resolveShowChildren2(element, value) {
1385
1463
  }
1386
1464
  function resolveSwitchContent2(element) {
1387
1465
  const props = element.props;
1388
- const children = normalizeContent2(props.children ?? element.children);
1466
+ const children = normalizeContent3(props.children ?? element.children);
1389
1467
  for (const child of children) {
1390
- const match = getMatchElement2(child);
1468
+ const match = getMatchElement(child);
1391
1469
  if (!match) {
1392
1470
  continue;
1393
1471
  }
@@ -1417,7 +1495,7 @@ function createDynamicElement2(element, tag) {
1417
1495
  return null;
1418
1496
  }
1419
1497
  const { component, ...props } = element.props;
1420
- const children = normalizeContent2(props.children ?? element.children);
1498
+ const children = normalizeContent3(props.children ?? element.children);
1421
1499
  return {
1422
1500
  tag,
1423
1501
  props,
@@ -1427,7 +1505,7 @@ function createDynamicElement2(element, tag) {
1427
1505
  function readReactive3(value) {
1428
1506
  return isSignal(value) || isComputed(value) ? value.value : value;
1429
1507
  }
1430
- function normalizeContent2(content) {
1508
+ function normalizeContent3(content) {
1431
1509
  if (content == null || typeof content === "boolean") {
1432
1510
  return [];
1433
1511
  }
@@ -1436,7 +1514,7 @@ function normalizeContent2(content) {
1436
1514
  function isElementLike2(value) {
1437
1515
  return value != null && typeof value === "object" && "tag" in value;
1438
1516
  }
1439
- function getMatchElement2(value) {
1517
+ function getMatchElement(value) {
1440
1518
  if (!isElementLike2(value)) {
1441
1519
  return null;
1442
1520
  }
@@ -1648,9 +1726,9 @@ function resolveShowChildren3(element, value) {
1648
1726
  }
1649
1727
  function resolveSwitchContent3(element) {
1650
1728
  const props = element.props;
1651
- const children = normalizeContent3(props.children ?? element.children);
1729
+ const children = normalizeContent4(props.children ?? element.children);
1652
1730
  for (const child of children) {
1653
- const match = getMatchElement3(child);
1731
+ const match = getMatchElement2(child);
1654
1732
  if (!match) {
1655
1733
  continue;
1656
1734
  }
@@ -1680,7 +1758,7 @@ function createDynamicElement3(element, tag) {
1680
1758
  return null;
1681
1759
  }
1682
1760
  const { component, ...props } = element.props;
1683
- const children = normalizeContent3(props.children ?? element.children);
1761
+ const children = normalizeContent4(props.children ?? element.children);
1684
1762
  return {
1685
1763
  tag,
1686
1764
  props,
@@ -1690,7 +1768,7 @@ function createDynamicElement3(element, tag) {
1690
1768
  function readReactive4(value) {
1691
1769
  return isSignal(value) || isComputed(value) ? value.value : value;
1692
1770
  }
1693
- function normalizeContent3(content) {
1771
+ function normalizeContent4(content) {
1694
1772
  if (content == null || typeof content === "boolean") {
1695
1773
  return [];
1696
1774
  }
@@ -1699,7 +1777,7 @@ function normalizeContent3(content) {
1699
1777
  function isElementLike3(value) {
1700
1778
  return value != null && typeof value === "object" && "tag" in value;
1701
1779
  }
1702
- function getMatchElement3(value) {
1780
+ function getMatchElement2(value) {
1703
1781
  if (!isElementLike3(value)) {
1704
1782
  return null;
1705
1783
  }
@@ -1722,5 +1800,5 @@ export {
1722
1800
  getPage
1723
1801
  };
1724
1802
 
1725
- //# debugId=46896ED83A2048E464756E2164756E21
1803
+ //# debugId=ED2C426FA5DC589D64756E2164756E21
1726
1804
  //# sourceMappingURL=index.development.js.map