valdres 0.2.0-alpha.2 → 0.2.0-alpha.3

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.
@@ -0,0 +1,18 @@
1
+ export { atom } from "./src/atom";
2
+ export { atomFamily } from "./src/atomFamily";
3
+ export { createStore } from "./src/createStore";
4
+ export { getDefaultStore, resetDefaultStore } from "./src/getDefaultStore";
5
+ export { selector } from "./src/selector";
6
+ export { selectorFamily } from "./src/selectorFamily";
7
+ export { isAtom } from "./src/utils/isAtom";
8
+ export { isSelector } from "./src/utils/isSelector";
9
+ export { isFamily } from "./src/utils/isFamily";
10
+ export { isPromiseLike } from "./src/utils/isPromiseLike";
11
+ export type { Atom } from "./src/types/Atom";
12
+ export type { AtomFamily } from "./src/types/AtomFamily";
13
+ export type { GetValue } from "./src/types/GetValue";
14
+ export type { Selector } from "./src/types/Selector";
15
+ export type { SelectorFamily } from "./src/types/SelectorFamily";
16
+ export type { State } from "./src/types/State";
17
+ export type { Store } from "./src/types/Store";
18
+ export type { StoreData } from "./src/types/StoreData";
package/dist/index.js CHANGED
@@ -86,6 +86,24 @@ var atomFamily = (defaultValue, debugLabel) => {
86
86
  atomFamily2._map = map;
87
87
  return atomFamily2;
88
88
  };
89
+ // src/lib/createStoreData.ts
90
+ var generateId = () => (Math.random() + 1).toString(36).substring(7);
91
+ var createStoreData = (id = generateId()) => ({
92
+ id,
93
+ values: new WeakMap,
94
+ expiredValues: new WeakMap,
95
+ subscriptions: new WeakMap,
96
+ subscriptionsRequireEqualCheck: new WeakMap,
97
+ stateConsumers: new WeakMap,
98
+ stateDependencies: new WeakMap
99
+ });
100
+
101
+ // src/utils/isAtom.ts
102
+ var isAtom = (state) => Object.hasOwn(state, "defaultValue");
103
+
104
+ // src/utils/isSelector.ts
105
+ var isSelector = (state) => Object.hasOwn(state, "get");
106
+
89
107
  // src/lib/updateStateSubscribers.ts
90
108
  var updateStateSubscribers = (state, data) => {
91
109
  const subscribtions = data.subscriptions.get(state);
@@ -299,12 +317,6 @@ var initAtom = (atom3, data) => {
299
317
  return value;
300
318
  };
301
319
 
302
- // src/utils/isAtom.ts
303
- var isAtom = (state) => Object.hasOwn(state, "defaultValue");
304
-
305
- // src/utils/isSelector.ts
306
- var isSelector = (state) => Object.hasOwn(state, "get");
307
-
308
320
  // src/utils/isFamily.ts
309
321
  var isFamily = (state) => Object.hasOwn(state, "_map");
310
322
 
@@ -326,8 +338,17 @@ var getState2 = (state, data) => {
326
338
  throw new Error("Invalid object passed to get");
327
339
  };
328
340
 
341
+ // src/lib/resetAtom.ts
342
+ var resetAtom = (atom3, data) => {
343
+ const res = initAtom(atom3, data);
344
+ if (!isPromiseLike(res)) {
345
+ propagateUpdatedAtoms([atom3], data);
346
+ }
347
+ return res;
348
+ };
349
+
329
350
  // src/lib/unsubscribe.ts
330
- var unsubscribe = (state, subscription, data, mountRes) => {
351
+ var unsubscribe = (state, subscription, data, mount) => {
331
352
  const subscribers = data.subscriptions.get(state);
332
353
  if (subscribers) {
333
354
  subscribers.delete(subscription);
@@ -343,9 +364,11 @@ var unsubscribe = (state, subscription, data, mountRes) => {
343
364
  data.subscriptionsRequireEqualCheck.delete(state);
344
365
  }
345
366
  }
346
- if (subscribers.size === 0) {
347
- if (state.onUnmount) {
348
- state.onUnmount(mountRes);
367
+ if (mount) {
368
+ if (subscribers.size === mount.mountSubscriptions.size) {
369
+ if (state.onUnmount) {
370
+ state.onUnmount(mount.onMountRes);
371
+ }
349
372
  }
350
373
  }
351
374
  }
@@ -375,17 +398,25 @@ var subscribe = (state, callback, requireDeepEqualCheckBeforeCallback, data) =>
375
398
  requireDeepEqualCheckBeforeCallback
376
399
  };
377
400
  }
378
- let mountRes;
379
- if (subscribers.size === 0 && state.onMount) {
380
- mountRes = state.onMount((value) => {
381
- setAtom(state, value, data);
382
- });
383
- }
384
401
  subscribers.add(subscription);
402
+ let mount;
403
+ if (subscribers.size === 1 && state.onMount) {
404
+ const store = storeFromStoreData2(data);
405
+ const mountSubscriptions = new Set;
406
+ const originalSub = store.sub;
407
+ store.sub = (state2, callback2) => {
408
+ mountSubscriptions.add(callback2);
409
+ return originalSub(state2, callback2);
410
+ };
411
+ mount = {
412
+ onMountRes: state.onMount(store, state),
413
+ mountSubscriptions
414
+ };
415
+ }
385
416
  if (requireDeepEqualCheckBeforeCallback && data.subscriptionsRequireEqualCheck.get(state) !== true) {
386
417
  data.subscriptionsRequireEqualCheck.set(state, true);
387
418
  }
388
- return () => unsubscribe(state, subscription, data, mountRes);
419
+ return () => unsubscribe(state, subscription, data, mount);
389
420
  };
390
421
 
391
422
  // src/lib/setAtoms.ts
@@ -461,41 +492,9 @@ var transaction = (callback, data) => {
461
492
  return result;
462
493
  };
463
494
 
464
- // src/lib/resetAtom.ts
465
- var resetAtom = (atom3, data) => {
466
- const res = initAtom(atom3, data);
467
- if (!isPromiseLike(res)) {
468
- propagateUpdatedAtoms([atom3], data);
469
- }
470
- return res;
471
- };
472
-
473
- // src/createStore.ts
474
- var generateId = () => (Math.random() + 1).toString(36).substring(7);
475
- var createStore = (id) => {
476
- const data = {
477
- id: id ?? generateId(),
478
- values: new WeakMap,
479
- expiredValues: new WeakMap,
480
- subscriptions: new WeakMap,
481
- subscriptionsRequireEqualCheck: new WeakMap,
482
- stateConsumers: new WeakMap,
483
- stateDependencies: new WeakMap
484
- };
495
+ // src/lib/storeFromStoreData.ts
496
+ var storeFromStoreData2 = (data) => {
485
497
  const get = (state) => getState2(state, data);
486
- const getWithDefault = (atom3, defaultValue) => {
487
- if (!isAtom(atom3))
488
- throw new Error("Only atom allowed");
489
- if (data.values.has(atom3)) {
490
- return data.values.get(atom3);
491
- } else {
492
- if (typeof defaultValue === "function") {
493
- defaultValue = defaultValue((state) => getState2(state, data));
494
- }
495
- data.values.set(atom3, defaultValue);
496
- return defaultValue;
497
- }
498
- };
499
498
  const set = (state, value) => {
500
499
  if (isAtom(state)) {
501
500
  return setAtom(state, value, data);
@@ -516,7 +515,6 @@ var createStore = (id) => {
516
515
  const txn = (callback) => transaction(callback, data);
517
516
  return {
518
517
  get,
519
- getWithDefault,
520
518
  set,
521
519
  sub,
522
520
  txn,
@@ -524,6 +522,12 @@ var createStore = (id) => {
524
522
  data
525
523
  };
526
524
  };
525
+
526
+ // src/createStore.ts
527
+ var createStore = (id) => {
528
+ const data = createStoreData(id);
529
+ return storeFromStoreData2(data);
530
+ };
527
531
  // src/getDefaultStore.ts
528
532
  if (!globalThis._valdresStore) {
529
533
  globalThis._valdresStore = createStore("default");
@@ -1,9 +1,9 @@
1
1
  import type { Atom } from "./types/Atom";
2
- type AtomOptions<M = any> = {
2
+ type AtomOptions<MountRes = undefined> = {
3
3
  label?: string;
4
4
  onInit?: () => void;
5
- onMount?: () => M;
6
- onUnmount?: (mountRes?: M) => void;
5
+ onMount?: () => MountRes;
6
+ onUnmount?: (mountRes?: MountRes) => void;
7
7
  };
8
8
  export declare const atom: <Value, FamilyKey = undefined, MountReturnValue = undefined>(defaultValue?: Value | (() => Value | Promise<Value>), options?: AtomOptions<MountReturnValue>) => Atom<Value, FamilyKey>;
9
9
  export {};
@@ -1,2 +1,4 @@
1
1
  import type { AtomFamily } from "./types/AtomFamily";
2
- export declare const atomFamily: <Value, Key>(defaultValue?: Value | ((arg: Key) => Value | Promise<Value>), debugLabel?: string) => AtomFamily<Value, Key>;
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>;
4
+ export {};
@@ -1,2 +1,2 @@
1
1
  export declare const getDefaultStore: () => any;
2
- export declare const resetDefaultStore: () => import("./types/Store").Store;
2
+ export declare const resetDefaultStore: () => import("..").Store;
@@ -0,0 +1,2 @@
1
+ import type { StoreData } from "../types/StoreData";
2
+ export declare const createStoreData: (id?: string) => StoreData;
@@ -1,4 +1,4 @@
1
1
  import type { StoreData } from "../types/StoreData";
2
2
  import type { Selector } from "../types/Selector";
3
3
  export declare const reEvaluateSelector: <V>(selector: Selector<V>, data: StoreData) => void;
4
- export declare const initSelector: <V>(selector: Selector<V>, data: StoreData) => V;
4
+ export declare const initSelector: <V>(selector: Selector<V>, data: StoreData) => V | Promise<V>;
@@ -0,0 +1,3 @@
1
+ import type { Store } from "../types/Store";
2
+ import type { StoreData } from "../types/StoreData";
3
+ export declare const storeFromStoreData: (data: StoreData) => Store;
@@ -1,3 +1,3 @@
1
1
  import type { State } from "../types/State";
2
2
  import type { StoreData } from "../types/StoreData";
3
- export declare const unsubscribe: <V>(state: State<V>, subscription: any, data: StoreData, mountRes?: any) => void;
3
+ export declare const unsubscribe: <V>(state: State<V>, subscription: any, data: StoreData, mount?: any) => void;
@@ -4,6 +4,7 @@ export type Atom<Value = unknown, FamilyKey = undefined, MountRes = undefined> =
4
4
  label?: string;
5
5
  family?: AtomFamily<Value, FamilyKey>;
6
6
  familyKey?: FamilyKey;
7
+ onInit?: () => void;
7
8
  onMount?: () => MountRes;
8
9
  onUnmount?: (mountRes?: MountRes) => void;
9
10
  };
@@ -1,2 +1,2 @@
1
1
  import type { Atom } from "./Atom";
2
- export type ResetAtom = <V>(state: Atom<V>) => V;
2
+ export type ResetAtom = <V>(state: Atom<V>) => V | Promise<V>;
@@ -1,3 +1,2 @@
1
1
  import type { Atom } from "../types/Atom";
2
- import type { State } from "../types/State";
3
- export declare const isAtom: (state: State) => state is Atom;
2
+ export declare const isAtom: (state: any) => state is Atom;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "valdres",
3
- "version": "0.2.0-alpha.2",
3
+ "version": "0.2.0-alpha.3",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "Eigil Sagafos"
@@ -14,7 +14,7 @@
14
14
  "dist"
15
15
  ],
16
16
  "scripts": {
17
- "build": "NODE_ENV=production bun scripts/build.ts",
17
+ "build": "rm -rf dist && NODE_ENV=production bun scripts/build.ts",
18
18
  "build:types": "tsc",
19
19
  "test": "bun test"
20
20
  },
@@ -34,5 +34,5 @@
34
34
  "access": "public",
35
35
  "registry": "https://registry.npmjs.org/"
36
36
  },
37
- "gitHead": "4f2355214e1e4d2cf9011705733b18ab425f8d20"
37
+ "gitHead": "8fc679bd330552fdc8db39c02cbf4a650d1d2615"
38
38
  }
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,2 +0,0 @@
1
- export declare const wait: (ms: number) => Promise<unknown>;
2
- export declare const waitCrash: (ms: number) => Promise<unknown>;