UNPKG

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