All files / eval / mod.ts

100.00% Branches 5/5
100.00% Lines 25/25
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
27
28
29
30
31
 
x8
 
 
 
x8
x8
x8
x8
x22
x8
x8
x13
x14
x14
x17
x8
x8
x41
x42
x42
x41
x45
x45
x180
x45
x41
x8
 
 
x8




























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

/** `*eval` directive. */
export const _eval = {
  name: "*eval",
  phase: Phase.CUSTOM_PROCESSING,
  init(renderer) {
    renderer.cache<Cache<typeof _eval>>(this.name, new WeakMap())
  },
  execute(renderer, element, { attributes: [attribute], cache }) {
    if (!renderer.isHtmlElement(element)) {
      return
    }
    cache.set(element, attribute)
  },
  async cleanup(renderer, element, { cache, ...options }) {
    if (!renderer.isHtmlElement(element)) {
      return
    }
    if (cache.has(element)) {
      const attribute = cache.get(element)!
      cache.delete(element)
      await renderer.evaluate(element, attribute.value, { ...options, args: [] })
    }
  },
} as Directive<WeakMap<HTMLElement, Attr>>

/** Default exports. */
export default _eval