sinho 0.3.7 → 0.3.8
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/array_mutation.js +51 -30
- package/dist/array_mutation.js.map +1 -1
- package/dist/bundle.js +51 -30
- package/dist/bundle.min.js +1 -1
- package/package.json +1 -1
- package/src/array_mutation.ts +74 -44
package/dist/array_mutation.js
CHANGED
|
@@ -1,50 +1,71 @@
|
|
|
1
1
|
import { useEffect, useSignal } from "./scope.js";
|
|
2
|
-
const getIndexMap = (array, keyFn) => {
|
|
3
|
-
const
|
|
2
|
+
const getIndexMap = (array, keyFn, oldIndexMap) => {
|
|
3
|
+
const _indexMap = new Map();
|
|
4
4
|
for (let i = 0; i < array.length; i++) {
|
|
5
5
|
const key = keyFn(array[i], i);
|
|
6
|
-
if (
|
|
6
|
+
if (_indexMap.has(key)) {
|
|
7
7
|
throw new Error(`Duplicate key '${key}'`);
|
|
8
8
|
}
|
|
9
|
-
|
|
9
|
+
_indexMap.set(key, i);
|
|
10
10
|
}
|
|
11
|
-
|
|
11
|
+
const _removedFromOld = [];
|
|
12
|
+
let removedBefore = 0;
|
|
13
|
+
for (const [key, i] of oldIndexMap) {
|
|
14
|
+
if (!_indexMap.has(key)) {
|
|
15
|
+
_removedFromOld.push({
|
|
16
|
+
_key: key,
|
|
17
|
+
_oldIndex: i,
|
|
18
|
+
_removedBefore: removedBefore++,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return { _indexMap, _removedFromOld };
|
|
12
23
|
};
|
|
13
24
|
export const useArrayMutation = (array, keyFn) => {
|
|
14
25
|
const [result, setResult] = useSignal({
|
|
15
26
|
_mutations: [],
|
|
16
27
|
_map: new Map(),
|
|
17
|
-
}
|
|
28
|
+
});
|
|
18
29
|
let indexMap = new Map();
|
|
19
30
|
useEffect(() => {
|
|
20
31
|
const mutations = [];
|
|
21
32
|
const oldIndexMap = indexMap;
|
|
22
|
-
const newIndexMap = getIndexMap(array(), keyFn);
|
|
23
|
-
const transformToOldIndex = (i = NaN) =>
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
33
|
+
const { _indexMap: newIndexMap, _removedFromOld } = getIndexMap(array(), keyFn, oldIndexMap);
|
|
34
|
+
const transformToOldIndex = (i = NaN) => {
|
|
35
|
+
let index = i;
|
|
36
|
+
for (const mutation of mutations) {
|
|
37
|
+
if (isNaN(index))
|
|
38
|
+
break;
|
|
39
|
+
if (mutation._type == "r") {
|
|
40
|
+
index =
|
|
41
|
+
index < mutation._index
|
|
42
|
+
? index
|
|
43
|
+
: index == mutation._index
|
|
44
|
+
? NaN
|
|
45
|
+
: index - 1;
|
|
46
|
+
}
|
|
47
|
+
else if (mutation._type == "a") {
|
|
48
|
+
index = index < mutation._index ? index : index + 1;
|
|
49
|
+
}
|
|
50
|
+
else if (mutation._type == "m") {
|
|
51
|
+
index =
|
|
52
|
+
mutation._to <= index && index < mutation._from
|
|
53
|
+
? index + 1
|
|
54
|
+
: index == mutation._from
|
|
55
|
+
? mutation._to
|
|
56
|
+
: index;
|
|
57
|
+
}
|
|
44
58
|
}
|
|
59
|
+
return index;
|
|
60
|
+
};
|
|
61
|
+
for (const entry of _removedFromOld) {
|
|
62
|
+
mutations.push({
|
|
63
|
+
_type: "r",
|
|
64
|
+
_key: entry._key,
|
|
65
|
+
_index: entry._oldIndex - entry._removedBefore,
|
|
66
|
+
});
|
|
45
67
|
}
|
|
46
|
-
for (
|
|
47
|
-
const key = keyFn(array()[i], i);
|
|
68
|
+
for (const [key, i] of newIndexMap) {
|
|
48
69
|
const oldIndex = transformToOldIndex(oldIndexMap.get(key));
|
|
49
70
|
if (isNaN(oldIndex)) {
|
|
50
71
|
mutations.push({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array_mutation.js","sourceRoot":"","sources":["../src/array_mutation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEtE,MAAM,WAAW,GAAG,CAClB,KAAmB,EACnB,KAA2C,
|
|
1
|
+
{"version":3,"file":"array_mutation.js","sourceRoot":"","sources":["../src/array_mutation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEtE,MAAM,WAAW,GAAG,CAClB,KAAmB,EACnB,KAA2C,EAC3C,WAAiC,EAQjC,EAAE;IACF,MAAM,SAAS,GAAG,IAAI,GAAG,EAAmB,CAAC;IAE7C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACtC,MAAM,GAAG,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QAE/B,IAAI,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,kBAAkB,GAAG,GAAG,CAAC,CAAC;QAC5C,CAAC;QAED,SAAS,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;IACxB,CAAC;IAED,MAAM,eAAe,GAIhB,EAAE,CAAC;IACR,IAAI,aAAa,GAAG,CAAC,CAAC;IAEtB,KAAK,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,IAAI,WAAW,EAAE,CAAC;QACnC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACxB,eAAe,CAAC,IAAI,CAAC;gBACnB,IAAI,EAAE,GAAG;gBACT,SAAS,EAAE,CAAC;gBACZ,cAAc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YACzB,SAAS,CAAC;gBACR,UAAU,EAAE,SAAS;gBACrB,IAAI,EAAE,WAAW;aAClB,CAAC,CAAC;QACL,CAAC;QAED,QAAQ,GAAG,WAAW,CAAC;IACzB,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC"}
|
package/dist/bundle.js
CHANGED
|
@@ -859,52 +859,73 @@ const h = new Proxy(createElement, {
|
|
|
859
859
|
get: (target, type) => (props, children) => target(type, props, children),
|
|
860
860
|
});
|
|
861
861
|
|
|
862
|
-
const getIndexMap = (array, keyFn) => {
|
|
863
|
-
const
|
|
862
|
+
const getIndexMap = (array, keyFn, oldIndexMap) => {
|
|
863
|
+
const _indexMap = new Map();
|
|
864
864
|
for (let i = 0; i < array.length; i++) {
|
|
865
865
|
const key = keyFn(array[i], i);
|
|
866
|
-
if (
|
|
866
|
+
if (_indexMap.has(key)) {
|
|
867
867
|
throw new Error(`Duplicate key '${key}'`);
|
|
868
868
|
}
|
|
869
|
-
|
|
869
|
+
_indexMap.set(key, i);
|
|
870
|
+
}
|
|
871
|
+
const _removedFromOld = [];
|
|
872
|
+
let removedBefore = 0;
|
|
873
|
+
for (const [key, i] of oldIndexMap) {
|
|
874
|
+
if (!_indexMap.has(key)) {
|
|
875
|
+
_removedFromOld.push({
|
|
876
|
+
_key: key,
|
|
877
|
+
_oldIndex: i,
|
|
878
|
+
_removedBefore: removedBefore++,
|
|
879
|
+
});
|
|
880
|
+
}
|
|
870
881
|
}
|
|
871
|
-
return
|
|
882
|
+
return { _indexMap, _removedFromOld };
|
|
872
883
|
};
|
|
873
884
|
const useArrayMutation = (array, keyFn) => {
|
|
874
885
|
const [result, setResult] = useSignal({
|
|
875
886
|
_mutations: [],
|
|
876
887
|
_map: new Map(),
|
|
877
|
-
}
|
|
888
|
+
});
|
|
878
889
|
let indexMap = new Map();
|
|
879
890
|
useEffect(() => {
|
|
880
891
|
const mutations = [];
|
|
881
892
|
const oldIndexMap = indexMap;
|
|
882
|
-
const newIndexMap = getIndexMap(array(), keyFn);
|
|
883
|
-
const transformToOldIndex = (i = NaN) =>
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
893
|
+
const { _indexMap: newIndexMap, _removedFromOld } = getIndexMap(array(), keyFn, oldIndexMap);
|
|
894
|
+
const transformToOldIndex = (i = NaN) => {
|
|
895
|
+
let index = i;
|
|
896
|
+
for (const mutation of mutations) {
|
|
897
|
+
if (isNaN(index))
|
|
898
|
+
break;
|
|
899
|
+
if (mutation._type == "r") {
|
|
900
|
+
index =
|
|
901
|
+
index < mutation._index
|
|
902
|
+
? index
|
|
903
|
+
: index == mutation._index
|
|
904
|
+
? NaN
|
|
905
|
+
: index - 1;
|
|
906
|
+
}
|
|
907
|
+
else if (mutation._type == "a") {
|
|
908
|
+
index = index < mutation._index ? index : index + 1;
|
|
909
|
+
}
|
|
910
|
+
else if (mutation._type == "m") {
|
|
911
|
+
index =
|
|
912
|
+
mutation._to <= index && index < mutation._from
|
|
913
|
+
? index + 1
|
|
914
|
+
: index == mutation._from
|
|
915
|
+
? mutation._to
|
|
916
|
+
: index;
|
|
917
|
+
}
|
|
904
918
|
}
|
|
919
|
+
return index;
|
|
920
|
+
};
|
|
921
|
+
for (const entry of _removedFromOld) {
|
|
922
|
+
mutations.push({
|
|
923
|
+
_type: "r",
|
|
924
|
+
_key: entry._key,
|
|
925
|
+
_index: entry._oldIndex - entry._removedBefore,
|
|
926
|
+
});
|
|
905
927
|
}
|
|
906
|
-
for (
|
|
907
|
-
const key = keyFn(array()[i], i);
|
|
928
|
+
for (const [key, i] of newIndexMap) {
|
|
908
929
|
const oldIndex = transformToOldIndex(oldIndexMap.get(key));
|
|
909
930
|
if (isNaN(oldIndex)) {
|
|
910
931
|
mutations.push({
|
package/dist/bundle.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const t=t=>({t:t,o:new Set,i:new Set,l:{...t?.l},u(t){const n=o;o=this;try{return t()}finally{o=n}},
|
|
1
|
+
const t=t=>({t:t,o:new Set,i:new Set,l:{...t?.l},u(t){const n=o;o=this;try{return t()}finally{o=n}},_(){[...this.i].forEach((t,n,e)=>{e[e.length-1-n]._()}),this.i=new Set,[...this.o].forEach((t,n,e)=>{const o=e[e.length-1-n];o.p?.(),o.u=()=>{},o.h.forEach(t=>t.o.delete(o)),o.h.clear()}),this.o=new Set}});let n,e,o=t(),s=!1;const r=()=>o,c=(t,o)=>{const r=()=>(!s&&n&&(n.h.add(r),r.o.add(n)),r.peek());r.o=new Set,r.peek=()=>t;const c=(n,s)=>{const l={...o,...s};if(l.equals??=(t,n)=>t===n,e){const o="function"==typeof n?n(r.peek()):n;!l?.force&&l.equals(o,r.peek())||(l?.force?t=o:e.m.push(()=>t=o),l?.silent||r.o.forEach(t=>{t.v?e.S.add(t):e.o.add(t)}))}else i(()=>c(n,l))};return[r,c]},i=t=>{if(e)return t();e={m:[],o:new Set,S:new Set};try{const n=t();return l(),n}finally{e=void 0}},l=()=>{for(;e&&e.m.length+e.o.size+e.S.size>0;){e.o.forEach(t=>t.p?.()),e.m.forEach(t=>t()),e.m=[];const t=e.S.values().next().value??e.o.values().next().value;t&&(t.u(),e.S.delete(t),e.o.delete(t))}};let u=!1;const f=(t,e)=>{const r=!!e,c={M:o,v:u,h:new Set,u(){const o=n,c=s;n=this;try{this.h.forEach(t=>t.o.delete(this)),this.h.clear(),e&&(s=!1,e.forEach(t=>t())),s=r,this.p?.();const n=this.M.u(()=>i(t));this.p=n?()=>{this.M.u(()=>i(n)),this.p=null}:null}finally{n=o,s=c}}};o.o.add(c),c.u(),c.h.size||c.p||o.o.delete(c)},a=(t,n)=>{const[e,o]=c(void 0,n);let s=!0;u=!0;try{f(()=>{o(t,s?{force:!0}:{}),s=!1})}finally{u=!1}return e},d=(n,s)=>{const r=e;e=void 0;const c=o,i=t(c);Object.assign(i.l,s?.details);try{c.i.add(i);return[i.u(n),()=>{c.i.delete(i),i._()}]}finally{e=r}},_=(t,n)=>{const[e,o]=c(t,n);return e.set=o,e},p={upgrade:t=>()=>p.get(t),get:t=>"function"==typeof t?t():t,peek(t){const n=s;s=!0;try{return this.get(t)}finally{s=n}}},h=(t={})=>({k:[],C(t){return this.N?.next().value??t()},...t}),y=()=>{const t=r();return t.l.j??=h()},m=(t,n)=>{const e=y(),o=h({...e,...t}),[s,r]=d(n,{details:{j:o}});return f(()=>r),s},w=t=>(t[0]??"").toLowerCase()+t.slice(1).replace(/[A-Z]/g,t=>"-"+t.toLowerCase()),v=t=>t.startsWith("on:")?t.slice(3):w(t.slice(2)),b=Symbol(),g=(t,n)=>({[b]:Math.random().toString(36).slice(2),O:t,A:n}),S=t=>!!t?.[b],x=(t,n,e)=>{n.addEventListener(t[b],t=>{const n=p.get(e);void 0!==n&&(t.stopPropagation(),t.detail(n))})},M=t=>{const n=y();return a(()=>{let e=t.O;return n.H?.dispatchEvent(new CustomEvent(t[b],{detail:t=>e=t,bubbles:!0,composed:!0})),e})};var k;!function(t){t.forEach=(n,e)=>n.forEach(n=>Array.isArray(n)?t.forEach(n,e):e(n)),t.last=(n,e=n.length-1)=>{if(n.length)for(let o=e;o>=0;o--){const e=n[o];if(!Array.isArray(e))return e;const s=t.last(e);if(s)return s}}}(k||(k={}));const C=t=>({build(){const n=t();return n.build?.()??n}}),E=(t,n)=>({L:"p",T:t,...n}),N=(t=CustomEvent)=>({L:"e",I:t}),j=Symbol();let O;const A=(t,n)=>{O?O.push([t,n]):f(t,n)},H=(t,n,e)=>{const o="string"==typeof t?t:null,s="string"==typeof t?n:t,i=("string"==typeof t?e:n)??{},l=[],u=new Map;for(const t in s){const n=s[t];if("p"==n.L&&n.attribute){"function"==typeof n.attribute&&(n.attribute={transform:n.attribute});const e=n.attribute={name:w(t),static:!1,transform:t=>t,...n.attribute};u.set(e.name,{name:t,meta:n}),e.static||l.push(e.name)}}i.shadow??={mode:"open"};class a extends HTMLElement{static[j]={D:o};static observedAttributes=l;props={};events={};[j]={};constructor(){super();for(const t in s){const n=s[t];if("p"==n.L){const e=S(n.T)?n.T:null,[o,s]=c(e?void 0:n.T);this.props[t]=o,e&&x(e,this,o),Object.defineProperty(this,t,{get:o.peek,set:t=>s(()=>e||void 0!==t?t:n.T,{force:!0})})}else if("e"==n.L&&t.startsWith("on")){const e=v(t);this.events[t]=t=>this.dispatchEvent(new n.I(e,t))}}}connectedCallback(){const t=(n=this,i.shadow?n.shadowRoot??n.attachShadow(i.shadow):n);var n;this[j].F=d(()=>m({$:!1,H:this,N:t.childNodes.values()},()=>{this[j].M=r();const n=O;O=[];try{k.forEach(this.render().build(),n=>{t.append(n)}),O.forEach(([t,n])=>f(t,n))}finally{O=n}}))[1]}disconnectedCallback(){this[j].F?.()}attributeChangedCallback(t,n,e){const o=u.get(t);o&&(this[o.name]=null!=e?o.meta.attribute.transform.call(this,e):void 0)}}return a},L=t=>!!t?.[j],T=(...t)=>{const[n,e]="string"==typeof t[0]?[t[0],t.slice(1)]:["",t];for(const t of e)customElements.define(n+(t[j].D??w(t.name)),t)},I=/acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|itera/i,D=(t,n,e)=>{"-"==n[0]?t.style.setProperty(n,""+e):t.style[n]=null==e?"":"number"!=typeof e||I.test(n)?""+e:e+"px"},F=(t,n,e,o)=>{const s=null==e||!1===e&&!n.includes("-");if(n.startsWith("prop:"))t[n.slice(5)]=e;else if(n.startsWith("attr:"))n=n.slice(5),s?t.removeAttribute(n):t.setAttribute(n,e);else if(!["innerHTML","outerHTML"].includes(n)){if(!["tabIndex","role",...o?["width","height","href","list","form","download","rowSpan","colSpan"]:[]].includes(n)&&n in t)try{return void(t[n]=e)}catch(t){}"function"==typeof e||(s?t.removeAttribute(n):t.setAttribute(n,e))}},$=({text:t,marker:n})=>C(()=>{const e=y(),o=n&&e.C(()=>document.createComment("")),s=e.C(()=>document.createTextNode(""));return f(()=>{const n=""+(p.get(t)??"");s.textContent!=n&&(s.textContent=n)}),o?[o,s]:[s]}),z=({children:t})=>C(()=>Array.isArray(t)?t.flatMap(t=>z({children:t}).build()):null==t?[]:["object"==typeof t?t.build():$({text:t}).build()]),B=(t,n,e,o)=>{const{ref:s,style:c,children:l,dangerouslySetInnerHTML:u,...a}=e;for(const n in c??{}){const e=c[n];f(()=>{D(t,n,p.get(e))})}for(const n in a){const e=a[n];if(n.startsWith("on")){const o=r(),s=t=>{o.u(()=>i(()=>e(t)))},c=v(n);f(()=>(t.addEventListener(c,s),()=>t.removeEventListener(c,s)))}else f(()=>{F(t,n,p.get(e),o)})}return u&&f(()=>{const n=p.get(u).__html;t.innerHTML!=n&&(t.innerHTML=n)}),s&&f(()=>(s.set(t),()=>s.set(void 0))),null!=e.children&&k.forEach(m({$:n,N:t.childNodes.values()},()=>z({children:e.children}).build()),n=>t.append(n)),t},P=(t,n={},e)=>(null!=e&&(n.children=e),L(t)?((t,n)=>C(()=>{const e=y().C(()=>new t);return customElements.upgrade(e),B(e,!1,n),[e]}))(t,n):"function"==typeof t?C(()=>t(n)):((t,n={})=>C(()=>{const e=y(),o="svg"==t||!!e.$;return[B(e.C(()=>o?document.createElementNS("http://www.w3.org/2000/svg",t):document.createElement(t)),(!o||"foreignObject"!==t)&&o,n,!0)]}))(t,n)),V=new Proxy(P,{get:(t,n)=>(e,o)=>t(n,e,o)}),Z=(t,n)=>{const[e,o]=c({B:[],P:new Map});let s=new Map;return f(()=>{const e=[],r=s,{V:c,Z:i}=((t,n,e)=>{const o=new Map;for(let e=0;e<t.length;e++){const s=n(t[e],e);if(o.has(s))throw Error(`Duplicate key '${s}'`);o.set(s,e)}const s=[];let r=0;for(const[t,n]of e)o.has(t)||s.push({q:t,G:n,J:r++});return{V:o,Z:s}})(t(),n,r),l=(t=NaN)=>{let n=t;for(const t of e){if(isNaN(n))break;"r"==t.K?n=n<t.R?n:n==t.R?NaN:n-1:"a"==t.K?n=n<t.R?n:n+1:"m"==t.K&&(n=t.U<=n&&n<t.W?n+1:n==t.W?t.U:n)}return n};for(const t of i)e.push({K:"r",q:t.q,R:t.G-t.J});for(const[t,n]of c){const o=l(r.get(t));isNaN(o)?e.push({K:"a",q:t,R:n}):o!=n&&e.push({K:"m",q:t,W:o,U:n})}e.length>0&&o({B:e,P:c}),s=c}),e},q=t=>C(()=>{const n=y(),e=p.upgrade(t.each??[]),o=n.C(()=>document.createComment("")),s=t.key??((t,n)=>n),r=[o,[]],i=new Map,l=Z(e,s),u=t=>k.last(r[1],t-1)??o;return f(()=>{for(const n of l().B)if("r"==n.K){const{X:t,F:e}=i.get(n.q)??{};e?.(),r[1].splice(n.R,1),k.forEach(t??[],t=>t.parentNode?.removeChild(t)),i.delete(n.q)}else if("a"==n.K){let o;const[,s]=d(()=>{const[s,i]=c(n.R),[a,d]=c(e()[n.R]);f(()=>{0<=s()&&s()<e().length&&d(()=>e()[s()])}),f(()=>{const t=l().P.get(n.q);null!=t&&i(t)}),o=t.children?.(a,s,e).build()??[],r[1].splice(n.R,0,o);let _=u(n.R);k.forEach(o,t=>{_.parentNode?.insertBefore(t,_.nextSibling),_=t})});i.set(n.q,{X:o,F:s})}else if("m"==n.K){const{X:t}=i.get(n.q)??{};r[1].splice(n.W,1),r[1].splice(n.U,0,t??[]);let e=u(n.U);k.forEach(t??[],t=>{e.parentNode?.insertBefore(t,e.nextSibling),e=t})}},[l]),r}),G=t=>(y().k=[],J({condition:t.condition,children:t.children})),J=t=>{const n=y(),e=n.k,o=p.upgrade(t.condition),s=a(()=>e.every(t=>!t())&&o());return n.k=[...e,o],C(()=>m({k:[]},()=>{const e=n.C(()=>document.createComment("")),o=[e,[]],r=a(()=>s()?z({children:t.children}):null);let c=[];return f(()=>{k.forEach(c,t=>t.parentNode?.removeChild(t)),o[1]=[];const[,t]=d(()=>{c=r()?.build()??[],o[1]=c;let t=e;k.forEach(c,n=>{t.parentNode?.insertBefore(n,t.nextSibling),t=n})});return t},[r]),o}))},K=({children:t})=>J({condition:!0,children:t}),Q=({mount:t,children:n})=>C(()=>m({N:void 0},()=>{const e=z({children:n}).build();return f(()=>(k.forEach(e,n=>t.appendChild(n)),()=>{k.forEach(e,t=>t.parentNode?.removeChild(t))}),[]),[]})),R=Symbol(),U=new Map,W=t=>{const n=t.children;if("function"==typeof n){const e=P("style",{},$({text:n,marker:!1}));return t.light?Q({mount:document.head,children:e}):e}if(n){const o=y(),s=t.light?document:o.H?.shadowRoot??document,r=((t,n,e)=>{if(!U.has(n)){const t=new CSSStyleSheet;t.replaceSync(n),U.set(n,{Y:t,tt:0})}const o=U.get(n);o.tt++,t.has(n)||t.set(n,{Y:o.Y,tt:0});const s=t.get(n);return s.tt++,f(()=>()=>{--s.tt||(t.delete(n),e()),--o.tt||U.delete(n)}),s.Y})((e=s,e[R]??=new Map),n,()=>{s.adoptedStyleSheets=s.adoptedStyleSheets.filter(t=>t!=r)});s.adoptedStyleSheets.push(r)}var e;return z({})},X=(t,...n)=>{const e=()=>t.reduce((t,e,o)=>t+e+(p.get(n[o])??""),"");return n.some(t=>"function"==typeof t)?e:e()},Y=(t,n,e)=>(n&&null!=e&&(n.key=e),P(t,n));export{H as Component,K as Else,J as ElseIf,q as For,z as Fragment,G as If,p as MaybeSignal,Q as Portal,W as Style,k as TemplateNodes,g as createContext,P as createElement,X as css,T as defineComponents,N as event,l as flushBatch,V as h,L as isComponent,Y as jsx,Y as jsxDEV,Y as jsxs,E as prop,i as useBatch,M as useContext,A as useEffect,a as useMemo,_ as useRef,c as useSignal,d as useSubscope};
|
package/package.json
CHANGED
package/src/array_mutation.ts
CHANGED
|
@@ -3,20 +3,45 @@ import { Signal, SignalLike, useEffect, useSignal } from "./scope.js";
|
|
|
3
3
|
const getIndexMap = <T>(
|
|
4
4
|
array: readonly T[],
|
|
5
5
|
keyFn: (entry: T, index: number) => unknown,
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
oldIndexMap: Map<unknown, number>,
|
|
7
|
+
): {
|
|
8
|
+
_indexMap: Map<unknown, number>;
|
|
9
|
+
_removedFromOld: Array<{
|
|
10
|
+
_key: unknown;
|
|
11
|
+
_oldIndex: number;
|
|
12
|
+
_removedBefore: number;
|
|
13
|
+
}>;
|
|
14
|
+
} => {
|
|
15
|
+
const _indexMap = new Map<unknown, number>();
|
|
8
16
|
|
|
9
17
|
for (let i = 0; i < array.length; i++) {
|
|
10
18
|
const key = keyFn(array[i], i);
|
|
11
19
|
|
|
12
|
-
if (
|
|
20
|
+
if (_indexMap.has(key)) {
|
|
13
21
|
throw new Error(`Duplicate key '${key}'`);
|
|
14
22
|
}
|
|
15
23
|
|
|
16
|
-
|
|
24
|
+
_indexMap.set(key, i);
|
|
17
25
|
}
|
|
18
26
|
|
|
19
|
-
|
|
27
|
+
const _removedFromOld: Array<{
|
|
28
|
+
_key: unknown;
|
|
29
|
+
_oldIndex: number;
|
|
30
|
+
_removedBefore: number;
|
|
31
|
+
}> = [];
|
|
32
|
+
let removedBefore = 0;
|
|
33
|
+
|
|
34
|
+
for (const [key, i] of oldIndexMap) {
|
|
35
|
+
if (!_indexMap.has(key)) {
|
|
36
|
+
_removedFromOld.push({
|
|
37
|
+
_key: key,
|
|
38
|
+
_oldIndex: i,
|
|
39
|
+
_removedBefore: removedBefore++,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return { _indexMap, _removedFromOld };
|
|
20
45
|
};
|
|
21
46
|
|
|
22
47
|
export type ArrayMutation =
|
|
@@ -41,54 +66,59 @@ export const useArrayMutation = <T extends unknown>(
|
|
|
41
66
|
array: SignalLike<readonly T[]>,
|
|
42
67
|
keyFn: (entry: T, index: number) => unknown,
|
|
43
68
|
): Signal<ArrayMutationResult> => {
|
|
44
|
-
const [result, setResult] = useSignal<ArrayMutationResult>(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
},
|
|
49
|
-
{ force: true },
|
|
50
|
-
);
|
|
69
|
+
const [result, setResult] = useSignal<ArrayMutationResult>({
|
|
70
|
+
_mutations: [],
|
|
71
|
+
_map: new Map(),
|
|
72
|
+
});
|
|
51
73
|
|
|
52
74
|
let indexMap = new Map<unknown, number>();
|
|
53
75
|
|
|
54
76
|
useEffect(() => {
|
|
55
77
|
const mutations: ArrayMutation[] = [];
|
|
56
78
|
const oldIndexMap = indexMap;
|
|
57
|
-
const newIndexMap = getIndexMap(
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
});
|
|
79
|
+
const { _indexMap: newIndexMap, _removedFromOld } = getIndexMap(
|
|
80
|
+
array(),
|
|
81
|
+
keyFn,
|
|
82
|
+
oldIndexMap,
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const transformToOldIndex = (i: number = NaN) => {
|
|
86
|
+
let index = i;
|
|
87
|
+
|
|
88
|
+
for (const mutation of mutations) {
|
|
89
|
+
if (isNaN(index)) break;
|
|
90
|
+
|
|
91
|
+
if (mutation._type == "r") {
|
|
92
|
+
index =
|
|
93
|
+
index < mutation._index
|
|
94
|
+
? index
|
|
95
|
+
: index == mutation._index
|
|
96
|
+
? NaN
|
|
97
|
+
: index - 1;
|
|
98
|
+
} else if (mutation._type == "a") {
|
|
99
|
+
index = index < mutation._index ? index : index + 1;
|
|
100
|
+
} else if (mutation._type == "m") {
|
|
101
|
+
index =
|
|
102
|
+
mutation._to <= index && index < mutation._from
|
|
103
|
+
? index + 1
|
|
104
|
+
: index == mutation._from
|
|
105
|
+
? mutation._to
|
|
106
|
+
: index;
|
|
107
|
+
}
|
|
87
108
|
}
|
|
109
|
+
|
|
110
|
+
return index;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
for (const entry of _removedFromOld) {
|
|
114
|
+
mutations.push({
|
|
115
|
+
_type: "r",
|
|
116
|
+
_key: entry._key,
|
|
117
|
+
_index: entry._oldIndex - entry._removedBefore,
|
|
118
|
+
});
|
|
88
119
|
}
|
|
89
120
|
|
|
90
|
-
for (
|
|
91
|
-
const key = keyFn(array()[i], i);
|
|
121
|
+
for (const [key, i] of newIndexMap) {
|
|
92
122
|
const oldIndex = transformToOldIndex(oldIndexMap.get(key));
|
|
93
123
|
|
|
94
124
|
if (isNaN(oldIndex)) {
|