Giter Club home page Giter Club logo

svelte-storez's Introduction

Storez

Svelte writable store with extra feature

  • Fully compatible with svelte writable store
  • Subscriber function receives the new and the old value
  • Read and persist value to localstorage
  • Keep previous values in history (undo store mutation with undo())
  • Write operation debouncing
  • Get current value without having to subscribe or use svelte/store get method

Installation

npm install storez

Usage example

Check out the Demo

Basic example

import storez from "storez";

const myStore = storez("my value");

const dispose = myStore.subscribe((newVal, oldVal) => {
  console.log(`'${oldVal}' has been changed to '${mewVal}'`);
});

myStore.set("changed value"); // console output: 'my value' has been changed to 'changed value'

With localStorage

import storez from "storez";

const myStore = storez("my value", { localstorage: { key: "myPersistedStore" } });

const dispose = myStore.subscribe(() => {...});

myStore.set("changed value");

localstorage.getItem("myPersistedStore") // === "my value"

dispose(); // persist in localStorage

localstorage.getItem("myPersistedStore") // === "changed value"

With history

import storez from "storez";

export const store = storez("my value", { history: { size: 1000 } });

store.set("changed value");

store.set("another value");

// Undo last mutation:
store.z.undo();

In the Svelte file:

<script>
  import { store } from "./store";
  const history = store.z.history; // is a Svelte derived store containing an array of all the previous values
  // e.g. ["my value", "changed value"]
</script>

<input type="text" bind:value="{$store}" />
<p>Value: {$store}</p>

<strong>History</strong>
<ul>
  {#each $history as item}
  <li>{item}</li>
  {/each}
</ul>
<button on:click="{store.z.undo}">Undo</button>

API

Constructor

// Svelte compatible store API
storez(val);
storez(val, start);

// Same with Storez options
storez(val, options);
storez(val, start, options);

Storez instance

const instance = storez("initial", ...);
instance.set("newValue");
instance.set("wrongValue");

instance.z.get() // returns the current value of the store (e.g. "wrongValue") This value is not reactive

// If History module enabled:
instance.z.history; // History module: Svelte readable store containing the state history
instance.z.undo(); // History module: undo last mutation
// Here: instance === newValue

Options

Module Options Type Details
(default) debounce Number Timeout between each insert in the store
localstorage key String Key under which the local storage will be saved
history size Number Overall size of the array of element kept in the history

svelte-storez's People

Contributors

plrenaudin avatar softwarerero avatar

Watchers

James Cloos avatar

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.