UNPKG

697 BJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var jotai = require('jotai');
6
7function atomWithStore(store) {
8 var baseAtom = jotai.atom(store.getState());
9
10 baseAtom.onMount = function (setValue) {
11 var callback = function callback() {
12 setValue(store.getState());
13 };
14
15 var unsub = store.subscribe(callback);
16 callback();
17 return unsub;
18 };
19
20 var derivedAtom = jotai.atom(function (get) {
21 return get(baseAtom);
22 }, function (get, _set, update) {
23 var newState = typeof update === 'function' ? update(get(baseAtom)) : update;
24 store.setState(newState, true);
25 });
26 return derivedAtom;
27}
28
29exports.atomWithStore = atomWithStore;