@yaasl/core 0.14.0 → 0.15.0-alpha.1
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/@types/base/create-selector.d.ts +11 -32
- package/dist/@types/base/create-slice.d.ts +5 -5
- package/dist/@types/base/stateful.d.ts +3 -7
- package/dist/cjs/base/create-derived.js +1 -1
- package/dist/cjs/base/create-selector.js +13 -26
- package/dist/cjs/base/create-slice.js +2 -4
- package/dist/cjs/base/stateful.js +1 -10
- package/dist/cjs/effects/sync.js +7 -10
- package/dist/esm/base/create-derived.js +1 -1
- package/dist/esm/base/create-selector.js +13 -25
- package/dist/esm/base/create-slice.js +3 -5
- package/dist/esm/base/stateful.js +1 -10
- package/dist/esm/effects/sync.js +7 -10
- package/package.json +3 -3
- package/dist/@types/base/destroyable.d.ts +0 -13
- package/dist/cjs/base/destroyable.js +0 -48
- package/dist/esm/base/destroyable.js +0 -42
|
@@ -1,34 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { InferValues } from "@yaasl/utils";
|
|
2
2
|
import { Stateful } from "./stateful";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
[K in keyof Obj]: `${Exclude<K, symbol>}${"" | `.${ObjPath<Obj[K]>}`}`;
|
|
6
|
-
}[keyof Obj] : never;
|
|
7
|
-
type ObjPathValue<State, Path> = State extends PathableValue ? Path extends `${infer Current}.${infer Next}` ? ObjPathValue<State[Current], Next> : State[Path & string] : never;
|
|
8
|
-
export declare class PathSelector<ParentValue, Path extends ObjPath<ParentValue>> extends Stateful<ObjPathValue<ParentValue, Path>> {
|
|
9
|
-
constructor(atom: Stateful<ParentValue>, path: Path);
|
|
3
|
+
export declare class CombinerSelector<ParentAtoms extends Stateful<any> | [Stateful<any>, ...Stateful<any>[]], CombinedValue> extends Stateful<CombinedValue> {
|
|
4
|
+
constructor(atoms: ParentAtoms, combiner: (...states: InferValues<ParentAtoms>) => CombinedValue);
|
|
10
5
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
* @param path The path to the value you want to select.
|
|
20
|
-
*
|
|
21
|
-
* @returns A PathSelector instance.
|
|
22
|
-
**/
|
|
23
|
-
<ParentValue, Path extends ObjPath<ParentValue>>(atom: Stateful<ParentValue>, path: Path): PathSelector<ParentValue, Path>;
|
|
24
|
-
/** Creates a value, selected from one atom with an object value by using a key path.
|
|
25
|
-
*
|
|
26
|
-
* @param atoms Atoms you need to combine to receive the new value.
|
|
27
|
-
* @param combiner Combiner function to use the atom values and create a new value.
|
|
28
|
-
*
|
|
29
|
-
* @returns A CombinerSelector instance.
|
|
30
|
-
**/
|
|
31
|
-
<ParentAtoms extends [Stateful<any>, ...Stateful<any>[]], CombinedValue>(states: ParentAtoms, combiner: (...res: InferValuesFromAtoms<ParentAtoms>) => CombinedValue): CombinerSelector<ParentAtoms, CombinedValue>;
|
|
32
|
-
}
|
|
33
|
-
export declare const createSelector: CreateSelectorOverloads;
|
|
34
|
-
export {};
|
|
6
|
+
/** Creates a value, selected from one or more atoms by using a combiner function.
|
|
7
|
+
*
|
|
8
|
+
* @param atoms One or more atoms you need to combine to receive the new value.
|
|
9
|
+
* @param combiner Combiner function to use the atom values and create a new value.
|
|
10
|
+
*
|
|
11
|
+
* @returns A CombinerSelector instance.
|
|
12
|
+
**/
|
|
13
|
+
export declare const createSelector: <ParentAtoms extends Stateful<any> | [Stateful<any>, ...Stateful<any>[]], CombinedValue>(atoms: ParentAtoms, combiner: (...states: InferValues<ParentAtoms>) => CombinedValue) => CombinerSelector<ParentAtoms, CombinedValue>;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { Actions, Reducers } from "./create-actions";
|
|
2
2
|
import { Atom, AtomConfig } from "./create-atom";
|
|
3
|
-
import { CombinerSelector
|
|
3
|
+
import { CombinerSelector } from "./create-selector";
|
|
4
|
+
import { Stateful } from "./stateful";
|
|
4
5
|
interface ReducersProp<State, R extends Reducers<State> | undefined> {
|
|
5
6
|
/** Reducers for custom actions to set the atom's value. */
|
|
6
7
|
reducers?: R;
|
|
7
8
|
}
|
|
8
|
-
type Selectors<State> = Record<string,
|
|
9
|
+
type Selectors<State> = Record<string, (state: State) => any>;
|
|
9
10
|
type ConditionalActions<State, R> = keyof R extends never ? {} : R extends Reducers<State> ? {
|
|
10
11
|
/** Actions that can be used to set the atom's value. */
|
|
11
12
|
actions: Actions<State, R>;
|
|
@@ -14,11 +15,10 @@ interface SelectorsProp<State, S extends Selectors<State> | undefined> {
|
|
|
14
15
|
/** Selectors to create values from the atom. */
|
|
15
16
|
selectors?: S;
|
|
16
17
|
}
|
|
17
|
-
type GetSelector<State, S extends ObjPath<State> | ((state: State) => any)> = S extends ObjPath<State> ? PathSelector<State, S> : S extends (state: State) => any ? CombinerSelector<[Atom<State>], ReturnType<S>> : never;
|
|
18
18
|
type ConditionalSelectors<State, S> = keyof S extends never ? {} : S extends Selectors<State> ? {
|
|
19
19
|
/** Selectors to create new values based on the atom's value. */
|
|
20
20
|
selectors: {
|
|
21
|
-
[K in keyof S]:
|
|
21
|
+
[K in keyof S]: CombinerSelector<Stateful<State>, ReturnType<S[K]>>;
|
|
22
22
|
};
|
|
23
23
|
} : {};
|
|
24
24
|
/** Creates a slice with actions and selectors.
|
|
@@ -27,7 +27,7 @@ type ConditionalSelectors<State, S> = keyof S extends never ? {} : S extends Sel
|
|
|
27
27
|
* @param config.name Name of the atom.
|
|
28
28
|
* @param config.effects Effects that will be applied on the atom.
|
|
29
29
|
* @param config.reducers Reducers for custom actions to set the atom's value.
|
|
30
|
-
* @param config.selectors
|
|
30
|
+
* @param config.selectors Combiner selectors to use the atom's values to create new ones.
|
|
31
31
|
*
|
|
32
32
|
* @returns An atom instance with actions and selectors.
|
|
33
33
|
**/
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export
|
|
3
|
-
export declare class Stateful<Value = unknown> extends Destroyable {
|
|
1
|
+
import { type Subscribable, type SubscriberCallback } from "@yaasl/utils";
|
|
2
|
+
export declare class Stateful<Value = unknown> implements Subscribable<Value> {
|
|
4
3
|
protected value: Value;
|
|
5
4
|
/** Promise that resolves when the states initialization was finished. */
|
|
6
5
|
didInit: PromiseLike<void> | boolean;
|
|
@@ -17,10 +16,7 @@ export declare class Stateful<Value = unknown> extends Destroyable {
|
|
|
17
16
|
*
|
|
18
17
|
* @returns A callback to unsubscribe the passed callback.
|
|
19
18
|
*/
|
|
20
|
-
subscribe(callback:
|
|
21
|
-
/** Make this atom unusable and remove all references.
|
|
22
|
-
**/
|
|
23
|
-
destroy(): void;
|
|
19
|
+
subscribe(callback: SubscriberCallback<Value>): () => boolean;
|
|
24
20
|
private emit;
|
|
25
21
|
protected update(value: Value): void;
|
|
26
22
|
protected setDidInit(didInit: boolean | PromiseLike<void>): void;
|
|
@@ -20,7 +20,7 @@ class Derive extends stateful_1.Stateful {
|
|
|
20
20
|
}
|
|
21
21
|
addGetDependency(dependency) {
|
|
22
22
|
if (!this.getterDependencies.has(dependency)) {
|
|
23
|
-
|
|
23
|
+
dependency.subscribe(() => this.deriveUpdate());
|
|
24
24
|
this.getterDependencies.add(dependency);
|
|
25
25
|
}
|
|
26
26
|
return dependency.get();
|
|
@@ -1,20 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createSelector = exports.CombinerSelector =
|
|
3
|
+
exports.createSelector = exports.CombinerSelector = void 0;
|
|
4
4
|
const utils_1 = require("@yaasl/utils");
|
|
5
5
|
const stateful_1 = require("./stateful");
|
|
6
|
-
const selectPath = (state, path) => path
|
|
7
|
-
.split(".")
|
|
8
|
-
.reduce((result, key) => result[key], state);
|
|
9
|
-
class PathSelector extends stateful_1.Stateful {
|
|
10
|
-
constructor(atom, path) {
|
|
11
|
-
super(selectPath(atom.get(), path));
|
|
12
|
-
this.subscribeTo(atom, state => this.update(selectPath(state, path)));
|
|
13
|
-
this.setDidInit(atom.didInit);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
exports.PathSelector = PathSelector;
|
|
17
|
-
// -- Combiner selector
|
|
18
6
|
const allDidInit = (atoms) => {
|
|
19
7
|
const inits = atoms
|
|
20
8
|
.map(atom => atom.didInit)
|
|
@@ -23,24 +11,23 @@ const allDidInit = (atoms) => {
|
|
|
23
11
|
};
|
|
24
12
|
class CombinerSelector extends stateful_1.Stateful {
|
|
25
13
|
constructor(atoms, combiner) {
|
|
14
|
+
const atomArray = (0, utils_1.toArray)(atoms);
|
|
26
15
|
const selectState = () => {
|
|
27
|
-
const values =
|
|
16
|
+
const values = atomArray.map(atom => atom.get());
|
|
28
17
|
return combiner(...values);
|
|
29
18
|
};
|
|
30
19
|
super(selectState());
|
|
31
|
-
|
|
32
|
-
this.setDidInit(allDidInit(
|
|
20
|
+
atomArray.forEach(atom => atom.subscribe(() => this.update(selectState())));
|
|
21
|
+
this.setDidInit(allDidInit(atomArray));
|
|
33
22
|
}
|
|
34
23
|
}
|
|
35
24
|
exports.CombinerSelector = CombinerSelector;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
throw new Error("Selector args do not match any overload");
|
|
45
|
-
};
|
|
25
|
+
/** Creates a value, selected from one or more atoms by using a combiner function.
|
|
26
|
+
*
|
|
27
|
+
* @param atoms One or more atoms you need to combine to receive the new value.
|
|
28
|
+
* @param combiner Combiner function to use the atom values and create a new value.
|
|
29
|
+
*
|
|
30
|
+
* @returns A CombinerSelector instance.
|
|
31
|
+
**/
|
|
32
|
+
const createSelector = (atoms, combiner) => new CombinerSelector(atoms, combiner);
|
|
46
33
|
exports.createSelector = createSelector;
|
|
@@ -7,9 +7,7 @@ const create_selector_1 = require("./create-selector");
|
|
|
7
7
|
const isEmpty = (obj) => !obj || Object.keys(obj).length === 0;
|
|
8
8
|
const createSelectors = (atom, selectors) => Object.fromEntries(Object.entries(selectors).map(([key, selector]) => [
|
|
9
9
|
key,
|
|
10
|
-
|
|
11
|
-
? (0, create_selector_1.createSelector)(atom, selector)
|
|
12
|
-
: (0, create_selector_1.createSelector)([atom], selector),
|
|
10
|
+
(0, create_selector_1.createSelector)(atom, selector),
|
|
13
11
|
]));
|
|
14
12
|
/** Creates a slice with actions and selectors.
|
|
15
13
|
*
|
|
@@ -17,7 +15,7 @@ const createSelectors = (atom, selectors) => Object.fromEntries(Object.entries(s
|
|
|
17
15
|
* @param config.name Name of the atom.
|
|
18
16
|
* @param config.effects Effects that will be applied on the atom.
|
|
19
17
|
* @param config.reducers Reducers for custom actions to set the atom's value.
|
|
20
|
-
* @param config.selectors
|
|
18
|
+
* @param config.selectors Combiner selectors to use the atom's values to create new ones.
|
|
21
19
|
*
|
|
22
20
|
* @returns An atom instance with actions and selectors.
|
|
23
21
|
**/
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Stateful = void 0;
|
|
4
|
-
|
|
5
|
-
class Stateful extends destroyable_1.Destroyable {
|
|
4
|
+
class Stateful {
|
|
6
5
|
constructor(value) {
|
|
7
|
-
super();
|
|
8
6
|
this.value = value;
|
|
9
7
|
/** Promise that resolves when the states initialization was finished. */
|
|
10
8
|
this.didInit = false;
|
|
@@ -27,13 +25,6 @@ class Stateful extends destroyable_1.Destroyable {
|
|
|
27
25
|
this.listeners.add(callback);
|
|
28
26
|
return () => this.listeners.delete(callback);
|
|
29
27
|
}
|
|
30
|
-
/** Make this atom unusable and remove all references.
|
|
31
|
-
**/
|
|
32
|
-
destroy() {
|
|
33
|
-
super.destroy();
|
|
34
|
-
this.value = null;
|
|
35
|
-
this.listeners = new Set();
|
|
36
|
-
}
|
|
37
28
|
emit(value, previous) {
|
|
38
29
|
this.listeners.forEach(listener => listener(value, previous));
|
|
39
30
|
}
|
package/dist/cjs/effects/sync.js
CHANGED
|
@@ -8,27 +8,24 @@ const getId = () => { var _a, _b; return (_b = (_a = (0, utils_1.getWindow)()) =
|
|
|
8
8
|
const getChannelName = (key) => ["yaasl", "sync-channel", base_1.CONFIG.name, key].filter(Boolean).join("/");
|
|
9
9
|
class SyncChannel {
|
|
10
10
|
constructor(key) {
|
|
11
|
+
this.id = getId();
|
|
11
12
|
this.listeners = new Set();
|
|
12
13
|
this.channel = new BroadcastChannel(getChannelName(key));
|
|
13
|
-
this.channel.
|
|
14
|
+
this.channel.addEventListener("message", event => {
|
|
14
15
|
const { id, data } = event.data;
|
|
15
|
-
if (id ===
|
|
16
|
+
if (id === this.id)
|
|
16
17
|
return;
|
|
17
18
|
this.listeners.forEach(listener => listener(data));
|
|
18
|
-
};
|
|
19
|
+
});
|
|
19
20
|
}
|
|
20
21
|
push(data) {
|
|
21
|
-
this.channel.postMessage({
|
|
22
|
-
id: SyncChannel.id,
|
|
23
|
-
data,
|
|
24
|
-
});
|
|
22
|
+
this.channel.postMessage({ id: this.id, data });
|
|
25
23
|
}
|
|
26
24
|
subscribe(listener) {
|
|
27
25
|
this.listeners.add(listener);
|
|
28
26
|
return () => this.listeners.delete(listener);
|
|
29
27
|
}
|
|
30
28
|
}
|
|
31
|
-
SyncChannel.id = getId();
|
|
32
29
|
/** Effect to synchronize the atoms value over tabs.
|
|
33
30
|
*
|
|
34
31
|
* @returns The effect to be used on atoms.
|
|
@@ -38,9 +35,9 @@ exports.sync = (0, create_effect_1.createEffect)(({ atom }) => {
|
|
|
38
35
|
let skip = false;
|
|
39
36
|
return {
|
|
40
37
|
didInit: () => {
|
|
41
|
-
channel.subscribe(
|
|
42
|
-
atom.set(data);
|
|
38
|
+
channel.subscribe(value => {
|
|
43
39
|
skip = true;
|
|
40
|
+
atom.set(value);
|
|
44
41
|
});
|
|
45
42
|
},
|
|
46
43
|
set: ({ value }) => {
|
|
@@ -17,7 +17,7 @@ export class Derive extends Stateful {
|
|
|
17
17
|
}
|
|
18
18
|
addGetDependency(dependency) {
|
|
19
19
|
if (!this.getterDependencies.has(dependency)) {
|
|
20
|
-
|
|
20
|
+
dependency.subscribe(() => this.deriveUpdate());
|
|
21
21
|
this.getterDependencies.add(dependency);
|
|
22
22
|
}
|
|
23
23
|
return dependency.get();
|
|
@@ -1,16 +1,5 @@
|
|
|
1
|
-
import { toVoid } from "@yaasl/utils";
|
|
1
|
+
import { toArray, toVoid } from "@yaasl/utils";
|
|
2
2
|
import { Stateful } from "./stateful";
|
|
3
|
-
const selectPath = (state, path) => path
|
|
4
|
-
.split(".")
|
|
5
|
-
.reduce((result, key) => result[key], state);
|
|
6
|
-
export class PathSelector extends Stateful {
|
|
7
|
-
constructor(atom, path) {
|
|
8
|
-
super(selectPath(atom.get(), path));
|
|
9
|
-
this.subscribeTo(atom, state => this.update(selectPath(state, path)));
|
|
10
|
-
this.setDidInit(atom.didInit);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
// -- Combiner selector
|
|
14
3
|
const allDidInit = (atoms) => {
|
|
15
4
|
const inits = atoms
|
|
16
5
|
.map(atom => atom.didInit)
|
|
@@ -19,22 +8,21 @@ const allDidInit = (atoms) => {
|
|
|
19
8
|
};
|
|
20
9
|
export class CombinerSelector extends Stateful {
|
|
21
10
|
constructor(atoms, combiner) {
|
|
11
|
+
const atomArray = toArray(atoms);
|
|
22
12
|
const selectState = () => {
|
|
23
|
-
const values =
|
|
13
|
+
const values = atomArray.map(atom => atom.get());
|
|
24
14
|
return combiner(...values);
|
|
25
15
|
};
|
|
26
16
|
super(selectState());
|
|
27
|
-
|
|
28
|
-
this.setDidInit(allDidInit(
|
|
17
|
+
atomArray.forEach(atom => atom.subscribe(() => this.update(selectState())));
|
|
18
|
+
this.setDidInit(allDidInit(atomArray));
|
|
29
19
|
}
|
|
30
20
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
throw new Error("Selector args do not match any overload");
|
|
40
|
-
};
|
|
21
|
+
/** Creates a value, selected from one or more atoms by using a combiner function.
|
|
22
|
+
*
|
|
23
|
+
* @param atoms One or more atoms you need to combine to receive the new value.
|
|
24
|
+
* @param combiner Combiner function to use the atom values and create a new value.
|
|
25
|
+
*
|
|
26
|
+
* @returns A CombinerSelector instance.
|
|
27
|
+
**/
|
|
28
|
+
export const createSelector = (atoms, combiner) => new CombinerSelector(atoms, combiner);
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { createActions } from "./create-actions";
|
|
2
2
|
import { Atom } from "./create-atom";
|
|
3
|
-
import { createSelector
|
|
3
|
+
import { createSelector } from "./create-selector";
|
|
4
4
|
const isEmpty = (obj) => !obj || Object.keys(obj).length === 0;
|
|
5
5
|
const createSelectors = (atom, selectors) => Object.fromEntries(Object.entries(selectors).map(([key, selector]) => [
|
|
6
6
|
key,
|
|
7
|
-
|
|
8
|
-
? createSelector(atom, selector)
|
|
9
|
-
: createSelector([atom], selector),
|
|
7
|
+
createSelector(atom, selector),
|
|
10
8
|
]));
|
|
11
9
|
/** Creates a slice with actions and selectors.
|
|
12
10
|
*
|
|
@@ -14,7 +12,7 @@ const createSelectors = (atom, selectors) => Object.fromEntries(Object.entries(s
|
|
|
14
12
|
* @param config.name Name of the atom.
|
|
15
13
|
* @param config.effects Effects that will be applied on the atom.
|
|
16
14
|
* @param config.reducers Reducers for custom actions to set the atom's value.
|
|
17
|
-
* @param config.selectors
|
|
15
|
+
* @param config.selectors Combiner selectors to use the atom's values to create new ones.
|
|
18
16
|
*
|
|
19
17
|
* @returns An atom instance with actions and selectors.
|
|
20
18
|
**/
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
export class Stateful extends Destroyable {
|
|
1
|
+
export class Stateful {
|
|
3
2
|
value;
|
|
4
3
|
/** Promise that resolves when the states initialization was finished. */
|
|
5
4
|
didInit = false;
|
|
6
5
|
listeners = new Set();
|
|
7
6
|
constructor(value) {
|
|
8
|
-
super();
|
|
9
7
|
this.value = value;
|
|
10
8
|
}
|
|
11
9
|
/** Read the value of state.
|
|
@@ -25,13 +23,6 @@ export class Stateful extends Destroyable {
|
|
|
25
23
|
this.listeners.add(callback);
|
|
26
24
|
return () => this.listeners.delete(callback);
|
|
27
25
|
}
|
|
28
|
-
/** Make this atom unusable and remove all references.
|
|
29
|
-
**/
|
|
30
|
-
destroy() {
|
|
31
|
-
super.destroy();
|
|
32
|
-
this.value = null;
|
|
33
|
-
this.listeners = new Set();
|
|
34
|
-
}
|
|
35
26
|
emit(value, previous) {
|
|
36
27
|
this.listeners.forEach(listener => listener(value, previous));
|
|
37
28
|
}
|
package/dist/esm/effects/sync.js
CHANGED
|
@@ -4,23 +4,20 @@ import { CONFIG } from "../base";
|
|
|
4
4
|
const getId = () => getWindow()?.crypto.randomUUID() ?? Math.random().toString(36).slice(2, 10);
|
|
5
5
|
const getChannelName = (key) => ["yaasl", "sync-channel", CONFIG.name, key].filter(Boolean).join("/");
|
|
6
6
|
class SyncChannel {
|
|
7
|
-
|
|
7
|
+
id = getId();
|
|
8
8
|
channel;
|
|
9
9
|
listeners = new Set();
|
|
10
10
|
constructor(key) {
|
|
11
11
|
this.channel = new BroadcastChannel(getChannelName(key));
|
|
12
|
-
this.channel.
|
|
12
|
+
this.channel.addEventListener("message", event => {
|
|
13
13
|
const { id, data } = event.data;
|
|
14
|
-
if (id ===
|
|
14
|
+
if (id === this.id)
|
|
15
15
|
return;
|
|
16
16
|
this.listeners.forEach(listener => listener(data));
|
|
17
|
-
};
|
|
17
|
+
});
|
|
18
18
|
}
|
|
19
19
|
push(data) {
|
|
20
|
-
this.channel.postMessage({
|
|
21
|
-
id: SyncChannel.id,
|
|
22
|
-
data,
|
|
23
|
-
});
|
|
20
|
+
this.channel.postMessage({ id: this.id, data });
|
|
24
21
|
}
|
|
25
22
|
subscribe(listener) {
|
|
26
23
|
this.listeners.add(listener);
|
|
@@ -36,9 +33,9 @@ export const sync = createEffect(({ atom }) => {
|
|
|
36
33
|
let skip = false;
|
|
37
34
|
return {
|
|
38
35
|
didInit: () => {
|
|
39
|
-
channel.subscribe(
|
|
40
|
-
atom.set(data);
|
|
36
|
+
channel.subscribe(value => {
|
|
41
37
|
skip = true;
|
|
38
|
+
atom.set(value);
|
|
42
39
|
});
|
|
43
40
|
},
|
|
44
41
|
set: ({ value }) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yaasl/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0-alpha.1",
|
|
4
4
|
"description": "yet another atomic store library (vanilla-js)",
|
|
5
5
|
"author": "PrettyCoffee",
|
|
6
6
|
"license": "MIT",
|
|
@@ -40,10 +40,10 @@
|
|
|
40
40
|
"validate": "run-s lint test build"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"@yaasl/utils": "^0.
|
|
43
|
+
"@yaasl/utils": "^0.15.0-alpha.1"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@yaasl/utils": "^0.
|
|
46
|
+
"@yaasl/utils": "^0.15.0-alpha.1"
|
|
47
47
|
},
|
|
48
48
|
"lint-staged": {
|
|
49
49
|
"*.{ts,tsx}": [
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { Callback, Stateful } from "./stateful";
|
|
2
|
-
export declare class Destroyable {
|
|
3
|
-
isDestroyed: boolean;
|
|
4
|
-
private dependents;
|
|
5
|
-
private unsubscribers;
|
|
6
|
-
/** Make this atom unusable and remove all references.
|
|
7
|
-
**/
|
|
8
|
-
destroy(): void;
|
|
9
|
-
/** Subscribe to another atom and automatically destroy the atom instance, if the parent is destroyed */
|
|
10
|
-
protected subscribeTo<T>(parent: Stateful<T>, callback: Callback<T>): void;
|
|
11
|
-
protected addDependent(dependent: Destroyable): void;
|
|
12
|
-
protected removeDependent(dependent: Destroyable): void;
|
|
13
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Destroyable = void 0;
|
|
4
|
-
const utils_1 = require("@yaasl/utils");
|
|
5
|
-
class Destroyable {
|
|
6
|
-
constructor() {
|
|
7
|
-
this.isDestroyed = false;
|
|
8
|
-
this.dependents = new Set();
|
|
9
|
-
this.unsubscribers = new Set();
|
|
10
|
-
}
|
|
11
|
-
/** Make this atom unusable and remove all references.
|
|
12
|
-
**/
|
|
13
|
-
destroy() {
|
|
14
|
-
this.dependents.forEach(dependent => dependent.destroy());
|
|
15
|
-
this.unsubscribers.forEach(unsubscribe => unsubscribe());
|
|
16
|
-
this.dependents.clear();
|
|
17
|
-
this.unsubscribers.clear();
|
|
18
|
-
this.isDestroyed = true;
|
|
19
|
-
// eslint-disable-next-line unicorn/consistent-function-scoping
|
|
20
|
-
const throwOnCall = () => {
|
|
21
|
-
const name = "name" in this ? this.name : undefined;
|
|
22
|
-
throw new Error((0, utils_1.consoleMessage)("The methods of a destroyed atom cannot be called.", {
|
|
23
|
-
scope: name,
|
|
24
|
-
}));
|
|
25
|
-
};
|
|
26
|
-
Object.assign(this, {
|
|
27
|
-
set: throwOnCall,
|
|
28
|
-
get: throwOnCall,
|
|
29
|
-
subscribe: throwOnCall,
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
/** Subscribe to another atom and automatically destroy the atom instance, if the parent is destroyed */
|
|
33
|
-
subscribeTo(parent, callback) {
|
|
34
|
-
parent.addDependent(this);
|
|
35
|
-
const unsubscribe = parent.subscribe(callback);
|
|
36
|
-
this.unsubscribers.add(() => {
|
|
37
|
-
unsubscribe();
|
|
38
|
-
parent.removeDependent(this);
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
addDependent(dependent) {
|
|
42
|
-
this.dependents.add(dependent);
|
|
43
|
-
}
|
|
44
|
-
removeDependent(dependent) {
|
|
45
|
-
this.dependents.delete(dependent);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
exports.Destroyable = Destroyable;
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { consoleMessage } from "@yaasl/utils";
|
|
2
|
-
export class Destroyable {
|
|
3
|
-
isDestroyed = false;
|
|
4
|
-
dependents = new Set();
|
|
5
|
-
unsubscribers = new Set();
|
|
6
|
-
/** Make this atom unusable and remove all references.
|
|
7
|
-
**/
|
|
8
|
-
destroy() {
|
|
9
|
-
this.dependents.forEach(dependent => dependent.destroy());
|
|
10
|
-
this.unsubscribers.forEach(unsubscribe => unsubscribe());
|
|
11
|
-
this.dependents.clear();
|
|
12
|
-
this.unsubscribers.clear();
|
|
13
|
-
this.isDestroyed = true;
|
|
14
|
-
// eslint-disable-next-line unicorn/consistent-function-scoping
|
|
15
|
-
const throwOnCall = () => {
|
|
16
|
-
const name = "name" in this ? this.name : undefined;
|
|
17
|
-
throw new Error(consoleMessage("The methods of a destroyed atom cannot be called.", {
|
|
18
|
-
scope: name,
|
|
19
|
-
}));
|
|
20
|
-
};
|
|
21
|
-
Object.assign(this, {
|
|
22
|
-
set: throwOnCall,
|
|
23
|
-
get: throwOnCall,
|
|
24
|
-
subscribe: throwOnCall,
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
/** Subscribe to another atom and automatically destroy the atom instance, if the parent is destroyed */
|
|
28
|
-
subscribeTo(parent, callback) {
|
|
29
|
-
parent.addDependent(this);
|
|
30
|
-
const unsubscribe = parent.subscribe(callback);
|
|
31
|
-
this.unsubscribers.add(() => {
|
|
32
|
-
unsubscribe();
|
|
33
|
-
parent.removeDependent(this);
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
addDependent(dependent) {
|
|
37
|
-
this.dependents.add(dependent);
|
|
38
|
-
}
|
|
39
|
-
removeDependent(dependent) {
|
|
40
|
-
this.dependents.delete(dependent);
|
|
41
|
-
}
|
|
42
|
-
}
|