All files / src/projection/node group.ts

42.86% Statements 6/14
0% Branches 0/6
20% Functions 1/5
46.15% Lines 6/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 32 33 34 35    32x                 32x 8x 8x   8x   8x                                  
import { IProjectionNode } from "./types"
 
const notify = (node: IProjectionNode) =>
    !node.isLayoutDirty && node.willUpdate(false)
 
export interface NodeGroup {
    add: (node: IProjectionNode) => void
    remove: (node: IProjectionNode) => void
    dirty: VoidFunction
}
 
export function nodeGroup(): NodeGroup {
    const nodes = new Set<IProjectionNode>()
    const subscriptions = new WeakMap<IProjectionNode, () => void>()
 
    const dirtyAll = () => nodes.forEach(notify)
 
    return {
        add: (node) => {
            nodes.add(node)
            subscriptions.set(
                node,
                node.addEventListener("willUpdate", dirtyAll)
            )
        },
        remove: (node) => {
            nodes.delete(node)
            subscriptions.get(node)?.()
            subscriptions.delete(node)
            dirtyAll()
        },
        dirty: dirtyAll,
    }
}