|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {COMPUTING, ERRORED, UNSET} from './computed'; |
| 10 | +import {defaultEquals, ValueEqualityFn} from './equality'; |
| 11 | +import { |
| 12 | + consumerAfterComputation, |
| 13 | + consumerBeforeComputation, |
| 14 | + producerAccessed, |
| 15 | + producerMarkClean, |
| 16 | + producerUpdateValueVersion, |
| 17 | + REACTIVE_NODE, |
| 18 | + ReactiveNode, |
| 19 | + SIGNAL, |
| 20 | +} from './graph'; |
| 21 | +import {signalSetFn, signalUpdateFn} from './signal'; |
| 22 | + |
| 23 | +export type ComputationFn<S, D> = (source: S, previous?: {source: S; value: D}) => D; |
| 24 | + |
| 25 | +export interface LinkedSignalNode<S, D> extends ReactiveNode { |
| 26 | + /** |
| 27 | + * Value of the source signal that was used to derive the computed value. |
| 28 | + */ |
| 29 | + sourceValue: S; |
| 30 | + |
| 31 | + /** |
| 32 | + * Current state value, or one of the sentinel values (`UNSET`, `COMPUTING`, |
| 33 | + * `ERROR`). |
| 34 | + */ |
| 35 | + value: D; |
| 36 | + |
| 37 | + /** |
| 38 | + * If `value` is `ERRORED`, the error caught from the last computation attempt which will |
| 39 | + * be re-thrown. |
| 40 | + */ |
| 41 | + error: unknown; |
| 42 | + |
| 43 | + /** |
| 44 | + * The source function represents reactive dependency based on which the linked state is reset. |
| 45 | + */ |
| 46 | + source: () => S; |
| 47 | + |
| 48 | + /** |
| 49 | + * The computation function which will produce a new value based on the source and, optionally - previous values. |
| 50 | + */ |
| 51 | + computation: ComputationFn<S, D>; |
| 52 | + |
| 53 | + equal: ValueEqualityFn<D>; |
| 54 | +} |
| 55 | + |
| 56 | +export type LinkedSignalGetter<S, D> = (() => D) & { |
| 57 | + [SIGNAL]: LinkedSignalNode<S, D>; |
| 58 | +}; |
| 59 | + |
| 60 | +export function createLinkedSignal<S, D>( |
| 61 | + sourceFn: () => S, |
| 62 | + computationFn: ComputationFn<S, D>, |
| 63 | + equalityFn?: ValueEqualityFn<D>, |
| 64 | +): LinkedSignalGetter<S, D> { |
| 65 | + const node: LinkedSignalNode<S, D> = Object.create(LINKED_SIGNAL_NODE); |
| 66 | + |
| 67 | + node.source = sourceFn; |
| 68 | + node.computation = computationFn; |
| 69 | + if (equalityFn != undefined) { |
| 70 | + node.equal = equalityFn; |
| 71 | + } |
| 72 | + |
| 73 | + const linkedSignalGetter = () => { |
| 74 | + // Check if the value needs updating before returning it. |
| 75 | + producerUpdateValueVersion(node); |
| 76 | + |
| 77 | + // Record that someone looked at this signal. |
| 78 | + producerAccessed(node); |
| 79 | + |
| 80 | + if (node.value === ERRORED) { |
| 81 | + throw node.error; |
| 82 | + } |
| 83 | + |
| 84 | + return node.value; |
| 85 | + }; |
| 86 | + |
| 87 | + const getter = linkedSignalGetter as LinkedSignalGetter<S, D>; |
| 88 | + getter[SIGNAL] = node; |
| 89 | + |
| 90 | + return getter; |
| 91 | +} |
| 92 | + |
| 93 | +export function linkedSignalSetFn<S, D>(node: LinkedSignalNode<S, D>, newValue: D) { |
| 94 | + producerUpdateValueVersion(node); |
| 95 | + signalSetFn(node, newValue); |
| 96 | + producerMarkClean(node); |
| 97 | +} |
| 98 | + |
| 99 | +export function linkedSignalUpdateFn<S, D>( |
| 100 | + node: LinkedSignalNode<S, D>, |
| 101 | + updater: (value: D) => D, |
| 102 | +): void { |
| 103 | + producerUpdateValueVersion(node); |
| 104 | + signalUpdateFn(node, updater); |
| 105 | + producerMarkClean(node); |
| 106 | +} |
| 107 | + |
| 108 | +// Note: Using an IIFE here to ensure that the spread assignment is not considered |
| 109 | +// a side-effect, ending up preserving `LINKED_SIGNAL_NODE` and `REACTIVE_NODE`. |
| 110 | +// TODO: remove when https://github.com/evanw/esbuild/issues/3392 is resolved. |
| 111 | +export const LINKED_SIGNAL_NODE = /* @__PURE__ */ (() => { |
| 112 | + return { |
| 113 | + ...REACTIVE_NODE, |
| 114 | + value: UNSET, |
| 115 | + dirty: true, |
| 116 | + error: null, |
| 117 | + equal: defaultEquals, |
| 118 | + |
| 119 | + producerMustRecompute(node: LinkedSignalNode<unknown, unknown>): boolean { |
| 120 | + // Force a recomputation if there's no current value, or if the current value is in the |
| 121 | + // process of being calculated (which should throw an error). |
| 122 | + return node.value === UNSET || node.value === COMPUTING; |
| 123 | + }, |
| 124 | + |
| 125 | + producerRecomputeValue(node: LinkedSignalNode<unknown, unknown>): void { |
| 126 | + if (node.value === COMPUTING) { |
| 127 | + // Our computation somehow led to a cyclic read of itself. |
| 128 | + throw new Error('Detected cycle in computations.'); |
| 129 | + } |
| 130 | + |
| 131 | + const oldValue = node.value; |
| 132 | + node.value = COMPUTING; |
| 133 | + |
| 134 | + const prevConsumer = consumerBeforeComputation(node); |
| 135 | + let newValue: unknown; |
| 136 | + try { |
| 137 | + const newSourceValue = node.source(); |
| 138 | + const prev = |
| 139 | + oldValue === UNSET || oldValue === ERRORED |
| 140 | + ? undefined |
| 141 | + : { |
| 142 | + source: node.sourceValue, |
| 143 | + value: oldValue, |
| 144 | + }; |
| 145 | + newValue = node.computation(newSourceValue, prev); |
| 146 | + node.sourceValue = newSourceValue; |
| 147 | + } catch (err) { |
| 148 | + newValue = ERRORED; |
| 149 | + node.error = err; |
| 150 | + } finally { |
| 151 | + consumerAfterComputation(node, prevConsumer); |
| 152 | + } |
| 153 | + |
| 154 | + if (oldValue !== UNSET && newValue !== ERRORED && node.equal(oldValue, newValue)) { |
| 155 | + // No change to `valueVersion` - old and new values are |
| 156 | + // semantically equivalent. |
| 157 | + node.value = oldValue; |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + node.value = newValue; |
| 162 | + node.version++; |
| 163 | + }, |
| 164 | + }; |
| 165 | +})(); |
0 commit comments