(
_owner?: Solid.Owner,
{initialRun = true}: DebugComputationOptions = {},
)
| 99 | */ |
| 100 | export function debugComputation(owner?: Solid.Owner, options?: DebugComputationOptions): void |
| 101 | export function debugComputation( |
| 102 | _owner?: Solid.Owner, |
| 103 | {initialRun = true}: DebugComputationOptions = {}, |
| 104 | ): void { |
| 105 | const owner = _owner === undefined ? getOwner() : _owner |
| 106 | if (!owner || !isSolidComputation(owner)) return console.warn('owner is not a computation') |
| 107 | |
| 108 | if (markDebugNode(owner, 'computation') === true) return |
| 109 | |
| 110 | const {type, typeName, name} = getNodeState(owner) |
| 111 | const SYMBOL = Symbol(name) |
| 112 | // log prev value only of the computation callback uses it |
| 113 | const usesPrev = !!owner.fn.length |
| 114 | const usesValue = usesPrev || type === NodeType.Memo |
| 115 | |
| 116 | let updateListeners: VoidFunction[] = [] |
| 117 | let signalUpdates: NodeStateWithValue[] = [] |
| 118 | |
| 119 | // patches source objects to track their value updates |
| 120 | // will unsubscribe from previous sources on each call |
| 121 | const observeSources = (sources: (Solid.Computation | Solid.Signal)[]): void => { |
| 122 | updateListeners.forEach(unsub => unsub()) |
| 123 | updateListeners = [] |
| 124 | sources.forEach(source => { |
| 125 | observeValueUpdate( |
| 126 | source, |
| 127 | value => signalUpdates.push({...getNodeState(source), value}), |
| 128 | SYMBOL, |
| 129 | ) |
| 130 | updateListeners.push(() => removeValueUpdateObserver(source, SYMBOL)) |
| 131 | }) |
| 132 | } |
| 133 | |
| 134 | // this is for logging the initial state after the first callback execution |
| 135 | // the "value" property is monkey patched for one function execution |
| 136 | if (initialRun) { |
| 137 | observeValueUpdate( |
| 138 | owner, |
| 139 | value => { |
| 140 | const timeElapsed = time() |
| 141 | removeValueUpdateObserver(owner, SYMBOL) |
| 142 | const sources = owner.sources ? array.deduped(owner.sources) : [] |
| 143 | |
| 144 | logComputation(getComputationCreatedLabel(typeName, name, timeElapsed), { |
| 145 | owner: {type, typeName, name}, |
| 146 | owned: owner.owned ?? [], |
| 147 | sources, |
| 148 | prev: UNUSED, |
| 149 | value: usesValue ? value : UNUSED, |
| 150 | causedBy: null, |
| 151 | }) |
| 152 | observeSources(sources) |
| 153 | }, |
| 154 | SYMBOL, |
| 155 | ) |
| 156 | } |
| 157 | // if debugComputation after the initial run of the computation |
| 158 | // sources should be observed immediately |
no test coverage detected
searching dependent graphs…