-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e8c958
commit a5aeb3a
Showing
8 changed files
with
149 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Axios from 'axios'; | ||
import { addCancelSignal } from './store'; | ||
|
||
export function getAxiosToken(key: string) { | ||
const newTokenSource = Axios.CancelToken.source(); | ||
addCancelSignal(key, { | ||
signal: newTokenSource.token, | ||
cancel: () => { | ||
newTokenSource.cancel(); | ||
}, | ||
}); | ||
return newTokenSource.token; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { addCancelSignal } from './store'; | ||
|
||
export function getFetchSignal(key: string) { | ||
const newAbortController = new AbortController(); | ||
addCancelSignal(key, { | ||
signal: newAbortController.signal, | ||
cancel: () => { | ||
newAbortController.abort(); | ||
}, | ||
}); | ||
return newAbortController.signal; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './axios'; | ||
export * from './fetch'; | ||
export * from './store'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
let instanceId = 0; | ||
function getInstanceId() { | ||
instanceId += 1; | ||
return instanceId; | ||
} | ||
|
||
export interface CancelSignal { | ||
signal: unknown; | ||
cancel: () => void; | ||
} | ||
|
||
const signalStore: { | ||
[key: string]: { | ||
[instance: number]: CancelSignal; | ||
}; | ||
} = {}; | ||
|
||
export function addCancelSignal(key: string, signal: CancelSignal) { | ||
if (!signalStore[key]) { | ||
signalStore[key] = {}; | ||
} | ||
signalStore[key][getInstanceId()] = signal; | ||
} | ||
|
||
export function doCancelSignal(key: string) { | ||
if (signalStore[key]) { | ||
Object.values(signalStore[key]).forEach((abortsignal) => { | ||
try { | ||
abortsignal.cancel(); | ||
} catch { | ||
// ignore | ||
} | ||
}); | ||
signalStore[key] = {}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { RecoilValue, useRecoilRefresher_UNSTABLE, useRecoilValueLoadable } from 'recoil'; | ||
import { doCancelSignal } from './signals/store'; | ||
|
||
export type RecoilQueryValue<V> = { | ||
loading: boolean; | ||
data?: V; | ||
error?: unknown; | ||
refresh: () => void; | ||
}; | ||
|
||
export type RecoilQueryOptions = { | ||
refreshOnMount?: 'always' | 'error'; | ||
cancelOnUnmount?: boolean | (() => void); | ||
}; | ||
|
||
//todo freeze options object | ||
|
||
export const useRecoilQuery = <V>( | ||
selector: RecoilValue<V>, | ||
options?: RecoilQueryOptions, | ||
): RecoilQueryValue<V> => { | ||
const { state, contents } = useRecoilValueLoadable<V>(selector); | ||
const loading = state === 'loading'; | ||
const error = state === 'hasError' ? contents : undefined; | ||
const data = (state === 'hasValue' ? contents : undefined) as V; | ||
|
||
const lastData = useRef<V>(); | ||
|
||
useEffect(() => { | ||
if (data !== undefined) { | ||
lastData.current = data; | ||
} | ||
}, [data]); | ||
|
||
const refresh = useRecoilRefresher_UNSTABLE(selector); | ||
|
||
useEffect(() => { | ||
if (options?.refreshOnMount === 'always' || (options?.refreshOnMount === 'error' && error)) { | ||
refresh(); | ||
} | ||
}, []); //eslint-disable-line | ||
|
||
useEffect( | ||
() => () => { | ||
if (options?.cancelOnUnmount === true) { | ||
doCancelSignal(selector.key); | ||
} | ||
if (typeof options?.cancelOnUnmount === 'function') { | ||
options?.cancelOnUnmount(); | ||
} | ||
}, | ||
[], //eslint-disable-line | ||
); | ||
|
||
return { | ||
loading, | ||
data: data !== undefined ? data : lastData.current, | ||
error, | ||
refresh, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters