Coder Social home page Coder Social logo

unissist's Introduction

npm travis

unissist

A tiny ~300b unistore persistence helper library state container with various storage adapters.

Table of Contents

Install

This project uses node and npm. Go check them out if you don't have them locally installed.

npm install --save unissist

Then with a module bundler like webpack or rollup, use as you would anything else:

// The helper:
import persistStore from 'unissist';

// localStorage Adapter
import localStorageAdapter from 'unissist/integrations/localStorageAdapter';

// indexedDB Adapter
import indexedDBAdapter from 'unissist/integrations/indexdbAdapter';

// AsyncStorage Adapter
import asyncStorageAdapter from 'unissist/integrations/asyncStorageAdapter';

The UMD build is also available on unpkg:

<script src="//unpkg.com/unistore/dist/unissist.umd.js"></script>

You can find the library on window.unissist.

Usage

// use unistore like you always would, but use the unissist helper to persist state
import createStore from 'unistore'
import persistStore from 'unissist'
import localStorageAdapter from 'unissist/integrations/localStorageAdapter';

const store = createStore({ count: 0 })
const adapter = localStorageAdapter();

persistStore(store, adapter);

// store state now includes a `hydrated` that tells you whether or not the state has been rehydrated from the store

Unissist also takes a third parameter that allows you to configure how your peristence works

import createStore from 'unistore'
import persistStore from 'unissist'
import localStorageAdapter from 'unissist/integrations/localStorageAdapter';

const store = createStore({ count: 0 })
const adapter = localStorageAdapter();

// these are the default values, except migration. By default, a version change simply drops the persisted state
let config = {
  version: 1,
  debounceTime: 100,
  migration: (oldState, oldversion) => ({ /* new state */ }),
}

persistStore(store, adapter);

Reporting Issues

Found a problem? Want a new feature? First of all, see if your issue or idea has already been reported. If not, just open a new clear and descriptive issue.

Credits

Jason Miller for unistore, assign util function, and a lot of the tooling that I stole used
Jake Archibald for idb-keyval where much of the code for the indexdbAdapter was stolen borrowed
Sean Groff for the name

License

MIT License © Donnie West

unissist's People

Contributors

donniewest avatar

Watchers

 avatar  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.