( fn: (onCleanup: WatchCleanupRegisterFn) => void, schedule: (watch: Watch) => void, allowSignalWrites: boolean, )
| 63 | } |
| 64 | |
| 65 | export 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); |
no test coverage detected
searching dependent graphs…