Skip to content

Commit

Permalink
refactor(store): setup unhandled error handler during NGXS initializa…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
arturovt committed Nov 20, 2024
1 parent 8fe2074 commit c507759
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
24 changes: 13 additions & 11 deletions packages/store/src/internal/unhandled-rxjs-error-callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ import { config } from 'rxjs';

const ɵɵunhandledRxjsErrorCallbacks = new WeakMap<any, VoidFunction>();

const existingHandler = config.onUnhandledError;
config.onUnhandledError = function (error: any) {
const unhandledErrorCallback = ɵɵunhandledRxjsErrorCallbacks.get(error);
if (unhandledErrorCallback) {
unhandledErrorCallback();
} else if (existingHandler) {
existingHandler.call(this, error);
} else {
throw error;
}
};
export function setupOnUnhandhedErrorHandler(): void {
const existingHandler = config.onUnhandledError;
config.onUnhandledError = function (error: any) {
const unhandledErrorCallback = ɵɵunhandledRxjsErrorCallbacks.get(error);
if (unhandledErrorCallback) {
unhandledErrorCallback();
} else if (existingHandler) {
existingHandler.call(this, error);
} else {
throw error;
}
};
}

export function executeUnhandledCallback(error: any) {
const unhandledErrorCallback = ɵɵunhandledRxjsErrorCallbacks.get(error);
Expand Down
7 changes: 7 additions & 0 deletions packages/store/src/standalone-features/initializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ import { StatesAndDefaults } from '../internal/internals';
import { SelectFactory } from '../decorators/select/select-factory';
import { InternalStateOperations } from '../internal/state-operations';
import { LifecycleStateManager } from '../internal/lifecycle-state-manager';
import { setupOnUnhandhedErrorHandler } from '../internal/unhandled-rxjs-error-callback';

/**
* This function is shared by both NgModule and standalone features.
* When using `NgxsModule.forRoot` and `provideStore`, we can depend on the
* same initialization functionality.
*/
export function rootStoreInitializer(): void {
// Override the RxJS `config.onUnhandledError` within the root store initializer,
// but only after other code has already executed.
// If users have a custom `config.onUnhandledError`, we might overwrite it too
// early and capture the original `config.onUnhandledError` before it is properly set.
setupOnUnhandhedErrorHandler();

const prebootFns = inject(NGXS_PREBOOT_FNS, { optional: true }) || [];
prebootFns.forEach(prebootFn => prebootFn());

Expand Down

0 comments on commit c507759

Please sign in to comment.