@snupo/jmc 1.0.8 → 1.0.10
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/jmc.d.ts +2 -1
- package/dist/jmc.d.ts.map +1 -1
- package/dist/jmc.js +22 -9
- package/dist/jmc.js.map +1 -1
- package/package.json +1 -1
package/dist/jmc.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { H, IUpdateContext, Selectors } from './h';
|
|
1
|
+
import type { H, IUpdateContext, Selectors, Func } from './h';
|
|
2
|
+
export declare const lazy: <T>(expr: Func<T>) => T;
|
|
2
3
|
export declare function patch(e: Node | null, h: H, uc?: IUpdateContext): void;
|
|
3
4
|
export declare function updateviewuc(u: IUpdateContext, ...sels: Selectors): void;
|
|
4
5
|
export declare function updateview(...sels: Selectors): void;
|
package/dist/jmc.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jmc.d.ts","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"jmc.d.ts","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,CAAC,EAAY,cAAc,EAAe,SAAS,EAAY,IAAI,EAAE,MAAM,KAAK,CAAA;AAoD9F,eAAO,MAAM,IAAI,GAAI,CAAC,QAAQ,IAAI,CAAC,CAAC,CAAC,KAAG,CAAc,CAAA;AAsFtD,wBAAgB,KAAK,CAAC,CAAC,EAAE,IAAI,GAAG,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,GAAS,cAAc,QAQpE;AAED,wBAAgB,YAAY,CAAC,CAAC,EAAE,cAAc,EAAE,GAAG,IAAI,EAAE,SAAS,GAAG,IAAI,CAAmC;AAC5G,wBAAgB,UAAU,CAAC,GAAG,IAAI,EAAE,SAAS,GAAG,IAAI,CAAoC"}
|
package/dist/jmc.js
CHANGED
|
@@ -31,8 +31,19 @@ let syncchildren = (uc, p, cn, i, ns) => {
|
|
|
31
31
|
cn.flat().forEach(hc => (i = sync(uc, p, i, hc, ns)));
|
|
32
32
|
return i;
|
|
33
33
|
};
|
|
34
|
+
// tbd: is update in place possible? - add tests for lazy update
|
|
35
|
+
function evalprops(p) {
|
|
36
|
+
if (!p)
|
|
37
|
+
return p;
|
|
38
|
+
const pr = {};
|
|
39
|
+
for (const k in p) {
|
|
40
|
+
pr[k] = p[k] instanceof Function ? p[k]() : p[k];
|
|
41
|
+
}
|
|
42
|
+
return pr;
|
|
43
|
+
}
|
|
44
|
+
export const lazy = (expr) => expr; // fake alert! in fact we pass the expression through, the framework evaluates the props later lazily
|
|
34
45
|
function sync(uc, parent, i, h, ns) {
|
|
35
|
-
// console.log('%csync2', "background:orange", parent.tagName, i, h
|
|
46
|
+
// console.log('%csync2', "background:orange", parent.tagName, i, h)
|
|
36
47
|
if (h === null || h === undefined)
|
|
37
48
|
return i; // skip. keep 0 and false renderable, per comment below
|
|
38
49
|
let c = parent.childNodes[i];
|
|
@@ -41,16 +52,17 @@ function sync(uc, parent, i, h, ns) {
|
|
|
41
52
|
let n;
|
|
42
53
|
let { tag, cn, p } = h;
|
|
43
54
|
cn ??= [];
|
|
55
|
+
let pr = evalprops(p);
|
|
44
56
|
if (c?.tagName?.toLowerCase() != tag.toLowerCase()) {
|
|
45
|
-
n = document.createElementNS(ns =
|
|
57
|
+
n = document.createElementNS(ns = pr?.xmlns ?? ns, tag); // replace namespace with propertyvalue if set. otherwise pass forward to children
|
|
46
58
|
c ? c.replaceWith(n) : parent.appendChild(n);
|
|
47
|
-
setprops(n, {},
|
|
48
|
-
|
|
59
|
+
setprops(n, {}, pr);
|
|
60
|
+
pr?.mounted?.(n);
|
|
49
61
|
}
|
|
50
62
|
else {
|
|
51
63
|
n = c;
|
|
52
|
-
setprops(n, n.h?.p,
|
|
53
|
-
if (
|
|
64
|
+
setprops(n, n.h?.p, pr); // tbd: this is wrong - try to break this with property updates, just patch sequences - create an own patch test file
|
|
65
|
+
if (pr?.update?.(c, uc))
|
|
54
66
|
return i + 1;
|
|
55
67
|
}
|
|
56
68
|
n.h = h;
|
|
@@ -64,7 +76,7 @@ function sync(uc, parent, i, h, ns) {
|
|
|
64
76
|
return syncchildren(uc, parent, h.cn ?? [], i, ns);
|
|
65
77
|
case 2 /* HKind.FunctionComponent */:
|
|
66
78
|
{
|
|
67
|
-
let hr = h.tag(h.p, h.cn);
|
|
79
|
+
let hr = h.tag(evalprops(h.p), h.cn);
|
|
68
80
|
let j = sync(uc, parent, i, hr, ns);
|
|
69
81
|
let n = parent.childNodes[i]; // now the dom element exists, unless the function returned undefined or null
|
|
70
82
|
if (n)
|
|
@@ -74,8 +86,9 @@ function sync(uc, parent, i, h, ns) {
|
|
|
74
86
|
case 3 /* HKind.ClassComponent */:
|
|
75
87
|
{
|
|
76
88
|
let isupdate = c?.h?.tag == h.tag;
|
|
77
|
-
let
|
|
78
|
-
ci
|
|
89
|
+
let pr = evalprops(h.p);
|
|
90
|
+
let ci = h.i = isupdate ? h.i : rebind(new h.tag(pr));
|
|
91
|
+
ci.props = pr; // for the update case we set props
|
|
79
92
|
// if component instance returns truthy for update(), then this update substitutes syncing
|
|
80
93
|
if (isupdate && ci.update?.(uc))
|
|
81
94
|
return i + 1;
|
package/dist/jmc.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jmc.js","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAOlC,IAAI,oBAAoB,GAAG,CAAC,CAAU,EAAE,CAAS,EAAE,EAAE;IACjD,IAAI,CAAY,CAAA;IAChB,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC3B,IAAI,CAAC;YAAC,CAAC,CAAC,MAAM,EAAE,CAAA;QAAC,CAAC;QAAC,OAAO,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AC,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAE,EAAE,EAAE,CAAC,CAAA,CAAC,CAAC,CAAC,CAAA;AAC7C,CAAC","sourcesContent":["import { rebind } from './base.js'\r\nimport { Expr, H, HElement, HKind, IUpdateContext, J, Selector, Selectors, Children } from './h'\r\n\r\nconst enum NodeType { // vaporizes (but for that must be in this file, otherwise not)\r\n TextNode = 3\r\n}\r\n\r\nlet removeexcesschildren = (n: Element, i: number) => {\r\n let c: ChildNode\r\n while ((c = n.childNodes[i])) {\r\n try { c.remove() } catch (_) { } // due to blur-event-handlers, this can fail\r\n }\r\n}\r\n\r\nlet iswebcomponent = (h: HElement) => h.tag.includes('-')\r\n\r\nconst isproperty = (e: Element, name: string, value: any) =>\r\n name !== \"viewBox\" &&\r\n (value instanceof Object ||\r\n value instanceof Function ||\r\n (e.namespaceURI !== \"http://www.w3.org/2000/svg\" && name in e && name != 'style'))\r\n\r\nlet setprops = (e: Element & J, old: J | undefined, now: J | undefined) => {\r\n old ??= {}\r\n now ??= {}\r\n for (let p in old)\r\n if (!(p in now))\r\n isproperty(e, p, old[p])\r\n ? e[p] = typeof old[p] === 'boolean' ? false : '' // not sure about this. safest handling of initial values would be to track them. tbd: unit test\r\n : e.removeAttribute(p)\r\n for (let p in now)\r\n isproperty(e, p, now[p])\r\n ? e[p] = now[p]\r\n : e.setAttribute(p, now[p])\r\n}\r\n\r\n/** synchronizes children starting at the i-th element. returns the index of the last child synchronized */\r\nlet syncchildren = (uc: IUpdateContext, p: Element, cn: Children, i: number, ns: string): number => {\r\n cn.flat().forEach(hc => (i = sync(uc, p, i, hc, ns)))\r\n return i\r\n}\r\n\r\nfunction sync(uc: IUpdateContext, parent: Element, i: number, h: H | undefined, ns: string): number {\r\n\r\n // console.log('%csync2', \"background:orange\", parent.tagName, i, h, ns)\r\n\r\n if (h === null || h === undefined) return i // skip. keep 0 and false renderable, per comment below\r\n let c = parent.childNodes[i]\r\n\r\n switch (h.kind) {\r\n case HKind.Element:\r\n\r\n let n: Element\r\n let { tag, cn, p } = h\r\n cn ??= []\r\n\r\n if ((<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()) {\r\n n = document.createElementNS(ns = p?.xmlns ?? ns, tag) // replace namespace with propertyvalue if set. otherwise pass forward to children\r\n c ? c.replaceWith(n) : parent.appendChild(n) \r\n setprops(n, {}, p)\r\n p?.mounted?.(n)\r\n } else {\r\n n = c as Element\r\n setprops(n, n.h?.p, p) // tbd: this is wrong - try to break this with property updates, just patch sequences - create an own patch test file\r\n if (p?.update?.(c, uc)) return i + 1\r\n }\r\n\r\n n.h = h\r\n\r\n if (!uc.patchElementOnly && !iswebcomponent(h as HElement)) {\r\n // tbd: make \"island\" attribute\r\n const j = syncchildren(uc, n, cn, 0, ns)\r\n removeexcesschildren(n, j)\r\n }\r\n\r\n return i + 1\r\n\r\n case HKind.Fragment:\r\n return syncchildren(uc, parent, h.cn ?? [], i, ns)\r\n\r\n case HKind.FunctionComponent:\r\n {\r\n let hr = h.tag(h.p, h.cn)\r\n let j = sync(uc, parent, i, hr, ns)\r\n let n = parent.childNodes[i]! // now the dom element exists, unless the function returned undefined or null\r\n if (n) n.h = h\r\n return j\r\n }\r\n\r\n case HKind.ClassComponent:\r\n {\r\n let isupdate = (c?.h as any)?.tag == h.tag\r\n\r\n let ci = h.i = isupdate ? h.i! : rebind(new h.tag(h.p))\r\n ci.props = h.p // for the update case we set props\r\n\r\n // if component instance returns truthy for update(), then this update substitutes syncing\r\n if (isupdate && ci.update?.(uc)) return i + 1\r\n\r\n let hr = ci.view()\r\n let j = sync(uc, parent, i, hr, ns)\r\n let n = parent.childNodes[i]!\r\n if (n) n.h = h // tbd: this could disallow component updates ?? i guess it is right\r\n\r\n ci.element = n\r\n if (!isupdate) ci?.mounted?.()\r\n\r\n return j\r\n }\r\n\r\n case undefined:\r\n // textnode\r\n let s = h as string // coerce type as the js runtime does\r\n if (c?.nodeType == NodeType.TextNode) {\r\n if (c.textContent != s) c.textContent = s // firefox updates equal text, loosing existing text selection\r\n } else {\r\n c ? c.replaceWith(s) : parent.append(s)\r\n }\r\n return i + 1\r\n }\r\n}\r\n\r\nexport function patch(e: Node | null, h: H, uc = {} as IUpdateContext) {\r\n if (!e) return\r\n delete uc.patchElementOnly\r\n if (uc.replace) (e as HTMLElement).replaceChildren()\r\n const p = e.parentElement as HTMLElement\r\n const i = [].indexOf.call<any, any, any>(p.childNodes, e)\r\n // always called deferred, because removing elements can trigger events and their handlers (like blur)\r\n sync(uc, p, i, h, p.namespaceURI!) // is this safe? use default html namespace?\r\n}\r\n\r\nexport function updateviewuc(u: IUpdateContext, ...sels: Selectors): void { updateviewinternal(u, ...sels) }\r\nexport function updateview(...sels: Selectors): void { updateviewinternal({}, ...sels) }\r\n\r\nfunction updateviewinternal(uc: IUpdateContext, ...sels: Selector[]): void {\r\n if (!sels.length) sels.push('body')\r\n sels.flatMap(s => (typeof s == 'string' ? [...document.querySelectorAll(s)] : s ? [s] : []))\r\n .forEach(e => { patch(e, e.h!, uc) })\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"jmc.js","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAQlC,IAAI,oBAAoB,GAAG,CAAC,CAAU,EAAE,CAAS,EAAE,EAAE;IACjD,IAAI,CAAY,CAAA;IAChB,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC3B,IAAI,CAAC;YAAC,CAAC,CAAC,MAAM,EAAE,CAAA;QAAC,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,4CAA4C;IACjF,CAAC;AACL,CAAC,CAAA;AAED,IAAI,cAAc,GAAG,CAAC,CAAW,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;AA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AAC,CAAC;AAExF,SAAS,kBAAkB,CAAC,EAAkB,EAAE,GAAG,IAAgB;IAC/D,IAAI,CAAC,IAAI,CAAC,MAAM;QAAE,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IACnC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;SACvF,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAE,EAAE,EAAE,CAAC,CAAA,CAAC,CAAC,CAAC,CAAA;AAC7C,CAAC","sourcesContent":["import { rebind } from './base.js'\r\nimport type { H, HElement, IUpdateContext, J, Selector, Selectors, Children, Func } from './h'\r\nimport { HKind } from './h'\r\n\r\nconst enum NodeType { // vaporizes (but for that must be in this file, otherwise not)\r\n TextNode = 3\r\n}\r\n\r\nlet removeexcesschildren = (n: Element, i: number) => {\r\n let c: ChildNode\r\n while ((c = n.childNodes[i])) {\r\n try { c.remove() } catch (_) { } // due to blur-event-handlers, this can fail\r\n }\r\n}\r\n\r\nlet iswebcomponent = (h: HElement) => h.tag.includes('-')\r\n\r\nconst isproperty = (e: Element, name: string, value: any) =>\r\n name !== \"viewBox\" &&\r\n (value instanceof Object ||\r\n value instanceof Function ||\r\n (e.namespaceURI !== \"http://www.w3.org/2000/svg\" && name in e && name != 'style'))\r\n\r\nlet setprops = (e: Element & J, old: J | undefined, now: J | undefined) => {\r\n old ??= {}\r\n now ??= {}\r\n for (let p in old)\r\n if (!(p in now))\r\n isproperty(e, p, old[p])\r\n ? e[p] = typeof old[p] === 'boolean' ? false : '' // not sure about this. safest handling of initial values would be to track them. tbd: unit test\r\n : e.removeAttribute(p)\r\n for (let p in now)\r\n isproperty(e, p, now[p])\r\n ? e[p] = now[p]\r\n : e.setAttribute(p, now[p])\r\n}\r\n\r\n/** synchronizes children starting at the i-th element. returns the index of the last child synchronized */\r\nlet syncchildren = (uc: IUpdateContext, p: Element, cn: Children, i: number, ns: string): number => {\r\n cn.flat().forEach(hc => (i = sync(uc, p, i, hc, ns)))\r\n return i\r\n}\r\n\r\n// tbd: is update in place possible? - add tests for lazy update\r\nfunction evalprops(p: J | undefined): J | undefined {\r\n if (!p) return p\r\n const pr = {} as J\r\n for (const k in p) {\r\n pr[k] = p[k] instanceof Function ? p[k]() : p[k]\r\n }\r\n return pr\r\n}\r\n\r\nexport const lazy = <T>(expr: Func<T>): T => expr as T // fake alert! in fact we pass the expression through, the framework evaluates the props later lazily\r\n\r\nfunction sync(uc: IUpdateContext, parent: Element, i: number, h: H | undefined, ns: string): number {\r\n\r\n // console.log('%csync2', \"background:orange\", parent.tagName, i, h)\r\n\r\n if (h === null || h === undefined) return i // skip. keep 0 and false renderable, per comment below\r\n let c = parent.childNodes[i]\r\n\r\n switch (h.kind) {\r\n case HKind.Element:\r\n\r\n let n: Element\r\n let { tag, cn, p } = h\r\n cn ??= []\r\n\r\n let pr = evalprops(p)\r\n\r\n if ((<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()) {\r\n n = document.createElementNS(ns = pr?.xmlns ?? ns, tag) // replace namespace with propertyvalue if set. otherwise pass forward to children\r\n c ? c.replaceWith(n) : parent.appendChild(n)\r\n setprops(n, {}, pr)\r\n pr?.mounted?.(n)\r\n } else {\r\n n = c as Element\r\n setprops(n, n.h?.p, pr) // tbd: this is wrong - try to break this with property updates, just patch sequences - create an own patch test file\r\n if (pr?.update?.(c, uc)) return i + 1\r\n }\r\n\r\n n.h = h\r\n\r\n if (!uc.patchElementOnly && !iswebcomponent(h as HElement)) {\r\n // tbd: make \"island\" attribute\r\n const j = syncchildren(uc, n, cn, 0, ns)\r\n removeexcesschildren(n, j)\r\n }\r\n\r\n return i + 1\r\n\r\n case HKind.Fragment:\r\n return syncchildren(uc, parent, h.cn ?? [], i, ns)\r\n\r\n case HKind.FunctionComponent:\r\n {\r\n let hr = h.tag(evalprops(h.p), h.cn)\r\n let j = sync(uc, parent, i, hr, ns)\r\n let n = parent.childNodes[i]! // now the dom element exists, unless the function returned undefined or null\r\n if (n) n.h = h\r\n return j\r\n }\r\n\r\n case HKind.ClassComponent:\r\n {\r\n let isupdate = (c?.h as any)?.tag == h.tag\r\n\r\n let pr = evalprops(h.p)\r\n\r\n let ci = h.i = isupdate ? h.i! : rebind(new h.tag(pr))\r\n ci.props = pr // for the update case we set props\r\n\r\n // if component instance returns truthy for update(), then this update substitutes syncing\r\n if (isupdate && ci.update?.(uc)) return i + 1\r\n\r\n let hr = ci.view()\r\n let j = sync(uc, parent, i, hr, ns)\r\n let n = parent.childNodes[i]!\r\n if (n) n.h = h // tbd: this could disallow component updates ?? i guess it is right\r\n\r\n ci.element = n\r\n if (!isupdate) ci?.mounted?.()\r\n\r\n return j\r\n }\r\n\r\n case undefined:\r\n // textnode\r\n let s = h as string // coerce type as the js runtime does\r\n if (c?.nodeType == NodeType.TextNode) {\r\n if (c.textContent != s) c.textContent = s // firefox updates equal text, loosing existing text selection\r\n } else {\r\n c ? c.replaceWith(s) : parent.append(s)\r\n }\r\n return i + 1\r\n }\r\n}\r\n\r\nexport function patch(e: Node | null, h: H, uc = {} as IUpdateContext) {\r\n if (!e) return\r\n delete uc.patchElementOnly\r\n if (uc.replace) (e as HTMLElement).replaceChildren()\r\n const p = e.parentElement as HTMLElement\r\n const i = [].indexOf.call<any, any, any>(p.childNodes, e)\r\n // always called deferred, because removing elements can trigger events and their handlers (like blur)\r\n sync(uc, p, i, h, p.namespaceURI!) // is this safe? use default html namespace?\r\n}\r\n\r\nexport function updateviewuc(u: IUpdateContext, ...sels: Selectors): void { updateviewinternal(u, ...sels) }\r\nexport function updateview(...sels: Selectors): void { updateviewinternal({}, ...sels) }\r\n\r\nfunction updateviewinternal(uc: IUpdateContext, ...sels: Selector[]): void {\r\n if (!sels.length) sels.push('body')\r\n sels.flatMap(s => (typeof s == 'string' ? [...document.querySelectorAll(s)] : s ? [s] : []))\r\n .forEach(e => { patch(e, e.h!, uc) })\r\n}\r\n"]}
|