valdres 1.0.0-beta.0 → 1.0.0-beta.2
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 +89 -85
- package/dist/types/atomFamily.d.ts +11 -1
- package/dist/types/index.d.ts +5 -1
- package/dist/types/lib/createGlobalAtomFamily.d.ts +1 -1
- package/dist/types/lib/initAtom.d.ts +1 -1
- package/dist/types/lib/initSelector.d.ts +2 -2
- package/dist/types/lib/transaction.d.ts +1 -1
- package/dist/types/types/SyncSetAtom.d.ts +4 -0
- package/dist/types/types/TransactionInterface.d.ts +2 -6
- package/dist/types/utils/isFamily.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -417,96 +417,97 @@ var evaluateSelector = (selector, data, initializedAtomsSet, circularDependencyS
|
|
|
417
417
|
throw new SelectorCircularDependencyError;
|
|
418
418
|
}
|
|
419
419
|
circularDependencySet.add(selector);
|
|
420
|
-
const prev = data.abortControllers.get(selector);
|
|
421
|
-
let options;
|
|
422
|
-
if (prev === false) {
|
|
423
|
-
let cached = syncOptionsCache.get(data);
|
|
424
|
-
if (!cached) {
|
|
425
|
-
cached = { signal: neverAbortedSignal, storeId: data.id };
|
|
426
|
-
syncOptionsCache.set(data, cached);
|
|
427
|
-
}
|
|
428
|
-
options = cached;
|
|
429
|
-
} else {
|
|
430
|
-
if (prev)
|
|
431
|
-
prev.abort();
|
|
432
|
-
const abortController = new AbortController;
|
|
433
|
-
data.abortControllers.set(selector, abortController);
|
|
434
|
-
options = { signal: abortController.signal, storeId: data.id };
|
|
435
|
-
}
|
|
436
|
-
let allDepsThisEval;
|
|
437
|
-
let result;
|
|
438
420
|
try {
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
421
|
+
const prev = data.abortControllers.get(selector);
|
|
422
|
+
let options;
|
|
423
|
+
if (prev === false) {
|
|
424
|
+
let cached = syncOptionsCache.get(data);
|
|
425
|
+
if (!cached) {
|
|
426
|
+
cached = { signal: neverAbortedSignal, storeId: data.id };
|
|
427
|
+
syncOptionsCache.set(data, cached);
|
|
428
|
+
}
|
|
429
|
+
options = cached;
|
|
430
|
+
} else {
|
|
431
|
+
if (prev)
|
|
432
|
+
prev.abort();
|
|
433
|
+
const abortController = new AbortController;
|
|
434
|
+
data.abortControllers.set(selector, abortController);
|
|
435
|
+
options = { signal: abortController.signal, storeId: data.id };
|
|
436
|
+
}
|
|
437
|
+
let allDepsThisEval;
|
|
438
|
+
let result;
|
|
439
|
+
try {
|
|
440
|
+
result = selector.get((state) => {
|
|
441
|
+
if (evaluationComplete) {
|
|
442
|
+
if (!evalCtx.revoked && allDepsThisEval) {
|
|
443
|
+
allDepsThisEval.add(state);
|
|
444
|
+
}
|
|
445
|
+
if (evalCtx.revoked) {
|
|
446
|
+
return getState(state, data, new Set);
|
|
447
|
+
}
|
|
448
|
+
return lateGet(state, selector, data);
|
|
443
449
|
}
|
|
444
|
-
|
|
445
|
-
|
|
450
|
+
const value = getState(state, data, initializedAtomsSet, circularDependencySet);
|
|
451
|
+
updatedDepsArray.push(state);
|
|
452
|
+
if (!depsChanged && (!currentDependencies || !currentDependencies.has(state))) {
|
|
453
|
+
depsChanged = true;
|
|
446
454
|
}
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
455
|
+
if (isPromiseLike(value))
|
|
456
|
+
throw new SuspendAndWaitForResolveError(value);
|
|
457
|
+
return value;
|
|
458
|
+
}, options);
|
|
459
|
+
} catch (error) {
|
|
460
|
+
if (error instanceof NeedsInitError)
|
|
461
|
+
throw error;
|
|
462
|
+
if (error instanceof SuspendAndWaitForResolveError) {
|
|
463
|
+
result = error;
|
|
464
|
+
} else if (error instanceof SelectorEvaluationError) {
|
|
465
|
+
throw error;
|
|
466
|
+
} else {
|
|
467
|
+
throw new SelectorEvaluationError(error);
|
|
453
468
|
}
|
|
454
|
-
if (isPromiseLike(value))
|
|
455
|
-
throw new SuspendAndWaitForResolveError(value);
|
|
456
|
-
return value;
|
|
457
|
-
}, options);
|
|
458
|
-
} catch (error) {
|
|
459
|
-
if (error instanceof NeedsInitError) {
|
|
460
|
-
circularDependencySet.delete(selector);
|
|
461
|
-
throw error;
|
|
462
|
-
}
|
|
463
|
-
if (error instanceof SuspendAndWaitForResolveError) {
|
|
464
|
-
result = error;
|
|
465
|
-
} else if (error instanceof SelectorEvaluationError) {
|
|
466
|
-
throw error;
|
|
467
|
-
} else {
|
|
468
|
-
throw new SelectorEvaluationError(error);
|
|
469
469
|
}
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
depsChanged = true;
|
|
475
|
-
}
|
|
476
|
-
if (depsChanged || !currentDependencies) {
|
|
477
|
-
const updatedDependencies = new Set(updatedDepsArray);
|
|
478
|
-
if (isAsyncResult && currentDependencies) {
|
|
479
|
-
for (const dep of currentDependencies) {
|
|
480
|
-
updatedDependencies.add(dep);
|
|
481
|
-
}
|
|
470
|
+
evaluationComplete = true;
|
|
471
|
+
const isAsyncResult = result instanceof SuspendAndWaitForResolveError || isPromiseLike(result);
|
|
472
|
+
if (!isAsyncResult && !depsChanged && currentDependencies && currentDependencies.size !== updatedDepsArray.length) {
|
|
473
|
+
depsChanged = true;
|
|
482
474
|
}
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
if (
|
|
486
|
-
const
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
addedDepsOut.add(state);
|
|
475
|
+
if (depsChanged || !currentDependencies) {
|
|
476
|
+
const updatedDependencies = new Set(updatedDepsArray);
|
|
477
|
+
if (isAsyncResult && currentDependencies) {
|
|
478
|
+
for (const dep of currentDependencies) {
|
|
479
|
+
updatedDependencies.add(dep);
|
|
480
|
+
}
|
|
490
481
|
}
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
if (!updatedDependencies.has(state)) {
|
|
482
|
+
const prev2 = currentDependencies ?? new Set;
|
|
483
|
+
for (const state of updatedDependencies) {
|
|
484
|
+
if (!prev2.has(state)) {
|
|
495
485
|
const set = getOrInitDependentsSet(state, data);
|
|
496
|
-
set.
|
|
497
|
-
if (
|
|
498
|
-
|
|
486
|
+
set.add(selector);
|
|
487
|
+
if (addedDepsOut)
|
|
488
|
+
addedDepsOut.add(state);
|
|
499
489
|
}
|
|
500
490
|
}
|
|
491
|
+
if (!isAsyncResult) {
|
|
492
|
+
for (const state of prev2) {
|
|
493
|
+
if (!updatedDependencies.has(state)) {
|
|
494
|
+
const set = getOrInitDependentsSet(state, data);
|
|
495
|
+
set.delete(selector);
|
|
496
|
+
if (removedDepsOut)
|
|
497
|
+
removedDepsOut.add(state);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
data.stateDependencies.set(selector, updatedDependencies);
|
|
501
502
|
}
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
503
|
+
if (isPromiseLike(result)) {
|
|
504
|
+
allDepsThisEval = new Set(updatedDepsArray);
|
|
505
|
+
pendingAsyncDeps.set(result, allDepsThisEval);
|
|
506
|
+
}
|
|
507
|
+
return result;
|
|
508
|
+
} finally {
|
|
509
|
+
circularDependencySet.delete(selector);
|
|
507
510
|
}
|
|
508
|
-
circularDependencySet.delete(selector);
|
|
509
|
-
return result;
|
|
510
511
|
};
|
|
511
512
|
var handleSelectorResult = (value, selector, data) => {
|
|
512
513
|
if (value instanceof SuspendAndWaitForResolveError) {
|
|
@@ -1566,14 +1567,17 @@ class Transaction {
|
|
|
1566
1567
|
set = (atom, value) => {
|
|
1567
1568
|
if (!isAtom(atom))
|
|
1568
1569
|
throw new Error("Not an atom");
|
|
1570
|
+
let resolved;
|
|
1569
1571
|
if (isFunction(value)) {
|
|
1570
1572
|
const currentValue = this.get(atom);
|
|
1571
|
-
|
|
1573
|
+
resolved = value(currentValue);
|
|
1574
|
+
} else {
|
|
1575
|
+
resolved = value;
|
|
1572
1576
|
}
|
|
1573
|
-
if (!atom.mutable && !isProd() &&
|
|
1574
|
-
|
|
1577
|
+
if (!atom.mutable && !isProd() && resolved !== null && (typeof resolved === "object" || typeof resolved === "function")) {
|
|
1578
|
+
resolved = deepFreeze(resolved);
|
|
1575
1579
|
}
|
|
1576
|
-
this._atomMap.set(atom,
|
|
1580
|
+
this._atomMap.set(atom, resolved);
|
|
1577
1581
|
if (!this.dirty)
|
|
1578
1582
|
this.dirty = true;
|
|
1579
1583
|
if (isFamilyAtom(atom)) {
|
|
@@ -1587,7 +1591,7 @@ class Transaction {
|
|
|
1587
1591
|
index.renderedArray = null;
|
|
1588
1592
|
this.recursivelyUpdateAtomFamilyIndexes(atom.family);
|
|
1589
1593
|
}
|
|
1590
|
-
return
|
|
1594
|
+
return resolved;
|
|
1591
1595
|
};
|
|
1592
1596
|
batchSetFamilyAtoms = (family, pairs) => {
|
|
1593
1597
|
if (!this._atomMap.has(family)) {
|
|
@@ -2399,9 +2403,9 @@ var isFamilySelector = (state) => isFamilyState(state) && isSelector(state);
|
|
|
2399
2403
|
|
|
2400
2404
|
// src/index.ts
|
|
2401
2405
|
if (globalThis.__valdres__) {
|
|
2402
|
-
throw new Error(`Error! An instance of valdres is already loaded. Loaded: ${globalThis.__valdres__}. Attempted to load: ${"1.0.0-beta.
|
|
2406
|
+
throw new Error(`Error! An instance of valdres is already loaded. Loaded: ${globalThis.__valdres__}. Attempted to load: ${"1.0.0-beta.2"}`);
|
|
2403
2407
|
} else {
|
|
2404
|
-
globalThis.__valdres__ = "1.0.0-beta.
|
|
2408
|
+
globalThis.__valdres__ = "1.0.0-beta.2";
|
|
2405
2409
|
}
|
|
2406
2410
|
export {
|
|
2407
2411
|
store,
|
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
import type { AtomFamily } from "./types/AtomFamily";
|
|
1
2
|
import type { AtomFamilyDefaultValue } from "./types/AtomFamilyDefaultValue";
|
|
3
|
+
import type { AtomFamilyAtom } from "./types/AtomFamilyAtom";
|
|
2
4
|
import type { AtomOptions } from "./types/AtomOptions";
|
|
3
|
-
|
|
5
|
+
import type { GlobalAtom } from "./types/GlobalAtom";
|
|
6
|
+
type GlobalAtomFamily<Value, Args extends [any, ...any[]] = [any, ...any[]]> = Omit<AtomFamily<Value, Args>, never> & {
|
|
7
|
+
(...args: Args): AtomFamilyAtom<Value, Args> & GlobalAtom<Value>;
|
|
8
|
+
};
|
|
9
|
+
export declare function atomFamily<Value extends any, Args extends [any, ...any[]] = [any, ...any[]]>(defaultValue: AtomFamilyDefaultValue<Value, Args> | undefined, options: AtomOptions<Value> & {
|
|
10
|
+
global: true;
|
|
11
|
+
}): GlobalAtomFamily<Value, Args>;
|
|
12
|
+
export declare function atomFamily<Value extends any, Args extends [any, ...any[]] = [any, ...any[]]>(defaultValue?: AtomFamilyDefaultValue<Value, Args>, options?: AtomOptions<Value>): AtomFamily<Value, Args>;
|
|
13
|
+
export {};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -27,12 +27,16 @@ export type { AtomFamily } from "./types/AtomFamily";
|
|
|
27
27
|
export type { FamilyKey } from "./types/FamilyKey";
|
|
28
28
|
export type { GetValue } from "./types/GetValue";
|
|
29
29
|
export type { Reactive } from "./types/Reactive";
|
|
30
|
-
export type { GlobalAtom } from "./types/GlobalAtom";
|
|
30
|
+
export type { GlobalAtom, MaxAgeInterval } from "./types/GlobalAtom";
|
|
31
|
+
export type { GlobalAtomGetSelfFunc } from "./types/GlobalAtomGetSelfFunc";
|
|
32
|
+
export type { GlobalAtomResetSelfFunc } from "./types/GlobalAtomResetSelfFunc";
|
|
33
|
+
export type { GlobalAtomSetSelfFunc } from "./types/GlobalAtomSetSelfFunc";
|
|
31
34
|
export type { ResetAtom } from "./types/ResetAtom";
|
|
32
35
|
export type { Selector, SelectorGetOptions } from "./types/Selector";
|
|
33
36
|
export type { SelectorFamily } from "./types/SelectorFamily";
|
|
34
37
|
export type { SetAtom } from "./types/SetAtom";
|
|
35
38
|
export type { SetAtomValue } from "./types/SetAtomValue";
|
|
39
|
+
export type { SyncSetAtom } from "./types/SyncSetAtom";
|
|
36
40
|
export type { State } from "./types/State";
|
|
37
41
|
export type { Store } from "./types/Store";
|
|
38
42
|
export type { StoreData } from "./types/StoreData";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { AtomFamily } from "../types/AtomFamily";
|
|
2
2
|
import type { AtomFamilyDefaultValue } from "../types/AtomFamilyDefaultValue";
|
|
3
3
|
import type { AtomOptions } from "../types/AtomOptions";
|
|
4
|
-
export declare const createGlobalAtomFamily: <Value extends
|
|
4
|
+
export declare const createGlobalAtomFamily: <Value extends unknown, Args extends [any, ...any[]] = [any, ...any[]]>(defaultValue: AtomFamilyDefaultValue<Value, Args>, options: AtomOptions<Value>) => AtomFamily<Value, Args>;
|
|
@@ -2,4 +2,4 @@ import type { Atom } from "../types/Atom";
|
|
|
2
2
|
import type { AtomFamilyAtom } from "../types/AtomFamilyAtom";
|
|
3
3
|
import type { StoreData } from "../types/StoreData";
|
|
4
4
|
export declare const getAtomInitValue: <V = any>(atom: Atom<V>, data: StoreData, initializedAtomsSet: Set<Atom>) => any;
|
|
5
|
-
export declare const initAtom: <Value extends
|
|
5
|
+
export declare const initAtom: <Value extends unknown, Args extends [any, ...any[]] = [any, ...any[]]>(atom: Atom<Value> | AtomFamilyAtom<Value, Args>, data: StoreData, initializedAtomsSet: Set<Atom>) => void;
|
|
@@ -19,6 +19,6 @@ export declare let _evalDepth: number;
|
|
|
19
19
|
* Each level uses ~8-10 JS stack frames, so 100 levels ≈ 800-1000 frames,
|
|
20
20
|
* safely under the typical ~10000 frame limit. */
|
|
21
21
|
export declare const MAX_EVAL_DEPTH = 100;
|
|
22
|
-
export declare const evaluateSelector: <V>(selector: Selector<V>, data: StoreData, initializedAtomsSet: Set<Atom>, circularDependencySet?: WeakSet<WeakKey>, addedDepsOut?: Set<State>, removedDepsOut?: Set<State>) =>
|
|
23
|
-
export declare const handleSelectorResult: <Value>(value: Value | Promise<Value> | SuspendAndWaitForResolveError, selector: Selector<Value>, data: StoreData) => Promise<any
|
|
22
|
+
export declare const evaluateSelector: <V>(selector: Selector<V>, data: StoreData, initializedAtomsSet: Set<Atom>, circularDependencySet?: WeakSet<WeakKey>, addedDepsOut?: Set<State>, removedDepsOut?: Set<State>) => V | Promise<V> | SuspendAndWaitForResolveError;
|
|
23
|
+
export declare const handleSelectorResult: <Value>(value: Value | Promise<Value> | SuspendAndWaitForResolveError, selector: Selector<Value>, data: StoreData) => Value | Promise<any>;
|
|
24
24
|
export declare const initSelector: <V>(selector: Selector<V>, data: StoreData, initializedAtomsSet: Set<Atom>, circularDependencySet?: WeakSet<WeakKey>) => boolean;
|
|
@@ -17,7 +17,7 @@ export declare class Transaction {
|
|
|
17
17
|
private hasTxnOrData;
|
|
18
18
|
private valueFromTxnOrData;
|
|
19
19
|
get: GetValue;
|
|
20
|
-
set: <V>(atom: Atom<V>, value: V | ((currentValue: V) => V)) => V
|
|
20
|
+
set: <V>(atom: Atom<V>, value: V | ((currentValue: V) => V)) => V;
|
|
21
21
|
batchSetFamilyAtoms: (family: any, pairs: any) => void;
|
|
22
22
|
del: (atom: AtomFamilyAtom<any, any>) => void;
|
|
23
23
|
scope: (scopeId: string, callback: (txn: Transaction) => any) => any;
|
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
import type { Atom } from "./Atom";
|
|
2
1
|
import type { AtomFamilyAtom } from "./AtomFamilyAtom";
|
|
3
2
|
import type { GetValue } from "./GetValue";
|
|
4
3
|
import type { ResetAtom } from "./ResetAtom";
|
|
5
|
-
import type { SetAtom } from "./SetAtom";
|
|
6
4
|
import type { StoreData } from "./StoreData";
|
|
5
|
+
import type { SyncSetAtom } from "./SyncSetAtom";
|
|
7
6
|
import type { TransactionFn } from "./TransactionFn";
|
|
8
|
-
export type SetAtom2 = {
|
|
9
|
-
<V>(atom: Atom<V>, value: V): V;
|
|
10
|
-
};
|
|
11
7
|
export type TransactionInterface = {
|
|
12
|
-
set:
|
|
8
|
+
set: SyncSetAtom;
|
|
13
9
|
get: GetValue;
|
|
14
10
|
del: (atom: AtomFamilyAtom<any, any>) => void;
|
|
15
11
|
reset: ResetAtom;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const isFamily: (state: any) => state is import("..").
|
|
1
|
+
export declare const isFamily: (state: any) => state is import("..").AtomFamily<unknown, [any, ...any[]]> | import("..").SelectorFamily<unknown, [any, ...any[]]>;
|