@snupo/jmc 1.3.0 → 1.4.1

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,33 +1,37 @@
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 lazy<T>(f: () => T) { return new Thunk(f) }
32
-
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
+
33
+ export type Live<T> = T | Thunk<T>
34
+
35
+ export function evallive<T>(v: Live<T>): T { return v instanceof Thunk ? v.f() : v }
36
+
33
37
  export function mount(o: Record<string, any>) { Object.assign(globalThis, o) }
package/src/jsx.ts CHANGED
@@ -669,7 +669,7 @@ export interface JmcIntrinsicElements extends IntrinsicElementAttributes {
669
669
  foreignObject: SVGProps<SVGForeignObjectElement>
670
670
  }
671
671
 
672
- /** each prop accepts its value or lazy(() => value) */
672
+ /** each prop accepts its value or live(() => value) */
673
673
  type Thunked<T> = { [K in keyof T]?: T[K] | Thunk<T[K]> }
674
674
 
675
675
  export type HTMLAttributes = Thunked<HTMLAttributesBase>
package/src/props.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { J } from './h.js'
2
- import { Thunk } from './base.js'
2
+ import { evallive, Thunk } from './base.js'
3
3
 
4
4
  /** settings to tweak jmc */
5
5
  export const jmcconfig = {
@@ -25,7 +25,7 @@ let removeprop = (e: Element & J, p: string, old: any) => {
25
25
  /** applies now over old and returns the applied values, with thunks evaluated, to serve as old on the next call */
26
26
  export let setprops = (e: Element & J, old: J = {}, now: J): J => {
27
27
  // evaluate thunks into a copy: now is h.p, which gets rendered again and must keep its thunks
28
- now = Object.fromEntries(Object.entries(now).map(([k, v]) => [k, v instanceof Thunk ? v.f() : v]))
28
+ now = Object.fromEntries(Object.entries(now).map(([k, v]) => [k, evallive(v)]))
29
29
 
30
30
  // removals
31
31
  for (let p in old)