-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (32 loc) · 898 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
31
32
33
34
35
36
37
38
39
40
import storage from 'store/dist/store.modern';
import storageObservePlugin from 'store/plugins/observe';
storage.addPlugin(storageObservePlugin);
function fromStorage(storage) {
function readable(init, key) {
if (storage.get(key) == null) storage.set(key, init);
return {
subscribe: function (fn) {
fn(storage.get(key));
const id = storage.observe(key, newVal => {
fn(newVal);
});
return function () {
storage.unobserve(id);
};
}
};
}
function writable(init, key) {
return Object.assign(readable(init, key), {
set: function (val) {
storage.set(key, val);
},
update: function (fn) {
storage.set(key, fn(storage.get(key)));
},
});
}
return { readable, writable };
}
const { readable, writable } = fromStorage(storage);
export { readable, writable };