@snupo/jmc 1.2.0 → 1.4.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/src/base.ts CHANGED
@@ -1,28 +1,33 @@
1
- export function rebind<T extends object>(o: T, proto = Object.getPrototypeOf(o)): T {
2
- Object.entries(Object.getOwnPropertyDescriptors(proto))
3
- .filter(([name, p]) => name != 'constructor' && p.value instanceof Function)
4
- .forEach(([name]) => (o as any)[name] = (o as any)[name].bind(o))
5
- return o
6
- }
7
-
8
- /** wraps o so that each method call is reported to logger, after the method returned */
9
- export const loggedmethodsex = <T extends Record<string, any>>(o: T, logger: (name: string, args: any[], result: any) => void) => new Proxy(o, {
10
- get(target, name: string, receiver) {
11
- if (typeof target[name] !== 'function') return Reflect.get(target, name, receiver)
12
- return function (this: T, ...args: any[]) {
13
- let result = target[name].apply(this, args)
14
- logger(name, args, result)
15
- return result
16
- }
17
- },
18
- })
19
-
20
- export const loggedmethodscolored = <T extends Record<string, any>>(bgcolor: string, o: T): T => loggedmethodsex(o, (name, args, result) => console.log('%c' + name, `background:${bgcolor};color:white;padding:2px;font-weight:bold`, args, result))
21
-
22
- export const loggedmethods = <T extends Record<string, any>>(o: T): T => loggedmethodscolored('#585059', o)
23
-
24
- export function closestby(e: Node | null, pred: (e: Node) => boolean): Node | null { return (e == null || pred(e)) ? e : closestby(e.parentNode, pred) }
25
-
26
- export function lazy<T>(f: () => T) { (f as any).thunk = 1; return f }
27
-
1
+ export function rebind<T extends object>(o: T, proto = Object.getPrototypeOf(o)): T {
2
+ Object.entries(Object.getOwnPropertyDescriptors(proto))
3
+ .filter(([name, p]) => name != 'constructor' && p.value instanceof Function)
4
+ .forEach(([name]) => (o as any)[name] = (o as any)[name].bind(o))
5
+ return o
6
+ }
7
+
8
+ /** wraps o so that each method call is reported to logger, after the method returned */
9
+ export const loggedmethodsex = <T extends Record<string, any>>(o: T, logger: (name: string, args: any[], result: any) => void) => new Proxy(o, {
10
+ get(target, name: string, receiver) {
11
+ if (typeof target[name] !== 'function') return Reflect.get(target, name, receiver)
12
+ return function (this: T, ...args: any[]) {
13
+ let result = target[name].apply(this, args)
14
+ logger(name, args, result)
15
+ return result
16
+ }
17
+ },
18
+ })
19
+
20
+ export const loggedmethodscolored = <T extends Record<string, any>>(bgcolor: string, o: T): T => loggedmethodsex(o, (name, args, result) => console.log('%c' + name, `background:${bgcolor};color:white;padding:2px;font-weight:bold`, args, result))
21
+
22
+ export const loggedmethods = <T extends Record<string, any>>(o: T): T => loggedmethodscolored('#585059', o)
23
+
24
+ export function closestby(e: Node | null, pred: (e: Node) => boolean): Node | null { return (e == null || pred(e)) ? e : closestby(e.parentNode, pred) }
25
+
26
+ /** a prop value evaluated at each render */
27
+ export class Thunk<T> {
28
+ constructor(public f: () => T) { }
29
+ }
30
+
31
+ export function live<T>(f: () => T) { return new Thunk(f) }
32
+
28
33
  export function mount(o: Record<string, any>) { Object.assign(globalThis, o) }
package/src/jmc.ts CHANGED
@@ -25,6 +25,9 @@ type JNode<N extends Node = ChildNode> = N & {
25
25
  ep?: J
26
26
  }
27
27
 
28
+ /** mounted callbacks collected by sync, run after it in the lifecycle phase. children before their parents */
29
+ let mounts: (() => void)[] = []
30
+
28
31
  let removeexcesschildren = (n: Element, i: number) => {
29
32
  let c: ChildNode
30
33
  while ((c = n.childNodes[i])) {
@@ -106,7 +109,7 @@ function sync(
106
109
  removeexcesschildren(n, j)
107
110
  }
108
111
  }
109
- if (fresh) p.mounted?.(n)
112
+ if (fresh && p.mounted) mounts.push(() => p.mounted(n))
110
113
 
111
114
  return i + 1
112
115
  }
@@ -130,7 +133,7 @@ function sync(
130
133
  let j = sync(parent, i, hv, old ?? h, depth, uc) // the attached H, so updates find the instance
131
134
  assertnonfragment(h, hv, i, j)
132
135
  ci.element = parent.childNodes[i]
133
- if (!old) ci.mounted?.()
136
+ if (!old && ci.mounted) mounts.push(() => ci.mounted!())
134
137
  return j
135
138
  }
136
139
 
@@ -166,6 +169,12 @@ export function patch(
166
169
  let p = e.parentElement!
167
170
  let i = [...p.childNodes].indexOf(e as ChildNode)
168
171
  sync(p, i, h, undefined, depth, uc)
172
+
173
+ // lifecycle phase: the dom is complete now. taken first, a mounted callback calling updateview collects anew
174
+ let ms = mounts
175
+ mounts = []
176
+ ms.forEach(m => m())
177
+
169
178
  return p.childNodes[i] as HTMLElement
170
179
  }
171
180