MCPcopy Index your code
hub / github.com/angular/angular / createWatch

Function createWatch

packages/core/primitives/signals/src/watch.ts:65–137  ·  view source on GitHub ↗
(
  fn: (onCleanup: WatchCleanupRegisterFn) => void,
  schedule: (watch: Watch) => void,
  allowSignalWrites: boolean,
)

Source from the content-addressed store, hash-verified

63}
64
65export function createWatch(
66 fn: (onCleanup: WatchCleanupRegisterFn) => void,
67 schedule: (watch: Watch) => void,
68 allowSignalWrites: boolean,
69): Watch {
70 const node: WatchNode = Object.create(WATCH_NODE);
71 if (allowSignalWrites) {
72 node.consumerAllowSignalWrites = true;
73 }
74
75 node.fn = fn;
76 node.schedule = schedule;
77
78 const registerOnCleanup = (cleanupFn: WatchCleanupFn) => {
79 node.cleanupFn = cleanupFn;
80 };
81
82 function isWatchNodeDestroyed(node: WatchNode) {
83 return node.fn === null && node.schedule === null;
84 }
85
86 function destroyWatchNode(node: WatchNode) {
87 if (!isWatchNodeDestroyed(node)) {
88 consumerDestroy(node); // disconnect watcher from the reactive graph
89 node.cleanupFn();
90
91 // nullify references to the integration functions to mark node as destroyed
92 node.fn = null;
93 node.schedule = null;
94 node.cleanupFn = NOOP_CLEANUP_FN;
95 }
96 }
97
98 const run = () => {
99 if (node.fn === null) {
100 // trying to run a destroyed watch is noop
101 return;
102 }
103
104 if (isInNotificationPhase()) {
105 throw new Error(
106 typeof ngDevMode !== 'undefined' && ngDevMode
107 ? 'Schedulers cannot synchronously execute watches while scheduling.'
108 : '',
109 );
110 }
111
112 node.dirty = false;
113 if (node.version > 0 && !consumerPollProducersForChange(node)) {
114 return;
115 }
116 node.version++;
117
118 const prevConsumer = consumerBeforeComputation(node);
119 try {
120 node.cleanupFn();
121 node.cleanupFn = NOOP_CLEANUP_FN;
122 node.fn(registerOnCleanup);

Callers 4

watch_spec.tsFile · 0.90
testingEffectFunction · 0.90
setupAndReturnRefFunction · 0.90
computed_spec.tsFile · 0.90

Calls 3

consumerMarkDirtyFunction · 0.90
destroyWatchNodeFunction · 0.85
createMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…