-
-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix/resolved-async-unavailable-with-loadable…
…-or-unwrap
- Loading branch information
Showing
6 changed files
with
303 additions
and
102 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
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,45 @@ | ||
import { atom } from '../../vanilla.ts' | ||
import type { WritableAtom } from '../../vanilla.ts' | ||
|
||
type Read<Value, Args extends unknown[], Result> = WritableAtom< | ||
Value, | ||
Args, | ||
Result | ||
>['read'] | ||
type Write<Value, Args extends unknown[], Result> = WritableAtom< | ||
Value, | ||
Args, | ||
Result | ||
>['write'] | ||
|
||
export function atomWithRefresh<Value, Args extends unknown[], Result>( | ||
read: Read<Value, Args, Result>, | ||
write: Write<Value, Args, Result>, | ||
): WritableAtom<Value, Args | [], Result | void> | ||
|
||
export function atomWithRefresh<Value>( | ||
read: Read<Value, [], void>, | ||
): WritableAtom<Value, [], void> | ||
|
||
export function atomWithRefresh<Value, Args extends unknown[], Result>( | ||
read: Read<Value, Args, Result>, | ||
write?: Write<Value, Args, Result>, | ||
) { | ||
const refreshAtom = atom(0) | ||
if (import.meta.env?.MODE !== 'production') { | ||
refreshAtom.debugPrivate = true | ||
} | ||
return atom( | ||
(get, options) => { | ||
get(refreshAtom) | ||
return read(get, options as any) | ||
}, | ||
(get, set, ...args: Args) => { | ||
if (args.length === 0) { | ||
set(refreshAtom, (c) => c + 1) | ||
} else if (write) { | ||
return write(get, set, ...args) | ||
} | ||
}, | ||
) | ||
} |
Oops, something went wrong.