valdres 0.2.0-alpha.29 → 0.2.0-alpha.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,31 +1,31 @@
1
- // src/lib/setAtom.ts
2
- import equal4 from "fast-deep-equal/es6";
1
+ // src/utils/isAtom.ts
2
+ var isAtom = (state) => Object.hasOwn(state, "defaultValue");
3
+
4
+ // src/utils/isFamilyState.ts
5
+ var isFamilyState = (state) => state && Object.hasOwn(state, "family");
6
+
7
+ // src/utils/isFamilyAtom.ts
8
+ var isFamilyAtom = (state) => isFamilyState(state) && isAtom(state);
3
9
 
4
10
  // src/utils/isPromiseLike.ts
5
11
  var isPromiseLike = (object) => {
6
12
  return object && object.then && typeof object.then === "function";
7
13
  };
8
14
 
9
- // src/lib/getState.ts
10
- import equal3 from "fast-deep-equal/es6";
15
+ // src/utils/isSelector.ts
16
+ var isSelector = (state) => state && Object.hasOwn(state, "get");
11
17
 
12
- // src/utils/isAtom.ts
13
- var isAtom = (state) => Object.hasOwn(state, "defaultValue");
18
+ // src/lib/getState.ts
19
+ import equal2 from "fast-deep-equal/es6";
14
20
 
15
21
  // src/utils/isAtomFamily.ts
16
22
  var isAtomFamily = (state) => state && Object.hasOwn(state, "__valdresAtomFamilyMap");
17
23
 
18
- // src/utils/isSelector.ts
19
- var isSelector = (state) => state && Object.hasOwn(state, "get");
20
-
21
24
  // src/utils/isSelectorFamily.ts
22
25
  var isSelectorFamily = (state) => state && Object.hasOwn(state, "__valdresSelectorFamilyMap");
23
26
 
24
- // src/utils/isFamilyState.ts
25
- var isFamilyState = (state) => state && Object.hasOwn(state, "family");
26
-
27
- // src/utils/isFamilyAtom.ts
28
- var isFamilyAtom = (state) => isFamilyState(state) && isAtom(state);
27
+ // src/lib/initSelector.ts
28
+ import equal from "fast-deep-equal/es6";
29
29
 
30
30
  // src/lib/updateStateSubscribers.ts
31
31
  var updateStateSubscribers = (state, data) => {
@@ -45,11 +45,7 @@ var updateStateSubscribers = (state, data) => {
45
45
  }
46
46
  };
47
47
 
48
- // src/lib/updateSelectorSubscribers.ts
49
- import equal2 from "fast-deep-equal/es6";
50
-
51
48
  // src/lib/initSelector.ts
52
- import equal from "fast-deep-equal/es6";
53
49
  class SuspendAndWaitForResolveError extends Error {
54
50
  promise;
55
51
  constructor(promise) {
@@ -126,7 +122,43 @@ var initSelector = (selector, data) => {
126
122
  return value;
127
123
  };
128
124
 
125
+ // src/lib/getState.ts
126
+ function getState(state, data) {
127
+ if (data.values.has(state))
128
+ return data.values.get(state);
129
+ if (isAtom(state)) {
130
+ if ("parent" in data)
131
+ return getState(state, data.parent);
132
+ return initAtom(state, data);
133
+ }
134
+ if (isSelector(state))
135
+ return initSelector(state, data);
136
+ if (isAtomFamily(state)) {
137
+ if ("parent" in data) {
138
+ const closestData = findClosestStoreWithAtomInitialized(state.__keysAtom, data);
139
+ return getState(state.__keysSelector, closestData);
140
+ }
141
+ return getState(state.__keysSelector, data);
142
+ }
143
+ if (isSelectorFamily(state)) {
144
+ const array = Array.from(state.__valdresSelectorFamilyMap.keys());
145
+ if (equal2(array, state._keyArray))
146
+ return state._keyArray;
147
+ state._keyArray = array;
148
+ return array;
149
+ }
150
+ throw new Error("Invalid object passed to get");
151
+ }
152
+ var findClosestStoreWithAtomInitialized = (atom, data) => {
153
+ if ("parent" in data === false)
154
+ return data;
155
+ if (data.values.has(atom))
156
+ return data;
157
+ return findClosestStoreWithAtomInitialized(atom, data.parent);
158
+ };
159
+
129
160
  // src/lib/updateSelectorSubscribers.ts
161
+ import equal3 from "fast-deep-equal/es6";
130
162
  var updateSelectorSubscribers = (selector, data) => {
131
163
  const subscribtions = data.subscriptions.get(selector);
132
164
  const familySubscriptions = selector.family && data.subscriptions.get(selector.family);
@@ -136,7 +168,7 @@ var updateSelectorSubscribers = (selector, data) => {
136
168
  try {
137
169
  const oldValue = data.expiredValues.get(selector);
138
170
  const newValue = initSelector(selector, data);
139
- if (equal2(newValue, oldValue))
171
+ if (equal3(newValue, oldValue))
140
172
  return;
141
173
  } catch (e) {
142
174
  }
@@ -189,6 +221,26 @@ var propagateUpdatedAtoms = (atoms, data) => {
189
221
  }
190
222
  };
191
223
 
224
+ // src/lib/setAtom.ts
225
+ import equal4 from "fast-deep-equal/es6";
226
+ var setAtom = (atom, newValue, data, skipOnSet = false) => {
227
+ const currentValue = getState(atom, data);
228
+ if (typeof newValue === "function") {
229
+ newValue = newValue(currentValue);
230
+ if (isPromiseLike(newValue) || isPromiseLike(currentValue))
231
+ throw new Error("Todo, how should we handle this?");
232
+ }
233
+ if (equal4(currentValue, newValue))
234
+ return;
235
+ data.values.set(atom, newValue);
236
+ if (atom.onSet && !skipOnSet)
237
+ atom.onSet(newValue, data);
238
+ if (currentValue?.__isEmptyAtomPromise__) {
239
+ currentValue.__resolveEmptyAtomPromise__(newValue);
240
+ }
241
+ propagateUpdatedAtoms([atom], data);
242
+ };
243
+
192
244
  // src/lib/initAtom.ts
193
245
  var getAtomInitValue = (atom, data) => {
194
246
  if (atom.defaultValue === undefined) {
@@ -233,60 +285,6 @@ var initAtom = (atom, data) => {
233
285
  return value;
234
286
  };
235
287
 
236
- // src/lib/getState.ts
237
- function getState(state, data) {
238
- if (data.values.has(state))
239
- return data.values.get(state);
240
- if (isAtom(state)) {
241
- if ("parent" in data)
242
- return getState(state, data.parent);
243
- return initAtom(state, data);
244
- }
245
- if (isSelector(state))
246
- return initSelector(state, data);
247
- if (isAtomFamily(state)) {
248
- if ("parent" in data) {
249
- const closestData = findClosestStoreWithAtomInitialized(state.__keysAtom, data);
250
- return getState(state.__keysSelector, closestData);
251
- }
252
- return getState(state.__keysSelector, data);
253
- }
254
- if (isSelectorFamily(state)) {
255
- const array = Array.from(state.__valdresSelectorFamilyMap.keys());
256
- if (equal3(array, state._keyArray))
257
- return state._keyArray;
258
- state._keyArray = array;
259
- return array;
260
- }
261
- throw new Error("Invalid object passed to get");
262
- }
263
- var findClosestStoreWithAtomInitialized = (atom, data) => {
264
- if ("parent" in data === false)
265
- return data;
266
- if (data.values.has(atom))
267
- return data;
268
- return findClosestStoreWithAtomInitialized(atom, data.parent);
269
- };
270
-
271
- // src/lib/setAtom.ts
272
- var setAtom = (atom, newValue, data, skipOnSet = false) => {
273
- const currentValue = getState(atom, data);
274
- if (typeof newValue === "function") {
275
- newValue = newValue(currentValue);
276
- if (isPromiseLike(newValue) || isPromiseLike(currentValue))
277
- throw new Error("Todo, how should we handle this?");
278
- }
279
- if (equal4(currentValue, newValue))
280
- return;
281
- data.values.set(atom, newValue);
282
- if (atom.onSet && !skipOnSet)
283
- atom.onSet(newValue, data);
284
- if (currentValue?.__isEmptyAtomPromise__) {
285
- currentValue.__resolveEmptyAtomPromise__(newValue);
286
- }
287
- propagateUpdatedAtoms([atom], data);
288
- };
289
-
290
288
  // src/lib/globalAtom.ts
291
289
  var getFirstItemInSet = (set) => {
292
290
  for (let item of set) {
@@ -297,13 +295,27 @@ var getFirstItemInSet = (set) => {
297
295
  var globalAtom = (defaultValue, options) => {
298
296
  const stores = new Set;
299
297
  let value = defaultValue;
298
+ let initialized = false;
299
+ let onReset;
300
300
  if (options.onSet)
301
301
  throw new Error("onSet on globalAtom is currently not supported");
302
- if (options.onInit)
303
- throw new Error("onInit on globalAtom is currently not supported");
304
- const onInit = (setSelf2, store) => {
305
- setSelf2(value);
306
- stores.add(store);
302
+ const onInit = (setSelf2, data) => {
303
+ if (!initialized) {
304
+ if (value === defaultValue) {
305
+ value = getAtomInitValue(atom, data);
306
+ }
307
+ setSelf2(value);
308
+ initialized = true;
309
+ if (options.onInit) {
310
+ onReset = options.onInit((newVal) => {
311
+ setSelf2(newVal);
312
+ value = newVal;
313
+ }, data);
314
+ }
315
+ } else {
316
+ setSelf2(value);
317
+ }
318
+ stores.add(data);
307
319
  };
308
320
  const onSet = (newValue, currentStore) => {
309
321
  value = newValue;
@@ -323,6 +335,20 @@ var globalAtom = (defaultValue, options) => {
323
335
  setAtom(atom, newValue, store);
324
336
  }
325
337
  };
338
+ const resetSelf = () => {
339
+ value = defaultValue;
340
+ initialized = false;
341
+ for (const store of stores) {
342
+ if (store.stateDependencies.has(atom)) {
343
+ throw new Error("TODO: Reset support for stateDependencies");
344
+ }
345
+ store.values.delete(atom);
346
+ store.expiredValues.delete(atom);
347
+ propagateUpdatedAtoms([atom], store);
348
+ stores.delete(store);
349
+ onReset?.();
350
+ }
351
+ };
326
352
  const atom = {
327
353
  ...options,
328
354
  defaultValue,
@@ -330,6 +356,7 @@ var globalAtom = (defaultValue, options) => {
330
356
  onInit,
331
357
  onSet,
332
358
  setSelf,
359
+ resetSelf,
333
360
  get currentValue() {
334
361
  return value;
335
362
  }
@@ -16,6 +16,7 @@ export { isSelectorFamily } from "./src/utils/isSelectorFamily";
16
16
  export type { Atom } from "./src/types/Atom";
17
17
  export type { AtomFamily } from "./src/types/AtomFamily";
18
18
  export type { GetValue } from "./src/types/GetValue";
19
+ export type { GlobalAtom } from "./src/types/GlobalAtom";
19
20
  export type { ResetAtom } from "./src/types/ResetAtom";
20
21
  export type { Selector } from "./src/types/Selector";
21
22
  export type { SelectorFamily } from "./src/types/SelectorFamily";
@@ -1,4 +1,4 @@
1
- import type { GlobalAtom } from "./../types/GlobalAtom";
2
- import type { AtomOptions } from "./../types/AtomOptions";
3
1
  import type { AtomDefaultValue } from "../types/AtomDefaultValue";
2
+ import type { AtomOptions } from "./../types/AtomOptions";
3
+ import type { GlobalAtom } from "./../types/GlobalAtom";
4
4
  export declare const globalAtom: <Value = any>(defaultValue: AtomDefaultValue<Value>, options: AtomOptions<Value>) => GlobalAtom<Value>;
@@ -1,5 +1,3 @@
1
1
  import type { AtomFamilyAtom } from "./AtomFamilyAtom";
2
- import type { GlobalAtomSetSelfFunc } from "./GlobalAtomSetSelfFunc";
3
- export type AtomFamilyGlobalAtom<Key = unknown, Value = unknown> = AtomFamilyAtom<Key, Value> & {
4
- setSelf: GlobalAtomSetSelfFunc<Value>;
5
- };
2
+ import type { GlobalAtom } from "./GlobalAtom";
3
+ export type AtomFamilyGlobalAtom<Key = unknown, Value = unknown> = AtomFamilyAtom<Key, Value> & GlobalAtom<Value>;
@@ -3,7 +3,7 @@ import type { StoreData } from "./StoreData";
3
3
  export type AtomOptions<Value = unknown> = {
4
4
  global?: boolean;
5
5
  label?: string;
6
- onInit?: (setSelf: (value: Value) => void, store: StoreData) => void;
6
+ onInit?: (setSelf: (value: Value) => void, store: StoreData) => (() => void) | void;
7
7
  onSet?: AtomOnSet<Value>;
8
8
  onMount?: () => () => void;
9
9
  maxAge?: number;
@@ -1,5 +1,7 @@
1
1
  import type { Atom } from "./Atom";
2
2
  import type { GlobalAtomSetSelfFunc } from "./GlobalAtomSetSelfFunc";
3
+ import type { GlobalAtomResetSelfFunc } from "./GlobalAtomResetSelfFunc";
3
4
  export type GlobalAtom<Value = unknown> = Atom<Value> & {
4
5
  setSelf: GlobalAtomSetSelfFunc<Value>;
6
+ resetSelf: GlobalAtomResetSelfFunc;
5
7
  };
@@ -0,0 +1 @@
1
+ export type GlobalAtomResetSelfFunc = () => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "valdres",
3
- "version": "0.2.0-alpha.29",
3
+ "version": "0.2.0-alpha.30",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "Eigil Sagafos"
@@ -39,5 +39,5 @@
39
39
  "access": "public",
40
40
  "registry": "https://registry.npmjs.org/"
41
41
  },
42
- "gitHead": "0c2a2c8d8c2697b2dc8dd67f12629528d51da1a7"
42
+ "gitHead": "5b94699fe0e875ccfd7e95e535bec8a7ecedcb30"
43
43
  }