-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Move "browser" logic to separate file
- Loading branch information
Showing
10 changed files
with
106 additions
and
118 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 |
---|---|---|
|
@@ -54,7 +54,7 @@ | |
"size-limit": [ | ||
{ | ||
"path": "./esm/index.js", | ||
"limit": "750B" | ||
"limit": "700B" | ||
} | ||
], | ||
"eslintConfig": { | ||
|
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export { DataProvider } from './context' | ||
export { createStore, hydrateInitialData } from './store' | ||
export { useFetchData } from './use-fetch-data' | ||
export { useFetchData } from './use-fetch-data.browser' |
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
2 changes: 1 addition & 1 deletion
2
src/use-fetch-data.test.js → src/use-fetch-data.browser.test.js
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,58 @@ | ||
import { useContext, useLayoutEffect } from 'react' | ||
import { DataContext } from './context' | ||
import { AsyncState, Key, Fetcher } from './types' | ||
import { useAsyncState, finished } from './use-async-state' | ||
|
||
/** | ||
* Requests data and preserves the result to the state. | ||
* The `key` must be unique for the whole application. | ||
* | ||
* @see https://github.com/exah/react-universal-data#useFetchData | ||
*/ | ||
|
||
export function useFetchData<T = any>( | ||
fetcher: Fetcher<T>, | ||
key: Key, | ||
ttl?: number | ||
): AsyncState<T> { | ||
const store = useContext(DataContext) | ||
const init = store.has(key) ? finished<T>(store.get(key)) : null | ||
const [state, actions] = useAsyncState<T>(init) | ||
|
||
useLayoutEffect(() => { | ||
if (store.has(key)) { | ||
if (!ttl) { | ||
store.delete(key) | ||
} else if (!store.hasTTL(key)) { | ||
store.setTTL(key, ttl) | ||
} | ||
|
||
return | ||
} | ||
|
||
let isCancelled = false | ||
function finish(result: T) { | ||
if (isCancelled) return | ||
|
||
actions.finish(result) | ||
|
||
if (!(result instanceof Error) && ttl) { | ||
store.setTTL(key, ttl) | ||
store.set(key, result) | ||
} | ||
} | ||
|
||
function cleanup() { | ||
isCancelled = true | ||
} | ||
|
||
actions.start() | ||
Promise.resolve() | ||
.then(() => fetcher(key, { isServer: false })) | ||
.then(finish, finish) | ||
|
||
return cleanup | ||
}, [store, key, ttl, actions, fetcher]) | ||
|
||
return state | ||
} |
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