forked from jamen/hyperapp-persist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
30 lines (23 loc) · 746 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module.exports = function (app, opts) {
return function (props) {
opts = opts || {}
var storage = opts.storage || 'hyperapp-persist'
var include = opts.include || []
var _init = props.init
props.init = function (state, actions) {
if (_init) _init.apply(null, arguments)
var data = localStorage.getItem(storage)
if (data) actions.__restore(JSON.parse(data))
window.addEventListener('unload', function () {
actions.__save()
})
}
props.actions.__restore = function (_s, _a, previous) {
return previous
}
props.actions.__save = function (state) {
localStorage.setItem(storage, JSON.stringify(state, include))
}
return app(props)
}
}