Giter VIP home page Giter VIP logo

choo-persist's Introduction

choo-persist stability

npm version build status downloads js-standard-style

Persist choo state to localStorage. localStorage is supported by all major browsers.

Usage

var persist = require('choo-persist')
var choo = require('choo')

var app = choo()
app.use(persist())

API

instance = persist([opts])

Load the app state from localStorage and set up listeners to write the state back on every event. Can take an optional argument of options:

  • opts.name: default 'choo-persist'; the localStorage key.
  • opts.filter(state): modify the state that's about to be saved. Useful for removing values that cannot be serialized to JSON.
var xtend = require('xtend')
var opts = {
  filter: function (state) {
    state = xtend(state) // clone the object
    delete state.sadArrayFilledWithFunctions
    return state
  }
}

Installation

$ npm install choo-persist

Should I use this while developing.

No; state is persisted between page reloads which might put your page in very odd states, with a very annoying way to clear. Consider using hot reloading for development instead.

How / when should I invalidate the database cache?

Ah, this is where good ol' data persistance comes into play - there's loads of approaches on this, but yeah you should def find a way to migrate data between incompatible models. Perhaps some day we'll have a good chapter on this in the choo handbook. Until then: have fun I guess?

License

MIT

choo-persist's People

Contributors

greenkeeperio-bot avatar runeh avatar tgfjt avatar yoshuawuyts 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

Watchers

 avatar  avatar  avatar  avatar

choo-persist's Issues

Keep synced for connection losts

This is like a feature request. Right now, this plugin allow a fast first load from indexDB, cool. But what happens when the connections is lost, and the app attemp to make an http request. It would be awesome to allow to the user to keep using the app, and save all changes to local db, and once the connection is availaible again, repost all the state to keep data synced.
Well, the thing is that I'm actually facing this problem right now, I'm writing a pwa with choo here and is working for now, but when I go offline, I have a fast first load, thanks to sw and idb, but can go any other place in the app because of xhr calls failling.

Idea

I think this could be done using choo hooks. If there is a call for the onError hook due to xhr failing, save those changes only in the db, maybe an special collection only for offline diffs. Then on every state change, check if that special collection has data, in that case POST that data to sync the remote db, and clear that collection.

Well that sounds simple but I know is not haha. I wanna hear your opinion about it. Does it make sense? Do you have any better idea?

Synchronize choo state with indexedDB

Hi!
The repo description talks about indexedDB, but when I use choo-persist everything ends up in a big localStorage item.

Is there an option that I need to pass to choo-persist to say 'use indexedDB' or WebSQL etc, or even 'use localForage', rather than localStorage?

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.