This repository has been archived by the owner on Nov 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
78 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
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,4 @@ | ||
// This file serves as a placeholder | ||
// if no context.js file is provided | ||
|
||
export default () => {} |
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,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) | ||
} | ||
} |
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