Skip to content
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog #

notes:

- state store now always included
- all mapping load functions are rebounced by default
- if an async store loses all subscriptions and then gains one the mapping load function will always evaluate even if the inputs have not changed
- can't use stores to hold errors
- rebounce clear is now called abort

## 1.0.17 (2023-6-20)

- *BREAKING CHANGE* chore: rearrange dependencies to minimize installed package size
Expand All @@ -8,7 +16,7 @@

## 1.0.16 (2023-6-20)

- fix: moduleResoltion: NoodeNext support
- fix: moduleResoltion: NodeNext support
- feat: allow for custom storage types for persisted stores

## 1.0.15 (2023-2-27)
Expand Down
12 changes: 11 additions & 1 deletion src/async-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import { get } from 'svelte/store';
* accessors to the properties of the store's loaded value
*/
export const asyncClient = <S extends Loadable<unknown>>(
loadable: S
loadable: S,
prereqFn?: () => Promise<void>
): S & AsyncClient<StoresValues<S>> => {
let prereqPromise;

// Generate an empty function that will be proxied.
// This lets us invoke the resulting asyncClient.
// An anonymous function is used instead of the function prototype
Expand All @@ -33,7 +36,11 @@ export const asyncClient = <S extends Loadable<unknown>>(
return loadable[property];
}
return async (...argumentsList: unknown[]) => {
prereqPromise ??= prereqFn?.();
await prereqPromise;

const storeValue = await loadable.load();

const original = storeValue[property];
if (typeof original === 'function') {
return Reflect.apply(original, storeValue, argumentsList);
Expand All @@ -43,6 +50,9 @@ export const asyncClient = <S extends Loadable<unknown>>(
};
},
apply: async (_, __, argumentsList) => {
prereqPromise ??= prereqFn?.();
await prereqPromise;

const storeValue = await loadable.load();
if (typeof storeValue === 'function') {
return Reflect.apply(storeValue, storeValue, argumentsList);
Expand Down
Loading