All files / once / 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
x16
x8
x8
x37
x39
x39
x8
x8
x37
x37
x39
x39
x111
x37
x62
x62
x41
x37
x8
 
 
x8




























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

/** `*once` directive. */
export const _once = {
  name: "*once",
  phase: Phase.POSTPROCESSING,
  init(renderer) {
    renderer.cache<Cache<typeof _once>>(this.name, new WeakSet())
  },
  setup(_, element, { cache }) {
    if (cache.has(element)) {
      return false
    }
  },
  cleanup(renderer, element, { cache }) {
    let target = element
    if ((renderer.isComment(element)) && (renderer.cache("*").has(element))) {
      target = renderer.cache("*").get(element)!
    }
    const attribute = renderer.getAttributes(target, this.name, { first: true })
    if (!attribute) {
      return
    }
    cache.add(element)
  },
} as Directive<WeakSet<HTMLElement | Comment>>

/** Default exports. */
export default _once