/
githubmirror
/
angular
Обзор
Документация
Войти
/
githubmirror
/
angular
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/core/src/linker/destroy_ref.ts
97 строк
3 KB
AleksanderBodurri
refactor(core): patch special provider classes with __NG_ELEMENT_ID__
13 май 2026, 22:15
13 май 2026, 22:15
ec63947
Код
Авторство
О чём код?
/** * @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 {EnvironmentInjector} from '../di'; import {isDestroyed} from '../render3/interfaces/type_checks'; import {LView} from '../render3/interfaces/view'; import {getLView} from '../render3/state'; import {removeLViewOnDestroy, storeLViewOnDestroy} from '../render3/util/view_utils'; import {registerSpecialProvider} from '../render3/debug/special_providers'; /** * `DestroyRef` lets you set callbacks to run for any cleanup or destruction behavior. * The scope of this destruction depends on where `DestroyRef` is injected. If `DestroyRef` * is injected in a component or directive, the callbacks run when that component or * directive is destroyed. Otherwise the callbacks run when a corresponding injector is destroyed. * * @see [Lifecycle DestroyRef](guide/components/lifecycle#destroyref) * * @publicApi */ export abstract class DestroyRef { // Here the `DestroyRef` acts primarily as a DI token. There are (currently) types of objects that // can be returned from the injector when asking for this token: // - `NodeInjectorDestroyRef` when retrieved from a node injector; // - `EnvironmentInjector` when retrieved from an environment injector /** * Registers a destroy callback in a given lifecycle scope. Returns a cleanup function that can * be invoked to unregister the callback. * * @usageNotes * ### Example * ```ts * const destroyRef = inject(DestroyRef); * * // register a destroy callback * const unregisterFn = destroyRef.onDestroy(() => doSomethingOnDestroy()); * * // stop the destroy callback from executing if needed * unregisterFn(); * ``` * * @see [Lifecycle DestroyRef](guide/components/lifecycle#destroyref) * */ abstract onDestroy(callback: () => void): () => void; /** * Indicates whether the instance has already been destroyed. * * @see [Detecting instance destruction](guide/components/lifecycle#detecting-instance-destruction) * */ abstract get destroyed(): boolean; /** * @internal * @nocollapse */ static __NG_ELEMENT_ID__: () => DestroyRef = injectDestroyRef; /** * @internal * @nocollapse */ static __NG_ENV_ID__: (injector: EnvironmentInjector) => DestroyRef = (injector) => injector; } if (typeof ngDevMode === 'undefined' || ngDevMode) { registerSpecialProvider(DestroyRef); } export class NodeInjectorDestroyRef extends DestroyRef { constructor(readonly _lView: LView) { super(); } override get destroyed() { return isDestroyed(this._lView); } override onDestroy(callback: () => void): () => void { const lView = this._lView; storeLViewOnDestroy(lView, callback); return () => removeLViewOnDestroy(lView, callback); } } function injectDestroyRef(): DestroyRef { return new NodeInjectorDestroyRef(getLView()); }