-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.cjs.js
47 lines (36 loc) · 1.13 KB
/
index.cjs.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
41
42
43
44
45
46
47
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var storage = _interopDefault(require('store/dist/store.modern'));
var storageObservePlugin = _interopDefault(require('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);
exports.readable = readable;
exports.writable = writable;