-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix:OH2-416 | Fix lock field error for dashoard settings
- Loading branch information
Showing
3 changed files
with
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { | ||
AsyncThunk, | ||
AsyncThunkPayloadCreator, | ||
createAsyncThunk, | ||
} from "@reduxjs/toolkit"; | ||
|
||
type DebounceSettings = { | ||
/** | ||
* The maximum time `payloadCreator` is allowed to be delayed before | ||
* it's invoked. | ||
* @defaultValue `0` | ||
*/ | ||
maxWait?: number; | ||
/** | ||
* Specify invoking on the leading edge of the timeout. | ||
* @defaultValue `false` | ||
*/ | ||
leading?: boolean; | ||
}; | ||
|
||
/** | ||
* A debounced analogue of the `createAsyncThunk` from `@reduxjs/toolkit` | ||
* @param typePrefix - a string action type value | ||
* @param payloadCreator - a callback function that should return a promise containing the result | ||
* of some asynchronous logic | ||
* @param wait - the number of milliseconds to delay. | ||
* @param options - the options object | ||
*/ | ||
const createDebouncedAsyncThunk = <Returned, ThunkArg = void>( | ||
typePrefix: string, | ||
payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg>, | ||
wait: number = 300, | ||
options?: DebounceSettings | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
): AsyncThunk<Returned, ThunkArg, {}> => { | ||
const { maxWait = 500, leading = false } = options ?? {}; | ||
let timer = 0; | ||
let maxTimer = 0; | ||
let resolve: ((value: boolean) => void) | undefined; | ||
const invoke = (): void => { | ||
window.clearTimeout(maxTimer); | ||
maxTimer = 0; | ||
if (resolve) { | ||
resolve(true); | ||
resolve = undefined; | ||
} | ||
}; | ||
const cancel = (): void => { | ||
if (resolve) { | ||
resolve(false); | ||
resolve = undefined; | ||
} | ||
}; | ||
return createAsyncThunk<Returned, ThunkArg>( | ||
typePrefix, | ||
payloadCreator as never, | ||
{ | ||
condition() { | ||
const immediate = leading && !timer; | ||
window.clearTimeout(timer); | ||
timer = window.setTimeout(() => { | ||
invoke(); | ||
timer = 0; | ||
}, wait); | ||
if (immediate) return true; | ||
cancel(); | ||
if (maxWait && !maxTimer) maxTimer = window.setTimeout(invoke, maxWait); | ||
return new Promise<boolean>((res) => { | ||
resolve = res; | ||
}); | ||
}, | ||
} | ||
); | ||
}; | ||
|
||
export default createDebouncedAsyncThunk; |
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