@preact/signals 2.7.1 → 2.8.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/CHANGELOG.md +6 -0
- package/package.json +1 -1
- package/utils/dist/index.d.ts +1 -1
- package/utils/dist/utils.js +1 -1
- package/utils/dist/utils.js.map +1 -1
- package/utils/dist/utils.min.js +1 -1
- package/utils/dist/utils.min.js.map +1 -1
- package/utils/dist/utils.mjs +1 -1
- package/utils/dist/utils.mjs.map +1 -1
- package/utils/dist/utils.module.js +1 -1
- package/utils/dist/utils.module.js.map +1 -1
- package/utils/src/index.tsx +8 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @preact/signals
|
|
2
2
|
|
|
3
|
+
## 2.8.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#878](https://github.com/preactjs/signals/pull/878) [`4aa565b`](https://github.com/preactjs/signals/commit/4aa565b3b668100b9c7ce09805da67cab8e3f5b2) Thanks [@JoviDeCroock](https://github.com/JoviDeCroock)! - Support returning a plain array in the `when` of a `For` component
|
|
8
|
+
|
|
3
9
|
## 2.7.1
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
package/package.json
CHANGED
package/utils/dist/index.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export declare namespace Show {
|
|
|
10
10
|
var displayName: string;
|
|
11
11
|
}
|
|
12
12
|
interface ForProps<T> {
|
|
13
|
-
each: Signal<Array<T>> | ReadonlySignal<Array<T>> | (() => Signal<Array<T>> | ReadonlySignal<Array<T>>);
|
|
13
|
+
each: Signal<Array<T>> | ReadonlySignal<Array<T>> | (() => Array<T> | Signal<Array<T>> | ReadonlySignal<Array<T>>);
|
|
14
14
|
fallback?: ComponentChildren;
|
|
15
15
|
children: (value: T, index: number) => ComponentChildren;
|
|
16
16
|
}
|
package/utils/dist/utils.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var r=require("@preact/signals"),n=require("preact"),t=require("preact/hooks"),
|
|
1
|
+
var r=require("@preact/signals-core"),n=require("@preact/signals"),e=require("preact"),t=require("preact/hooks"),u=function(r){return"function"==typeof r.children?r.children(r.v,r.i):r.children};u.displayName="Item";function i(r){var n="function"==typeof r.when?r.when():r.when.value;if(!n)return r.fallback||null;else return e.createElement(u,{v:n,children:r.children})}i.displayName="Show";function o(n){var i=t.useMemo(function(){return new Map},[]),o="function"==typeof n.each?n.each():n.each,c=o instanceof r.Signal?o.value:o;if(!c.length)return n.fallback||null;var f=new Set(i.keys()),a=c.map(function(r,t){f.delete(r);if(!i.has(r)){var o=e.createElement(u,{v:r,i:t,children:n.children});i.set(r,o);return o}return i.get(r)});f.forEach(function(r){i.delete(r)});return e.createElement(e.Fragment,null,a)}o.displayName="For";var c={configurable:!0,get:function(){return this.value},set:function(r){this.value=r}};exports.For=o;exports.Show=i;exports.useLiveSignal=function(r){var e=n.useSignal(r);if(e.peek()!==r)e.value=r;return e};exports.useSignalRef=function(r){var e=n.useSignal(r);if(!("current"in e))Object.defineProperty(e,"current",c);return e};//# sourceMappingURL=utils.js.map
|
package/utils/dist/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sources":["../src/index.tsx"],"sourcesContent":["import { ReadonlySignal, Signal } from \"@preact/signals-core\";\nimport { useSignal } from \"@preact/signals\";\nimport { Fragment, createElement, ComponentChildren } from \"preact\";\nimport { useMemo } from \"preact/hooks\";\n\ninterface ShowProps<T = boolean> {\n\twhen: Signal<T> | ReadonlySignal<T> | (() => T);\n\tfallback?: ComponentChildren;\n\tchildren: ComponentChildren | ((value: NonNullable<T>) => ComponentChildren);\n}\n\nconst Item = (props: any) => {\n\treturn typeof props.children === \"function\"\n\t\t? props.children(props.v, props.i)\n\t\t: props.children;\n};\n\nItem.displayName = \"Item\";\n\nexport function Show<T = boolean>(\n\tprops: ShowProps<T>\n): ComponentChildren | null {\n\tconst value =\n\t\ttypeof props.when === \"function\" ? props.when() : props.when.value;\n\tif (!value) return props.fallback || null;\n\treturn <Item v={value} children={props.children} />;\n}\n\nShow.displayName = \"Show\";\n\ninterface ForProps<T> {\n\teach:\n\t\t| Signal<Array<T>>\n\t\t| ReadonlySignal<Array<T>>\n\t\t| (() => Signal<Array<T>> | ReadonlySignal<Array<T>>);\n\tfallback?: ComponentChildren;\n\tchildren: (value: T, index: number) => ComponentChildren;\n}\n\nexport function For<T>(props: ForProps<T>): ComponentChildren | null {\n\tconst cache = useMemo(() => new Map(), []);\n\
|
|
1
|
+
{"version":3,"file":"utils.js","sources":["../src/index.tsx"],"sourcesContent":["import { ReadonlySignal, Signal } from \"@preact/signals-core\";\nimport { useSignal } from \"@preact/signals\";\nimport { Fragment, createElement, ComponentChildren } from \"preact\";\nimport { useMemo } from \"preact/hooks\";\n\ninterface ShowProps<T = boolean> {\n\twhen: Signal<T> | ReadonlySignal<T> | (() => T);\n\tfallback?: ComponentChildren;\n\tchildren: ComponentChildren | ((value: NonNullable<T>) => ComponentChildren);\n}\n\nconst Item = (props: any) => {\n\treturn typeof props.children === \"function\"\n\t\t? props.children(props.v, props.i)\n\t\t: props.children;\n};\n\nItem.displayName = \"Item\";\n\nexport function Show<T = boolean>(\n\tprops: ShowProps<T>\n): ComponentChildren | null {\n\tconst value =\n\t\ttypeof props.when === \"function\" ? props.when() : props.when.value;\n\tif (!value) return props.fallback || null;\n\treturn <Item v={value} children={props.children} />;\n}\n\nShow.displayName = \"Show\";\n\ninterface ForProps<T> {\n\teach:\n\t\t| Signal<Array<T>>\n\t\t| ReadonlySignal<Array<T>>\n\t\t| (() => Array<T> | Signal<Array<T>> | ReadonlySignal<Array<T>>);\n\tfallback?: ComponentChildren;\n\tchildren: (value: T, index: number) => ComponentChildren;\n}\n\nexport function For<T>(props: ForProps<T>): ComponentChildren | null {\n\tconst cache = useMemo(() => new Map(), []);\n\tconst list = (\n\t\ttypeof props.each === \"function\" ? props.each() : props.each\n\t) as Signal<Array<T>> | Array<T>;\n\n\tconst listValue = list instanceof Signal ? list.value : list;\n\n\tif (!listValue.length) return props.fallback || null;\n\n\tconst removed = new Set(cache.keys());\n\n\tconst items = listValue.map((value, key) => {\n\t\tremoved.delete(value);\n\t\tif (!cache.has(value)) {\n\t\t\tconst result = <Item v={value} i={key} children={props.children} />;\n\t\t\tcache.set(value, result);\n\t\t\treturn result;\n\t\t}\n\t\treturn cache.get(value);\n\t});\n\n\tremoved.forEach(value => {\n\t\tcache.delete(value);\n\t});\n\n\treturn createElement(Fragment, null, items);\n}\n\nFor.displayName = \"For\";\n\nexport function useLiveSignal<T>(value: T): Signal<T> {\n\tconst s = useSignal(value);\n\tif (s.peek() !== value) s.value = value;\n\treturn s;\n}\n\nexport function useSignalRef<T>(value: T): Signal<T> & { current: T } {\n\tconst ref = useSignal(value) as Signal<T> & { current: T };\n\tif (!(\"current\" in ref))\n\t\tObject.defineProperty(ref, \"current\", refSignalProto);\n\treturn ref;\n}\nconst refSignalProto = {\n\tconfigurable: true,\n\tget(this: Signal) {\n\t\treturn this.value;\n\t},\n\tset(this: Signal, v: any) {\n\t\tthis.value = v;\n\t},\n};\n"],"names":["Item","props","children","v","i","displayName","Show","value","when","fallback","createElement","For","cache","useMemo","Map","list","each","listValue","Signal","length","removed","Set","keys","items","map","key","has","result","set","get","forEach","Fragment","refSignalProto","configurable","this","exports","useLiveSignal","s","useSignal","peek","useSignalRef","ref","Object","defineProperty"],"mappings":"iHAWMA,EAAO,SAACC,GACb,MAAiC,mBAAnBA,EAAMC,SACjBD,EAAMC,SAASD,EAAME,EAAGF,EAAMG,GAC9BH,EAAMC,QACV,EAEAF,EAAKK,YAAc,OAEH,SAAAC,EACfL,GAEA,IAAMM,EACiB,mBAAfN,EAAMO,KAAsBP,EAAMO,OAASP,EAAMO,KAAKD,MAC9D,IAAKA,EAAO,OAAON,EAAMQ,UAAY,UACrC,OAAOC,EAAAA,cAACV,EAAK,CAAAG,EAAGI,EAAOL,SAAUD,E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object"==typeof exports&&"undefined"!=typeof module?n(exports,require("@preact/signals-core"),require("@preact/signals"),require("preact"),require("preact/hooks")):"function"==typeof define&&define.amd?define(["exports","@preact/signals-core","@preact/signals","preact","preact/hooks"],n):n((e||self).preactSignalsutils={},e.preactSignalsCore,e.signals,e.preact,e.preactHooks)}(this,function(e,n,r,t,i){var o=function(e){return"function"==typeof e.children?e.children(e.v,e.i):e.children};o.displayName="Item";function u(e){var n="function"==typeof e.when?e.when():e.when.value;if(!n)return e.fallback||null;else return t.createElement(o,{v:n,children:e.children})}u.displayName="Show";function f(e){var r=i.useMemo(function(){return new Map},[]),u="function"==typeof e.each?e.each():e.each,f=u instanceof n.Signal?u.value:u;if(!f.length)return e.fallback||null;var c=new Set(r.keys()),a=f.map(function(n,i){c.delete(n);if(!r.has(n)){var u=t.createElement(o,{v:n,i:i,children:e.children});r.set(n,u);return u}return r.get(n)});c.forEach(function(e){r.delete(e)});return t.createElement(t.Fragment,null,a)}f.displayName="For";var c={configurable:!0,get:function(){return this.value},set:function(e){this.value=e}};e.For=f;e.Show=u;e.useLiveSignal=function(e){var n=r.useSignal(e);if(n.peek()!==e)n.value=e;return n};e.useSignalRef=function(e){var n=r.useSignal(e);if(!("current"in n))Object.defineProperty(n,"current",c);return n}});//# sourceMappingURL=utils.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.min.js","sources":["../src/index.tsx"],"sourcesContent":["import { ReadonlySignal, Signal } from \"@preact/signals-core\";\nimport { useSignal } from \"@preact/signals\";\nimport { Fragment, createElement, ComponentChildren } from \"preact\";\nimport { useMemo } from \"preact/hooks\";\n\ninterface ShowProps<T = boolean> {\n\twhen: Signal<T> | ReadonlySignal<T> | (() => T);\n\tfallback?: ComponentChildren;\n\tchildren: ComponentChildren | ((value: NonNullable<T>) => ComponentChildren);\n}\n\nconst Item = (props: any) => {\n\treturn typeof props.children === \"function\"\n\t\t? props.children(props.v, props.i)\n\t\t: props.children;\n};\n\nItem.displayName = \"Item\";\n\nexport function Show<T = boolean>(\n\tprops: ShowProps<T>\n): ComponentChildren | null {\n\tconst value =\n\t\ttypeof props.when === \"function\" ? props.when() : props.when.value;\n\tif (!value) return props.fallback || null;\n\treturn <Item v={value} children={props.children} />;\n}\n\nShow.displayName = \"Show\";\n\ninterface ForProps<T> {\n\teach:\n\t\t| Signal<Array<T>>\n\t\t| ReadonlySignal<Array<T>>\n\t\t| (() => Signal<Array<T>> | ReadonlySignal<Array<T>>);\n\tfallback?: ComponentChildren;\n\tchildren: (value: T, index: number) => ComponentChildren;\n}\n\nexport function For<T>(props: ForProps<T>): ComponentChildren | null {\n\tconst cache = useMemo(() => new Map(), []);\n\
|
|
1
|
+
{"version":3,"file":"utils.min.js","sources":["../src/index.tsx"],"sourcesContent":["import { ReadonlySignal, Signal } from \"@preact/signals-core\";\nimport { useSignal } from \"@preact/signals\";\nimport { Fragment, createElement, ComponentChildren } from \"preact\";\nimport { useMemo } from \"preact/hooks\";\n\ninterface ShowProps<T = boolean> {\n\twhen: Signal<T> | ReadonlySignal<T> | (() => T);\n\tfallback?: ComponentChildren;\n\tchildren: ComponentChildren | ((value: NonNullable<T>) => ComponentChildren);\n}\n\nconst Item = (props: any) => {\n\treturn typeof props.children === \"function\"\n\t\t? props.children(props.v, props.i)\n\t\t: props.children;\n};\n\nItem.displayName = \"Item\";\n\nexport function Show<T = boolean>(\n\tprops: ShowProps<T>\n): ComponentChildren | null {\n\tconst value =\n\t\ttypeof props.when === \"function\" ? props.when() : props.when.value;\n\tif (!value) return props.fallback || null;\n\treturn <Item v={value} children={props.children} />;\n}\n\nShow.displayName = \"Show\";\n\ninterface ForProps<T> {\n\teach:\n\t\t| Signal<Array<T>>\n\t\t| ReadonlySignal<Array<T>>\n\t\t| (() => Array<T> | Signal<Array<T>> | ReadonlySignal<Array<T>>);\n\tfallback?: ComponentChildren;\n\tchildren: (value: T, index: number) => ComponentChildren;\n}\n\nexport function For<T>(props: ForProps<T>): ComponentChildren | null {\n\tconst cache = useMemo(() => new Map(), []);\n\tconst list = (\n\t\ttypeof props.each === \"function\" ? props.each() : props.each\n\t) as Signal<Array<T>> | Array<T>;\n\n\tconst listValue = list instanceof Signal ? list.value : list;\n\n\tif (!listValue.length) return props.fallback || null;\n\n\tconst removed = new Set(cache.keys());\n\n\tconst items = listValue.map((value, key) => {\n\t\tremoved.delete(value);\n\t\tif (!cache.has(value)) {\n\t\t\tconst result = <Item v={value} i={key} children={props.children} />;\n\t\t\tcache.set(value, result);\n\t\t\treturn result;\n\t\t}\n\t\treturn cache.get(value);\n\t});\n\n\tremoved.forEach(value => {\n\t\tcache.delete(value);\n\t});\n\n\treturn createElement(Fragment, null, items);\n}\n\nFor.displayName = \"For\";\n\nexport function useLiveSignal<T>(value: T): Signal<T> {\n\tconst s = useSignal(value);\n\tif (s.peek() !== value) s.value = value;\n\treturn s;\n}\n\nexport function useSignalRef<T>(value: T): Signal<T> & { current: T } {\n\tconst ref = useSignal(value) as Signal<T> & { current: T };\n\tif (!(\"current\" in ref))\n\t\tObject.defineProperty(ref, \"current\", refSignalProto);\n\treturn ref;\n}\nconst refSignalProto = {\n\tconfigurable: true,\n\tget(this: Signal) {\n\t\treturn this.value;\n\t},\n\tset(this: Signal, v: any) {\n\t\tthis.value = v;\n\t},\n};\n"],"names":["Item","props","children","v","i","displayName","Show","value","when","fallback","createElement","For","cache","useMemo","Map","list","each","listValue","Signal","length","removed","Set","keys","items","map","key","has","result","set","get","forEach","Fragment","refSignalProto","configurable","this","exports","useLiveSignal","s","useSignal","peek","useSignalRef","ref","Object","defineProperty"],"mappings":"+cAWA,IAAMA,EAAO,SAACC,GACb,MAAiC,mBAAnBA,EAAMC,SACjBD,EAAMC,SAASD,EAAME,EAAGF,EAAMG,GAC9BH,EAAMC,QACV,EAEAF,EAAKK,YAAc,OAEH,SAAAC,EACfL,GAEA,IAAMM,EACiB,mBAAfN,EAAMO,KAAsBP,EAAMO,OAASP,EAAMO,KAAKD,MAC9D,IAAKA,EAAO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}
|
package/utils/dist/utils.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{useSignal}from"@preact/signals";import{createElement as t,Fragment as
|
|
1
|
+
import{Signal as n}from"@preact/signals-core";import{useSignal}from"@preact/signals";import{createElement as t,Fragment as r}from"preact";import{useMemo as o}from"preact/hooks";const e=n=>"function"==typeof n.children?n.children(n.v,n.i):n.children;e.displayName="Item";function c(n){const r="function"==typeof n.when?n.when():n.when.value;if(!r)return n.fallback||null;else return t(e,{v:r,children:n.children})}c.displayName="Show";function i(c){const i=o(()=>new Map,[]),u="function"==typeof c.each?c.each():c.each,f=u instanceof n?u.value:u;if(!f.length)return c.fallback||null;const s=new Set(i.keys()),p=f.map((n,r)=>{s.delete(n);if(!i.has(n)){const o=t(e,{v:n,i:r,children:c.children});i.set(n,o);return o}return i.get(n)});s.forEach(n=>{i.delete(n)});return t(r,null,p)}i.displayName="For";function u(n){const t=useSignal(n);if(t.peek()!==n)t.value=n;return t}function f(n){const t=useSignal(n);if(!("current"in t))Object.defineProperty(t,"current",s);return t}const s={configurable:!0,get(){return this.value},set(n){this.value=n}};export{i as For,c as Show,u as useLiveSignal,f as useSignalRef};//# sourceMappingURL=utils.mjs.map
|
package/utils/dist/utils.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.mjs","sources":["../src/index.tsx"],"sourcesContent":["import { ReadonlySignal, Signal } from \"@preact/signals-core\";\nimport { useSignal } from \"@preact/signals\";\nimport { Fragment, createElement, ComponentChildren } from \"preact\";\nimport { useMemo } from \"preact/hooks\";\n\ninterface ShowProps<T = boolean> {\n\twhen: Signal<T> | ReadonlySignal<T> | (() => T);\n\tfallback?: ComponentChildren;\n\tchildren: ComponentChildren | ((value: NonNullable<T>) => ComponentChildren);\n}\n\nconst Item = (props: any) => {\n\treturn typeof props.children === \"function\"\n\t\t? props.children(props.v, props.i)\n\t\t: props.children;\n};\n\nItem.displayName = \"Item\";\n\nexport function Show<T = boolean>(\n\tprops: ShowProps<T>\n): ComponentChildren | null {\n\tconst value =\n\t\ttypeof props.when === \"function\" ? props.when() : props.when.value;\n\tif (!value) return props.fallback || null;\n\treturn <Item v={value} children={props.children} />;\n}\n\nShow.displayName = \"Show\";\n\ninterface ForProps<T> {\n\teach:\n\t\t| Signal<Array<T>>\n\t\t| ReadonlySignal<Array<T>>\n\t\t| (() => Signal<Array<T>> | ReadonlySignal<Array<T>>);\n\tfallback?: ComponentChildren;\n\tchildren: (value: T, index: number) => ComponentChildren;\n}\n\nexport function For<T>(props: ForProps<T>): ComponentChildren | null {\n\tconst cache = useMemo(() => new Map(), []);\n\
|
|
1
|
+
{"version":3,"file":"utils.mjs","sources":["../src/index.tsx"],"sourcesContent":["import { ReadonlySignal, Signal } from \"@preact/signals-core\";\nimport { useSignal } from \"@preact/signals\";\nimport { Fragment, createElement, ComponentChildren } from \"preact\";\nimport { useMemo } from \"preact/hooks\";\n\ninterface ShowProps<T = boolean> {\n\twhen: Signal<T> | ReadonlySignal<T> | (() => T);\n\tfallback?: ComponentChildren;\n\tchildren: ComponentChildren | ((value: NonNullable<T>) => ComponentChildren);\n}\n\nconst Item = (props: any) => {\n\treturn typeof props.children === \"function\"\n\t\t? props.children(props.v, props.i)\n\t\t: props.children;\n};\n\nItem.displayName = \"Item\";\n\nexport function Show<T = boolean>(\n\tprops: ShowProps<T>\n): ComponentChildren | null {\n\tconst value =\n\t\ttypeof props.when === \"function\" ? props.when() : props.when.value;\n\tif (!value) return props.fallback || null;\n\treturn <Item v={value} children={props.children} />;\n}\n\nShow.displayName = \"Show\";\n\ninterface ForProps<T> {\n\teach:\n\t\t| Signal<Array<T>>\n\t\t| ReadonlySignal<Array<T>>\n\t\t| (() => Array<T> | Signal<Array<T>> | ReadonlySignal<Array<T>>);\n\tfallback?: ComponentChildren;\n\tchildren: (value: T, index: number) => ComponentChildren;\n}\n\nexport function For<T>(props: ForProps<T>): ComponentChildren | null {\n\tconst cache = useMemo(() => new Map(), []);\n\tconst list = (\n\t\ttypeof props.each === \"function\" ? props.each() : props.each\n\t) as Signal<Array<T>> | Array<T>;\n\n\tconst listValue = list instanceof Signal ? list.value : list;\n\n\tif (!listValue.length) return props.fallback || null;\n\n\tconst removed = new Set(cache.keys());\n\n\tconst items = listValue.map((value, key) => {\n\t\tremoved.delete(value);\n\t\tif (!cache.has(value)) {\n\t\t\tconst result = <Item v={value} i={key} children={props.children} />;\n\t\t\tcache.set(value, result);\n\t\t\treturn result;\n\t\t}\n\t\treturn cache.get(value);\n\t});\n\n\tremoved.forEach(value => {\n\t\tcache.delete(value);\n\t});\n\n\treturn createElement(Fragment, null, items);\n}\n\nFor.displayName = \"For\";\n\nexport function useLiveSignal<T>(value: T): Signal<T> {\n\tconst s = useSignal(value);\n\tif (s.peek() !== value) s.value = value;\n\treturn s;\n}\n\nexport function useSignalRef<T>(value: T): Signal<T> & { current: T } {\n\tconst ref = useSignal(value) as Signal<T> & { current: T };\n\tif (!(\"current\" in ref))\n\t\tObject.defineProperty(ref, \"current\", refSignalProto);\n\treturn ref;\n}\nconst refSignalProto = {\n\tconfigurable: true,\n\tget(this: Signal) {\n\t\treturn this.value;\n\t},\n\tset(this: Signal, v: any) {\n\t\tthis.value = v;\n\t},\n};\n"],"names":["Signal","useSignal","createElement","Fragment","useMemo","Item","props","children","v","i","displayName","Show","value","when","fallback","For","cache","Map","list","each","listValue","length","removed","Set","keys","items","map","key","delete","has","result","set","get","forEach","useLiveSignal","s","peek","useSignalRef","ref","Object","defineProperty","refSignalProto","configurable","this"],"mappings":"iBAWAA,MAAA,8BAAAC,cAAA,0CAAAC,cAAAC,MAAA,2BAAAC,MAAA,eAAA,MAAMC,EAAQC,GACoB,mBAAnBA,EAAMC,SACjBD,EAAMC,SAASD,EAAME,EAAGF,EAAMG,GAC9BH,EAAMC,SAGVF,EAAKK,YAAc,OAEH,SAAAC,EACfL,GAEA,MAAMM,EACiB,mBAAfN,EAAMO,KAAsBP,EAAMO,OAASP,EAAMO,KAAKD,MA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
|
|
1
|
+
import{Signal as n}from"@preact/signals-core";import{useSignal}from"@preact/signals";import{createElement as r,Fragment as t}from"preact";import{useMemo as e}from"preact/hooks";var i=function(n){return"function"==typeof n.children?n.children(n.v,n.i):n.children};i.displayName="Item";function o(n){var t="function"==typeof n.when?n.when():n.when.value;if(!t)return n.fallback||null;else return r(i,{v:t,children:n.children})}o.displayName="Show";function u(o){var u=e(function(){return new Map},[]),f="function"==typeof o.each?o.each():o.each,c=f instanceof n?f.value:f;if(!c.length)return o.fallback||null;var a=new Set(u.keys()),p=c.map(function(n,t){a.delete(n);if(!u.has(n)){var e=r(i,{v:n,i:t,children:o.children});u.set(n,e);return e}return u.get(n)});a.forEach(function(n){u.delete(n)});return r(t,null,p)}u.displayName="For";function f(n){var r=useSignal(n);if(r.peek()!==n)r.value=n;return r}function c(n){var r=useSignal(n);if(!("current"in r))Object.defineProperty(r,"current",a);return r}var a={configurable:!0,get:function(){return this.value},set:function(n){this.value=n}};export{u as For,o as Show,f as useLiveSignal,c as useSignalRef};//# sourceMappingURL=utils.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.module.js","sources":["../src/index.tsx"],"sourcesContent":["import { ReadonlySignal, Signal } from \"@preact/signals-core\";\nimport { useSignal } from \"@preact/signals\";\nimport { Fragment, createElement, ComponentChildren } from \"preact\";\nimport { useMemo } from \"preact/hooks\";\n\ninterface ShowProps<T = boolean> {\n\twhen: Signal<T> | ReadonlySignal<T> | (() => T);\n\tfallback?: ComponentChildren;\n\tchildren: ComponentChildren | ((value: NonNullable<T>) => ComponentChildren);\n}\n\nconst Item = (props: any) => {\n\treturn typeof props.children === \"function\"\n\t\t? props.children(props.v, props.i)\n\t\t: props.children;\n};\n\nItem.displayName = \"Item\";\n\nexport function Show<T = boolean>(\n\tprops: ShowProps<T>\n): ComponentChildren | null {\n\tconst value =\n\t\ttypeof props.when === \"function\" ? props.when() : props.when.value;\n\tif (!value) return props.fallback || null;\n\treturn <Item v={value} children={props.children} />;\n}\n\nShow.displayName = \"Show\";\n\ninterface ForProps<T> {\n\teach:\n\t\t| Signal<Array<T>>\n\t\t| ReadonlySignal<Array<T>>\n\t\t| (() => Signal<Array<T>> | ReadonlySignal<Array<T>>);\n\tfallback?: ComponentChildren;\n\tchildren: (value: T, index: number) => ComponentChildren;\n}\n\nexport function For<T>(props: ForProps<T>): ComponentChildren | null {\n\tconst cache = useMemo(() => new Map(), []);\n\
|
|
1
|
+
{"version":3,"file":"utils.module.js","sources":["../src/index.tsx"],"sourcesContent":["import { ReadonlySignal, Signal } from \"@preact/signals-core\";\nimport { useSignal } from \"@preact/signals\";\nimport { Fragment, createElement, ComponentChildren } from \"preact\";\nimport { useMemo } from \"preact/hooks\";\n\ninterface ShowProps<T = boolean> {\n\twhen: Signal<T> | ReadonlySignal<T> | (() => T);\n\tfallback?: ComponentChildren;\n\tchildren: ComponentChildren | ((value: NonNullable<T>) => ComponentChildren);\n}\n\nconst Item = (props: any) => {\n\treturn typeof props.children === \"function\"\n\t\t? props.children(props.v, props.i)\n\t\t: props.children;\n};\n\nItem.displayName = \"Item\";\n\nexport function Show<T = boolean>(\n\tprops: ShowProps<T>\n): ComponentChildren | null {\n\tconst value =\n\t\ttypeof props.when === \"function\" ? props.when() : props.when.value;\n\tif (!value) return props.fallback || null;\n\treturn <Item v={value} children={props.children} />;\n}\n\nShow.displayName = \"Show\";\n\ninterface ForProps<T> {\n\teach:\n\t\t| Signal<Array<T>>\n\t\t| ReadonlySignal<Array<T>>\n\t\t| (() => Array<T> | Signal<Array<T>> | ReadonlySignal<Array<T>>);\n\tfallback?: ComponentChildren;\n\tchildren: (value: T, index: number) => ComponentChildren;\n}\n\nexport function For<T>(props: ForProps<T>): ComponentChildren | null {\n\tconst cache = useMemo(() => new Map(), []);\n\tconst list = (\n\t\ttypeof props.each === \"function\" ? props.each() : props.each\n\t) as Signal<Array<T>> | Array<T>;\n\n\tconst listValue = list instanceof Signal ? list.value : list;\n\n\tif (!listValue.length) return props.fallback || null;\n\n\tconst removed = new Set(cache.keys());\n\n\tconst items = listValue.map((value, key) => {\n\t\tremoved.delete(value);\n\t\tif (!cache.has(value)) {\n\t\t\tconst result = <Item v={value} i={key} children={props.children} />;\n\t\t\tcache.set(value, result);\n\t\t\treturn result;\n\t\t}\n\t\treturn cache.get(value);\n\t});\n\n\tremoved.forEach(value => {\n\t\tcache.delete(value);\n\t});\n\n\treturn createElement(Fragment, null, items);\n}\n\nFor.displayName = \"For\";\n\nexport function useLiveSignal<T>(value: T): Signal<T> {\n\tconst s = useSignal(value);\n\tif (s.peek() !== value) s.value = value;\n\treturn s;\n}\n\nexport function useSignalRef<T>(value: T): Signal<T> & { current: T } {\n\tconst ref = useSignal(value) as Signal<T> & { current: T };\n\tif (!(\"current\" in ref))\n\t\tObject.defineProperty(ref, \"current\", refSignalProto);\n\treturn ref;\n}\nconst refSignalProto = {\n\tconfigurable: true,\n\tget(this: Signal) {\n\t\treturn this.value;\n\t},\n\tset(this: Signal, v: any) {\n\t\tthis.value = v;\n\t},\n};\n"],"names":["Item","props","children","v","i","displayName","Show","value","when","fallback","createElement","For","cache","useMemo","Map","list","each","listValue","Signal","length","removed","Set","keys","items","map","key","has","result","set","get","forEach","Fragment","useLiveSignal","s","useSignal","peek","useSignalRef","ref","Object","defineProperty","refSignalProto","configurable","this"],"mappings":"iLAWA,IAAMA,EAAO,SAACC,GACb,MAAiC,mBAAnBA,EAAMC,SACjBD,EAAMC,SAASD,EAAME,EAAGF,EAAMG,GAC9BH,EAAMC,QACV,EAEAF,EAAKK,YAAc,OAEH,SAAAC,EACfL,GAEA,IAAMM,EACiB,mBAAfN,EAAMO,KAAsBP,EAAMO,OAASP,EAAMO,KAAKD,MAC9D,IAAKA,EAAO,OAAON,EAAMQ,UAAY,UACrC,OAAOC,EAACV,EAAK,CAAAG,EAAGI,EAAOL,SAAUD,EAAMC,UACxC,CAEAI,EAAKD,YAAc,OAWb,SAAUM,EAAOV,GACtB,IAAMW,EAAQC,EAAQ,WAAM,OAAA,IAAIC,GAAK,EAAE,IACjCC,EACiB,mBAAfd,EAAMe,KAAsBf,EAAMe,OAASf,EAAMe,KAGnDC,EAAYF,aAAgBG,EAASH,EAAKR,MAAQQ,EAExD,IAAKE,EAAUE,OAAQ,OAAOlB,EAAMQ,UAAY,KAEhD,IAAMW,EAAU,IAAIC,IAAIT,EAAMU,QAExBC,EAAQN,EAAUO,IAAI,SAACjB,EAAOkB,GACnCL,SAAeb,GACf,IAAKK,EAAMc,IAAInB,GAAQ,CACtB,IAAMoB,EAASjB,EAACV,EAAK,CAAAG,EAAGI,EAAOH,EAAGqB,EAAKvB,SAAUD,EAAMC,WACvDU,EAAMgB,IAAIrB,EAAOoB,GACjB,OAAOA,CACR,CACA,OAAOf,EAAMiB,IAAItB,EAClB,GAEAa,EAAQU,QAAQ,SAAAvB,GACfK,EAAY,OAACL,EACd,GAEA,OAAOG,EAAcqB,EAAU,KAAMR,EACtC,CAEAZ,EAAIN,YAAc,eAEF2B,EAAiBzB,GAChC,IAAM0B,EAAIC,UAAU3B,GACpB,GAAI0B,EAAEE,SAAW5B,EAAO0B,EAAE1B,MAAQA,EAClC,OAAO0B,CACR,CAEgB,SAAAG,EAAgB7B,GAC/B,IAAM8B,EAAMH,UAAU3B,GACtB,KAAM,YAAa8B,GAClBC,OAAOC,eAAeF,EAAK,UAAWG,GACvC,OAAOH,CACR,CACA,IAAMG,EAAiB,CACtBC,cAAc,EACdZ,IAAA,WACC,OAAWa,KAACnC,KACb,EACAqB,aAAkBzB,GACjBuC,KAAKnC,MAAQJ,CACd,UACAQ,SAAAL,UAAA0B,mBAAAI"}
|
package/utils/src/index.tsx
CHANGED
|
@@ -32,24 +32,24 @@ interface ForProps<T> {
|
|
|
32
32
|
each:
|
|
33
33
|
| Signal<Array<T>>
|
|
34
34
|
| ReadonlySignal<Array<T>>
|
|
35
|
-
| (() => Signal<Array<T>> | ReadonlySignal<Array<T>>);
|
|
35
|
+
| (() => Array<T> | Signal<Array<T>> | ReadonlySignal<Array<T>>);
|
|
36
36
|
fallback?: ComponentChildren;
|
|
37
37
|
children: (value: T, index: number) => ComponentChildren;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
export function For<T>(props: ForProps<T>): ComponentChildren | null {
|
|
41
41
|
const cache = useMemo(() => new Map(), []);
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
>
|
|
46
|
-
).value;
|
|
42
|
+
const list = (
|
|
43
|
+
typeof props.each === "function" ? props.each() : props.each
|
|
44
|
+
) as Signal<Array<T>> | Array<T>;
|
|
47
45
|
|
|
48
|
-
|
|
46
|
+
const listValue = list instanceof Signal ? list.value : list;
|
|
47
|
+
|
|
48
|
+
if (!listValue.length) return props.fallback || null;
|
|
49
49
|
|
|
50
50
|
const removed = new Set(cache.keys());
|
|
51
51
|
|
|
52
|
-
const items =
|
|
52
|
+
const items = listValue.map((value, key) => {
|
|
53
53
|
removed.delete(value);
|
|
54
54
|
if (!cache.has(value)) {
|
|
55
55
|
const result = <Item v={value} i={key} children={props.children} />;
|