( node: FormKitNode, dependencies?: FormKitDependencies )
| 73 | * @public |
| 74 | */ |
| 75 | export function createObserver( |
| 76 | node: FormKitNode, |
| 77 | dependencies?: FormKitDependencies |
| 78 | ): FormKitObservedNode { |
| 79 | // The dependencies touched during tracking |
| 80 | const deps: FormKitDependencies = |
| 81 | dependencies || Object.assign(new Map(), { active: false }) |
| 82 | // A registry of event receipts returned by the event system |
| 83 | const receipts: FormKitObserverReceipts = new Map() |
| 84 | |
| 85 | /** |
| 86 | * Simple function to add a dependency to the deps map. |
| 87 | * @param event - The name of the event type (like commit/input etc) |
| 88 | */ |
| 89 | const addDependency = function (event: string) { |
| 90 | if (!deps.active) return |
| 91 | if (!deps.has(node)) deps.set(node, new Set()) |
| 92 | deps.get(node)?.add(event) |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Proxies the props of a node so we know which ones were messed with, could |
| 97 | * potentially be more generalized in the future if we want to support |
| 98 | * more sub-objects. |
| 99 | * @param props - The props object from a node |
| 100 | * @returns |
| 101 | */ |
| 102 | const observeProps = function (props: FormKitProps) { |
| 103 | return new Proxy(props, { |
| 104 | get(...args) { |
| 105 | typeof args[1] === 'string' && addDependency(`prop:${args[1]}`) |
| 106 | return Reflect.get(...args) |
| 107 | }, |
| 108 | }) |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Observes the FormKit ledger "value". |
| 113 | * @param ledger - A formkit ledger counter. |
| 114 | */ |
| 115 | const observeLedger = function (ledger: FormKitLedger) { |
| 116 | return new Proxy(ledger, { |
| 117 | get(...args) { |
| 118 | if (args[1] === 'value') { |
| 119 | return (key: string) => { |
| 120 | addDependency(`count:${key}`) |
| 121 | return ledger.value(key) |
| 122 | } |
| 123 | } |
| 124 | return Reflect.get(...args) |
| 125 | }, |
| 126 | }) |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Return values from our observer proxy first pass through this function |
| 131 | * which gives us a chance to listen sub-dependencies and properties. |
| 132 | */ |
no outgoing calls
no test coverage detected