Skip to content

Commit

Permalink
Merge pull request #184 from atomic-state/features/promise-fallback-v…
Browse files Browse the repository at this point in the history
…alue

Adds clientOnly prop
  • Loading branch information
danybeltran authored May 27, 2024
2 parents ab88bf0 + b63faa2 commit d08483b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "http-react",
"version": "3.5.0",
"version": "3.5.1",
"description": "React hooks for data fetching",
"main": "dist/index.js",
"scripts": {
Expand Down
52 changes: 50 additions & 2 deletions src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ export function SSRSuspense({
}

export function FetchConfig(props: FetchContextType) {
const { children, defaults = {}, value = {}, suspense = [] } = props
const {
children,
defaults = {},
value = {},
suspense = [],
clientOnly
} = props

const previousConfig = useHRFContext()

Expand Down Expand Up @@ -93,7 +99,49 @@ export function FetchConfig(props: FetchContextType) {
}
}

getAsyncFallbackValues()
if (clientOnly) {
for (let valueKey in value) {
const resolvedKey = serialize({
idString: serialize(valueKey)
})

if (!isDefined(valuesMemory.get(resolvedKey))) {
valuesMemory.set(resolvedKey, value[valueKey]?.data ?? value[valueKey])
}
if (!isDefined(fetcherDefaults.get(resolvedKey))) {
fetcherDefaults.set(
resolvedKey,
value[valueKey]?.data ?? value[valueKey]
)
}

if (!isDefined(cacheProvider.get(resolvedKey))) {
cacheProvider.set(resolvedKey, value[valueKey]?.data ?? value[valueKey])
}
}

for (let defaultKey in defaults) {
const { id = defaultKey } = defaults[defaultKey]
const resolvedKey = serialize({
idString: serialize(id)
})

if (isDefined(id)) {
if (!isDefined(valuesMemory.get(resolvedKey))) {
valuesMemory.set(resolvedKey, defaults[defaultKey]?.value)
}
if (!isDefined(fetcherDefaults.get(resolvedKey))) {
fetcherDefaults.set(resolvedKey, defaults[defaultKey]?.value)
}
}

if (!isDefined(cacheProvider.get(resolvedKey))) {
cacheProvider.set(resolvedKey, defaults[defaultKey]?.value)
}
}
} else {
getAsyncFallbackValues()
}

for (let suspenseKey of suspense) {
const key = serialize({
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type HTTP_METHODS =
| 'UNLINK'

export type FetchContextType = {
clientOnly?: boolean
fetcher?(
url: string,
config: FetchConfigType
Expand Down

0 comments on commit d08483b

Please sign in to comment.