valdres 0.2.0-alpha.2 → 0.2.0-alpha.4
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 +19 -0
- package/dist/index.js +56 -52
- package/dist/src/atom.d.ts +3 -3
- package/dist/src/atomFamily.d.ts +3 -1
- package/dist/src/getDefaultStore.d.ts +1 -1
- package/dist/src/lib/createStoreData.d.ts +2 -0
- package/dist/src/lib/initSelector.d.ts +1 -1
- package/dist/src/lib/storeFromStoreData.d.ts +3 -0
- package/dist/src/lib/unsubscribe.d.ts +1 -1
- package/dist/src/types/Atom.d.ts +1 -0
- package/dist/src/types/ResetAtom.d.ts +1 -1
- package/dist/src/utils/isAtom.d.ts +1 -2
- package/package.json +3 -3
- package/dist/src/atom.test.d.ts +0 -1
- package/dist/src/atomFamily.test.d.ts +0 -1
- package/dist/src/createStore.test.d.ts +0 -1
- package/dist/src/lib/initAtom.test.d.ts +0 -1
- package/dist/src/lib/initSelector.test.d.ts +0 -1
- package/dist/src/lib/recursivlyUpdateSelectors.d.ts +0 -1
- package/dist/src/lib/setAtom.test.d.ts +0 -1
- package/dist/src/lib/subscribe.test.d.ts +0 -1
- package/dist/src/lib/transaction.test.d.ts +0 -1
- package/dist/src/selector.test.d.ts +0 -1
- package/dist/src/selectorFamily.test.d.ts +0 -1
- package/dist/test/utils/wait.d.ts +0 -2
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
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 { SetAtom } from "./src/types/SetAtom";
|
|
17
|
+
export type { State } from "./src/types/State";
|
|
18
|
+
export type { Store } from "./src/types/Store";
|
|
19
|
+
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,
|
|
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 (
|
|
347
|
-
if (
|
|
348
|
-
state.onUnmount
|
|
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,
|
|
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/
|
|
465
|
-
var
|
|
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");
|
package/dist/src/atom.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { Atom } from "./types/Atom";
|
|
2
|
-
type AtomOptions<
|
|
2
|
+
type AtomOptions<MountRes = undefined> = {
|
|
3
3
|
label?: string;
|
|
4
4
|
onInit?: () => void;
|
|
5
|
-
onMount?: () =>
|
|
6
|
-
onUnmount?: (mountRes?:
|
|
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 {};
|
package/dist/src/atomFamily.d.ts
CHANGED
|
@@ -1,2 +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>;
|
|
4
|
+
export {};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export declare const getDefaultStore: () => any;
|
|
2
|
-
export declare const resetDefaultStore: () => import("
|
|
2
|
+
export declare const resetDefaultStore: () => import("..").Store;
|
|
@@ -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>;
|
|
@@ -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,
|
|
3
|
+
export declare const unsubscribe: <V>(state: State<V>, subscription: any, data: StoreData, mount?: any) => void;
|
package/dist/src/types/Atom.d.ts
CHANGED
|
@@ -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>;
|
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.4",
|
|
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": "
|
|
37
|
+
"gitHead": "1bde879ca28be7fb3e98add8bbcd2c568f8c6911"
|
|
38
38
|
}
|
package/dist/src/atom.test.d.ts
DELETED
|
@@ -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 {};
|