Skip to content

Commit

Permalink
feat(core): create function to assert not running inside reactive con…
Browse files Browse the repository at this point in the history
…text (angular#52049)

Some functions or code should never run inside reactive contexts. A
function to assert that will help putting guard rails in place.

PR Close angular#52049
  • Loading branch information
devversion authored and ChellappanRajan committed Jan 23, 2024
1 parent 2b216bb commit 81db497
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions goldens/public-api/core/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const enum RuntimeErrorCode {
// (undocumented)
APPLICATION_REF_ALREADY_DESTROYED = 406,
// (undocumented)
ASSERTION_NOT_INSIDE_REACTIVE_CONTEXT = 602,
// (undocumented)
ASYNC_INITIALIZERS_STILL_RUNNING = 405,
// (undocumented)
BOOTSTRAP_COMPONENTS_NOT_FOUND = -403,
Expand Down
3 changes: 3 additions & 0 deletions goldens/public-api/core/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ export function asNativeElements(debugEls: DebugElement[]): any;
// @public
export function assertInInjectionContext(debugFn: Function): void;

// @public
export function assertNotInReactiveContext(debugFn: Function, extraContext?: string): void;

// @public
export function assertPlatform(requiredToken: any): PlatformRef;

Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/core_reactivity_export_internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ export {
ZoneAwareQueueingScheduler as ɵZoneAwareQueueingScheduler,
FlushableEffectRunner as ɵFlushableEffectRunner,
} from './render3/reactivity/effect';
export {
assertNotInReactiveContext,
} from './render3/reactivity/asserts';
// clang-format on
1 change: 1 addition & 0 deletions packages/core/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const enum RuntimeErrorCode {
// Signal Errors
SIGNAL_WRITE_FROM_ILLEGAL_CONTEXT = 600,
REQUIRE_SYNC_WITHOUT_SYNC_EMIT = 601,
ASSERTION_NOT_INSIDE_REACTIVE_CONTEXT = 602,

// Styling Errors

Expand Down
30 changes: 30 additions & 0 deletions packages/core/src/render3/reactivity/asserts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @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 {RuntimeError, RuntimeErrorCode} from '../../errors';
import {getActiveConsumer} from '../../signals';

/**
* Asserts that the current stack frame is not within a reactive context. Useful
* to disallow certain code from running inside a reactive context (see {@link toSignal}).
*
* @param debugFn a reference to the function making the assertion (used for the error message).
*
* @publicApi
*/
export function assertNotInReactiveContext(debugFn: Function, extraContext?: string): void {
// Taking a `Function` instead of a string name here prevents the unminified name of the function
// from being retained in the bundle regardless of minification.
if (getActiveConsumer() !== null) {
throw new RuntimeError(
RuntimeErrorCode.ASSERTION_NOT_INSIDE_REACTIVE_CONTEXT,
ngDevMode &&
`${debugFn.name}() cannot be called from within a reactive context.${
extraContext ? ` ${extraContext}` : ''}`);
}
}
2 changes: 1 addition & 1 deletion packages/core/src/signals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
export {defaultEquals, isSignal, Signal, SIGNAL, ValueEqualityFn} from './src/api';
export {computed, CreateComputedOptions} from './src/computed';
export {setThrowInvalidWriteToSignalError} from './src/errors';
export {consumerAfterComputation, consumerBeforeComputation, consumerDestroy, isInNotificationPhase, producerAccessed, producerNotifyConsumers, producerUpdatesAllowed, producerUpdateValueVersion, REACTIVE_NODE, ReactiveNode, setActiveConsumer} from './src/graph';
export {consumerAfterComputation, consumerBeforeComputation, consumerDestroy, getActiveConsumer, isInNotificationPhase, producerAccessed, producerNotifyConsumers, producerUpdatesAllowed, producerUpdateValueVersion, REACTIVE_NODE, ReactiveNode, setActiveConsumer} from './src/graph';
export {CreateSignalOptions, setPostSignalSetFn, signal, WritableSignal} from './src/signal';
export {untracked} from './src/untracked';
export {Watch, watch, WatchCleanupFn, WatchCleanupRegisterFn} from './src/watch';
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/signals/src/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export function setActiveConsumer(consumer: ReactiveNode|null): ReactiveNode|nul
return prev;
}

export function getActiveConsumer(): ReactiveNode|null {
return activeConsumer;
}

export function isInNotificationPhase(): boolean {
return inNotificationPhase;
}
Expand Down

0 comments on commit 81db497

Please sign in to comment.