Skip to content

Commit

Permalink
fix: check whether error is an object
Browse files Browse the repository at this point in the history
  • Loading branch information
arturovt committed Mar 22, 2024
1 parent 1c2bb7d commit 2d575ac
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 31 deletions.
19 changes: 12 additions & 7 deletions packages/store/src/internal/unhandled-rxjs-error-callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ export function executeUnhandledCallback(error: any) {
}

export function assignUnhandledCallback(error: any, callback: VoidFunction) {
let hasBeenCalled = false;
__unhandledRxjsErrorCallbacks.set(error, () => {
if (!hasBeenCalled) {
hasBeenCalled = true;
callback();
}
});
// Since the error can be essentially anything, we must ensure that we only
// handle objects, as weak maps do not allow any other key type besides objects.
// The error can also be a string if thrown in the following manner: `throwError('My Error')`.
if (error !== null && typeof error === 'object') {
let hasBeenCalled = false;
__unhandledRxjsErrorCallbacks.set(error, () => {
if (!hasBeenCalled) {
hasBeenCalled = true;
callback();
}
});
}
return error;
}
6 changes: 3 additions & 3 deletions packages/store/tests/action.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ describe('Action', () => {
function setup() {
const recorder: string[] = [];
const record = (message: string) => recorder.push(message);
const observable = new Subject();
const observable = new Subject<void>();
const completeObservableFn = () => {
record('(completeObservableFn) - next');
observable?.next();
Expand Down Expand Up @@ -827,17 +827,17 @@ describe('Action', () => {
'cancellableAction(1) - start',
'action1 obs - subscribe',
'Action 2 - dispatching',
'Action 1 - dispatch complete',
'action1 obs - unsubscribe',
'Action 1 - dispatch complete',
'cancellableAction(2) - start',
'action2 obs - subscribe',
'action2 obs - next Value2',
'cancellableAction(2) - observable tap',
'complete 2',
'action2 obs - complete',
'action2 obs - unsubscribe',
'Action 2 - dispatch next',
'Action 2 - dispatch complete',
'action2 obs - unsubscribe',
'complete 1'
]);
}));
Expand Down
20 changes: 1 addition & 19 deletions packages/store/tests/actions-stream.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TestBed } from '@angular/core/testing';
import { NgxsModule, ActionStatus, Actions } from '@ngxs/store';
import { ActionStatus } from '@ngxs/store';
import { ɵOrderedSubject } from '@ngxs/store/internals';
import { Subject } from 'rxjs';

Expand Down Expand Up @@ -87,22 +87,4 @@ describe('The Actions stream', () => {
'3rd Subscriber:SUCCESSFUL'
]);
});

it('has to add subscriber to the internal "_subscriptions" property', () => {
// Arrange & act
TestBed.configureTestingModule({
imports: [NgxsModule.forRoot()]
});

const actions$: Actions = TestBed.inject(Actions);
const subscription = actions$.subscribe(() => {});

const isArrayBeforeUnsubscribe = Array.isArray(subscription['_subscriptions']);
subscription.unsubscribe();
const isNullyAfterUnsubscribe = subscription['_subscriptions'] === null;

// Assert
expect(isArrayBeforeUnsubscribe).toBeTruthy();
expect(isNullyAfterUnsubscribe).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Dispatching an empty array with errors (https://github.com/ngxs/store/
class AppState {
@Action(ActionError)
actionError() {
return throwError('ActionError: should be shown in the console');
return throwError(() => new Error('ActionError: should be shown in the console'));
}

@Action(ActionEmptyArray)
Expand Down Expand Up @@ -163,7 +163,7 @@ describe('Dispatching an empty array with errors (https://github.com/ngxs/store/
expect(subscription.closed).toEqual(true);
});

it('dispatch([ new ActionEmptyArray(), new ActionDispatchError() ])', () => {
it('dispatch([ new ActionEmptyArray(), new ActionDispatchError() ])', async () => {
subscription = store
.dispatch([new ActionEmptyArray(), new ActionDispatchError()])
.pipe(finalize(() => events.push('finalize')))
Expand Down

0 comments on commit 2d575ac

Please sign in to comment.