├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | tmp/ 4 | npm-debug.log* 5 | .DS_Store 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | node_js: 2 | - "0.12" 3 | - "4" 4 | sudo: false 5 | language: node_js 6 | script: "npm run test:cov" 7 | # after_script: "npm i -g codecov.io && cat ./coverage/lcov.info | codecov" 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Yoshua Wuyts 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # choo-persist [![stability][0]][1] 2 | [![npm version][2]][3] [![build status][4]][5] 3 | [![downloads][8]][9] [![js-standard-style][10]][11] 4 | 5 | Persist [choo][choo] state to [localStorage][mdn]. localStorage is supported 6 | by [all major browsers][caniuse]. 7 | 8 | ## Usage 9 | ```js 10 | var persist = require('choo-persist') 11 | var choo = require('choo') 12 | 13 | var app = choo() 14 | app.use(persist()) 15 | 16 | ``` 17 | 18 | ## API 19 | ### `instance = persist([opts])` 20 | Load the app state from `localStorage` and set up listeners to write the state 21 | back on every event. Can take an optional argument of options: 22 | - __opts.name:__ default `'choo-persist'`; the `localStorage` key. 23 | - __opts.filter(state):__ modify the state that's about to be saved. Useful 24 | for removing values that cannot be serialized to JSON. 25 | 26 | ```js 27 | var xtend = require('xtend') 28 | var opts = { 29 | filter: function (state) { 30 | state = xtend(state) // clone the object 31 | delete state.sadArrayFilledWithFunctions 32 | return state 33 | } 34 | } 35 | ``` 36 | 37 | ## Installation 38 | ```sh 39 | $ npm install choo-persist 40 | ``` 41 | 42 | ## Should I use this while developing. 43 | No; state is persisted between page reloads which might put your page in very 44 | odd states, with a very annoying way to clear. Consider using hot reloading for 45 | development instead. 46 | 47 | ## How / when should I invalidate the database cache? 48 | Ah, this is where good ol' data persistance comes into play - there's loads of 49 | approaches on this, but yeah you should def find a way to migrate data between 50 | incompatible models. Perhaps some day we'll have a good chapter on this in the 51 | choo handbook. Until then: have fun I guess? 52 | 53 | ## License 54 | [MIT](https://tldrlegal.com/license/mit-license) 55 | 56 | [0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square 57 | [1]: https://nodejs.org/api/documentation.html#documentation_stability_index 58 | [2]: https://img.shields.io/npm/v/choo-persist.svg?style=flat-square 59 | [3]: https://npmjs.org/package/choo-persist 60 | [4]: https://img.shields.io/travis/yoshuawuyts/choo-persist/master.svg?style=flat-square 61 | [5]: https://travis-ci.org/yoshuawuyts/choo-persist 62 | [6]: https://img.shields.io/codecov/c/github/yoshuawuyts/choo-persist/master.svg?style=flat-square 63 | [7]: https://codecov.io/github/yoshuawuyts/choo-persist 64 | [8]: http://img.shields.io/npm/dm/choo-persist.svg?style=flat-square 65 | [9]: https://npmjs.org/package/choo-persist 66 | [10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 67 | [11]: https://github.com/feross/standard 68 | [caniuse]: http://caniuse.com/#feat=namevalue-storage 69 | [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Storage/LocalStorage 70 | [choo]: https://github.com/yoshuawuyts/choo 71 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var mutate = require('xtend/mutable') 2 | 3 | module.exports = persist 4 | 5 | function persist (opts) { 6 | opts = opts || {} 7 | 8 | var name = opts.name || 'choo-persist' 9 | var filter = opts.filter 10 | 11 | return function (state, bus) { 12 | var savedState = null 13 | try { 14 | savedState = JSON.parse(window.localStorage.getItem(name)) 15 | } catch (e) { 16 | savedState = {} 17 | } 18 | 19 | mutate(state, savedState) 20 | bus.on('*', listener) 21 | 22 | bus.on('clear', function () { 23 | bus.removeListener('*', listener) 24 | try { 25 | window.localStorage.removeItem(name) 26 | } catch (e) { 27 | bus.emit('log:warn', 'Could not wipe localStorage ' + name) 28 | } 29 | bus.emit('log:warn', 'Wiping localStorage ' + name) 30 | }) 31 | 32 | function listener (eventName, data) { 33 | var savedState = filter ? filter(state) : state 34 | try { 35 | window.localStorage.setItem(name, JSON.stringify(savedState)) 36 | } catch (e) { 37 | bus.removeListener('*', listener) 38 | bus.emit('log:warn', 'Could not set item to localStorage ' + name) 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "choo-persist", 3 | "version": "3.0.2", 4 | "description": "Synchronize choo state with indexedDB", 5 | "main": "index.js", 6 | "scripts": { 7 | "deps": "dependency-check . && dependency-check . --extra --no-dev", 8 | "test": "standard && npm run deps", 9 | "start": "bankai start --entry=example.js -p 1337 --open", 10 | "test:cov": "standard && npm run deps" 11 | }, 12 | "repository": "yoshuawuyts/choo-persist", 13 | "keywords": [ 14 | "choo", 15 | "state", 16 | "persist", 17 | "indexedDB", 18 | "local", 19 | "localstorage" 20 | ], 21 | "license": "MIT", 22 | "dependencies": { 23 | "xtend": "^4.0.1" 24 | }, 25 | "devDependencies": { 26 | "bankai": "^3.3.0", 27 | "choo": "^4.0.0-0", 28 | "dependency-check": "^2.6.0", 29 | "istanbul": "^0.4.5", 30 | "standard": "^8.0.0", 31 | "tape": "^4.6.0" 32 | } 33 | } 34 | --------------------------------------------------------------------------------