lithent 1.5.2 → 1.7.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/README.md +10 -10
- package/dist/index.d.ts +5 -6
- package/dist/lithent.mjs +5 -31
- package/dist/lithent.mjs.map +1 -1
- package/dist/lithent.umd.js +1 -1
- package/dist/lithent.umd.js.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/examples/dist/assets/index-9a408428.js +2 -0
- package/examples/dist/assets/index-9a408428.js.map +1 -0
- package/examples/dist/index.html +1 -1
- package/helper/dist/hook/store.d.ts +1 -1
- package/helper/dist/lithentHelper.mjs +64 -50
- package/helper/dist/lithentHelper.mjs.map +1 -1
- package/helper/dist/lithentHelper.umd.js +1 -1
- package/helper/dist/lithentHelper.umd.js.map +1 -1
- package/helper/src/hook/store.ts +61 -15
- package/package.json +1 -6
- package/src/diff.ts +1 -0
- package/src/index.ts +5 -16
- package/src/tests/storeObserver.tsx +190 -0
- package/src/tests/test2.tsx +63 -15
- package/src/types/index.ts +1 -0
- package/src/utils/redraw.ts +2 -39
- package/src/wDom.ts +4 -0
- package/examples/dist/assets/index-85bc9221.js +0 -2
- package/examples/dist/assets/index-85bc9221.js.map +0 -1
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { mountCallback as f, updateCallback as
|
|
2
|
-
const
|
|
3
|
-
let
|
|
1
|
+
import { mountCallback as f, updateCallback as g, nextTick as m, render as x } from "lithent";
|
|
2
|
+
const C = (n, t) => {
|
|
3
|
+
let u = n;
|
|
4
4
|
return {
|
|
5
5
|
get value() {
|
|
6
|
-
return
|
|
6
|
+
return u;
|
|
7
7
|
},
|
|
8
8
|
get v() {
|
|
9
|
-
return
|
|
9
|
+
return u;
|
|
10
10
|
},
|
|
11
|
-
set value(
|
|
12
|
-
|
|
11
|
+
set value(c) {
|
|
12
|
+
u = c, t();
|
|
13
13
|
},
|
|
14
|
-
set v(
|
|
15
|
-
|
|
14
|
+
set v(c) {
|
|
15
|
+
u = c, t();
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
|
-
},
|
|
19
|
-
let t =
|
|
18
|
+
}, P = (n) => {
|
|
19
|
+
let t = n;
|
|
20
20
|
return {
|
|
21
21
|
get value() {
|
|
22
22
|
return t();
|
|
@@ -24,54 +24,68 @@ const x = (r, t) => {
|
|
|
24
24
|
get v() {
|
|
25
25
|
return t();
|
|
26
26
|
},
|
|
27
|
-
set value(
|
|
27
|
+
set value(u) {
|
|
28
28
|
throw new Error("You can't change 'computed'");
|
|
29
29
|
},
|
|
30
|
-
set v(
|
|
30
|
+
set v(u) {
|
|
31
31
|
throw new Error("You can't change 'computed'");
|
|
32
32
|
}
|
|
33
33
|
};
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
f(() => (
|
|
37
|
-
},
|
|
34
|
+
}, U = (n, t = () => {
|
|
35
|
+
}, u = () => []) => {
|
|
36
|
+
f(() => (n(), t)), g(() => (t && t(), n), u);
|
|
37
|
+
}, v = /* @__PURE__ */ new Map(), h = {}, r = {}, k = (n) => {
|
|
38
38
|
const t = Symbol();
|
|
39
|
-
return
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
return o.forEach((u) => {
|
|
48
|
-
u() || s.push(u);
|
|
49
|
-
}), s.forEach(
|
|
50
|
-
(u) => o.splice(o.indexOf(u), 1)
|
|
51
|
-
), !0;
|
|
52
|
-
}
|
|
53
|
-
}), U = (r, t) => {
|
|
54
|
-
let e = [], n = null;
|
|
55
|
-
return (o) => {
|
|
56
|
-
const s = r(), u = e.every((i, p) => i === s[p]);
|
|
57
|
-
if (e = s, u && n)
|
|
58
|
-
return n;
|
|
59
|
-
const l = t(o);
|
|
60
|
-
return n = l, l;
|
|
39
|
+
return v.set(t, n), (u, c) => {
|
|
40
|
+
if (u)
|
|
41
|
+
if (c) {
|
|
42
|
+
const l = { value: u }, s = d(t, l);
|
|
43
|
+
return c(s), l.value = null, s;
|
|
44
|
+
} else
|
|
45
|
+
h[t] ?? (h[t] = []), h[t].push(u);
|
|
46
|
+
return d(t);
|
|
61
47
|
};
|
|
62
|
-
},
|
|
63
|
-
|
|
48
|
+
}, d = (n, t) => {
|
|
49
|
+
const u = [];
|
|
50
|
+
return new Proxy(v.get(n), {
|
|
51
|
+
get(c, l) {
|
|
52
|
+
var s;
|
|
53
|
+
return t != null && t.value && (r[n] ?? (r[n] = {}), (s = r[n])[l] ?? (s[l] = []), r[n][l].includes(t.value) || (r[n][l].push(t.value), u.push(l))), u.includes(l) || !t ? c[l] : null;
|
|
54
|
+
},
|
|
55
|
+
set(c, l, s) {
|
|
56
|
+
(u.includes(l) || !t) && (c[l] = s);
|
|
57
|
+
const i = h[n] || [], a = t ? r[n][l] || [] : [], o = [];
|
|
58
|
+
return i.forEach((e) => {
|
|
59
|
+
e() || o.push(e);
|
|
60
|
+
}), a.forEach((e) => {
|
|
61
|
+
e() || o.push(e);
|
|
62
|
+
}), o.forEach((e) => {
|
|
63
|
+
i.splice(i.indexOf(e), 1), a.splice(a.indexOf(e), 1);
|
|
64
|
+
}), !0;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}, D = (n, t) => {
|
|
68
|
+
let u = [], c = null;
|
|
69
|
+
return (l) => {
|
|
70
|
+
const s = n(), i = u.every((o, e) => o === s[e]);
|
|
71
|
+
if (u = s, i && c)
|
|
72
|
+
return c;
|
|
73
|
+
const a = t(l);
|
|
74
|
+
return c = a, a;
|
|
75
|
+
};
|
|
76
|
+
}, S = (n, t, u) => {
|
|
77
|
+
let c = () => {
|
|
64
78
|
};
|
|
65
|
-
return
|
|
66
|
-
|
|
67
|
-
}), () =>
|
|
79
|
+
return m().then(() => {
|
|
80
|
+
c = x(n(), t, u);
|
|
81
|
+
}), () => c();
|
|
68
82
|
};
|
|
69
83
|
export {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
84
|
+
D as cacheUpdate,
|
|
85
|
+
P as computed,
|
|
86
|
+
U as effect,
|
|
87
|
+
S as nextTickRender,
|
|
88
|
+
C as state,
|
|
89
|
+
k as store
|
|
76
90
|
};
|
|
77
91
|
//# sourceMappingURL=lithentHelper.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lithentHelper.mjs","sources":["../src/hook/state.ts","../src/hook/computed.ts","../src/hook/effect.ts","../src/hook/store.ts","../src/hook/cacheUpdate.ts","../src/hook/nextTickRender.ts"],"sourcesContent":["export const state = <T>(\n value: T,\n renew: () => boolean\n): {\n value: T;\n v: T;\n} => {\n let result = value;\n\n return {\n get value() {\n return result;\n },\n get v() {\n return result;\n },\n set value(newValue: T) {\n result = newValue;\n renew();\n },\n set v(newValue: T) {\n result = newValue;\n renew();\n },\n };\n};\n","export const computed = <T>(\n value: () => T\n): {\n value: T;\n v: T;\n} => {\n let result = value;\n\n return {\n get value() {\n return result();\n },\n get v() {\n return result();\n },\n set value(_newValue: T) {\n throw new Error(`You can't change 'computed'`);\n },\n set v(_newValue: T) {\n throw new Error(`You can't change 'computed'`);\n },\n };\n};\n","import { updateCallback, mountCallback } from 'lithent';\n\nexport const effect = (\n forward: () => (() => void) | void,\n backward: () => (() => void) | void = () => {},\n dependencies: () => any[] = () => []\n) => {\n mountCallback(() => {\n forward();\n\n return backward;\n });\n\n updateCallback(() => {\n if (backward) {\n backward();\n }\n\n return forward;\n }, dependencies);\n};\n","/**\n * DataStore\n */\nconst storeGroup = new Map<string | symbol, unknown>();\nconst storeRenderList: {\n [key: string | symbol]:
|
|
1
|
+
{"version":3,"file":"lithentHelper.mjs","sources":["../src/hook/state.ts","../src/hook/computed.ts","../src/hook/effect.ts","../src/hook/store.ts","../src/hook/cacheUpdate.ts","../src/hook/nextTickRender.ts"],"sourcesContent":["export const state = <T>(\n value: T,\n renew: () => boolean\n): {\n value: T;\n v: T;\n} => {\n let result = value;\n\n return {\n get value() {\n return result;\n },\n get v() {\n return result;\n },\n set value(newValue: T) {\n result = newValue;\n renew();\n },\n set v(newValue: T) {\n result = newValue;\n renew();\n },\n };\n};\n","export const computed = <T>(\n value: () => T\n): {\n value: T;\n v: T;\n} => {\n let result = value;\n\n return {\n get value() {\n return result();\n },\n get v() {\n return result();\n },\n set value(_newValue: T) {\n throw new Error(`You can't change 'computed'`);\n },\n set v(_newValue: T) {\n throw new Error(`You can't change 'computed'`);\n },\n };\n};\n","import { updateCallback, mountCallback } from 'lithent';\n\nexport const effect = (\n forward: () => (() => void) | void,\n backward: () => (() => void) | void = () => {},\n dependencies: () => any[] = () => []\n) => {\n mountCallback(() => {\n forward();\n\n return backward;\n });\n\n updateCallback(() => {\n if (backward) {\n backward();\n }\n\n return forward;\n }, dependencies);\n};\n","/**\n * DataStore\n */\ntype StoreValue = {\n [key: string | symbol]: (() => boolean)[];\n};\n\nconst storeGroup = new Map<string | symbol, unknown>();\nconst storeRenderList: StoreValue = {};\nconst storeRenderObserveMap: {\n [key: string | symbol]: StoreValue;\n} = {};\n\nexport const store = <T extends {}>(value: T) => {\n const storeKey = Symbol();\n storeGroup.set(storeKey, value);\n\n return (renew?: () => boolean, makeObserver?: (store: T) => unknown[]) => {\n if (renew) {\n if (makeObserver) {\n const renewRef: { value: (() => boolean) | null } = { value: renew };\n const makedProxy = updater<T>(storeKey, renewRef);\n\n makeObserver(makedProxy);\n renewRef.value = null;\n\n return makedProxy;\n } else {\n storeRenderList[storeKey] ??= [];\n storeRenderList[storeKey].push(renew);\n }\n }\n\n return updater<T>(storeKey);\n };\n};\n\nconst updater = <T extends { [key: string | symbol]: unknown }>(\n storeKey: string | symbol,\n renewRef?: { value: (() => boolean) | null }\n) => {\n const allowedAccessProp: (keyof T)[] = [];\n\n return new Proxy(storeGroup.get(storeKey) as T, {\n get(target: T, prop: keyof T) {\n if (renewRef?.value) {\n storeRenderObserveMap[storeKey] ??= {};\n storeRenderObserveMap[storeKey][prop] ??= [];\n\n if (!storeRenderObserveMap[storeKey][prop].includes(renewRef.value)) {\n storeRenderObserveMap[storeKey][prop].push(renewRef.value);\n allowedAccessProp.push(prop);\n }\n }\n\n if (allowedAccessProp.includes(prop) || !renewRef) {\n return target[prop];\n }\n\n return null;\n },\n set(target, prop: keyof T, value) {\n if (allowedAccessProp.includes(prop) || !renewRef) {\n target[prop] = value;\n }\n\n const renderList = storeRenderList[storeKey] || [];\n const renderObserveList: (() => boolean)[] = renewRef\n ? storeRenderObserveMap[storeKey][prop] || []\n : [];\n const trashCollections: (() => boolean)[] = [];\n\n renderList.forEach(renew => {\n if (!renew()) {\n trashCollections.push(renew);\n }\n });\n\n renderObserveList.forEach(renew => {\n if (!renew()) {\n trashCollections.push(renew);\n }\n });\n\n trashCollections.forEach(deleteTarget => {\n renderList.splice(renderList.indexOf(deleteTarget), 1);\n renderObserveList.splice(renderObserveList.indexOf(deleteTarget), 1);\n });\n\n return true;\n },\n });\n};\n","import { WDom, Props } from 'lithent';\n\nexport const cacheUpdate = (\n checkFunction: () => unknown[],\n updater: (props: Props) => WDom\n) => {\n let originalDefs: unknown[] = [];\n let originalUpdater: WDom | null = null;\n\n return (props: Props) => {\n const newDefs = checkFunction();\n const isSame = originalDefs.every((def, index) => def === newDefs[index]);\n\n originalDefs = newDefs;\n\n if (isSame && originalUpdater) {\n return originalUpdater;\n }\n\n const newUpdater = updater(props);\n originalUpdater = newUpdater;\n\n return newUpdater;\n };\n};\n","import { render, WDom, nextTick } from 'lithent';\n\nexport const nextTickRender = (\n wDomMaker: () => WDom,\n wrapElement: HTMLElement | null,\n afterElement?: HTMLElement | null\n) => {\n let destroy = () => {};\n\n nextTick().then(() => {\n destroy = render(wDomMaker(), wrapElement, afterElement);\n });\n\n return () => destroy();\n};\n"],"names":["state","value","renew","result","newValue","computed","_newValue","effect","forward","backward","dependencies","mountCallback","updateCallback","storeGroup","storeRenderList","storeRenderObserveMap","store","storeKey","makeObserver","renewRef","makedProxy","updater","allowedAccessProp","target","prop","_a","renderList","renderObserveList","trashCollections","deleteTarget","cacheUpdate","checkFunction","originalDefs","originalUpdater","props","newDefs","isSame","def","index","newUpdater","nextTickRender","wDomMaker","wrapElement","afterElement","destroy","nextTick","render"],"mappings":"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"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(l,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("lithent")):typeof define=="function"&&define.amd?define(["exports","lithent"],a):(l=typeof globalThis<"u"?globalThis:l||self,a(l.lithentHelper={},l.lithent))})(this,function(l,a){"use strict";const m=(n,t)=>{let e=n;return{get value(){return e},get v(){return e},set value(u){e=u,t()},set v(u){e=u,t()}}},E=n=>{let t=n;return{get value(){return t()},get v(){return t()},set value(e){throw new Error("You can't change 'computed'")},set v(e){throw new Error("You can't change 'computed'")}}},T=(n,t=()=>{},e=()=>[])=>{a.mountCallback(()=>(n(),t)),a.updateCallback(()=>(t&&t(),n),e)},v=new Map,d={},r={},x=n=>{const t=Symbol();return v.set(t,n),(e,u)=>{if(e)if(u){const c={value:e},i=g(t,c);return u(i),c.value=null,i}else d[t]??(d[t]=[]),d[t].push(e);return g(t)}},g=(n,t)=>{const e=[];return new Proxy(v.get(n),{get(u,c){var i;return t!=null&&t.value&&(r[n]??(r[n]={}),(i=r[n])[c]??(i[c]=[]),r[n][c].includes(t.value)||(r[n][c].push(t.value),e.push(c))),e.includes(c)||!t?u[c]:null},set(u,c,i){(e.includes(c)||!t)&&(u[c]=i);const f=d[n]||[],o=t?r[n][c]||[]:[],h=[];return f.forEach(s=>{s()||h.push(s)}),o.forEach(s=>{s()||h.push(s)}),h.forEach(s=>{f.splice(f.indexOf(s),1),o.splice(o.indexOf(s),1)}),!0}})},P=(n,t)=>{let e=[],u=null;return c=>{const i=n(),f=e.every((h,s)=>h===i[s]);if(e=i,f&&u)return u;const o=t(c);return u=o,o}},S=(n,t,e)=>{let u=()=>{};return a.nextTick().then(()=>{u=a.render(n(),t,e)}),()=>u()};l.cacheUpdate=P,l.computed=E,l.effect=T,l.nextTickRender=S,l.state=m,l.store=x,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
|
|
2
2
|
//# sourceMappingURL=lithentHelper.umd.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lithentHelper.umd.js","sources":["../src/hook/state.ts","../src/hook/computed.ts","../src/hook/effect.ts","../src/hook/store.ts","../src/hook/cacheUpdate.ts","../src/hook/nextTickRender.ts"],"sourcesContent":["export const state = <T>(\n value: T,\n renew: () => boolean\n): {\n value: T;\n v: T;\n} => {\n let result = value;\n\n return {\n get value() {\n return result;\n },\n get v() {\n return result;\n },\n set value(newValue: T) {\n result = newValue;\n renew();\n },\n set v(newValue: T) {\n result = newValue;\n renew();\n },\n };\n};\n","export const computed = <T>(\n value: () => T\n): {\n value: T;\n v: T;\n} => {\n let result = value;\n\n return {\n get value() {\n return result();\n },\n get v() {\n return result();\n },\n set value(_newValue: T) {\n throw new Error(`You can't change 'computed'`);\n },\n set v(_newValue: T) {\n throw new Error(`You can't change 'computed'`);\n },\n };\n};\n","import { updateCallback, mountCallback } from 'lithent';\n\nexport const effect = (\n forward: () => (() => void) | void,\n backward: () => (() => void) | void = () => {},\n dependencies: () => any[] = () => []\n) => {\n mountCallback(() => {\n forward();\n\n return backward;\n });\n\n updateCallback(() => {\n if (backward) {\n backward();\n }\n\n return forward;\n }, dependencies);\n};\n","/**\n * DataStore\n */\nconst storeGroup = new Map<string | symbol, unknown>();\nconst storeRenderList: {\n [key: string | symbol]:
|
|
1
|
+
{"version":3,"file":"lithentHelper.umd.js","sources":["../src/hook/state.ts","../src/hook/computed.ts","../src/hook/effect.ts","../src/hook/store.ts","../src/hook/cacheUpdate.ts","../src/hook/nextTickRender.ts"],"sourcesContent":["export const state = <T>(\n value: T,\n renew: () => boolean\n): {\n value: T;\n v: T;\n} => {\n let result = value;\n\n return {\n get value() {\n return result;\n },\n get v() {\n return result;\n },\n set value(newValue: T) {\n result = newValue;\n renew();\n },\n set v(newValue: T) {\n result = newValue;\n renew();\n },\n };\n};\n","export const computed = <T>(\n value: () => T\n): {\n value: T;\n v: T;\n} => {\n let result = value;\n\n return {\n get value() {\n return result();\n },\n get v() {\n return result();\n },\n set value(_newValue: T) {\n throw new Error(`You can't change 'computed'`);\n },\n set v(_newValue: T) {\n throw new Error(`You can't change 'computed'`);\n },\n };\n};\n","import { updateCallback, mountCallback } from 'lithent';\n\nexport const effect = (\n forward: () => (() => void) | void,\n backward: () => (() => void) | void = () => {},\n dependencies: () => any[] = () => []\n) => {\n mountCallback(() => {\n forward();\n\n return backward;\n });\n\n updateCallback(() => {\n if (backward) {\n backward();\n }\n\n return forward;\n }, dependencies);\n};\n","/**\n * DataStore\n */\ntype StoreValue = {\n [key: string | symbol]: (() => boolean)[];\n};\n\nconst storeGroup = new Map<string | symbol, unknown>();\nconst storeRenderList: StoreValue = {};\nconst storeRenderObserveMap: {\n [key: string | symbol]: StoreValue;\n} = {};\n\nexport const store = <T extends {}>(value: T) => {\n const storeKey = Symbol();\n storeGroup.set(storeKey, value);\n\n return (renew?: () => boolean, makeObserver?: (store: T) => unknown[]) => {\n if (renew) {\n if (makeObserver) {\n const renewRef: { value: (() => boolean) | null } = { value: renew };\n const makedProxy = updater<T>(storeKey, renewRef);\n\n makeObserver(makedProxy);\n renewRef.value = null;\n\n return makedProxy;\n } else {\n storeRenderList[storeKey] ??= [];\n storeRenderList[storeKey].push(renew);\n }\n }\n\n return updater<T>(storeKey);\n };\n};\n\nconst updater = <T extends { [key: string | symbol]: unknown }>(\n storeKey: string | symbol,\n renewRef?: { value: (() => boolean) | null }\n) => {\n const allowedAccessProp: (keyof T)[] = [];\n\n return new Proxy(storeGroup.get(storeKey) as T, {\n get(target: T, prop: keyof T) {\n if (renewRef?.value) {\n storeRenderObserveMap[storeKey] ??= {};\n storeRenderObserveMap[storeKey][prop] ??= [];\n\n if (!storeRenderObserveMap[storeKey][prop].includes(renewRef.value)) {\n storeRenderObserveMap[storeKey][prop].push(renewRef.value);\n allowedAccessProp.push(prop);\n }\n }\n\n if (allowedAccessProp.includes(prop) || !renewRef) {\n return target[prop];\n }\n\n return null;\n },\n set(target, prop: keyof T, value) {\n if (allowedAccessProp.includes(prop) || !renewRef) {\n target[prop] = value;\n }\n\n const renderList = storeRenderList[storeKey] || [];\n const renderObserveList: (() => boolean)[] = renewRef\n ? storeRenderObserveMap[storeKey][prop] || []\n : [];\n const trashCollections: (() => boolean)[] = [];\n\n renderList.forEach(renew => {\n if (!renew()) {\n trashCollections.push(renew);\n }\n });\n\n renderObserveList.forEach(renew => {\n if (!renew()) {\n trashCollections.push(renew);\n }\n });\n\n trashCollections.forEach(deleteTarget => {\n renderList.splice(renderList.indexOf(deleteTarget), 1);\n renderObserveList.splice(renderObserveList.indexOf(deleteTarget), 1);\n });\n\n return true;\n },\n });\n};\n","import { WDom, Props } from 'lithent';\n\nexport const cacheUpdate = (\n checkFunction: () => unknown[],\n updater: (props: Props) => WDom\n) => {\n let originalDefs: unknown[] = [];\n let originalUpdater: WDom | null = null;\n\n return (props: Props) => {\n const newDefs = checkFunction();\n const isSame = originalDefs.every((def, index) => def === newDefs[index]);\n\n originalDefs = newDefs;\n\n if (isSame && originalUpdater) {\n return originalUpdater;\n }\n\n const newUpdater = updater(props);\n originalUpdater = newUpdater;\n\n return newUpdater;\n };\n};\n","import { render, WDom, nextTick } from 'lithent';\n\nexport const nextTickRender = (\n wDomMaker: () => WDom,\n wrapElement: HTMLElement | null,\n afterElement?: HTMLElement | null\n) => {\n let destroy = () => {};\n\n nextTick().then(() => {\n destroy = render(wDomMaker(), wrapElement, afterElement);\n });\n\n return () => destroy();\n};\n"],"names":["state","value","renew","result","newValue","computed","_newValue","effect","forward","backward","dependencies","mountCallback","updateCallback","storeGroup","storeRenderList","storeRenderObserveMap","store","storeKey","makeObserver","renewRef","makedProxy","updater","allowedAccessProp","target","prop","_a","renderList","renderObserveList","trashCollections","deleteTarget","cacheUpdate","checkFunction","originalDefs","originalUpdater","props","newDefs","isSame","def","index","newUpdater","nextTickRender","wDomMaker","wrapElement","afterElement","destroy","nextTick","render"],"mappings":"8QAAa,MAAAA,EAAQ,CACnBC,EACAC,IAIG,CACH,IAAIC,EAASF,EAEN,MAAA,CACL,IAAI,OAAQ,CACH,OAAAE,CACT,EACA,IAAI,GAAI,CACC,OAAAA,CACT,EACA,IAAI,MAAMC,EAAa,CACZD,EAAAC,EACHF,GACR,EACA,IAAI,EAAEE,EAAa,CACRD,EAAAC,EACHF,GACR,CAAA,CAEJ,ECzBaG,EACXJ,GAIG,CACH,IAAIE,EAASF,EAEN,MAAA,CACL,IAAI,OAAQ,CACV,OAAOE,EAAO,CAChB,EACA,IAAI,GAAI,CACN,OAAOA,EAAO,CAChB,EACA,IAAI,MAAMG,EAAc,CAChB,MAAA,IAAI,MAAM,6BAA6B,CAC/C,EACA,IAAI,EAAEA,EAAc,CACZ,MAAA,IAAI,MAAM,6BAA6B,CAC/C,CAAA,CAEJ,ECpBaC,EAAS,CACpBC,EACAC,EAAsC,IAAM,CAAC,EAC7CC,EAA4B,IAAM,KAC/B,CACHC,EAAAA,cAAc,KACJH,IAEDC,EACR,EAEDG,EAAAA,eAAe,KACTH,GACOA,IAGJD,GACNE,CAAY,CACjB,ECbMG,MAAiB,IACjBC,EAA8B,CAAA,EAC9BC,EAEF,CAAA,EAESC,EAAuBf,GAAa,CAC/C,MAAMgB,EAAW,SACN,OAAAJ,EAAA,IAAII,EAAUhB,CAAK,EAEvB,CAACC,EAAuBgB,IAA2C,CACxE,GAAIhB,EACF,GAAIgB,EAAc,CACV,MAAAC,EAA8C,CAAE,MAAOjB,GACvDkB,EAAaC,EAAWJ,EAAUE,CAAQ,EAEhD,OAAAD,EAAaE,CAAU,EACvBD,EAAS,MAAQ,KAEVC,CAAA,MAESN,EAAAG,KAAAH,EAAAG,GAAc,IACdH,EAAAG,CAAQ,EAAE,KAAKf,CAAK,EAIxC,OAAOmB,EAAWJ,CAAQ,CAAA,CAE9B,EAEMI,EAAU,CACdJ,EACAE,IACG,CACH,MAAMG,EAAiC,CAAA,EAEvC,OAAO,IAAI,MAAMT,EAAW,IAAII,CAAQ,EAAQ,CAC9C,IAAIM,EAAWC,EAAe,OAW5B,OAVIL,GAAA,MAAAA,EAAU,QACUJ,EAAAE,KAAAF,EAAAE,GAAc,KACpCQ,EAAAV,EAAsBE,CAAQ,GAA9BO,KAAAC,EAAAD,GAA0C,CAAA,GAErCT,EAAsBE,CAAQ,EAAEO,CAAI,EAAE,SAASL,EAAS,KAAK,IAChEJ,EAAsBE,CAAQ,EAAEO,CAAI,EAAE,KAAKL,EAAS,KAAK,EACzDG,EAAkB,KAAKE,CAAI,IAI3BF,EAAkB,SAASE,CAAI,GAAK,CAACL,EAChCI,EAAOC,CAAI,EAGb,IACT,EACA,IAAID,EAAQC,EAAevB,EAAO,EAC5BqB,EAAkB,SAASE,CAAI,GAAK,CAACL,KACvCI,EAAOC,CAAI,EAAIvB,GAGjB,MAAMyB,EAAaZ,EAAgBG,CAAQ,GAAK,CAAA,EAC1CU,EAAuCR,EACzCJ,EAAsBE,CAAQ,EAAEO,CAAI,GAAK,CAAC,EAC1C,GACEI,EAAsC,CAAA,EAE5C,OAAAF,EAAW,QAAiBxB,GAAA,CACrBA,KACH0B,EAAiB,KAAK1B,CAAK,CAC7B,CACD,EAEDyB,EAAkB,QAAiBzB,GAAA,CAC5BA,KACH0B,EAAiB,KAAK1B,CAAK,CAC7B,CACD,EAED0B,EAAiB,QAAwBC,GAAA,CACvCH,EAAW,OAAOA,EAAW,QAAQG,CAAY,EAAG,CAAC,EACrDF,EAAkB,OAAOA,EAAkB,QAAQE,CAAY,EAAG,CAAC,CAAA,CACpE,EAEM,EACT,CAAA,CACD,CACH,EC1FaC,EAAc,CACzBC,EACAV,IACG,CACH,IAAIW,EAA0B,CAAA,EAC1BC,EAA+B,KAEnC,OAAQC,GAAiB,CACvB,MAAMC,EAAUJ,IACVK,EAASJ,EAAa,MAAM,CAACK,EAAKC,IAAUD,IAAQF,EAAQG,CAAK,CAAC,EAIxE,GAFeN,EAAAG,EAEXC,GAAUH,EACL,OAAAA,EAGH,MAAAM,EAAalB,EAAQa,CAAK,EACd,OAAAD,EAAAM,EAEXA,CAAA,CAEX,ECtBaC,EAAiB,CAC5BC,EACAC,EACAC,IACG,CACH,IAAIC,EAAU,IAAM,CAAA,EAEXC,OAAAA,EAAA,SAAA,EAAE,KAAK,IAAM,CACpBD,EAAUE,EAAAA,OAAOL,EAAa,EAAAC,EAAaC,CAAY,CAAA,CACxD,EAEM,IAAMC,EAAQ,CACvB"}
|
package/helper/src/hook/store.ts
CHANGED
|
@@ -1,19 +1,34 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* DataStore
|
|
3
3
|
*/
|
|
4
|
-
|
|
5
|
-
const storeRenderList: {
|
|
4
|
+
type StoreValue = {
|
|
6
5
|
[key: string | symbol]: (() => boolean)[];
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
const storeGroup = new Map<string | symbol, unknown>();
|
|
9
|
+
const storeRenderList: StoreValue = {};
|
|
10
|
+
const storeRenderObserveMap: {
|
|
11
|
+
[key: string | symbol]: StoreValue;
|
|
7
12
|
} = {};
|
|
8
13
|
|
|
9
14
|
export const store = <T extends {}>(value: T) => {
|
|
10
15
|
const storeKey = Symbol();
|
|
11
16
|
storeGroup.set(storeKey, value);
|
|
12
17
|
|
|
13
|
-
return (renew?: () => boolean) => {
|
|
18
|
+
return (renew?: () => boolean, makeObserver?: (store: T) => unknown[]) => {
|
|
14
19
|
if (renew) {
|
|
15
|
-
|
|
16
|
-
|
|
20
|
+
if (makeObserver) {
|
|
21
|
+
const renewRef: { value: (() => boolean) | null } = { value: renew };
|
|
22
|
+
const makedProxy = updater<T>(storeKey, renewRef);
|
|
23
|
+
|
|
24
|
+
makeObserver(makedProxy);
|
|
25
|
+
renewRef.value = null;
|
|
26
|
+
|
|
27
|
+
return makedProxy;
|
|
28
|
+
} else {
|
|
29
|
+
storeRenderList[storeKey] ??= [];
|
|
30
|
+
storeRenderList[storeKey].push(renew);
|
|
31
|
+
}
|
|
17
32
|
}
|
|
18
33
|
|
|
19
34
|
return updater<T>(storeKey);
|
|
@@ -21,15 +36,38 @@ export const store = <T extends {}>(value: T) => {
|
|
|
21
36
|
};
|
|
22
37
|
|
|
23
38
|
const updater = <T extends { [key: string | symbol]: unknown }>(
|
|
24
|
-
storeKey: string | symbol
|
|
25
|
-
) =>
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
39
|
+
storeKey: string | symbol,
|
|
40
|
+
renewRef?: { value: (() => boolean) | null }
|
|
41
|
+
) => {
|
|
42
|
+
const allowedAccessProp: (keyof T)[] = [];
|
|
43
|
+
|
|
44
|
+
return new Proxy(storeGroup.get(storeKey) as T, {
|
|
45
|
+
get(target: T, prop: keyof T) {
|
|
46
|
+
if (renewRef?.value) {
|
|
47
|
+
storeRenderObserveMap[storeKey] ??= {};
|
|
48
|
+
storeRenderObserveMap[storeKey][prop] ??= [];
|
|
49
|
+
|
|
50
|
+
if (!storeRenderObserveMap[storeKey][prop].includes(renewRef.value)) {
|
|
51
|
+
storeRenderObserveMap[storeKey][prop].push(renewRef.value);
|
|
52
|
+
allowedAccessProp.push(prop);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (allowedAccessProp.includes(prop) || !renewRef) {
|
|
57
|
+
return target[prop];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return null;
|
|
29
61
|
},
|
|
30
62
|
set(target, prop: keyof T, value) {
|
|
31
|
-
|
|
32
|
-
|
|
63
|
+
if (allowedAccessProp.includes(prop) || !renewRef) {
|
|
64
|
+
target[prop] = value;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const renderList = storeRenderList[storeKey] || [];
|
|
68
|
+
const renderObserveList: (() => boolean)[] = renewRef
|
|
69
|
+
? storeRenderObserveMap[storeKey][prop] || []
|
|
70
|
+
: [];
|
|
33
71
|
const trashCollections: (() => boolean)[] = [];
|
|
34
72
|
|
|
35
73
|
renderList.forEach(renew => {
|
|
@@ -38,10 +76,18 @@ const updater = <T extends { [key: string | symbol]: unknown }>(
|
|
|
38
76
|
}
|
|
39
77
|
});
|
|
40
78
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
79
|
+
renderObserveList.forEach(renew => {
|
|
80
|
+
if (!renew()) {
|
|
81
|
+
trashCollections.push(renew);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
trashCollections.forEach(deleteTarget => {
|
|
86
|
+
renderList.splice(renderList.indexOf(deleteTarget), 1);
|
|
87
|
+
renderObserveList.splice(renderObserveList.indexOf(deleteTarget), 1);
|
|
88
|
+
});
|
|
44
89
|
|
|
45
90
|
return true;
|
|
46
91
|
},
|
|
47
92
|
});
|
|
93
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lithent",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "An extensible virtual DOM library for lightweight use in a variety of environments.",
|
|
5
5
|
"packageManager": "pnpm@7.6.0",
|
|
6
6
|
"main": "./dist/lithent.umd.js",
|
|
@@ -68,15 +68,10 @@
|
|
|
68
68
|
"lithent"
|
|
69
69
|
],
|
|
70
70
|
"devDependencies": {
|
|
71
|
-
"@babel/cli": "^7.18.9",
|
|
72
|
-
"@babel/core": "^7.18.9",
|
|
73
|
-
"@babel/plugin-transform-react-jsx": "^7.18.6",
|
|
74
|
-
"@babel/preset-typescript": "^7.18.6",
|
|
75
71
|
"@nabla/vite-plugin-eslint": "^1.4.1",
|
|
76
72
|
"@typescript-eslint/eslint-plugin": "^5.30.7",
|
|
77
73
|
"@typescript-eslint/parser": "^5.30.7",
|
|
78
74
|
"@vitest/coverage-v8": "^0.32.2",
|
|
79
|
-
"babel-plugin-module-resolver": "^4.1.0",
|
|
80
75
|
"eslint": "^7.32.0",
|
|
81
76
|
"eslint-config-prettier": "^8.3.0",
|
|
82
77
|
"eslint-plugin-prettier": "^4.0.0",
|
package/src/diff.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
export { h, Fragment, mount } from '@/wDom';
|
|
2
|
+
export { render } from '@/render';
|
|
3
|
+
export { mountCallback } from '@/hook/mountCallback';
|
|
4
|
+
export { updateCallback } from '@/hook/updateCallback';
|
|
5
|
+
export { ref, nextTick } from '@/hook/ref';
|
|
6
6
|
|
|
7
7
|
export type {
|
|
8
8
|
WDom,
|
|
@@ -26,14 +26,3 @@ declare global {
|
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
-
|
|
30
|
-
export {
|
|
31
|
-
h,
|
|
32
|
-
Fragment,
|
|
33
|
-
render,
|
|
34
|
-
mount,
|
|
35
|
-
mountCallback,
|
|
36
|
-
updateCallback,
|
|
37
|
-
ref,
|
|
38
|
-
nextTick,
|
|
39
|
-
};
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { h, render, mount, Fragment, ref, nextTick } from '@/index';
|
|
2
|
+
|
|
3
|
+
type StoreValue = {
|
|
4
|
+
[key: string | symbol]: (() => boolean)[];
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
const testChangeRef1 = ref<null | (() => void)>(null);
|
|
8
|
+
const testChangeRef2 = ref<null | (() => void)>(null);
|
|
9
|
+
const testChangeRef3 = ref<null | (() => void)>(null);
|
|
10
|
+
|
|
11
|
+
const storeGroup = new Map<string | symbol, unknown>();
|
|
12
|
+
|
|
13
|
+
const storeRenderList: StoreValue = {};
|
|
14
|
+
|
|
15
|
+
const storeRenderObserveMap: {
|
|
16
|
+
[key: string | symbol]: StoreValue;
|
|
17
|
+
} = {};
|
|
18
|
+
|
|
19
|
+
export const store = <T extends {}>(value: T) => {
|
|
20
|
+
const storeKey = Symbol();
|
|
21
|
+
storeGroup.set(storeKey, value);
|
|
22
|
+
|
|
23
|
+
return (renew?: () => boolean, makeObserver?: (store: T) => unknown[]) => {
|
|
24
|
+
if (renew) {
|
|
25
|
+
if (makeObserver) {
|
|
26
|
+
const renewRef: { value: (() => boolean) | null } = { value: renew };
|
|
27
|
+
const makedProxy = updater<T>(storeKey, renewRef);
|
|
28
|
+
|
|
29
|
+
makeObserver(makedProxy);
|
|
30
|
+
renewRef.value = null;
|
|
31
|
+
|
|
32
|
+
return makedProxy;
|
|
33
|
+
} else {
|
|
34
|
+
storeRenderList[storeKey] ??= [];
|
|
35
|
+
storeRenderList[storeKey].push(renew);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return updater<T>(storeKey);
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const updater = <T extends { [key: string | symbol]: unknown }>(
|
|
44
|
+
storeKey: string | symbol,
|
|
45
|
+
renewRef?: { value: (() => boolean) | null }
|
|
46
|
+
) => {
|
|
47
|
+
const allowedAccessProp: (keyof T)[] = [];
|
|
48
|
+
|
|
49
|
+
return new Proxy(storeGroup.get(storeKey) as T, {
|
|
50
|
+
get(target: T, prop: keyof T) {
|
|
51
|
+
if (renewRef?.value) {
|
|
52
|
+
storeRenderObserveMap[storeKey] ??= {};
|
|
53
|
+
storeRenderObserveMap[storeKey][prop] ??= [];
|
|
54
|
+
|
|
55
|
+
if (!storeRenderObserveMap[storeKey][prop].includes(renewRef.value)) {
|
|
56
|
+
storeRenderObserveMap[storeKey][prop].push(renewRef.value);
|
|
57
|
+
allowedAccessProp.push(prop);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (allowedAccessProp.includes(prop) || !renewRef) {
|
|
62
|
+
return target[prop];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return null;
|
|
66
|
+
},
|
|
67
|
+
set(target, prop: keyof T, value) {
|
|
68
|
+
if (allowedAccessProp.includes(prop) || !renewRef) {
|
|
69
|
+
target[prop] = value;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const renderList = storeRenderList[storeKey] || [];
|
|
73
|
+
const renderObserveList: (() => boolean)[] = renewRef
|
|
74
|
+
? storeRenderObserveMap[storeKey][prop] || []
|
|
75
|
+
: [];
|
|
76
|
+
const trashCollections: (() => boolean)[] = [];
|
|
77
|
+
|
|
78
|
+
renderList.forEach(renew => {
|
|
79
|
+
if (!renew()) {
|
|
80
|
+
trashCollections.push(renew);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
renderObserveList.forEach(renew => {
|
|
85
|
+
if (!renew()) {
|
|
86
|
+
trashCollections.push(renew);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
trashCollections.forEach(deleteTarget => {
|
|
91
|
+
renderList.splice(renderList.indexOf(deleteTarget), 1);
|
|
92
|
+
renderObserveList.splice(renderObserveList.indexOf(deleteTarget), 1);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
return true;
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const assignShardStore = store<{
|
|
101
|
+
count1: number;
|
|
102
|
+
count2: number;
|
|
103
|
+
count3: number;
|
|
104
|
+
}>({
|
|
105
|
+
count1: 0,
|
|
106
|
+
count2: 0,
|
|
107
|
+
count3: 0,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const Component = mount(renew => {
|
|
111
|
+
const shardStore = assignShardStore(renew, store => [
|
|
112
|
+
store.count1,
|
|
113
|
+
store.count3,
|
|
114
|
+
]);
|
|
115
|
+
|
|
116
|
+
const changeCount1 = () => {
|
|
117
|
+
shardStore.count1 += 1;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const changeCount2 = () => {
|
|
121
|
+
shardStore.count2 += 1;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const changeCount3 = () => {
|
|
125
|
+
shardStore.count3 += 1;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
testChangeRef1.value = changeCount1;
|
|
129
|
+
testChangeRef2.value = changeCount2;
|
|
130
|
+
testChangeRef3.value = changeCount3;
|
|
131
|
+
|
|
132
|
+
return () => (
|
|
133
|
+
<Fragment>
|
|
134
|
+
<button type="button" onClick={changeCount1}>
|
|
135
|
+
count1: {shardStore.count1}
|
|
136
|
+
</button>
|
|
137
|
+
<button type="button" onClick={changeCount2}>
|
|
138
|
+
count2: {shardStore.count2}
|
|
139
|
+
</button>
|
|
140
|
+
<button type="button" onClick={changeCount3}>
|
|
141
|
+
count3: {shardStore.count3}
|
|
142
|
+
</button>
|
|
143
|
+
</Fragment>
|
|
144
|
+
);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
document.body.innerHTML = `<div id="root"><span>1</span><span>2</span><span>3</span></div>`;
|
|
148
|
+
const testbad = document.createElement('div');
|
|
149
|
+
testbad.id = 'root';
|
|
150
|
+
|
|
151
|
+
testbad.innerHTML = '<span>1</span><span>2</span><span>3</span>';
|
|
152
|
+
|
|
153
|
+
const testWrap =
|
|
154
|
+
(document.getElementById('root') as HTMLElement) ||
|
|
155
|
+
document.createElement('div');
|
|
156
|
+
|
|
157
|
+
render(
|
|
158
|
+
<Component />,
|
|
159
|
+
testWrap,
|
|
160
|
+
testWrap.querySelector('span:nth-of-type(2)') as HTMLElement
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
render(
|
|
164
|
+
<Component />,
|
|
165
|
+
testWrap,
|
|
166
|
+
testWrap.querySelector('span:nth-of-type(3)') as HTMLElement
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
if (import.meta.vitest) {
|
|
170
|
+
const { it, expect } = import.meta.vitest;
|
|
171
|
+
|
|
172
|
+
it('If the observer function is specified when assigning from helper/store, only the specified value can be retrieved.', () => {
|
|
173
|
+
expect(testWrap.outerHTML).toBe(
|
|
174
|
+
'<div id="root"><span>1</span><button type="button">count1: 0</button><button type="button">count2: </button><button type="button">count3: 0</button><span>2</span><button type="button">count1: 0</button><button type="button">count2: </button><button type="button">count3: 0</button><span>3</span></div>'
|
|
175
|
+
);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('If you define an observer function to get a value from store, you must also restrict updates to the value.', () => {
|
|
179
|
+
if (testChangeRef1.value && testChangeRef2.value && testChangeRef3.value) {
|
|
180
|
+
testChangeRef1.value();
|
|
181
|
+
testChangeRef2.value();
|
|
182
|
+
testChangeRef3.value();
|
|
183
|
+
}
|
|
184
|
+
nextTick().then(() => {
|
|
185
|
+
expect(testWrap.outerHTML).toBe(
|
|
186
|
+
'<div id="root"><span>1</span><button type="button">count1: 1</button><button type="button">count2: </button><button type="button">count3: 1</button><span>2</span><button type="button">count1: 1</button><button type="button">count2: </button><button type="button">count3: 1</button><span>3</span></div>'
|
|
187
|
+
);
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
}
|