Skip to content

Commit

Permalink
fix(action): fixed action abort error type
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcourtice committed Aug 11, 2021
1 parent 34b32ad commit 176b41e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
11 changes: 11 additions & 0 deletions extensions/action/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class ActionAbortError extends Error {
public name: string;
public instanceId: symbol;

constructor(name: string, instanceId: symbol) {
super(`Action ${name} as been cancelled`);

this.name = name;
this.instanceId = instanceId;
}
}
17 changes: 14 additions & 3 deletions extensions/action/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import {
Mutator,
} from '@harlem/core';

import {
ActionAbortError,
} from './errors';

import type {
Action,
ActionBody,
Expand All @@ -22,6 +26,10 @@ import type {
ActionStoreState,
} from './types';

export {
ActionAbortError,
} from './errors';

export * from './types';

export default function actionsExtension<TState extends BaseState>() {
Expand Down Expand Up @@ -84,8 +92,9 @@ export default function actionsExtension<TState extends BaseState>() {
const id = Symbol(name);

const complete = () => (tasks.delete(task), removeInstance(name, id));
const fail = () => reject(new ActionAbortError(name, id));

onAbort(() => (complete(), reject()));
onAbort(() => (complete(), fail()));
addInstance(name, id, payload);

try {
Expand All @@ -95,9 +104,11 @@ export default function actionsExtension<TState extends BaseState>() {
resolve(result);
incrementRunCount(name);
} catch (error) {
if (!(error instanceof DOMException)) {
reject(error);
if (error instanceof DOMException) {
fail(); // Fetch has been cancelled
}

reject(error);
} finally {
complete();
}
Expand Down
8 changes: 5 additions & 3 deletions extensions/action/test/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
bootstrap,
} from '@harlem/testing';

import actionsExtension from '../src';
import actionsExtension, {
ActionAbortError,
} from '../src';

interface UserInfo {
firstName: string;
Expand Down Expand Up @@ -114,8 +116,8 @@ describe('Actions Extension', () => {

try {
await task;
} catch {
// do nothing
} catch (error) {
expect(error).toBeInstanceOf(ActionAbortError);
} finally {
expect(state.details.firstName).toBe('');
expect(state.details.lastName).toBe('');
Expand Down

0 comments on commit 176b41e

Please sign in to comment.