Skip to content

Commit 8ccfc0f

Browse files
author
Lenz Weber
committed
move MockAbortController out and also use it in Async component
1 parent ef8deff commit 8ccfc0f

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

packages/react-async/src/Async.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react"
22

3-
import globalScope from "./globalScope"
3+
import globalScope, { MockAbortController } from "./globalScope"
44
import { IfInitial, IfPending, IfFulfilled, IfRejected, IfSettled } from "./helpers"
55
import propTypes from "./propTypes"
66
import { ActionTypes, init, dispatchMiddleware, reducer as asyncReducer } from "./reducer"
@@ -93,7 +93,7 @@ export const createInstance = <T extends {}>(
9393
private counter = 0
9494
private args: any[] = []
9595
private promise?: Promise<T> = undefined
96-
private abortController: AbortController = { abort: () => {} } as any
96+
private abortController: AbortController = new MockAbortController()
9797
private debugLabel?: string
9898
private dispatch: (action: AsyncAction<T>, ...args: any[]) => void
9999

packages/react-async/src/globalScope.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ const globalScope = (() => {
2222
*/
2323
globalScope.__REACT_ASYNC__ = globalScope.__REACT_ASYNC__ || {}
2424

25+
export const noop = () => {}
26+
export class MockAbortController implements AbortController {
27+
public abort = noop
28+
readonly signal = {} as AbortSignal
29+
}
30+
2531
export default globalScope

packages/react-async/src/useAsync.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useCallback, useDebugValue, useEffect, useMemo, useRef, useReducer } from "react"
22

3-
import globalScope from "./globalScope"
3+
import globalScope, { MockAbortController, noop } from "./globalScope"
44
import { ActionTypes, init, dispatchMiddleware, reducer as asyncReducer } from "./reducer"
55

66
import {
@@ -32,12 +32,6 @@ export interface FetchOptions<T> extends AsyncOptions<T> {
3232
json?: boolean
3333
}
3434

35-
const noop = () => {}
36-
class MockAbortController implements AbortController {
37-
public abort = noop
38-
readonly signal = {} as AbortSignal
39-
}
40-
4135
function useAsync<T extends {}>(options: AsyncOptions<T>): AsyncState<T>
4236
function useAsync<T extends {}>(promiseFn: PromiseFn<T>, options?: AsyncOptions<T>): AsyncState<T>
4337

0 commit comments

Comments
 (0)