Options
All
  • Public
  • Public/Protected
  • All
Menu

Index

Interfaces

Type aliases

Functions

Type aliases

IStorageStore

IStorageStore<T>: function

Represents the Svelte Store factory returned by storage

Type parameters

  • T

Type declaration

    • (key: string, default_value: T): Writable<T>
    • Parameters

      • key: string
      • default_value: T

      Returns Writable<T>

Functions

storage

  • Returns a Readable (Server) / Writable (Browser) Svelte Store with a reactive binding to a given Storage adapter

    NOTE: Only JSON-compatible values are supported

    As a semi-complete example:

    import {get} from "svelte/store";
    
    import {storage} from "svelte-commons/lib/stores/browser";
    
    // Below, creating a factory function wrapper around the `localStorage` Web Storage API
    const local_storage = storage(window.localStorage, {
        // Both `event` and `event_source` tells the Store what event string to
        // listen to and what `EventTarget` to listen from for tab-sync
        event: "storage",
        event_source: window,
    
        // `.prefix` tells the Store to prefix all storage keys with a specific string (defaults to `svelte-commons.`)
        prefix: "my_key_prefix."
    });
    
    // Now we can use our new `local_storage` factory to make a reactive Store binding
    // to a specific `localStorage` key.
    const store = local_storage("my_string_key", "I am default");
    
    // Using `get`, we can see the Store is already at its default
    console.log(get(store)); // logs: `I am default`
    
    // Since there is nothing set yet, the actual localStorage key is empty
    console.log(window.localStorage.getItem("my_key_prefix.my_string_key")) // logs: `null`
    
    // After setting a value, both the Store and `localStorage` have the same value
    store.set("But, this is not default");
    console.log(
        get(store),
        window.localStorage.getItem("my_key_prefix.my_string_key")
    ); // logs: `But, this is not default`, `"But, this is not default"`
    
    // By setting the Store to the default value OR `undefined`, the
    // `localStorage` item is removed
    store.set("I am default");
    
    console.log(
        get(store),
        window.localStorage.getItem("my_key_prefix.my_string_key")
    ); // logs: `I am default`, `null`

    Type parameters

    Parameters

    Returns IStorageStore<T>

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Type alias with type parameter
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc