valdres 0.2.0-alpha.6 → 0.2.0-alpha.8

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
@@ -101,6 +101,9 @@ var createStoreData = (id = generateId()) => ({
101
101
  // src/utils/isAtom.ts
102
102
  var isAtom = (state) => Object.hasOwn(state, "defaultValue");
103
103
 
104
+ // src/lib/getState.ts
105
+ import equal4 from "fast-deep-equal";
106
+
104
107
  // src/lib/updateStateSubscribers.ts
105
108
  var updateStateSubscribers = (state, data) => {
106
109
  const subscribtions = data.subscriptions.get(state);
@@ -145,7 +148,7 @@ var evaluateSelector = (selector, data) => {
145
148
  let result;
146
149
  try {
147
150
  result = selector.get((state) => {
148
- const value = getState2(state, data);
151
+ const value = getState(state, data);
149
152
  updatedDependencies.add(state);
150
153
  if (isPromiseLike(value))
151
154
  throw new SuspendAndWaitForResolveError(value);
@@ -243,13 +246,13 @@ var recursivlyResetSelectorTree = (selectors, data, clearedSelectors) => {
243
246
  };
244
247
  var propagateUpdatedAtoms = (atoms, data) => {
245
248
  const clearedSelectors = new Set;
246
- for (const atom3 of atoms) {
247
- const consumers = data.stateConsumers.get(atom3);
249
+ for (const atom2 of atoms) {
250
+ const consumers = data.stateConsumers.get(atom2);
248
251
  if (consumers && consumers.size) {
249
252
  recursivlyResetSelectorTree(consumers, data, clearedSelectors);
250
253
  }
251
- if (atom3.family) {
252
- const consumersFamily = data.stateConsumers.get(atom3.family);
254
+ if (atom2.family) {
255
+ const consumersFamily = data.stateConsumers.get(atom2.family);
253
256
  if (consumersFamily?.size) {
254
257
  recursivlyResetSelectorTree(consumersFamily, data, clearedSelectors);
255
258
  }
@@ -258,15 +261,15 @@ var propagateUpdatedAtoms = (atoms, data) => {
258
261
  for (const selector of clearedSelectors) {
259
262
  updateSelectorSubscribers(selector, data);
260
263
  }
261
- for (const atom3 of atoms) {
262
- updateStateSubscribers(atom3, data);
264
+ for (const atom2 of atoms) {
265
+ updateStateSubscribers(atom2, data);
263
266
  }
264
267
  };
265
268
 
266
269
  // src/lib/setAtom.ts
267
270
  import equal3 from "fast-deep-equal";
268
- var setAtom = (atom3, newValue, data) => {
269
- const currentValue = getState2(atom3, data);
271
+ var setAtom = (atom2, newValue, data) => {
272
+ const currentValue = getState(atom2, data);
270
273
  if (typeof newValue === "function") {
271
274
  newValue = newValue(currentValue);
272
275
  if (isPromiseLike(newValue) || isPromiseLike(currentValue))
@@ -274,16 +277,16 @@ var setAtom = (atom3, newValue, data) => {
274
277
  }
275
278
  if (equal3(currentValue, newValue))
276
279
  return;
277
- data.values.set(atom3, newValue);
280
+ data.values.set(atom2, newValue);
278
281
  if (currentValue?.__isEmptyAtomPromise__) {
279
282
  currentValue.__resolveEmptyAtomPromise__(newValue);
280
283
  }
281
- propagateUpdatedAtoms([atom3], data);
284
+ propagateUpdatedAtoms([atom2], data);
282
285
  };
283
286
 
284
287
  // src/lib/initAtom.ts
285
- var getAtomInitValue = (atom3, data) => {
286
- if (atom3.defaultValue === undefined) {
288
+ var getAtomInitValue = (atom2, data) => {
289
+ if (atom2.defaultValue === undefined) {
287
290
  let promiseResolve;
288
291
  const promise = new Promise((resolve) => {
289
292
  promiseResolve = resolve;
@@ -291,25 +294,26 @@ var getAtomInitValue = (atom3, data) => {
291
294
  promise.__isEmptyAtomPromise__ = true;
292
295
  promise.__resolveEmptyAtomPromise__ = promiseResolve;
293
296
  return promise;
294
- } else if (typeof atom3.defaultValue === "function") {
295
- const value = atom3.defaultValue();
297
+ } else if (typeof atom2.defaultValue === "function") {
298
+ const value = atom2.defaultValue();
296
299
  if (isPromiseLike(value)) {
297
300
  value.then((resolvedValue) => {
298
- data.values.set(atom3, resolvedValue);
299
- propagateUpdatedAtoms([atom3], data);
301
+ data.values.set(atom2, resolvedValue);
302
+ propagateUpdatedAtoms([atom2], data);
300
303
  });
301
304
  }
302
305
  return value;
303
306
  } else {
304
- return atom3.defaultValue;
307
+ return atom2.defaultValue;
305
308
  }
306
309
  };
307
- var initAtom = (atom3, data) => {
308
- const value = getAtomInitValue(atom3, data);
309
- data.values.set(atom3, value);
310
- if (atom3.onInit)
311
- atom3.onInit((newVal) => {
312
- setAtom(atom3, newVal, data);
310
+ var initAtom = (atom2, data) => {
311
+ let value = getAtomInitValue(atom2, data);
312
+ data.values.set(atom2, value);
313
+ if (atom2.onInit)
314
+ atom2.onInit((newVal) => {
315
+ value = newVal;
316
+ setAtom(atom2, newVal, data);
313
317
  });
314
318
  return value;
315
319
  };
@@ -321,7 +325,7 @@ var isSelector = (state) => Object.hasOwn(state, "get");
321
325
  var isFamily = (state) => Object.hasOwn(state, "_map");
322
326
 
323
327
  // src/lib/getState.ts
324
- var getState2 = (state, data) => {
328
+ function getState(state, data) {
325
329
  if (data.values.has(state))
326
330
  return data.values.get(state);
327
331
  if (isAtom(state))
@@ -329,20 +333,20 @@ var getState2 = (state, data) => {
329
333
  if (isSelector(state))
330
334
  return initSelector(state, data);
331
335
  if (isFamily(state)) {
332
- const res = [];
333
- for (const atom3 of state._map.values()) {
334
- res.push([atom3.familyKey, getState2(atom3, data)]);
335
- }
336
- return res;
336
+ const array = Array.from(state._map.keys());
337
+ if (equal4(array, state._keyArray))
338
+ return state._keyArray;
339
+ state._keyArray = array;
340
+ return array;
337
341
  }
338
342
  throw new Error("Invalid object passed to get");
339
- };
343
+ }
340
344
 
341
345
  // src/lib/resetAtom.ts
342
- var resetAtom = (atom3, data) => {
343
- const res = initAtom(atom3, data);
346
+ var resetAtom = (atom2, data) => {
347
+ const res = initAtom(atom2, data);
344
348
  if (!isPromiseLike(res)) {
345
- propagateUpdatedAtoms([atom3], data);
349
+ propagateUpdatedAtoms([atom2], data);
346
350
  }
347
351
  return res;
348
352
  };
@@ -366,8 +370,8 @@ var unsubscribe = (state, subscription, data, mount) => {
366
370
  }
367
371
  if (mount) {
368
372
  if (subscribers.size === mount.mountSubscriptions.size) {
369
- if (state.onUnmount) {
370
- state.onUnmount(mount.onMountRes);
373
+ if (typeof mount.onUnmount === "function") {
374
+ mount.onUnmount();
371
375
  }
372
376
  }
373
377
  }
@@ -401,7 +405,7 @@ var subscribe = (state, callback, requireDeepEqualCheckBeforeCallback, data) =>
401
405
  subscribers.add(subscription);
402
406
  let mount;
403
407
  if (subscribers.size === 1 && state.onMount) {
404
- const store = storeFromStoreData2(data);
408
+ const store = storeFromStoreData(data);
405
409
  const mountSubscriptions = new Set;
406
410
  const originalSub = store.sub;
407
411
  store.sub = (state2, callback2) => {
@@ -409,7 +413,7 @@ var subscribe = (state, callback, requireDeepEqualCheckBeforeCallback, data) =>
409
413
  return originalSub(state2, callback2);
410
414
  };
411
415
  mount = {
412
- onMountRes: state.onMount(store, state),
416
+ onUnmount: state.onMount(store, state),
413
417
  mountSubscriptions
414
418
  };
415
419
  }
@@ -420,14 +424,14 @@ var subscribe = (state, callback, requireDeepEqualCheckBeforeCallback, data) =>
420
424
  };
421
425
 
422
426
  // src/lib/setAtoms.ts
423
- import equal4 from "fast-deep-equal";
427
+ import equal5 from "fast-deep-equal";
424
428
  var setAtoms = (pairs, data) => {
425
429
  const updatedAtoms = [];
426
- for (let [atom3, value] of pairs) {
427
- const currentValue = getState2(atom3, data);
428
- if (!equal4(currentValue, value)) {
429
- updatedAtoms.push(atom3);
430
- data.values.set(atom3, value);
430
+ for (let [atom2, value] of pairs) {
431
+ const currentValue = getState(atom2, data);
432
+ if (!equal5(currentValue, value)) {
433
+ updatedAtoms.push(atom2);
434
+ data.values.set(atom2, value);
431
435
  }
432
436
  }
433
437
  propagateUpdatedAtoms(updatedAtoms, data);
@@ -461,7 +465,7 @@ var transaction = (callback, data) => {
461
465
  let dirtySelectors = new Set;
462
466
  const txnGet = (state) => {
463
467
  if (isAtom(state)) {
464
- return txnAtomMap.has(state) ? txnAtomMap.get(state) : getState2(state, data);
468
+ return txnAtomMap.has(state) ? txnAtomMap.get(state) : getState(state, data);
465
469
  } else if (isSelector(state)) {
466
470
  if (txnSelectorCache.has(state)) {
467
471
  return txnSelectorCache.get(state);
@@ -483,25 +487,25 @@ var transaction = (callback, data) => {
483
487
  throw new Error("Unsupported state");
484
488
  }
485
489
  };
486
- const txnSet = (atom3, value) => {
487
- if (!isAtom(atom3))
490
+ const txnSet = (atom2, value) => {
491
+ if (!isAtom(atom2))
488
492
  throw new Error("Not an atom");
489
493
  if (typeof value === "function") {
490
- const currentValue = txnGet(atom3);
494
+ const currentValue = txnGet(atom2);
491
495
  value = value(currentValue);
492
496
  }
493
- for (const selector of findDependencies(atom3, data)) {
497
+ for (const selector of findDependencies(atom2, data)) {
494
498
  dirtySelectors.add(selector);
495
499
  txnSelectorCache.delete(selector);
496
500
  }
497
- if (txnSubscribers.get(atom3)?.size) {
498
- recursivlyResetTxnSelectorCache(atom3, txnSubscribers, txnSelectorCache);
501
+ if (txnSubscribers.get(atom2)?.size) {
502
+ recursivlyResetTxnSelectorCache(atom2, txnSubscribers, txnSelectorCache);
499
503
  }
500
- txnAtomMap.set(atom3, value);
504
+ txnAtomMap.set(atom2, value);
501
505
  };
502
- const txnReset = (atom3) => {
503
- const value = getAtomInitValue(atom3, data);
504
- txnAtomMap.set(atom3, value);
506
+ const txnReset = (atom2) => {
507
+ const value = getAtomInitValue(atom2, data);
508
+ txnAtomMap.set(atom2, value);
505
509
  return value;
506
510
  };
507
511
  const commit = () => {
@@ -514,14 +518,14 @@ var transaction = (callback, data) => {
514
518
  };
515
519
 
516
520
  // src/lib/storeFromStoreData.ts
517
- var storeFromStoreData2 = (data) => {
518
- const get = (state) => getState2(state, data);
521
+ var storeFromStoreData = (data) => {
522
+ const get = (state) => getState(state, data);
519
523
  const set = (state, value) => {
520
524
  if (!isAtom(state))
521
525
  throw new Error("Invalid state object");
522
526
  return setAtom(state, value, data);
523
527
  };
524
- const reset = (atom3) => resetAtom(atom3, data);
528
+ const reset = (atom2) => resetAtom(atom2, data);
525
529
  const sub = (state, callback, deepEqualCheckBeforeCallback = true) => subscribe(state, callback, deepEqualCheckBeforeCallback, data);
526
530
  const txn = (callback) => transaction(callback, data);
527
531
  return {
@@ -537,7 +541,7 @@ var storeFromStoreData2 = (data) => {
537
541
  // src/createStore.ts
538
542
  var createStore = (id) => {
539
543
  const data = createStoreData(id);
540
- return storeFromStoreData2(data);
544
+ return storeFromStoreData(data);
541
545
  };
542
546
  // src/createStoreWithSelectorSet.ts
543
547
  var setSelector = (selector, values, store) => {
@@ -545,7 +549,7 @@ var setSelector = (selector, values, store) => {
545
549
  };
546
550
  var createStoreWithSelectorSet = (id) => {
547
551
  const data = createStoreData(id);
548
- const store = storeFromStoreData2(data);
552
+ const store = storeFromStoreData(data);
549
553
  store.set = (state, value, ...rest) => {
550
554
  if (isAtom(state))
551
555
  return setAtom(state, value, data);
@@ -1,9 +1,8 @@
1
1
  import type { Atom } from "./types/Atom";
2
- type AtomOptions<Value, MountRes = undefined> = {
2
+ type AtomOptions<Value> = {
3
3
  label?: string;
4
4
  onInit?: (setSelf: (value: Value) => void) => void;
5
- onMount?: () => MountRes;
6
- onUnmount?: (mountRes?: MountRes) => void;
5
+ onMount?: () => () => void;
7
6
  };
8
- export declare const atom: <Value, FamilyKey = undefined, MountReturnValue = undefined>(defaultValue?: Value | (() => Value | Promise<Value>), options?: AtomOptions<Value, MountReturnValue>) => Atom<Value, FamilyKey>;
7
+ export declare const atom: <Value, FamilyKey = undefined>(defaultValue?: Value | (() => Value | Promise<Value>), options?: AtomOptions<Value>) => Atom<Value, FamilyKey>;
9
8
  export {};
@@ -1,4 +1,4 @@
1
1
  import type { AtomFamily } from "./types/AtomFamily";
2
2
  type DefaultValueCallback<Key, Value> = (arg: Key) => Value | Promise<Value>;
3
- export declare const atomFamily: <Value, Key>(defaultValue?: Value | DefaultValueCallback<Key, Value>, debugLabel?: string) => AtomFamily<Value, Key>;
3
+ export declare const atomFamily: <Value = unknown, Key = unknown>(defaultValue?: Value | DefaultValueCallback<Key, Value>, debugLabel?: string) => AtomFamily<Value, Key>;
4
4
  export {};
@@ -1,3 +1,7 @@
1
- import type { State } from "../types/State";
2
1
  import type { StoreData } from "../types/StoreData";
3
- export declare const getState: <V>(state: State<V>, data: StoreData) => V | Promise<V>;
2
+ import type { Atom } from "../types/Atom";
3
+ import type { AtomFamily } from "../types/AtomFamily";
4
+ import type { Selector } from "../types/Selector";
5
+ export declare function getState<V, K>(atom: Atom<V>, data: StoreData): V;
6
+ export declare function getState<V, K>(selector: Selector<V>, data: StoreData): V;
7
+ export declare function getState<V, K>(family: AtomFamily<V, K>, data: StoreData): K[];
@@ -1,10 +1,9 @@
1
1
  import type { AtomFamily } from "./AtomFamily";
2
- export type Atom<Value = unknown, FamilyKey = undefined, MountRes = undefined> = {
2
+ export type Atom<Value = unknown, FamilyKey = undefined> = {
3
3
  defaultValue?: Value | (() => Value | Promise<Value>);
4
4
  label?: string;
5
5
  family?: AtomFamily<Value, FamilyKey>;
6
6
  familyKey?: FamilyKey;
7
7
  onInit?: (setSelf: (value: Value) => void) => void;
8
- onMount?: () => MountRes;
9
- onUnmount?: (mountRes?: MountRes) => void;
8
+ onMount?: () => void | (() => void);
10
9
  };
@@ -1,2 +1,8 @@
1
- import type { State } from "./State";
2
- export type GetValue = <V>(state: State<V>) => V | Promise<V>;
1
+ import type { Atom } from "./Atom";
2
+ import type { AtomFamily } from "./AtomFamily";
3
+ import type { Selector } from "./Selector";
4
+ export type GetValue = {
5
+ <V, K>(atom: Atom<V>): V;
6
+ <V, K>(selector: Selector<V>): V;
7
+ <V, K>(family: AtomFamily<V, K>): K[];
8
+ };
@@ -1,9 +1,9 @@
1
1
  import type { GetValue } from "./GetValue";
2
2
  import type { SelectorFamily } from "./SelectorFamily";
3
- export type Selector<Value = any, FamilyKey = undefined, MountRes = unknown> = {
3
+ export type Selector<Value = unknown, FamilyKey = undefined> = {
4
4
  get: (get: GetValue) => Value;
5
5
  debugLabel?: string;
6
6
  family?: SelectorFamily<Value, FamilyKey>;
7
7
  familyKey?: FamilyKey;
8
- onMount?: () => MountRes;
8
+ onMount?: () => void | (() => void);
9
9
  };
package/package.json CHANGED
@@ -1,10 +1,15 @@
1
1
  {
2
2
  "name": "valdres",
3
- "version": "0.2.0-alpha.6",
3
+ "version": "0.2.0-alpha.8",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "Eigil Sagafos"
7
7
  },
8
+ "homepage": "https://valdres.dev",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/eigilsagafos/valdres.git"
12
+ },
8
13
  "type": "module",
9
14
  "exports": {
10
15
  "types": "./dist/index.d.ts",
@@ -28,11 +33,11 @@
28
33
  "react": ">=18",
29
34
  "react-dom": ">=18",
30
35
  "recoil": "0.7.7",
31
- "typescript": "5.5.4"
36
+ "typescript": "5.6.2"
32
37
  },
33
38
  "publishConfig": {
34
39
  "access": "public",
35
40
  "registry": "https://registry.npmjs.org/"
36
41
  },
37
- "gitHead": "727e6a247df61e016e0f7905397a9121abf4e82c"
42
+ "gitHead": "c7f6e2d76adc15985444d02244522841d52aa233"
38
43
  }