-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add promiseStore to easily wrap a Promise as a store. Useful for Svel…
…teKit streamed data handling
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte-ux': patch | ||
--- | ||
|
||
Add promiseStore to easily wrap a Promise as a store. Useful for SvelteKit streamed data handling |
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,65 @@ | ||
import { writable } from 'svelte/store'; | ||
|
||
export type PromiseStoreData<T> = { | ||
loading: boolean; | ||
data?: T; | ||
error?: Error; | ||
aborted?: boolean; | ||
}; | ||
|
||
const WAITING = Symbol(); | ||
|
||
/** | ||
* Add promiseStore to easily wrap a Promise as a store. Useful for SvelteKit streamed data handling | ||
*/ | ||
export function promiseStore<T>(initialValue?: Promise<T> | null) { | ||
let currentPromise: Promise<T> | null = null; | ||
|
||
let store = writable<PromiseStoreData<T>>({ | ||
loading: false, | ||
aborted: false, | ||
}); | ||
|
||
let result = { | ||
subscribe: store.subscribe, | ||
|
||
async setPromise(promise: Promise<T> | undefined) { | ||
if (!promise) { | ||
currentPromise = null; | ||
store.set({ | ||
data: undefined, | ||
loading: false, | ||
aborted: false, | ||
}); | ||
return; | ||
} | ||
|
||
// If promise is already resolved, update immediately | ||
const resolved = await Promise.race([promise, Promise.resolve(WAITING)]); | ||
if (resolved === WAITING) { | ||
store.update((s) => ({ ...s, loading: true })); | ||
} | ||
|
||
if (promise !== currentPromise) { | ||
currentPromise = promise; | ||
try { | ||
const data = await promise; | ||
if (currentPromise === promise) { | ||
store.set({ data, loading: false, aborted: false }); | ||
} | ||
} catch (err) { | ||
if (currentPromise === promise) { | ||
const error = err as Error; | ||
store.set({ error, aborted: error.name === 'AbortError', loading: false }); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
if (initialValue) { | ||
result.setPromise(initialValue); | ||
} | ||
|
||
return result; | ||
} |
7b2f662
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
svelte-ux – ./
svelte-ux-git-master-techniq-team.vercel.app
svelte-ux.vercel.app
svelte-ux.techniq.dev
svelte-ux-techniq-team.vercel.app