UNPKG

839 BJavaScriptView Raw
1System.register(['jotai'], (function (exports) {
2 'use strict';
3 var atom;
4 return {
5 setters: [function (module) {
6 atom = module.atom;
7 }],
8 execute: (function () {
9
10 exports('atomWithStore', atomWithStore);
11
12 function atomWithStore(store) {
13 const baseAtom = atom(store.getState());
14 baseAtom.onMount = (setValue) => {
15 const callback = () => {
16 setValue(store.getState());
17 };
18 const unsub = store.subscribe(callback);
19 callback();
20 return unsub;
21 };
22 const derivedAtom = atom((get) => get(baseAtom), (get, _set, update) => {
23 const newState = typeof update === "function" ? update(get(baseAtom)) : update;
24 store.setState(newState, true);
25 });
26 return derivedAtom;
27 }
28
29 })
30 };
31}));