Giter Site home page Giter Site logo

svelte-store's Introduction

Square Svelte Store

Extension of svelte default stores for dead-simple handling of complex asynchronous behavior.

What it does

Square Svelte Store builds upon Svelte's default store behavior to empower your app to reactively respond to asynchronous data. Familiar syntax lets you build out async stores as easily as the ones you are already using, with full compatibility between them. Behind-the-scenes smarts handle order of operations, lazy loading, and limiting network calls, allowing you to focus on the relationships between data.

A preview...

// You can declare an asyncDerived store just like a derived store,
// but with an async function to set the store's value!
const searchResults = asyncDerived(
  [authToken, searchTerms],
  async ([$authToken, $searchTerms]) => {
    const rawResults = await search($authToken, $searchTerms);
    return formatResults(rawResults);
  }
);

The Basics

Square Svelte Store is intended as a replacement for importing from svelte/store. It includes all of the features of svelte/store while also adding new stores and extending functionality for compatibility between them.

Loadable

Stores exported by @square/svelte-store are a new type: Loadable. Loadable stores work the same as regular stores--you can derive from them, subscribe to them, and access their value reactively in a component by using the $ accessor. But they also include extra functionality: a load function is available on every store. This function is asynchronous, and resolves to the value of the store after it has finished its async behavior. This lets you control the display of your app based on the status of async routines while also maintaining reactivity!

{#await myLoadableStore.load()}
 <p>Currently loading...</p>
{:then}
 <p>Your loaded data is: {$myLoadableStore}</p>
{/await}

What's better is that any derived store loads all of its parents before loading itself, allowing you to awaitloading of the derived store to automatically wait for all required data dependencies. This means that no matter how complex the relationships between your async and synchronous data gets you will always be able to ensure that a given store has its final value simply by awaiting .load()!

Reloadable

While hydrating your app with data, some endpoints you will only need to access once. Others you will need to access multiple times. By default async stores will only load once unless a store they derive from changes. However if you would like an async store to be able to load new data you can declare it to be reloadable during creation. If you do so, the store, and any stores that ultimately derive from it, will have access to a reload function. Calling the reload function of a Reloadable store will cause it fetch new data, and calling the reload function of any store that derives from a Reloadable store will cause that Reloadable store to reload. In this manner you can call reload on a store and it will reload any sources of data that should be refreshed without unnecessarily creating promises for data that should not be refreshed.

The New Stores

asyncReadable

An asyncReadable store provides easy asynchronous support to readable stores. Like a readable store, an asyncReadable store takes in an initial value and a function that is called when the store is first subscribed to. For an asyncReadable store this function is an async loadFunction which takes no arguments and returns the loaded value of the store. An optional third parameter can specify options for the store, in this case declaring it to be reloadable.

asyncReadable stores are super simple! Let's see it in action...

const userInfo = asyncReadable(
  {},
  async () => {
    const response = await fetch('https://ourdomain.com/users/info');
    const userObject = await response.json();
    return userObject;
  },
  { reloadable: true }
);

Now we have a Loadable and reloadable userInfo store! As soon as our app renders a component that needs data from userInfo it will begin to load. We can {#await userInfo.load()} in our components that need userInfo. This will delay rendering until we have the data we need. Since we have declared the store to be reloadable we can call userInfo.reload() to pull new data (and reactively update our components once we have it).

derived

Okay this isn't a new store, but it does have some new features! We declare a derived store the same as ever, but it now gives us access to a load function. This load function resolves after all parents have loaded and the derived store has calculated its final value.

What does that mean for our app..?

const userSettings = derived(userInfo, ($userInfo) => $userInfo?.settings);
const darkMode = derived(userSettings, ($userSetting) => $userSettings?.darkMode);

Now we've got a darkMode store that tracks whether our user has selected darkMode for our app. When we use this store in a component we can call darkMode.load(). This awaits userSettings loading, which in turn awaits userInfo. In this way, we can load a derived store to automatically load the sources of its data and to wait for its final value. What's more, since darkMode derives from a reloadable source, we can call darkMode.reload() to get new userInfo if we encounter a situation where the user's darkMode setting may have changed.

This isn't very impressive with our simple example, but as we build out our app and encounter situations where derived values come fom multiple endpoints through several layers of derivations this becomes much more useful. Being able to call load and reload on just the data you need is much more convenient than tracking down all of the dependencies involved!

asyncDerived

An asyncDerived store works just like a derived store, but with an asynchronous call to get the final value of the store!

Let's jump right in...

const results = asyncDerived(
  [authToken, page],
  async ([$authToken, $page]) => {
    const requestBody = JSON.stringify({ authorization: $authToken });
    const response = await fetch(
      `https://ourdomain.com/list?page=${$page}`,
      requestBody
    );
    return response.json();
  }
);

Here we have a store that reflects a paginated set of results from an endpoint. Just like a regular derived store we include a function that maps the values of parent stores to the value of this store. Of course with an async store we use an async function. However, while regular derived stores will invoke that function whenever any of the parent values changes (including initialization) an asyncDerived store will only do so after all of the parents have finished loading. This means you don't need to worry about creating unnecessary or premature network calls.

After the stores have finished loading any new changes to the parent stores will create a new network request. In this example if we write to the page store when the user changes pages we will automatically make a new request that will update our results store. Just like with asyncReadable stores we can include a boolean to indicate that an asyncDerived store will be Reloadable.

asyncWritable

Here's where things get a little more complicated. Just like the other async stores this store mirrors an existing store. Like a regular writable store this store will have set and update functions that lets you set the store's value. But why would we want to set the value of the store if the store's value comes from a network call? To answer this let's consider the following use case: in our app we have a list of favorite shortcuts for our user. They can rearrange these shortcuts in order to personalize their experience. When a user rearranges their shortcuts we could manually make a new network request to save their choice, then reload the async store that tracks the list of shortcuts. However that would mean that the user would not see the results of their customization until the network request completes. Instead we can use an asyncWritable store. When the user customizes their list of shortcuts we will optimistically update the corresponding store. This update kicks off a network request to save the user's customization to our backend. Finally, when the network request completes we update our store to reflect the canonical version of the user's list.

So how do we accomplish this using an asyncWritable store..?

const shortcuts = asyncWritable(
  [],
  async () => {
    const response = await fetch('https://ourdomain.com/shortcuts');
    return response.json();
  },
  async (newShortcutsList) => {
    const postBody = JSON.stringify({ shortcuts: newShortcutsList });
    const response = await fetch('https://ourdomain.com/shortcuts', {
      method: 'POST',
      body: postBody,
    });
    return response.json();
  }
);

Our first two arguments work just like an asyncDerived store--we can pass any number of stores and we can use their values to set the value of the store once the parents have loaded. If we don't need to derive from any store we can pass [] as our first argument. For our third argument we optionally provide a write function that is invoked when we set or update the value of the store ourself. It takes in the new value of the store and then performs the work to persist that to the backend. If we invoke shortcuts.set() first the store updates to the value we pass to the function. Then it invokes the async function we provided during definition in order to persist the new data. Finally it sets the value of the store to what we return from the async function. If our endpoint does not return any useful data we can instead have our async function return void and skip this step.

One final feature is that we can include a second argument for our write function that will receive the values of parent stores.

Let's look at what that looks like...

const shortcuts = asyncWritable(
  authToken,
  async ($authToken) => {
    const requestBody = JSON.stringify({ authorization: $authToken });
    const response = await fetch(
      'https://ourdomain.com/shortcuts',
      requestBody
    );
    return response.json();
  },
  async (newShortcutsList, $authToken) => {
    const postBody = JSON.stringify({
      authorization: $authToken,
      shortcuts: newShortcutsList,
    });
    const response = await fetch('https://ourdomain.com/shortcuts', {
      method: 'POST',
      body: postBody,
    });
    return response.json();
  }
);

In this example we derive from an authToken store and include it in both our GET and POST requests.

Some niche features of asyncWritable stores allow for more specific error handling of write functions. The write function we provide as the third argument can be written to accept a third argument that receives the value of the store before it was set. This allows for resetting the value of the store in the case of a write failure by catching the error and returning the old value. A similar feature is that both the set and update functions can take a second argument that indicates whether the async write functionality should be called during the set process.

readable/writable

Similarly to derived stores, addtional load functionality is bundled with readable and writable stores. Both readable and writable stores include a .load() function that will resolve when the value of the store is first set. If an initial value is provided when creating the store, this means the store will load immeadietly. However, if a value is not provided (left undefined) then the store will only load after it is set to a value. This makes it easy to wait on user input, an event listener, etc. in your application.

It's easy to wait for user input...

<script>
  const hasConsent = writable((set) => {
    const setConsent = () => set(true);
    addEventListener('CONSENT_EVENT', setConsent);

    return () => removeEventListener('CONSENT_EVENT', setConsent);  
  });
  const needsConsent = asyncDerived(
    (hasConsent),
    async ($hasConsent) => {
      // this won't run until hasConsent has loaded
      if (!$hasConsent) {
        return "no consent given"
      }
      const asyncMessage = await Promise.resolve('data fetched from server');
      return asyncMessage;
    }
  );
</script>

<button on:click={() => hasConsent.set(true)>I consent!</button>
<button on:click={() => hasConsent.set(false)>I don't consent!</button>

{#await needsConsent.load()}
  <p>I will only load after hasConsent has been populated</p>
  <p>{$needsConsent}</p>
{/await}

persisted

Sometimes data needs to persist outside the lifecycle of our app. By using persisted stores you can accomplish this while gaining all of the other benefits of Loadable stores. A persisted store synchronizes (stringifiable) store data with a sessionStorage item, localStorage item, or cookie. The persisted store loads to the value of the corresponding storage item, if found, otherwise it will load to the provided initial value and persist that value to storage. Any changes to the store will also be persisted!

We can persist a user name across page loads...

<script>
  // if we don't specify what kind of storage, default to localStorage
  const userName = persisted('John Doe', 'USER_DATA');
</script>

// If we reload the page, this input will still have the same value!
<input bind:value={$userName}>

If data isn't already in storage, it may need to be fetched asynchronously. In this case we can pass a Loadable store to our persisted store in place of an initial value. Doing so will load the Loadable store if no storage item is found and then synchronize the persisted store and storage with the loaded value. We can also declare the persisted store to be reloadable, in which case a call to .reload() will attempt to reload the parent Loadable store and persist the new data to storage.

Persisting remote data is simple...

const remoteSessionToken = asyncReadable(
  undefined, 
  async () => {
    const session = await generateSession();
    return session.token;
  },
  { reloadable: true },
);

const sessionToken = persisted(
  remoteSessionToken,
  'SESSION_TOKEN',
  { reloadable: true, storageType: 'SESSION_STORAGE' }
);

With this setup we can persist our remote data across a page session! The first page load of the session will load from the remote source, but successive page loads will use the persisted token in session storage. What's more is that because Loadable stores are lazily loaded, remoteSessionToken will only fetch remote data when its needed for sessionToken (provided there are no other subscribers to remoteSessionToken). If our session token ever expires we can force new data to be loaded by calling sessionToken.reload()!

If an external source updates the storage item of the persisted store the two values will go out of sync. In such a case we can call .resync() on the store in order to update the store the parsed value of the storage item.

We are also able to wipe stored data by calling clear() on the store. The storage item will be removed and the value of the store set to null.

persisted configuration / custom storage

Persisted stores have three built in storage types: LOCAL_STORAGE, SESSION_STORAGE, and COOKIE. These should be sufficient for most use cases, but have the disadvantage of only being able to store JSON serializable data. If more advanced behavior is required we can define a custom storage type to handle this, such as integrating IndexedDB. All that is required is for us to provide the relevant setter/getter/deleter functions for interfacing with our storage.

One time setup is all that is needed for custom storage...

configureCustomStorageType('INDEXED_DB', {
  getStorageItem: (key) => /* get from IndexedDB */,
  setStorageItem: (key, value) => /* persist to IndexedDB */,
  removeStorageItem: (key) => /* delete from IndexedDB */,
});

const customStore = persisted('defaultValue', 'indexedDbKey', {
  storageType: 'INDEXED_DB',
});

persisted configuration / consent

Persisting data to storage or cookies is subject to privacy laws regarding consent in some jurisdictions. Instead of building two different data flows that depend on whether tracking consent has been given or not, you can instead configure your persisted stores to work in both cases. To do so you will need to call the configurePersistedConsent function and pass in a consent checker that will accept a consent level and return a boolean indicating whether your user has consented to that level of tracking. You can then provide a consent level when building your persisted stores that will be passed to to the checker before storing data.

GDPR compliance is easy...

configurePersistedConsent(
  (consentLevel) =>  window.consentLevels.includes(consentLevel);
);

const hasDismissedTooltip = persisted(
  false, 
  'TOOLTIP_DISMISSED', 
  { 
    storageType: 'COOKIE',
    consentLevel: 'TRACKING'
  }
);

Here we hypothesize a setup where a user's consentLevels are accessible through the window object. We would like to track the dismissal of a tooltip and ideally persist that across page loads. To do so we set up a hasDismissedTooltip store that can bet set like any other writable store. If the user has consented to the TRACKING consent level, then setting the store will also set a TOOLTIP_DISMISSED cookie. Otherwise no data will be persisted and the store will initialize to the default value false on each page load.

Note that if no consent level is provided, undefined will be passed to the consent checker. This can be handled to provide a default consent for your persisted stores when a consent level is not provided.

state

State stores are a kind of non-Loadable Readable store that can be generated alongside async stores in order to track their load state. This can be done by passing the trackState to the store options during creation. This is particular useful for reloadable or asyncDerived stores which might go into a state of pulling new data.

State stores can be used to conditionally render our data...

<script>
  let searchInput;
  const searchTerms = writable();
  const {store: searchResults, state: searchState} = asyncDerived(
    searchTerms,
    async ($searchTerms) => {
      const response = await search($searchTerms);
      return response.results;
    },
    { trackState: true }
  )
</script>

<input bind:value={searchInput}>
  <button on:click={() => searchTerms.set(searchInput)}>search</button>
  {#if $searchState.isLoading}
    <SearchTips />
  {:else if $searchState.isLoaded}
    <SearchResults results={$searchResults} />
  {:else if $searchState.isReloading}
    <ActivityIcon />
    <SearchResults results={$searchResults} />
  {:else if $searchState.isError}
    <SearchError />
  {/if}
<input >

We are able to easily track the current activity of our search flow using trackState. Our searchState will initialize to LOADING. When the searchTerms store is first set it will load, which will kick off searchTerms own loading process. After that completes searchState will update to LOADED. Any further changes to searchTerms will kick off a new load process, at which point searchTerms will update to RELOADING. We are also able to check summary states: isPending is true when LOADING or RELOADING and isSettled is true when LOADED or ERROR.

Note that trackState is (currently) only available on asyncStores -- asyncReadable, asyncWritable, and asyncDerived.

asyncClient

An asyncClient is a special kind of store that expands the functionality of another Loadable store. Creating an asyncClient allows you to start accessing the propeties of the object in your store before it has loaded. This is done by transforming all of the object's properties into asynchronous functions that will resolve when the store has loaded.

Confusing in concept, but simple in practice...

const logger = asyncClient(readable(
  undefined,
  (set) => {
    addEventListener('LOGGING_READY', () => {
      set({
        logError: (error) => window.log('ERROR', error.message),
        logMessage: (message) => window.log('INFO', message),
      });
    })
  }
));

logger.logMessage('Logging ready');

In this example we assume a hypothetical flow where a LOGGING_READY event is fired upon an external library adding a generic logger to the window object. We create a readable store that loads when this event fires, and set up an object with two functions for logging either errors or non-error messages. If we did not use an asyncClient we would need to call logMessage like so: logger.load().then(($logger) => $logger.logMessage('Logging ready')) However, by turning the readable store into an asyncClient we can instead call logger.logMessage immeadietly and the message will be logged when the LOGGING_READY event fires.

Note that the asyncClient is still a store, and so can perform all of the store functionality of what it wraps. This means, for example, that you can make an asyncClient of a writable store and have access to the set and update functions.

Non-function properties of the object loaded by the asyncClient can also be accessed using an async function. I.e. if an asyncClient loads to {foo: 'bar'}, myClient.foo() will resolve to 'bar' when the asyncClient has loaded. The property access for an asyncClient is performed dynamically, and that means that any property can attempt to be accessed. If the property can not be found when the asyncClient loads, this will resolve to undefined. It is recommended to use typescript to ensure that the accessed properties are members of the store's type.

If a store loads directly to a function, an asyncClient can be used to asynchronously invoke that function.

We can call loaded functions easily...

const logMessage = asyncClient(readable(
  undefined,
  (set) => {
    addEventListener('LOGGING_READY', () => {
      set((message) => window.log('INFO', message));
    })
  }
));

logMessage('Logging ready')

Instead of defining a store that holds an object with function properties, we instead have the store hold a function directly. As before, logMessage will be called when the LOGGING_READY event fires and the store loads.

Additional functions

isLoadable and isReloadable

The isLoadable and isReloadable functions let you check if a store is Loadable or Reloadable at runtime.

loadAll

The loadAll function can take in an array of stores and returns a promise that will resolve when any loadable stores provided finish loading. This is useful if you have a component that uses multiple stores and want to delay rendering until those stores have populated.

safeLoad

The safeLoad function works similarly to loadAll, however any loading errors of the given stores will be caught, and a boolean returned representing whether loading all of the provided stores was performed successfully or not. This can be useful when you wish to handle possible loading errors, yet still want to render content upon failure.

{#await safeLoad(myStore) then loadedSuccessfully}
  {#if !loadedSuccessfully}
    <ErrorBanner/>
  {/if}
  <ComponentContent/>
{/await}

logAsyncErrors

Using safeLoad or {#await}{:then}{:catch} blocks in templates allows you to catch and handle errors that occur during our async stores loading. However this can lead to a visibility problem: if you always catch the errors you may not be aware that your users are experiencing them. To deal with this you can pass an error logger into the logAsyncErrors function before you set up your stores. Then any time one of our async stores experiences an error while loading it will automatically call your error logging function regardless of how you handle the error downstream.

aborting / rebounce (BETA)

Async functionality based on user input is prone to subtle race conditions and async stores are no exception. For example, imagine you want to get a paginated list of items. When the user changes pages a new request is made and then assigned to a currentItems variable. If a user changes pages quickly the requests to the items endpoint may resolve in a different order than they were made. If this happens, currentItems will reflect the last request to resolve, instead of the last request to be made. Thus the user will see an incorrect page of items.

The solution for this problem is to abort old requests and to only resolve the most recent one. This can be performed on fetch requests using abort controllers. To support this pattern, async stores have special handling for rejections of aborted requests. If a store encounters an abort rejection while loading, the store's value will not update, and the rejection will be caught.

While fetch requests have built in abort controller support, async functions do not. Thus the rebounce function is provided. It can be used to wrap an async function and automatically abort any in-flight calls when a new call is made.

Use rebounce to abort stale async calls...

let currentItems;

const getItems = async (page) => {
  const results = await itemsRequest(page)
  return results.items;
}

const rebouncedGetItems = rebounce(getItems)

const changePage = async (page) => {
  currentItems = await rebouncedGetItems(page);
}

changePage(1);
changePage(2);
changePage(3);

Without using rebounce, currentItems would end up equaling the value of getItems that resolves last. However, when we called the rebounced getItems it will equal value of getItems that is called last. This is because a rebounced function returns a promise that resolves to the returned value of the function, but this promise is aborted when another call to the function is made. This means that when we call changePage three times, the first and second calls to rebouncedGetItems will reject and only the third call will update currentItems.

Using rebounce with stores is straight forward...

const rebouncedGetItems = rebounce(
  async (page) => {
    const results = await itemsRequest(page)
    return results.items;
  },
  200
);

const currentItems = asyncDerived(page, ($page) => {
  return rebouncedGetItems($page);
});

Here we have created a store for our currentItems. Whenever we update the page store we will automatically get our new items. By using rebounce, currentItems will always reflect the most up-to-date page value. Note we have also provided a number when calling rebounce. This creates a corresponding millisecond delay before the rebounced function is called. Successive calls within that time frame will abort the previous calls before the rebounced function is invoked. This is useful for limiting network requests. In this example, if our user continues to change the page, an itemsRequest will not be made until 200 ms has passed since page was updated. This means, if our user rapidly clicks through pages 1 to 10, a network request will only be made (and our currentItems store updated) when they have settled on page 10.

NOTES:

  • In flight rebounces can be manually aborted using clear. rebouncedFunction.clear()
  • Aborting an async function will cause it to reject, but it will not undo any side effects that have been created! Therefore it is critical that side effects are avoided within the rebounced function. Instead they should instead be performed after the rebounce has resolved.

Putting it all Together

The usefulness of async stores becomes more obvious when dealing with complex relationships between different pieces of async data.

Let's consider an example scenario that will put our @square/svelte-stores to work. We are developing a social media website that lets users share and view blogs. In a sidebar we have a list of shortcuts to the users favorite blogs with along with a blurb from their most recent post. We would like to test a feature with 5% of users where we also provide a few suggested blogs alongside their favorites. As the user views new blogs, their suggested list of blogs also updates based on their indicated interests. To support this we have a number of endpoints.

  • A personalization endpoint provides a list of the user's favorite and suggested blogs.
  • A preview endpoint lets us fetch a blurb for the most recent post of a given blog.
  • A favorites endpoint lets us POST updates a user makes to their favorites.
  • A testing endpoint lets us determine if the user should be included in the feature test.
  • A user endpoint lets us gather user info, including a token for identifying the user when calling other endpoints.

We've got some challenges here. We need the user's ID before we take any other step. We need to query the testing endpoint before we will know whether to display suggestions alongside favorites. And whenever a users shortcuts update we'll need to update our preview blurbs to match.

Without async stores this could get messy! However by approaching this using stores all we need to worry about is one piece of data at a time, and the pieces we need to get it.

Let's look at an interactive implementation...

Extras

If you are using eslint, eslint-plugin-square-svelte-store will enforce usage of square-svelte-store and can be used to autofix usages of svelte/store.

// .eslintrc.js
module.exports = {
  plugins: ['square-svelte-store'],
  rules: {'square-svelte-store/use-square-svelte-stores': 'error'}
}

Testing

Testing mode can be enabled using the enableStoreTestingMode function before running your tests. If testing mode is enabled async stores will include an additional function, reset. This function can be called in between tests in order to force stores to reset their load state and return to their initial value. This is useful to test different load conditions for your app, such as endpoint failures.

svelte-store's People

Contributors

akolyte01 avatar benjamin-t-frost avatar bmcbarron avatar brandonmarino avatar coldino avatar crsschwalm avatar miedzikd avatar pzeinlinger avatar sarcevicantonio avatar smblee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

svelte-store's Issues

Confused about asyncDerived store not updating on new store values

Hi! I recently discovered this awesome library and started rewriting my webapp using it as it facilitates async state management a lot. However, I'm having some trouble making the asyncDerived store work correctly.

I have an asyncDerived store that depends on another (synchronous) store which is updated over time. When the sync store is updated I would expect the asyncDerived store to fire off an update, but that's not the case. Here's a code snippet:

const blockNumber = writable<bigint | null>(null); /* updated over time by an external module */

const balance = asyncDerived(
    [blockNumber],
    async ([$blockNumber]) => {
        if (!$blockNumber) return; /* needed because blockNumber is first set in the browser context (bigint is not serializable) */
        return await getBalance();
    });

The problem is that the load function runs only once (and it won't load the balance even the first time, because blockNumber is initially null). So then I tried to change asyncDerived to derived, without touching anything else, and surprisingly it works, the store gets updated on every new blockNumber. I thought you couldn't run an async function in this case?

The problem with a regular derived store is that I cannot reload it at will (unless I create a dummy store as a dependency and update it to force a refresh). But in the case of the asyncDerived store shown above, if I set reloadable: true and do the following, the store actually gets updated on new blockNumber values:

if (browser} {
        blockNumber.subscribe(async () => {
        await balance.reload?.();
    });
}

Although, in this case, the first good value for the balance store will only get loaded after the first new blockNumber value.

Any insights on what I'm doing wrong with the async store?

IndexedDB?

Amazing project, congratulations!

Is there a way to persist to IndexedDB?

Directory import error

Hi! Love this library, just started using it, but I'm getting a "Directory import 'node_modules/@square/svelte-store/lib/async-client' is not supported resolving ES modules imported from node_modules/@square/svelte-store/lib/index.js." Any ideas how I can fix this? Thanks!

I'm importing it like so:

import { asyncDerived } from "@square/svelte-store"

Merge into Svelte main

Hey all! With the 1.0 release I think we've arrived at a pretty complete and robust set of features. With that in mind I opened a discussion in the main Svelte repository about the possibility of merging these features in.

Is that something you'd like to see? Or does existing as a separate package make more sense to you?

weigh in here: sveltejs/svelte#8011

Add autocatch+and+log feature

Existing functionality works well for happy-path but unhappy-path could use some work.

As is, if an async store fails to load it will return the promise rejection with its load function. This can be caught in templates by using safeLoad but if this is done then the error is not exposed in the console or caught by automatic error logging.

This can be worked around by adding a try catch block to every async store and manually logging any errors before re-throwing. Instead of doing this every time this should be an option in this package.

Allow for the passing in of an error logger callback in a configuration step that will be called whenever there is an error in the load function of an async store.

AsyncDerived returns undefined

Hi!
I'm running into a weird issue on my first time using asyncDerived stores.
I have an asyncWritable store called collections defined as such

export const collections = asyncWritable(
    [],
    async () => {
        const response = await fetch('/collections');
        return response.json();
    },
    async (newCollections) => {
        console.log('newCollections', newCollections)
        const postBody = JSON.stringify(newCollections);
        await fetch('/collections', {
            method: 'POST',
            body: postBody
        });
    }
);

From this i derived a collection store that corresponds to the currently selected collection:

export const currentCollection = persisted('', 'currentCollection');

export const collection = derived(
    [currentCollection, collections],
    ([$currentCollection, $collections]) => {
        if ($collections && $collections.length > 0 && $currentCollection && $currentCollection !== '') {
		let collection = $collections.find(({name}) => name === $currentCollection);
		collection.id = $collections.findIndex(({name}) => name === $currentCollection);
		return collection;
	}
    }
)

This setup works wonderfully but I'm having issue trying to create a another derived store that gets all the comments ids from the current collection and maps them after fetching from google APIs :

export const comments = asyncDerived(
    collection,
    async ($collection) => {
        if ($collection && $collection.comments) {
            console.log('collection', $collection)
            let commentLinks = $collection.comments;
            let commentIds = commentLinks.map(link => link.split('&lc=')[1]);

            await Promise.all(commentIds.map(async (id) => {
                if(id !== undefined) {
                    const response = await fetch(`https://youtube.googleapis.com/youtube/v3/comments?part=snippet&parentId=${id}`);
                    const json = await response.json();
                    return json.items[0];
                }
            })).then((comments) => {
                // here the comments object correctly contains all the data
                return comments;
            })
        }
    }
)

In fact when I try to run

console.log(await comments.load())
//or
comments.load().then(($comments) => {
   console.log($comments)
 })

Both show that the store is undefined.
I'm probably missing something trivial, but i feel like this should work.
Thanks in advance for any help!

Documentation: asyncWritable initial value?

How do I set an initial value in an asyncWritable?

I have read in the docs:

asyncWritable

Our first two arguments work just like an asyncDerived store--we can pass any number of stores and we can use their values to set the value of the store once the parents have loaded.

asyncDerived

However, while regular derived stores will invoke that function whenever any of the parent values changes (including initialization) an asyncDerived store will only do so after all of the parents have finished loading.

Intended use:

{#await my_store.load() then}
	<ul>
	    {#each $my_store as val}
	      <li>
	        {...val}
	      </li>
	    {/each}
	</ul>
{/await}

And sometimes getting the error Uncaught (in promise) Error: {#each} only iterates over array-like objects.

Readable/Writable load functions cannot be reset/reloaded in tests

Introduced in: v0.2.0

const hasConsent = actionWritable((set) => {
    const setConsent = set(true);
    addEventListener('CONSENT_EVENT', setConsent);

    return () => removeEventListener('CONSENT_EVENT', setConsent);  
});

This works perfectly for production purposes, however I'm running into issues in testing. How should we reload or reset this store in a test environment?

What I have tried:

  • Using flagForReload which is defined by the Loadable<any> type. I received the following error: flagForReload is not a function. Seems like we have some divergence between the type and exposed functions for readable/writable.
  • Setting the store back to undefined. This had no affect on the load state.

I can see from your discussions here that this is intended for one off situations and shouldn't be able to be reset. I believe we need to be able to reset these stores in testing environments.

`persisted()` returns `undefined` first time it's accessed, even when `initial` is set

I have the following setup

export type LastLocation = {
    location: null|[number, number]
    state: "DENIED"|"ALLOWED"|"UNKNOWN"|"ASKED"
}

export const lastLocation = persisted<LastLocation>({
    location: null,
    state: "UNKNOWN",
}, "last-location", {
    storageType: "LOCAL_STORAGE",
})

which I then use in my component

<script lang="ts">
    import {lastLocation} from "./utils"

    if($lastLocation.state === "UNKNOWN") {
        // error
    }
</script>

The problem is, that the first time the store is initialized and saved to the local storage with it's default value, $lastLocation is undefined. I need to reload the store/page.
image

This line is the culprit https://github.com/square/svelte-store/blob/main/src/persisted/index.ts#L140

The initial value should be passed, NOT undefined

enableStoreTestingMode always throws error, regardless of where it is located

I've been trying to get enableStoreTestingMode running on my tests, however, I can't seem to figure out where it can be placed to not cause an exception.

I've tried to place it in my setup file, in various unit test files, or anywhere else. I'm wondering if it's related to when vitest runs its setupFiles. Maybe my application is being launched before setupFiles runs?

Working on a small reproduction repo.

Using vitest, in vite.config.ts, I have setup-dom-env.ts in setupFiles, so it should run before any of my tests:

test: {
      include: ["src/**/*.{test,spec}.{js,ts}"],
      environment: "jsdom",
      environmentMatchGlobs: [["**/*.dom.test.{js,ts}", "jsdom"]], // TODO: happy-dom not working with MSWjs
      globals: true,
      setupFiles: "./tests/setup-dom-env.ts",
    },

In setup-dom-test.ts:

// Enable Svelte Store testing mode (adds a "reset" method to all stores)
import { enableStoreTestingMode } from "@square/svelte-store";
enableStoreTestingMode();

---

Error: Testing mode MUST be enabled before store creation
  Module.enableStoreTestingMode node_modules/.pnpm/@square+svelte-store@1.0.15/node_modules/@square/svelte-store/src/config.ts:17:3
  tests/setup-dom-env.ts:10:1
      8| // Enable Svelte Store testing mode (adds a "reset" method to all stores)
      9| import { enableStoreTestingMode } from "@square/svelte-store";
     10| enableStoreTestingMode();

Alternate approach, using the beforeAll hook.

beforeAll(() => enableStoreTestingMode());

---

Error: Testing mode MUST be enabled before store creation
  Module.enableStoreTestingMode node_modules/.pnpm/@square+svelte-store@1.0.15/node_modules/@square/svelte-store/src/config.ts:17:3
  tests/setup-dom-env.ts:28:17
     28| beforeAll(() => enableStoreTestingMode());

Is it possible to run async function on user action?

Hi,

As far as I understand, this library is extremely useful when you want to retrieve some data on a page load. But I tried to make it work for another pretty common use case - user action. For example, button click:

<script lang="ts">
  import { asyncReadable } from "@square/svelte-store"

  const data = asyncReadable(undefined, async () => {
    const resp = await fetch("https://catfact.ninja/fact")
    console.log(resp)
    return await resp.json()
  })
</script>

<button on:click={() => data.load && data.load()}>Fetch data</button>

<div>{JSON.stringify($data)}</div>

Unfortunately, the server (I'm using SvelteKit) is issuing the network request during the render, not on click handler. It seems like the problem is in the template, cause when I comment out this line:

<div>{JSON.stringify($data)}</div>

Then it doesn't try to load content on a first load. I somehow expected that it would use default value, until you call .load() somewhere, either in one of callbacks, or in template using {#await}. But seems that it calls a load under the hood when I access the store (what's the point of initial value then?).

I also tried something like this (which didn't help either)

<script lang="ts">
  import { asyncReadable } from "@square/svelte-store"

  let requestIsLoading = false
  const data = asyncReadable(undefined, async () => {
    const resp = await fetch("https://catfact.ninja/fact")
    return await resp.json()
  })
</script>

<button on:click={() => (requestIsLoading = true)}>Fetch data</button>

{#if requestIsLoading}
  {#await data.load?.()}
    <div>loading</div>
  {:then}
    <div>{JSON.stringify($data)}</div>
  {/await}
{/if}

Would appreciate any feedback regarding this direction. Thanks in advance.

Typescript complains: load() of Loadable<T> may be undefined

I have a Typescript problem when recreating your examples.

{#await myLoadable.load()}
[...]

Typescripts complains that load might be undefined. I wonder why the interface is like that.

I would have to wrap each Loadable in an #if, like

{#if myLoadable.load}
  {#await myLoadable.load()}

But that is rather cumbersome.

Love the the idea of the package - any other way I am missing to fix this? Thanks!

Typescript support for initial `undefined` state

Example:

const getData = (): Promise<Data[]> => { /* ... */ }
const store = asyncDerived([], getData)
<script lang="ts">
    import {store} from '$lib/stores'
    import {derived} from '@square/svelte-store'
    const transformedData = derived(store, data => {
        // according to TypeScript, data here has type of Data[], when in reality it's Data[] | undefined
    })
</script>

is there any way to enforce undefined checks inside the derived stores?

one way of doing it that i have in mind is by defining a wrapper similar to how nanostores suggest to do it:

import { atom, onMount } from 'nanostores'

type Posts = { isLoading: true } | { isLoading: false, posts: Post[] }

let posts = atom<Posts>({ isLoading: true })

onMount(posts, () => {
  fetch('/posts').then(async response => {
    let data = await response.json()
    posts.set({ isLoading: false, posts: data })
  })
  return () => {
    posts.set({ isLoading: true })
  }
})

how would you approach this?

asyncDerived can create duplicate subscriptions

I have a component that's only displayed conditionally. Inside that component, I have an asyncDerived store that derives from a writable store that's defined in a global stores.js file.

App.svelte

{#if displayMyComponent}
<MyComponent />
{/if}

MyComponent.svelte

<script>
import { writable, asyncDerived } from '@square/svelte-store';
import { searchText } from 'stores';

const sortCol = writable('foo');

const myStore = asyncDerived(
  [searchText, sortCol],
  async ([theSearchText, theSortCol]) => {
    console.log('updating');
    return await getData(theSearchText, theSortCol);
  },
  false
);
</script>

stores.js

import { writable } from '@square/svelte-store';

export const searchText = writable('');

The first time displayMyComponent becomes true and the component is displayed, the stores work as expected.

However, when displayMyComponent is toggled from false back to true, thus destroying and reloading the component, whenever the value searchText changes, the derived function runs twice: I see "updating" twice in the console and getData produces two identical network requests. Toggling the display flag again produces triplicate behavior.

Notably, this duplication doesn't happen when the value of sortCol changes no matter how many times the component is destroyed and reloaded. I don't think this happens when you're using plain Svelte derived stores, either.

It seems like there needs to be an unsubscription that happens when the component is destroyed. Is there something I can put in onDestroy that will break the dependency between searchText and myStore? Or a better way to define myStore?

Can persisted stores be configured to not sync using JSON

Maybe a weird workflow here, but I persist a handful of strings to my local storage. Just vanilla strings.

When I reload my page, the persisted store appears to try to serialize/deserialize to/from JSON (which is stated in the docs), but it leads to this after 3 reloads

current:locale = "\"\\\"\\\\\\\"fr\\\\\\\"\\\"\""

I was just wondering if there was any way (or if it's simple enough), to set the JSON part of the serialization as an optional thing, and otherwise just read/persist flat strings.

Overhaul typing to use strict mode

This package was not written in typescript's strict mode, and as such consumers that are in strict mode run into some inconsistent typing, particularly when it comes to potentially undefined values. This package should be overhauled to be strict mode compatible, at which point further typing issues will be easier to identify and resolve.

loading a readable/writable store does not run start function

Consider the store

export const receivedEvent = readable<boolean>(undefined, (set) => {
  const setStore = () => set(true);
  addEventListener(SOME_EVENT , setStore);
  return () => removeEventListener(SOME_EVENT, setStore);
});

This store should load the first time SOME_EVENT is fired. However if you await receivedEvent.load() without subscribing to the store, the store will never load. This is because there is no subscription to the store, so its start function never runs, and thus the event listener is never created.

What's the name of the npm package?

This package looks rad & I'd love to try it.

i'd like some kind of quickstart at the top of the readme, with the npm install command y'know!

Can a regular svelte-store become Loadable?

I'm using a 3rd party library which exposes a Svelte store, and I was wondering if it could be modified or consumed by the Loadable stores, while maintaining the same level of reactivity?

This i18n library (https://github.com/kaisermann/svelte-i18n) exposes a locale store, and I was trying to use persisted to clean up my local storage handling for languages. However, when I change my language, nothing seems to happen (e.g. the cascade of store updates don't appear to occur).

So, this got me thinking I was supposed to wrap that i18n store in a derived or something else first? Or somehow pull it into this Loadable ecosystem?

Feature: Set `"type": "module" + Sourcefiles for Vite / SvelteKit

After installing and using the package my dev console started priting this:

@square/svelte-store doesn't appear to be written in CJS, but also doesn't appear to be a valid ES module (i.e. it doesn't have "type": "module" or an .mjs extension for the entry point). Please contact the package author to fix.
Sourcemap for "/Users/sarcevicantonio/dev/priv/kcalCalc/node_modules/.pnpm/@[email protected]/node_modules/@square/svelte-store/lib/index.js" points to missing source files

Since the package is using ESM and isn't using .mjs extensions, the package.json type field should be set to type.

Since Sourcemaps point to missing .ts Sourcefiles, maybe the .ts files could be shipped as well?

Love the package btw! Helps me handle async data cleanly with such little code 🤯

js-cookie import

After migration to v1.0.11 I have one more issue when running webpack build

 ERROR  Failed to compile with 3 errors                                                         

error  in ./node_modules/@square/svelte-store/lib/persisted/storage-utils.js
export 'get' (imported as 'Cookies') was not found in 'js-cookie' (possible exports: default)

error  in ./node_modules/@square/svelte-store/lib/persisted/storage-utils.js
export 'set' (imported as 'Cookies') was not found in 'js-cookie' (possible exports: default)

error  in ./node_modules/@square/svelte-store/lib/persisted/storage-utils.js
export 'remove' (imported as 'Cookies') was not found in 'js-cookie' (possible exports: default)

webpack compiled with 3 errors and 3 warnings

It looks like js-cookie has old typescript mappings in "@types/js-cookie" module?

More info here:
js-cookie/js-cookie#717

Issues after migration to v1

After migration to v1 I got some warnings and errors... webpack can't compile page now ;/

 WARN  Compiled with 3 warnings                                                                                                                                                                            
 warn  in ./assets/svelte/apps/x1/stores/authMethodsStore.ts                                                                                                                                     
export 'asyncDerived' (imported as 'asyncDerived') was not found in '@square/svelte-store' (possible exports: get)

 warn  in ./assets/svelte/apps/x1/stores/configStore.ts                                                                                                                                          
export 'asyncReadable' (imported as 'asyncReadable') was not found in '@square/svelte-store' (possible exports: get)

 warn  in ./assets/svelte/apps/x1/stores/userStore.ts                                                                                                                                           
export 'persisted' (imported as 'persisted') was not found in '@square/svelte-store' (possible exports: get)

 ERROR  Failed to compile with 5 errors                                                                                                                                                                   

Module build failed: Module not found:
"./node_modules/@square/svelte-store/lib/index.js" contains a reference to the file "./async-client".
This file can not be found, please check it for typos or update it if the file got moved.

"./node_modules/@square/svelte-store/lib/index.js" contains a reference to the file "./async-stores".
This file can not be found, please check it for typos or update it if the file got moved.

"./node_modules/@square/svelte-store/lib/index.js" contains a reference to the file "./persisted".
This file can not be found, please check it for typos or update it if the file got moved.

"./node_modules/@square/svelte-store/lib/index.js" contains a reference to the file "./standard-stores".
This file can not be found, please check it for typos or update it if the file got moved.

"./node_modules/@square/svelte-store/lib/index.js" contains a reference to the file "./utils".
This file can not be found, please check it for typos or update it if the file got moved.

webpack compiled with 5 errors and 3 warnings

Any tips?

Preloading a store

Firstly thank you for this superb store extension.

I would like to preload/prefetch the data in the store during the SSR. Is this the recommended way to do it?

import type { PageLoad } from './$types';
import { productsStore } from '$lib/stores/products';

export const load = (() => {
    productsStore.load();
}) satisfies PageLoad;

Thenable store

Currently the Loadable implementation introduces a new type and requires extra syntax:

{#await myLoadableStore.load()}
 <p>Currently loading...</p>
{:then}
 <p>Your loaded data is: {$myLoadableStore}</p>
{/await}

What about just making the store itself implement .then() ie making it a Thenable.
The syntax would then look like this:

{#await myLoadableStore}
 <p>Currently loading...</p>
{:then}
 <p>Your loaded data is: {$myLoadableStore}</p>
{/await}

Code Samples

Thanks for this great tool! It's a big help in organizing things and makes it easier to manage and reason about.

Just wondering what's the recommended approach for handling individual items in a store?
For example, updating a single item stored inside an AsyncWritable store, sending it back to the server, etc. instead of a whole list of items.
I've noticed the example of removing an item from a store and it would be nice if there's also an example of how to use it when adding / updating items.

Thanks

This library is awesome

Thanks so much guys for creating this. It makes my business logic so much better. Keep it up!

(There were no Github discussions, so I figured I'd throw it here).

Fix typing in AsyncStoreOptions

https://github.com/square/svelte-store/blob/ccb97fce941344c510f2e80eedb523a477df6213/src/async-stores/types.ts#L37C1-L41C2

I think "true" should be "boolean", the following returns an error

  const {store: positionStore, state: positionState} = asyncReadable(
    {},
    async () => {
      const response = await fetch(URL_API + 'positions');
      const userObject = await response.json();
      console.log(userObject)
      return userObject;
    },
    { reloadable: false, trackState: true }
  );

Type 'false' is not assignable to type 'true'.ts(2322)
(property) reloadable?: true

Why load and reload are optional?

Hi,

I'm using TypeScript and since load and reload are optional in stores, I need to write my code like this:

export let asyncStore: Loadable<unknown>

asyncStore.reload?.()

{#await asyncStore.load?.()}
    <div />
{:then} 
    <div />
{/await}

What is the point they are optional? Is there some use case when your store doesn't have them?

Suggested pattern for usage with Dexie (or IndexedDB)

Does anyone have experience or an example to share of how to implement @square/svelte-store on an IndexedDB that has multiple tables and indices?

From what I can gather, to implement IndexedDB using this library, one would not just create an INDEXED_DB StorageType but rather a StorageType for each individual table. Additionally, due to the restrictive interface of the getter/setter/deleter functions, if there were multiple indices, there would need to be a StorageType for each desired index within a table. TABLE1_KEYPATH1 ... TABLE1_KEYPATH2 ... etc, which other than having some maintenance issues, would likely face some performance issues as well (potential for duplicate, dereferenced data)

For example with Dexie, where a "key" can be a object

Interface for Table.get()

table.get(primaryKey): Promise
table.get({keyPath1: value1, keyPath2: value2, ...}): Promise

From what I can see, this is not compatible with the existing interfaces of getStorageItem/setStorageItem/deleteStorageItem, forcing what might be considered an anti-pattern on an implementation (storage types for each index as mentioned above, or wrap this library to JSON.stringify arguments and then JSON.parse the arguments in the functions)

Please let me know if I am missing something.

asyncDerived stores doesn't update when dependencies are Set or Map

Hi,
in my project I had a writable store containing a Map with number as keys, that map is a dependency of an asyncDerived store, but I realized my derived store was not triggering the update.
I tried to compare it with the svelte "standard" stores and it works

<script>
	import { writable, derived, asyncWritable, asyncDerived } from '@square/svelte-store';

	// Standard
	const stdSet = writable(new Set([Math.random()]));
	const stdMap = writable(new Map([[Math.random(), Math.random()]]));
	const stdDer = derived([stdSet, stdMap], ([$set, $map]) => ({ set: $set, map: $map }));

	function editStdSet() {
		stdSet.update((set) => set.add(Math.random()));
	}
	function editStdMap() {
		stdMap.update((map) => map.set(Math.random(), Math.random()));
	}

	$: console.log('standard set', $stdSet.size);
	$: console.log('standard map', $stdMap.size);
	$: console.log('standard derived', { set: $stdDer.set.size, map: $stdDer.map.size });

	// Async
	const asySet = asyncWritable([], async () => new Set([Math.random()]));
	const asyMap = asyncWritable([], async () => new Map([[Math.random(), Math.random()]]));
	const asyDer = asyncDerived([asySet, asyMap], async ([$set, $map]) => ({ set: $set, map: $map }));
	function editAsySet() {
		asySet.update((set) => set.add(Math.random()));
	}
	function editAsyMap() {
		asyMap.update((map) => map.set(Math.random(), Math.random()));
	}

	$: asySet.load().then(() => console.log('async set', $asySet.size));
	$: asyMap.load().then(() => console.log('async map', $asyMap.size));
	$: asyDer.load().then(() => console.log('async derived', { set: $asyDer.set.size, map: $asyDer.map.size }));

	// Standard writable w/ async derived
	const stdAsyDer = asyncDerived([stdSet, stdMap], async ([$set, $map]) => ({ set: $set, map: $map }));

	$: stdAsyDer.load().then(() => console.log('standard writable w/ async derived', { set: $stdAsyDer.set.size, map: $stdAsyDer.map.size }));
</script>

<p>
	<button on:click={editStdSet}>Reset Std Set</button>
	<button on:click={editStdMap}>Reset Std Map</button>
</p>

<p>
	<button on:click={editAsySet}>Reset Asy Set</button>
	<button on:click={editAsyMap}>Reset Asy Map</button>
</p>

debounce/throttle

How would one use debounce/throttle when using asyncWritable? Currently sending requests to an API and the asyncWritable triggers on an input with bind:value because I have a 3rd argument set.

exports is not defined in ES module scope

When using the current version 0.1.3, I have the following error. Rolling back to 0.1.1 fixes the problem.

Error message:

exports is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and 'C:\Dev\[...]\node_modules\.pnpm\@[email protected]\node_modules\@square\svelte-store\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.

How can I return a unsubscribe function?

Using a normal svelte store, the return value from a readable or writable would be a function thats being called on unsubscribe.
But I cannot figure out how to return a unsubscribe function with asyncReadable or asyncWritable.

Document naive await values are not reactive

currently we have the following in the documentation (which is reactive)

{#await myLoadableStore.load()}
 <p>Currently loading...</p>
{:then}
 <p>Your loaded data is: {$myLoadableStore}</p>
{/await}

we could warn that the following won't be reactive

{#await myLoadableStore.load()}
 <p>Currently loading...</p>
{:then myValue}
 <p>Your loaded data is: {myValue}</p>
{/await}

Passing arguments to load() and reactively reloading on argument update

My app has projects each project has it's own id and a project can have members. So in SvelteKit it looks something like: (app)/projects/[id]/members/[id] . In my members +page.svelte I use an await block for the asyncWritable store. What I want to know is how to rerun the await block inside the members +page.svelte and also pass some arbitrary data argument to the asyncWritable which can be used as the project id filter in my API URL so it only list the members of current project something like members.load($page.params.id) .

Currently, I access project id inside the store file using get(page).params.id but when I navigate to a different project ($page.params.id has changed) the members list doesn't update. I want the members.load() to rerun when $page.params.id changes. I know something similar can be achieved using combination of SvelteKit's native load() and invalidate() but how to do above in context of square svelte stores?

Add sessionStorage and localStorage functionality

Hello from Poland :)
I really appreciate what you are doing :) your module helps me keep my code as clean as possible :)

What do you think about adding sessionStorage and localStorage functionality?

I did something like that for myself and I'm quite happy about that. This is how this can look for asyncReadable function:

export const asyncSessionReadable = <T>(sessionItemName: string, initial: T, loadFunction: () => Promise<T>, reloadable?: boolean): Loadable<T> => {
    const finalLoadFunction = async () => {
        const result = await loadFunction();
        sessionStorage.setItem(sessionItemName, JSON.stringify(result));
        return result;
    };

    const { subscribe, load } = asyncReadable(
        {},
        async () => {
            if (sessionStorage.getItem(sessionItemName)) {
                return JSON.parse(sessionStorage.getItem(sessionItemName));
            }
            const result = await finalLoadFunction();
            return result;
        },
        reloadable,
    );

    return {
        subscribe,
        load,
        reload: finalLoadFunction,
    };
};

How to delay the asyncDerived store kicking off until previous writeable has completed?

I have two stores, and I'm trying to figure out how to sync them up correctly. Using MockServiceWorkerJS, I seem to get into a problem that I'm requesting my updated Posts object before the starredStore has had the chance to finish its update handler.

Is there a way to delay updating downstream stores until the upstream ones have completed their updates?

export const starredStore: WritableLoadable<string[]> = asyncWritable(
  [],
  async () => {
    const response = await fetch(`/starred`);
    // Error handling
    return await response.json();
  },
  async (newStarred) => {
    const postBody = JSON.stringify(newStarred);
    const response = await fetch(`/starred`, {
      method: "PUT",
      headers: {
        "Content-Type": "application/json",
      },
      body: postBody,
    });
    // Error handling
    return newStarred;
  }
);


export const starredPosts: Loadable<any[]> = asyncDerived(
  starredStore,
  async (_) => {
    const response = await fetch(`/posts`);
    // Error handling
    return await response.json();
  }
);

Question: Async reentrant safety of load?

It is possible to want to access the contents of a single async store from multiple places. Is it necessary for application code to prevent multiple concurrent (async concurrent, not thread) calls to its load function to prevent duplicate fetches, for example? This of course extends to the entire derived tree and becomes much more complicated for the application.

Having looked at the source I think this is correctly prevented. Having done some basic testing it also looks to avoid duplicate processing. Still, I thought it was better to ask to make it clear.

`trackState` typing

I just noticed that until I passed trackState: true, my attempts to use isLoading or isPending were failing. But typescript didn't warn me about it. So I thought that it actually can be typed in some whey similar to this snipped

But, then question. Is this desired?

Publish the latest version

Can the latest version be published to remove the peer dependency warnings

➤ YN0060: │ svelte is listed by your project with version 4.2.10, which doesn't satisfy what @square/svelte-store (pf597d) requests (^3.0.0).

Error Handling

I came to this project after the Svelte Summit talk and it looks very promising (pun intended).

I just wanted to ask what would be the canonical way to handle errors? For example, when your post request to update fails how do you restore to the state before the optimistic update without refetching all the data from an api? Is it done automatically on a failed promise? Also, what happens if a get request fails due to network issues? Will load promise still resolve with the previous data?

By the way, the idea behind this project is awesome. I’ve been thinking to build something myself to solve similar problems. You did a great work :)

Svelte 5 Support

Svelte 5 will be released sometime this year with the new runes API (Preview Docs) and while stores are not deprecated, $runes will allow you to do all the same things. So, are there plans to eventually support Svelte 5 when it's officially released? I really like this package's ideas and its svelte API compared to the competitors and hope it stays maintained ❤️

Existing inline functionality is not reactive

Example: {#await safeLoad(myStore) then loadedSuccessfully}
This generates a single promise that resolves on the first load of the store.
If that resolves successfully but then the store is reloaded and fails, the template does not have any access to this new state information.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.