/** * @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.io/license */ import { getLView } from '../render3/state'; import { removeLViewOnDestroy, storeLViewOnDestroy } from '../render3/util/view_utils'; /** * `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. * * @publicApi */ export class DestroyRef { /** * @internal * @nocollapse */ static { this.__NG_ELEMENT_ID__ = injectDestroyRef; } /** * @internal * @nocollapse */ static { this.__NG_ENV_ID__ = (injector) => injector; } } class NodeInjectorDestroyRef extends DestroyRef { constructor(_lView) { super(); this._lView = _lView; } onDestroy(callback) { storeLViewOnDestroy(this._lView, callback); return () => removeLViewOnDestroy(this._lView, callback); } } function injectDestroyRef() { return new NodeInjectorDestroyRef(getLView()); } //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGVzdHJveV9yZWYuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi9wYWNrYWdlcy9jb3JlL3NyYy9saW5rZXIvZGVzdHJveV9yZWYudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7OztHQU1HO0FBSUgsT0FBTyxFQUFDLFFBQVEsRUFBQyxNQUFNLGtCQUFrQixDQUFDO0FBQzFDLE9BQU8sRUFBQyxvQkFBb0IsRUFBRSxtQkFBbUIsRUFBQyxNQUFNLDRCQUE0QixDQUFDO0FBRXJGOzs7Ozs7O0dBT0c7QUFDSCxNQUFNLE9BQWdCLFVBQVU7SUF3QjlCOzs7T0FHRzthQUNJLHNCQUFpQixHQUFxQixnQkFBZ0IsQ0FBQztJQUU5RDs7O09BR0c7YUFDSSxrQkFBYSxHQUFrRCxDQUFDLFFBQVEsRUFBRSxFQUFFLENBQUMs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