valdres 0.2.0-alpha.4 → 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 +2 -0
- package/dist/index.js +54 -25
- package/dist/src/atom.d.ts +3 -3
- package/dist/src/createStoreWithSelectorSet.d.ts +2 -0
- package/dist/src/lib/transaction.d.ts +2 -1
- package/dist/src/lib/unsubscribe.d.ts +3 -1
- package/dist/src/types/Atom.d.ts +1 -1
- package/dist/src/types/Selector.d.ts +2 -1
- package/dist/src/types/SetAtom.d.ts +2 -1
- package/dist/src/types/SetAtomValue.d.ts +1 -0
- package/dist/src/types/Subscription.d.ts +1 -0
- package/package.json +2 -2
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,7 +179,7 @@ var handleSelectorResult = (value, selector, data) => {
|
|
|
182
179
|
value.then((resolved) => {
|
|
183
180
|
data.values.set(selector, resolved);
|
|
184
181
|
updateStateSubscribers(selector, data);
|
|
185
|
-
console.log(
|
|
182
|
+
console.log("Should we reEvaluate?");
|
|
186
183
|
});
|
|
187
184
|
return value;
|
|
188
185
|
} else {
|
|
@@ -209,7 +206,7 @@ var updateSelectorSubscribers = (selector, data) => {
|
|
|
209
206
|
const familySubscriptions = selector.family && data.subscriptions.get(selector.family);
|
|
210
207
|
if (!subscribtions?.size && !familySubscriptions?.size)
|
|
211
208
|
return;
|
|
212
|
-
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)) {
|
|
213
210
|
try {
|
|
214
211
|
const oldValue = data.expiredValues.get(selector);
|
|
215
212
|
const newValue = initSelector(selector, data);
|
|
@@ -317,6 +314,9 @@ var initAtom = (atom3, data) => {
|
|
|
317
314
|
return value;
|
|
318
315
|
};
|
|
319
316
|
|
|
317
|
+
// src/utils/isSelector.ts
|
|
318
|
+
var isSelector = (state) => Object.hasOwn(state, "get");
|
|
319
|
+
|
|
320
320
|
// src/utils/isFamily.ts
|
|
321
321
|
var isFamily = (state) => Object.hasOwn(state, "_map");
|
|
322
322
|
|
|
@@ -446,23 +446,41 @@ var findDependencies = (state, data, result = new Set) => {
|
|
|
446
446
|
}
|
|
447
447
|
return result;
|
|
448
448
|
};
|
|
449
|
+
var recursivlyResetTxnSelectorCache = (state, txnSubscribers, txnSelectorCache) => {
|
|
450
|
+
for (const dep of txnSubscribers.get(state)) {
|
|
451
|
+
txnSelectorCache.delete(dep);
|
|
452
|
+
if (txnSubscribers.get(dep)?.size) {
|
|
453
|
+
recursivlyResetTxnSelectorCache(dep, txnSubscribers, txnSelectorCache);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
};
|
|
449
457
|
var transaction = (callback, data) => {
|
|
450
458
|
let txnAtomMap = new Map;
|
|
451
459
|
let txnSelectorCache = new Map;
|
|
460
|
+
let txnSubscribers = new Map;
|
|
452
461
|
let dirtySelectors = new Set;
|
|
453
462
|
const txnGet = (state) => {
|
|
454
463
|
if (isAtom(state)) {
|
|
455
464
|
return txnAtomMap.has(state) ? txnAtomMap.get(state) : getState2(state, data);
|
|
456
|
-
} else {
|
|
465
|
+
} else if (isSelector(state)) {
|
|
457
466
|
if (txnSelectorCache.has(state)) {
|
|
458
467
|
return txnSelectorCache.get(state);
|
|
459
|
-
} else if (dirtySelectors.has(state)) {
|
|
460
|
-
const res = state.get(txnGet);
|
|
461
|
-
txnSelectorCache.set(state, res);
|
|
462
|
-
return res;
|
|
463
|
-
} else {
|
|
464
|
-
return getState2(state, data);
|
|
465
468
|
}
|
|
469
|
+
const deps = new Set;
|
|
470
|
+
const res = state.get((s) => {
|
|
471
|
+
deps.add(s);
|
|
472
|
+
return txnGet(s);
|
|
473
|
+
});
|
|
474
|
+
for (const dep of deps) {
|
|
475
|
+
if (!txnSubscribers.has(dep)) {
|
|
476
|
+
txnSubscribers.set(dep, new Set);
|
|
477
|
+
}
|
|
478
|
+
txnSubscribers.get(dep).add(state);
|
|
479
|
+
}
|
|
480
|
+
txnSelectorCache.set(state, res);
|
|
481
|
+
return res;
|
|
482
|
+
} else {
|
|
483
|
+
throw new Error("Unsupported state");
|
|
466
484
|
}
|
|
467
485
|
};
|
|
468
486
|
const txnSet = (atom3, value) => {
|
|
@@ -476,6 +494,9 @@ var transaction = (callback, data) => {
|
|
|
476
494
|
dirtySelectors.add(selector);
|
|
477
495
|
txnSelectorCache.delete(selector);
|
|
478
496
|
}
|
|
497
|
+
if (txnSubscribers.get(atom3)?.size) {
|
|
498
|
+
recursivlyResetTxnSelectorCache(atom3, txnSubscribers, txnSelectorCache);
|
|
499
|
+
}
|
|
479
500
|
txnAtomMap.set(atom3, value);
|
|
480
501
|
};
|
|
481
502
|
const txnReset = (atom3) => {
|
|
@@ -496,19 +517,9 @@ var transaction = (callback, data) => {
|
|
|
496
517
|
var storeFromStoreData2 = (data) => {
|
|
497
518
|
const get = (state) => getState2(state, data);
|
|
498
519
|
const set = (state, value) => {
|
|
499
|
-
if (isAtom(state))
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
if (isSelector(state)) {
|
|
503
|
-
if (state.set) {
|
|
504
|
-
txn((set2, get2) => state.set({ get: get2, set: set2 }, value));
|
|
505
|
-
return;
|
|
506
|
-
} else {
|
|
507
|
-
throw new Error("set on selector is not supported");
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
throw new Error("Invalid state object passed to set");
|
|
511
|
-
}
|
|
520
|
+
if (!isAtom(state))
|
|
521
|
+
throw new Error("Invalid state object");
|
|
522
|
+
return setAtom(state, value, data);
|
|
512
523
|
};
|
|
513
524
|
const reset = (atom3) => resetAtom(atom3, data);
|
|
514
525
|
const sub = (state, callback, deepEqualCheckBeforeCallback = true) => subscribe(state, callback, deepEqualCheckBeforeCallback, data);
|
|
@@ -528,6 +539,23 @@ var createStore = (id) => {
|
|
|
528
539
|
const data = createStoreData(id);
|
|
529
540
|
return storeFromStoreData2(data);
|
|
530
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
|
+
};
|
|
531
559
|
// src/getDefaultStore.ts
|
|
532
560
|
if (!globalThis._valdresStore) {
|
|
533
561
|
globalThis._valdresStore = createStore("default");
|
|
@@ -570,6 +598,7 @@ export {
|
|
|
570
598
|
isFamily,
|
|
571
599
|
isAtom,
|
|
572
600
|
getDefaultStore,
|
|
601
|
+
createStoreWithSelectorSet,
|
|
573
602
|
createStore,
|
|
574
603
|
atomFamily,
|
|
575
604
|
atom
|
package/dist/src/atom.d.ts
CHANGED
|
@@ -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 {};
|
|
@@ -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>(
|
|
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
|
-
|
|
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;
|
package/dist/src/types/Atom.d.ts
CHANGED
|
@@ -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
|
};
|
|
@@ -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.
|
|
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": "
|
|
37
|
+
"gitHead": "727e6a247df61e016e0f7905397a9121abf4e82c"
|
|
38
38
|
}
|