Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
galvez committed Aug 4, 2022
1 parent 527ec85 commit 6bd34ac
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/fastify-dx-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
"type": "module",
"main": "index.js",
"name": "fastify-dx-react",
"version": "0.0.4",
"version": "0.0.5",
"files": [
"virtual/create.jsx",
"virtual/create.tsx",
"virtual/root.jsx",
"virtual/root.tsx",
"virtual/layouts.js",
"virtual/layouts/default.jsx",
"virtual/context.js",
"virtual/context.ts",
"virtual/mount.js",
"virtual/mount.ts",
"virtual/resource.js",
"virtual/core.jsx",
"virtual/routes.js",
Expand Down
4 changes: 4 additions & 0 deletions packages/fastify-dx-react/virtual/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file serves as a placeholder
// if no context.js file is provided

export default () => {}
68 changes: 68 additions & 0 deletions packages/fastify-dx-react/virtual/resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

const fetchMap = new Map()
const resourceMap = new Map()

export function waitResource (path, id, promise) {
const resourceId = `${path}:${id}`
const loader = resourceMap.get(resourceId)
if (loader) {
if (loader.error) {
throw loader.error
}
if (loader.suspended) {
throw loader.promise
}
resourceMap.delete(resourceId)

return loader.result
} else {
const loader = {
suspended: true,
error: null,
result: null,
promise: null,
}
loader.promise = promise()
.then((result) => { loader.result = result })
.catch((loaderError) => { loader.error = loaderError })
.finally(() => { loader.suspended = false })

resourceMap.set(resourceId, loader)

return waitResource(path, id)
}
}

export function waitFetch (path) {
const loader = fetchMap.get(path)
if (loader) {
if (loader.error || loader.data?.statusCode === 500) {
if (loader.data?.statusCode === 500) {
throw new Error(loader.data.message)
}
throw loader.error
}
if (loader.suspended) {
throw loader.promise
}
fetchMap.delete(path)

return loader.data
} else {
const loader = {
suspended: true,
error: null,
data: null,
promise: null,
}
loader.promise = fetch(`/-/data${path}`)
.then((response) => response.json())
.then((loaderData) => { loader.data = loaderData })
.catch((loaderError) => { loader.error = loaderError })
.finally(() => { loader.suspended = false })

fetchMap.set(path, loader)

return waitFetch(path)
}
}
2 changes: 1 addition & 1 deletion starters/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint": "eslint . --ext .js,.jsx --fix"
},
"dependencies": {
"fastify-dx-react": "^0.0.3",
"fastify-dx-react": "^0.0.4",
"fastify-vite": "^3.0.0-beta.23",
"ky-universal": "^0.10.1"
},
Expand Down

0 comments on commit 6bd34ac

Please sign in to comment.