essor 0.0.6-beta.10 → 0.0.6-beta.13

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/essor.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as csstype from 'csstype';
2
2
 
3
- declare const __essor_version = "0.0.6-beta.9";
3
+ declare const __essor_version = "0.0.6-beta.12";
4
4
 
5
5
  type EffectFn = () => void;
6
6
  declare class Signal$1<T> {
@@ -28,6 +28,7 @@ declare class Computed<T> {
28
28
  declare function useComputed<T>(fn: () => T): Computed<T>;
29
29
  declare function isComputed<T>(value: any): value is Computed<T>;
30
30
  declare function useEffect(fn: EffectFn): () => void;
31
+ type ExcludeType = ((key: string | symbol) => boolean) | (string | symbol)[];
31
32
  type SignalObject<T> = {
32
33
  [K in keyof T]: Signal$1<T[K]> | T[K];
33
34
  };
@@ -38,7 +39,7 @@ type SignalObject<T> = {
38
39
  * @param {(key: string) => boolean | string[]} exclude - A function that determines which keys to exclude from the SignalObject.
39
40
  * @return {SignalObject<T>} The created SignalObject.
40
41
  */
41
- declare function signalObject<T extends object>(initialValues: T, exclude?: ((key: string) => boolean) | string[]): SignalObject<T>;
42
+ declare function signalObject<T extends object>(initialValues: T, exclude?: ExcludeType): SignalObject<T>;
42
43
  /**
43
44
  * Returns the current value of a signal, signal object, or plain object, excluding specified keys.
44
45
  *
@@ -46,10 +47,10 @@ declare function signalObject<T extends object>(initialValues: T, exclude?: ((ke
46
47
  * @param {(key: string) => boolean | string[]} [exclude] - A function that determines which keys to exclude from the unwrapped object.
47
48
  * @return {T} The unwrapped value of the signal, signal object, or plain object.
48
49
  */
49
- declare function unSignal<T>(signal: SignalObject<T> | T | Signal$1<T>, exclude?: ((key: string) => boolean) | string[]): T;
50
+ declare function unSignal<T>(signal: SignalObject<T> | T | Signal$1<T>, exclude?: ExcludeType): T;
50
51
  declare function isReactive(obj: any): any;
51
52
  declare function unReactive(obj: any): any;
52
- declare function useReactive<T extends object>(initialValue: T): T;
53
+ declare function useReactive<T extends object>(initialValue: T, exclude?: ExcludeType): T;
53
54
 
54
55
  type WatchSource<T = any> = Signal$1<T> | Computed<T> | (() => T);
55
56
  type WatchCallback<V = any, OV = any> = (value: V, oldValue: OV) => any;
@@ -2353,7 +2354,7 @@ declare class ComponentNode$1 implements JSX.Element {
2353
2354
  patchProps(props: Record<string, any>): void;
2354
2355
  }
2355
2356
 
2356
- declare function h(template: EssorComponent | HTMLTemplateElement, props: Record<string, any>, key?: string): JSX.Element;
2357
+ declare function h<K extends keyof HTMLElementTagNameMap>(_template: EssorComponent | HTMLTemplateElement | K, props: Record<string, any>, key?: string): JSX.Element;
2357
2358
  declare function isJsxElement(node: unknown): node is EssorNode;
2358
2359
  declare function template(html: string): HTMLTemplateElement;
2359
2360
  declare function Fragment(props: {
@@ -18,7 +18,7 @@ var __spreadValues = (a, b) => {
18
18
  };
19
19
 
20
20
  // src/version.ts
21
- var __essor_version = "0.0.6-beta.9";
21
+ var __essor_version = "0.0.6-beta.12";
22
22
 
23
23
  // ../shared/dist/essor-shared.js
24
24
  var isObject = (val) => val !== null && typeof val === "object";
@@ -37,9 +37,7 @@ var isPrimitive = (val) => ["string", "number", "boolean", "symbol", "undefined"
37
37
  function coerceArray(data) {
38
38
  return Array.isArray(data) ? data.flat() : [data];
39
39
  }
40
- function hasChanged(value, oldValue) {
41
- return !Object.is(value, oldValue);
42
- }
40
+ var hasChanged = (value, oldValue) => value !== oldValue && (value === value || oldValue === oldValue);
43
41
  var noop = Function.prototype;
44
42
  function startsWith(str, searchString) {
45
43
  return str.indexOf(searchString) === 0;
@@ -212,7 +210,7 @@ var Signal = class {
212
210
  return this._value;
213
211
  }
214
212
  set value(newValue) {
215
- if (this._value !== newValue) {
213
+ if (hasChanged(newValue, this._value)) {
216
214
  this._value = newValue;
217
215
  trigger(this, "value");
218
216
  }
@@ -247,7 +245,7 @@ var Computed = class {
247
245
  }
248
246
  run() {
249
247
  const newValue = this.fn();
250
- if (newValue !== this._value) {
248
+ if (hasChanged(newValue, this._value)) {
251
249
  this._value = newValue;
252
250
  this._deps.forEach((effect) => effect());
253
251
  }
@@ -280,12 +278,12 @@ function useEffect(fn) {
280
278
  activeEffect = null;
281
279
  };
282
280
  }
283
- function shouldExclude(key, exclude) {
281
+ function isExclude(key, exclude) {
284
282
  return Array.isArray(exclude) ? exclude.includes(key) : isFunction(exclude) ? exclude(key) : false;
285
283
  }
286
284
  function signalObject(initialValues, exclude) {
287
285
  const signals = Object.entries(initialValues).reduce((acc, [key, value]) => {
288
- acc[key] = shouldExclude(key, exclude) || isSignal(value) ? value : useSignal(value);
286
+ acc[key] = isExclude(key, exclude) || isSignal(value) ? value : useSignal(value);
289
287
  return acc;
290
288
  }, {});
291
289
  return signals;
@@ -300,7 +298,7 @@ function unSignal(signal, exclude) {
300
298
  }
301
299
  if (isObject(signal)) {
302
300
  return Object.entries(signal).reduce((acc, [key, value]) => {
303
- if (shouldExclude(key, exclude)) {
301
+ if (isExclude(key, exclude)) {
304
302
  acc[key] = value;
305
303
  } else {
306
304
  acc[key] = isSignal(value) ? value.peek() : value;
@@ -321,7 +319,7 @@ function unReactive(obj) {
321
319
  return Object.assign({}, obj);
322
320
  }
323
321
  var reactiveMap = /* @__PURE__ */ new WeakMap();
324
- function useReactive(initialValue) {
322
+ function useReactive(initialValue, exclude) {
325
323
  if (!isObject(initialValue)) {
326
324
  return initialValue;
327
325
  }
@@ -336,6 +334,9 @@ function useReactive(initialValue) {
336
334
  if (key === REACTIVE_MARKER) return true;
337
335
  const getValue = Reflect.get(target, key, receiver);
338
336
  const value = isSignal(getValue) ? getValue.value : getValue;
337
+ if (isExclude(key, exclude)) {
338
+ return value;
339
+ }
339
340
  track(target, key);
340
341
  if (isObject(value)) {
341
342
  return useReactive(value);
@@ -343,6 +344,10 @@ function useReactive(initialValue) {
343
344
  return value;
344
345
  },
345
346
  set(target, key, value, receiver) {
347
+ if (isExclude(key, exclude)) {
348
+ Reflect.set(target, key, value, receiver);
349
+ return true;
350
+ }
346
351
  let oldValue = Reflect.get(target, key, receiver);
347
352
  if (isSignal(oldValue)) {
348
353
  oldValue = oldValue.value;
@@ -569,7 +574,7 @@ var _ComponentNode = class _ComponentNode {
569
574
  return (_b = (_a = this.rootNode) == null ? void 0 : _a.mount(parent, before)) != null ? _b : [];
570
575
  }
571
576
  _ComponentNode.ref = this;
572
- this.rootNode = this.template(useReactive(this.proxyProps));
577
+ this.rootNode = this.template(useReactive(this.proxyProps, ["children"]));
573
578
  _ComponentNode.ref = null;
574
579
  this.mounted = true;
575
580
  const mountedNode = (_d = (_c = this.rootNode) == null ? void 0 : _c.mount(parent, before)) != null ? _d : [];
@@ -609,7 +614,7 @@ var _ComponentNode = class _ComponentNode {
609
614
  const newValue = (_e = (_d = this.proxyProps)[key]) != null ? _e : _d[key] = useSignal(prop);
610
615
  const track2 = this.getNodeTrack(key);
611
616
  track2.cleanup = useEffect(() => {
612
- newValue.value = prop;
617
+ newValue.value = isFunction(prop) ? prop() : prop;
613
618
  });
614
619
  }
615
620
  }
@@ -621,8 +626,14 @@ _ComponentNode.context = {};
621
626
  var ComponentNode = _ComponentNode;
622
627
 
623
628
  // src/template/template.ts
624
- function h(template2, props, key) {
625
- return isFunction(template2) ? new ComponentNode(template2, props, key) : new TemplateNode(template2, props, key);
629
+ function h(_template, props, key) {
630
+ if (isHtmlTagName(_template)) {
631
+ _template = template(convertToHtmlTag(_template));
632
+ props = {
633
+ 1: props
634
+ };
635
+ }
636
+ return isFunction(_template) ? new ComponentNode(_template, props, key) : new TemplateNode(_template, props, key);
626
637
  }
627
638
  function isJsxElement(node) {
628
639
  return node instanceof ComponentNode || node instanceof TemplateNode;
@@ -753,6 +764,173 @@ function addEventListener(node, eventName, handler) {
753
764
  node.addEventListener(eventName, handler);
754
765
  return () => node.removeEventListener(eventName, handler);
755
766
  }
767
+ var selfClosingTags = [
768
+ "area",
769
+ "base",
770
+ "br",
771
+ "col",
772
+ "embed",
773
+ "hr",
774
+ "img",
775
+ "input",
776
+ "link",
777
+ "meta",
778
+ "param",
779
+ "source",
780
+ "track",
781
+ "wbr"
782
+ ];
783
+ function convertToHtmlTag(tag) {
784
+ if (selfClosingTags.includes(tag)) {
785
+ return `<${tag}/>`;
786
+ } else {
787
+ return `<${tag}></${tag}>`;
788
+ }
789
+ }
790
+ var htmlTags = [
791
+ "a",
792
+ "abbr",
793
+ "acronym",
794
+ "address",
795
+ "applet",
796
+ "area",
797
+ "article",
798
+ "aside",
799
+ "audio",
800
+ "b",
801
+ "base",
802
+ "basefont",
803
+ "bdi",
804
+ "bdo",
805
+ "bgsound",
806
+ "big",
807
+ "blink",
808
+ "blockquote",
809
+ "body",
810
+ "br",
811
+ "button",
812
+ "canvas",
813
+ "caption",
814
+ "center",
815
+ "cite",
816
+ "code",
817
+ "col",
818
+ "colgroup",
819
+ "command",
820
+ "content",
821
+ "data",
822
+ "datalist",
823
+ "dd",
824
+ "del",
825
+ "details",
826
+ "dfn",
827
+ "dialog",
828
+ "dir",
829
+ "div",
830
+ "dl",
831
+ "dt",
832
+ "em",
833
+ "embed",
834
+ "fieldset",
835
+ "figcaption",
836
+ "figure",
837
+ "font",
838
+ "footer",
839
+ "form",
840
+ "frame",
841
+ "frameset",
842
+ "h1",
843
+ "h2",
844
+ "h3",
845
+ "h4",
846
+ "h5",
847
+ "h6",
848
+ "head",
849
+ "header",
850
+ "hgroup",
851
+ "hr",
852
+ "html",
853
+ "i",
854
+ "iframe",
855
+ "image",
856
+ "img",
857
+ "input",
858
+ "ins",
859
+ "kbd",
860
+ "keygen",
861
+ "label",
862
+ "legend",
863
+ "li",
864
+ "link",
865
+ "listing",
866
+ "main",
867
+ "map",
868
+ "mark",
869
+ "marquee",
870
+ "menu",
871
+ "menuitem",
872
+ "meta",
873
+ "meter",
874
+ "nav",
875
+ "nobr",
876
+ "noframes",
877
+ "noscript",
878
+ "object",
879
+ "ol",
880
+ "optgroup",
881
+ "option",
882
+ "output",
883
+ "p",
884
+ "param",
885
+ "picture",
886
+ "plaintext",
887
+ "pre",
888
+ "progress",
889
+ "q",
890
+ "rb",
891
+ "rp",
892
+ "rt",
893
+ "rtc",
894
+ "ruby",
895
+ "s",
896
+ "samp",
897
+ "script",
898
+ "section",
899
+ "select",
900
+ "shadow",
901
+ "small",
902
+ "source",
903
+ "spacer",
904
+ "span",
905
+ "strike",
906
+ "strong",
907
+ "style",
908
+ "sub",
909
+ "summary",
910
+ "sup",
911
+ "table",
912
+ "tbody",
913
+ "td",
914
+ "template",
915
+ "textarea",
916
+ "tfoot",
917
+ "th",
918
+ "thead",
919
+ "time",
920
+ "title",
921
+ "tr",
922
+ "track",
923
+ "tt",
924
+ "u",
925
+ "ul",
926
+ "var",
927
+ "video",
928
+ "wbr",
929
+ "xmp"
930
+ ];
931
+ function isHtmlTagName(str) {
932
+ return htmlTags.includes(str);
933
+ }
756
934
 
757
935
  // src/template/patch.ts
758
936
  function patchChildren(parent, childrenMap, nextChildren, before) {
@@ -977,11 +1155,8 @@ var TemplateNode = class _TemplateNode {
977
1155
  const track2 = this.getNodeTrack(trackKey, true, isRoot);
978
1156
  patchChild(track2, node, props.children, null);
979
1157
  } else {
980
- props.children.forEach((item, index) => {
1158
+ props.children.filter(Boolean).forEach((item, index) => {
981
1159
  var _a;
982
- if (!item) {
983
- return;
984
- }
985
1160
  const [child, path] = isArray(item) ? item : [item, null];
986
1161
  const before = isNil(path) ? null : (_a = this.treeMap.get(path)) != null ? _a : null;
987
1162
  const trackKey = `${key}:${attr}:${index}`;
@@ -1004,10 +1179,7 @@ var TemplateNode = class _TemplateNode {
1004
1179
  const track2 = this.getNodeTrack(`${key}:${attr}`);
1005
1180
  const val = props[attr];
1006
1181
  const triggerValue = isSignal(val) ? val : useSignal(val);
1007
- const cleanup = useEffect(() => {
1008
- triggerValue.value = isSignal(val) ? val.value : val;
1009
- patchAttribute(track2, node, attr, triggerValue.value);
1010
- });
1182
+ patchAttribute(track2, node, attr, triggerValue.value);
1011
1183
  let cleanupBind;
1012
1184
  const updateKey = `update${capitalizeFirstLetter(attr)}`;
1013
1185
  if (props[updateKey]) {
@@ -1016,7 +1188,6 @@ var TemplateNode = class _TemplateNode {
1016
1188
  });
1017
1189
  }
1018
1190
  track2.cleanup = () => {
1019
- cleanup && cleanup();
1020
1191
  cleanupBind && cleanupBind();
1021
1192
  };
1022
1193
  }
@@ -1064,7 +1235,7 @@ function onDestroy(cb) {
1064
1235
  }
1065
1236
  function throwIfOutsideComponent(hook) {
1066
1237
  if (!ComponentNode.ref) {
1067
- throw new Error(
1238
+ console.error(
1068
1239
  `"${hook}" can only be called within the component function body
1069
1240
  and cannot be used in asynchronous or deferred calls.`
1070
1241
  );