Files
angular/packages/core/test/signals/effect_util.ts
Alex Rickabaugh 8f5cbcc845 refactor: move signals code into primitives package (#51986)
This commit reorganizes the Angular code a bit, and moves signals into a
newly defined `@angular/core/primitives` location. This will be used inside
g3 to allow non-Angular targets to depend on the signals core without
incurring a dependency on the whole framework.

PR Close #51986
2023-10-06 15:12:00 -07:00

34 lines
823 B
TypeScript

/**
* @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 {createWatch, Watch, WatchCleanupFn} from '@angular/core/primitives/signals';
let queue = new Set<Watch>();
/**
* A wrapper around `Watch` that emulates the `effect` API and allows for more streamlined testing.
*/
export function testingEffect(effectFn: (onCleanup: (cleanupFn: WatchCleanupFn) => void) => void):
void {
const w = createWatch(effectFn, queue.add.bind(queue), true);
// Effects start dirty.
w.notify();
}
export function flushEffects(): void {
for (const watch of queue) {
queue.delete(watch);
watch.run();
}
}
export function resetEffects(): void {
queue.clear();
}