refactor(core): adjust signature of the EffectRef (#49490)

Before this change the effect creation function was returning
an instance of the Effect interface with explicit destroy and
schedule operations. This commit makes the following changes
to the return type of effect:
- the interface is renamed from Effect to EffectRef;
- the only available method on this interface is destroy.

We want to tighten the interface in question so only a minimal
set of useful operations is initially exposed.

PR Close #49490
This commit is contained in:
Pawel Kozlowski
2023-03-20 14:51:47 +01:00
parent e2d3da5c71
commit 66f8ed15e8
4 changed files with 9 additions and 24 deletions
+4 -6
View File
@@ -495,14 +495,12 @@ export interface DoCheck {
}
// @public
export interface Effect {
readonly consumer: Consumer;
destroy(): void;
schedule(): void;
}
export function effect(effectFn: () => void): EffectRef;
// @public
export function effect(effectFn: () => void): Effect;
export interface EffectRef {
destroy(): void;
}
// @public
export class ElementRef<T = any> {
@@ -6,4 +6,4 @@
* found in the LICENSE file at https://angular.io/license
*/
export {computed, effect, Effect, isSignal, SettableSignal, Signal, signal, untracked, ValueEqualityFn} from './signals';
export {computed, effect, EffectRef, isSignal, SettableSignal, Signal, signal, untracked, ValueEqualityFn} from './signals';
+1 -1
View File
@@ -8,7 +8,7 @@
export {isSignal, Signal, ValueEqualityFn} from './src/api';
export {computed} from './src/computed';
export {effect, Effect} from './src/effect';
export {effect, EffectRef} from './src/effect';
export {setActiveConsumer} from './src/graph';
export {SettableSignal, signal} from './src/signal';
export {untracked} from './src/untracked';
+3 -16
View File
@@ -6,29 +6,18 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Consumer} from './graph';
import {Watch} from './watch';
/**
* A global reactive effect, which can be manually scheduled or destroyed.
* A global reactive effect, which can be manually destroyed.
*
* @developerPreview
*/
export interface Effect {
/**
* Schedule the effect for manual execution, if it's not already.
*/
schedule(): void;
export interface EffectRef {
/**
* Shut down the effect, removing it from any upcoming scheduled executions.
*/
destroy(): void;
/**
* Direct access to the effect's `Consumer` for advanced use cases.
*/
readonly consumer: Consumer;
}
/**
@@ -36,7 +25,7 @@ export interface Effect {
*
* @developerPreview
*/
export function effect(effectFn: () => void): Effect {
export function effect(effectFn: () => void): EffectRef {
const watch = new Watch(effectFn, queueWatch);
globalWatches.add(watch);
@@ -44,8 +33,6 @@ export function effect(effectFn: () => void): Effect {
watch.notify();
return {
consumer: watch,
schedule: watch.notify.bind(watch),
destroy: () => {
queuedWatches.delete(watch);
globalWatches.delete(watch);