All files / set / mod.ts

100.00% Branches 5/5
100.00% Lines 20/20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 
x8
 
 
 
x8
x8
x8
x8
x28
x8
x8
x20
x31
x31
x32
x32
x32
x31
x31
x93
x20
x8
 
 
x8























// Imports
import { type Cache, type Context, type Directive, type Nullable, Phase } from "@mizu/internal/engine"
export type * from "@mizu/internal/engine"

/** `*set` directive. */
export const _set = {
  name: "*set",
  phase: Phase.CONTEXT,
  init(renderer) {
    renderer.cache<Cache<typeof _set>>(this.name, new WeakMap())
  },
  async execute(renderer, element, { attributes: [attribute], cache, ...options }) {
    if (!cache.has(element)) {
      const context = await renderer.evaluate(element, attribute.value, options)
      if (typeof context !== "object") {
        renderer.warn(`[${this.name}] expects an object but got ${typeof context}, ignoring`, element)
        return
      }
      cache.set(element, context ? options.context.with(context as Record<PropertyKey, unknown>) : null)
    }
    return { context: cache.get(element) }
  },
} as Directive<WeakMap<HTMLElement | Comment, Nullable<Context>>>

/** Default exports. */
export default _set