-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.js
159 lines (132 loc) · 3.83 KB
/
core.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { Atom, Selector } from "./Store.js";
/**
* @typedef {Object} selectorCallbacks
* @property {(a: atom) => any} getAtom
* @property {(s: selector) => Promise<any>} getSelector
*/
/**
* @typedef {Object} atomOptions
* @property {string} key
* @property {any} [default]
* @property {Array<() => void |(() => () => void)>} [effects]
* @property {() => Promise<any>} [loadable]
*/
/**
* @typedef {Object} selectorOptions
* @property {string} key
* @property {(cbs: selectorCallbacks) => any | Promise<any>} get
*/
/**
* @typedef {Promise} selector
* @property {string} key
*/
/**
* @typedef {Object} atom
* @property {string} key
* @property {() => any} getState
* @property {() => void} update
*/
/** @type {Map<string, Atom>} */
export const atoms = new Map();
/** @type {Map<string, Selector>} */
export const selectors = new Map();
/** @param {Atom} atom */
const notify = atom => {
atom.notify();
atom.children?.forEach(child => {
atom.dispatchEvent(new CustomEvent(child, { detail: {key: child}}))
});
}
/**
* @param {Atom} atom
* @param {any | (() => any)} val
*/
export const updateAtom = (atom, val) => {
atom.state = typeof val === 'function' ? val(atom.state) : val;
notify(atom);
atom.cleanupEffects = atom.effects?.map(effect => effect()) || [];
}
/**
* @param {atomOptions} atomOptions
* @returns {[atom, (val?: any) => void | ((val?: any) => Promise<any>)]}
*/
export const atom = ({key, default: val, loadable, effects}) => {
if(!atoms.has(key)) {
const atom = new Atom();
if(val && loadable) throw new Error("Atom can't have a default and be loadable");
atom.state = typeof val === 'function' ? val() : val;
if(loadable) {
atom.state = { status: 'initialized', result: null };
atom.loadable = loadable;
}
atom.effects = effects || [];
atom.cleanupEffects = atom.effects?.map(effect => effect());
atom.key = key;
atoms.set(key, atom);
}
return [{
key,
getState: () => atoms.get(key).state,
update: () => {notify(atoms.get(key))}
},
loadable
? (val) => {
const atom = atoms.get(key);
atom.state.status = 'loading';
notify(atom);
atom.cleanupEffects = atom.effects?.map(effect => effect()) || [];
return atom.loadable(val)
.then((res) => {atom.state = {status: 'success', result: res}})
.catch((err) => {atom.state = {status: 'error', result: err}})
.finally(() => {notify(atom)});
}
: (val) => {
const atom = atoms.get(key);
updateAtom(atom, val);
}
]
}
/** @param {selectorOptions} selector */
export const selector = async ({key, get}) => {
const parents = new Set();
const updateSelectorVal = async () => {
const selector = selectors.get(key);
selector.value = await selector.get();
selector.notify();
}
/**
* @param {atom} atom
* @returns {any} state
*/
const getAtom = ({key: atomKey}) => {
const atom = atoms.get(atomKey);
atom.addEventListener(key, updateSelectorVal);
parents.add(atomKey);
atom.children.add(key);
return atom.state;
}
/**
* @param {selector} parentSelector
* @returns {Promise<any>} state
*/
const getSelector = async (parentSelector) => {
const { key: parentKey } = await parentSelector;
const parent = selectors.get(parentKey);
parent.addEventListener(parentKey, updateSelectorVal);
parents.add(parentKey);
parent.children.add(key);
return parent.value;
}
const createGet = ({getAtom, getSelector}) => async () => get({getAtom, getSelector});
if(!selectors.has(key)) {
const selector = new Selector();
selector.key = key;
selector.get = createGet({getAtom, getSelector});
selector.parents = parents;
selectors.set(key, selector);
selector.value = await selector.get();
}
return {
key,
}
}