/
githubmirror
/
angular
Обзор
Документация
Войти
/
githubmirror
/
angular
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/core/primitives/signals/src/effect.ts
58 строк
2 KB
Milo
refactor(core): use version>0 instead of hasRun (#62467)
22 сен 2025, 19:51
22 сен 2025, 19:51
5406e1a
Код
Авторство
О чём код?
/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ import { consumerAfterComputation, consumerBeforeComputation, consumerPollProducersForChange, REACTIVE_NODE, ReactiveNode, } from './graph'; /** * An effect can, optionally, register a cleanup function. If registered, the cleanup is executed * before the next effect run. The cleanup function makes it possible to "cancel" any work that the * previous effect run might have started. */ export type EffectCleanupFn = () => void; /** * A callback passed to the effect function that makes it possible to register cleanup logic. */ export type EffectCleanupRegisterFn = (cleanupFn: EffectCleanupFn) => void; export interface BaseEffectNode extends ReactiveNode { fn: () => void; destroy(): void; cleanup(): void; run(): void; } export const BASE_EFFECT_NODE: Omit<BaseEffectNode, 'fn' | 'destroy' | 'cleanup' | 'run'> = /* @__PURE__ */ (() => ({ ...REACTIVE_NODE, consumerIsAlwaysLive: true, consumerAllowSignalWrites: true, dirty: true, kind: 'effect', }))(); export function runEffect(node: BaseEffectNode) { node.dirty = false; if (node.version > 0 && !consumerPollProducersForChange(node)) { return; } node.version++; const prevNode = consumerBeforeComputation(node); try { node.cleanup(); node.fn(); } finally { consumerAfterComputation(node, prevNode); } }