valdres 0.2.0-alpha.5 → 0.2.0-alpha.6

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.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { atom } from "./src/atom";
2
2
  export { atomFamily } from "./src/atomFamily";
3
3
  export { createStore } from "./src/createStore";
4
+ export { createStoreWithSelectorSet } from "./src/createStoreWithSelectorSet";
4
5
  export { getDefaultStore, resetDefaultStore } from "./src/getDefaultStore";
5
6
  export { selector } from "./src/selector";
6
7
  export { selectorFamily } from "./src/selectorFamily";
@@ -14,6 +15,7 @@ export type { GetValue } from "./src/types/GetValue";
14
15
  export type { Selector } from "./src/types/Selector";
15
16
  export type { SelectorFamily } from "./src/types/SelectorFamily";
16
17
  export type { SetAtom } from "./src/types/SetAtom";
18
+ export type { SetAtomValue } from "./src/types/SetAtomValue";
17
19
  export type { State } from "./src/types/State";
18
20
  export type { Store } from "./src/types/Store";
19
21
  export type { StoreData } from "./src/types/StoreData";
package/dist/index.js CHANGED
@@ -101,9 +101,6 @@ var createStoreData = (id = generateId()) => ({
101
101
  // src/utils/isAtom.ts
102
102
  var isAtom = (state) => Object.hasOwn(state, "defaultValue");
103
103
 
104
- // src/utils/isSelector.ts
105
- var isSelector = (state) => Object.hasOwn(state, "get");
106
-
107
104
  // src/lib/updateStateSubscribers.ts
108
105
  var updateStateSubscribers = (state, data) => {
109
106
  const subscribtions = data.subscriptions.get(state);
@@ -182,6 +179,7 @@ var handleSelectorResult = (value, selector, data) => {
182
179
  value.then((resolved) => {
183
180
  data.values.set(selector, resolved);
184
181
  updateStateSubscribers(selector, data);
182
+ console.log("Should we reEvaluate?");
185
183
  });
186
184
  return value;
187
185
  } else {
@@ -208,7 +206,7 @@ var updateSelectorSubscribers = (selector, data) => {
208
206
  const familySubscriptions = selector.family && data.subscriptions.get(selector.family);
209
207
  if (!subscribtions?.size && !familySubscriptions?.size)
210
208
  return;
211
- if (subscribtions?.size && data.subscriptionsRequireEqualCheck.get(selector) || familySubscriptions?.size && data.subscriptionsRequireEqualCheck.get(selector.family)) {
209
+ if (subscribtions?.size && data.subscriptionsRequireEqualCheck.get(selector) || familySubscriptions?.size && selector.family && data.subscriptionsRequireEqualCheck.get(selector.family)) {
212
210
  try {
213
211
  const oldValue = data.expiredValues.get(selector);
214
212
  const newValue = initSelector(selector, data);
@@ -316,6 +314,9 @@ var initAtom = (atom3, data) => {
316
314
  return value;
317
315
  };
318
316
 
317
+ // src/utils/isSelector.ts
318
+ var isSelector = (state) => Object.hasOwn(state, "get");
319
+
319
320
  // src/utils/isFamily.ts
320
321
  var isFamily = (state) => Object.hasOwn(state, "_map");
321
322
 
@@ -516,19 +517,9 @@ var transaction = (callback, data) => {
516
517
  var storeFromStoreData2 = (data) => {
517
518
  const get = (state) => getState2(state, data);
518
519
  const set = (state, value) => {
519
- if (isAtom(state)) {
520
- return setAtom(state, value, data);
521
- } else {
522
- if (isSelector(state)) {
523
- if (state.set) {
524
- txn((set2, get2) => state.set({ get: get2, set: set2 }, value));
525
- return;
526
- } else {
527
- throw new Error("set on selector is not supported");
528
- }
529
- }
530
- throw new Error("Invalid state object passed to set");
531
- }
520
+ if (!isAtom(state))
521
+ throw new Error("Invalid state object");
522
+ return setAtom(state, value, data);
532
523
  };
533
524
  const reset = (atom3) => resetAtom(atom3, data);
534
525
  const sub = (state, callback, deepEqualCheckBeforeCallback = true) => subscribe(state, callback, deepEqualCheckBeforeCallback, data);
@@ -548,6 +539,23 @@ var createStore = (id) => {
548
539
  const data = createStoreData(id);
549
540
  return storeFromStoreData2(data);
550
541
  };
542
+ // src/createStoreWithSelectorSet.ts
543
+ var setSelector = (selector, values, store) => {
544
+ return selector.set(store.set, store.get, store.reset, ...values);
545
+ };
546
+ var createStoreWithSelectorSet = (id) => {
547
+ const data = createStoreData(id);
548
+ const store = storeFromStoreData2(data);
549
+ store.set = (state, value, ...rest) => {
550
+ if (isAtom(state))
551
+ return setAtom(state, value, data);
552
+ if (isSelector(state))
553
+ return setSelector(state, [value, ...rest], store);
554
+ throw new Error("Invalid state object");
555
+ };
556
+ store.kind = "storeWithSelectorSet";
557
+ return store;
558
+ };
551
559
  // src/getDefaultStore.ts
552
560
  if (!globalThis._valdresStore) {
553
561
  globalThis._valdresStore = createStore("default");
@@ -590,6 +598,7 @@ export {
590
598
  isFamily,
591
599
  isAtom,
592
600
  getDefaultStore,
601
+ createStoreWithSelectorSet,
593
602
  createStore,
594
603
  atomFamily,
595
604
  atom
@@ -1,9 +1,9 @@
1
1
  import type { Atom } from "./types/Atom";
2
- type AtomOptions<MountRes = undefined> = {
2
+ type AtomOptions<Value, MountRes = undefined> = {
3
3
  label?: string;
4
- onInit?: () => void;
4
+ onInit?: (setSelf: (value: Value) => void) => void;
5
5
  onMount?: () => MountRes;
6
6
  onUnmount?: (mountRes?: MountRes) => void;
7
7
  };
8
- export declare const atom: <Value, FamilyKey = undefined, MountReturnValue = undefined>(defaultValue?: Value | (() => Value | Promise<Value>), options?: AtomOptions<MountReturnValue>) => Atom<Value, FamilyKey>;
8
+ export declare const atom: <Value, FamilyKey = undefined, MountReturnValue = undefined>(defaultValue?: Value | (() => Value | Promise<Value>), options?: AtomOptions<Value, MountReturnValue>) => Atom<Value, FamilyKey>;
9
9
  export {};
@@ -0,0 +1,2 @@
1
+ import type { Store } from "./types/Store";
2
+ export declare const createStoreWithSelectorSet: (id?: string) => Store;
@@ -1,8 +1,9 @@
1
1
  import type { State } from "../types/State";
2
2
  import type { StoreData } from "../types/StoreData";
3
+ import type { Atom } from "../types/Atom";
3
4
  type GetValdresValue = <V>(state: State<V>) => V;
4
5
  type SetValdresValue = <V>(state: State<V>, value: V) => void;
5
- type ResetValdresValue = <V>(state: State<V>) => V;
6
+ type ResetValdresValue = <V>(atom: Atom<V>) => V;
6
7
  type TransactionInterface = (set: SetValdresValue, get: GetValdresValue, reset: ResetValdresValue, commit: () => void) => void;
7
8
  export declare const transaction: (callback: TransactionInterface, data: StoreData) => void;
8
9
  export {};
@@ -1,3 +1,5 @@
1
+ import type { Family } from "../types/Family";
1
2
  import type { State } from "../types/State";
2
3
  import type { StoreData } from "../types/StoreData";
3
- export declare const unsubscribe: <V>(state: State<V>, subscription: any, data: StoreData, mount?: any) => void;
4
+ import type { Subscription } from "../types/Subscription";
5
+ export declare const unsubscribe: <V>(state: State<V> | Family<V>, subscription: Subscription, data: StoreData, mount?: any) => void;
@@ -4,7 +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
+ onInit?: (setSelf: (value: Value) => void) => void;
8
8
  onMount?: () => MountRes;
9
9
  onUnmount?: (mountRes?: MountRes) => void;
10
10
  };
@@ -1,8 +1,9 @@
1
1
  import type { GetValue } from "./GetValue";
2
2
  import type { SelectorFamily } from "./SelectorFamily";
3
- export type Selector<Value = any, FamilyKey = undefined> = {
3
+ export type Selector<Value = any, FamilyKey = undefined, MountRes = unknown> = {
4
4
  get: (get: GetValue) => Value;
5
5
  debugLabel?: string;
6
6
  family?: SelectorFamily<Value, FamilyKey>;
7
7
  familyKey?: FamilyKey;
8
+ onMount?: () => MountRes;
8
9
  };
@@ -1,2 +1,3 @@
1
1
  import type { Atom } from "./Atom";
2
- export type SetAtom = <V>(atom: Atom<V>, value: V | ((current: V) => V)) => void;
2
+ import type { SetAtomValue } from "./SetAtomValue";
3
+ export type SetAtom<V = unknown> = (atom: Atom<V>, value: SetAtomValue<V>) => void;
@@ -0,0 +1 @@
1
+ export type SetAtomValue<V> = V | ((current: V) => V);
@@ -0,0 +1 @@
1
+ export type Subscription = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "valdres",
3
- "version": "0.2.0-alpha.5",
3
+ "version": "0.2.0-alpha.6",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "Eigil Sagafos"
@@ -34,5 +34,5 @@
34
34
  "access": "public",
35
35
  "registry": "https://registry.npmjs.org/"
36
36
  },
37
- "gitHead": "75721256957ea160c2a82ac4ebb5a12e4c447f1f"
37
+ "gitHead": "727e6a247df61e016e0f7905397a9121abf4e82c"
38
38
  }