@yaasl/core 0.10.2 → 0.11.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.
Files changed (34) hide show
  1. package/dist/@types/base/Destroyable.d.ts +13 -0
  2. package/dist/@types/base/Stateful.d.ts +6 -3
  3. package/dist/@types/base/config.d.ts +1 -1
  4. package/dist/@types/base/createActions.d.ts +2 -2
  5. package/dist/@types/base/createAtom.d.ts +1 -1
  6. package/dist/@types/base/createDerived.d.ts +1 -1
  7. package/dist/@types/effects/EffectDispatcher.d.ts +2 -2
  8. package/dist/@types/effects/createEffect.d.ts +2 -2
  9. package/dist/cjs/base/Destroyable.js +47 -0
  10. package/dist/cjs/base/Stateful.js +10 -1
  11. package/dist/cjs/base/createDerived.js +1 -1
  12. package/dist/cjs/base/createSelector.js +2 -2
  13. package/dist/esm/base/Destroyable.js +41 -0
  14. package/dist/{mjs → esm}/base/Stateful.js +10 -1
  15. package/dist/{mjs → esm}/base/createDerived.js +1 -1
  16. package/dist/{mjs → esm}/base/createSelector.js +2 -2
  17. package/package.json +8 -6
  18. /package/dist/{mjs → esm}/base/config.js +0 -0
  19. /package/dist/{mjs → esm}/base/createActions.js +0 -0
  20. /package/dist/{mjs → esm}/base/createAtom.js +0 -0
  21. /package/dist/{mjs → esm}/base/createSlice.js +0 -0
  22. /package/dist/{mjs → esm}/base/index.js +0 -0
  23. /package/dist/{mjs → esm}/effects/EffectDispatcher.js +0 -0
  24. /package/dist/{mjs → esm}/effects/createEffect.js +0 -0
  25. /package/dist/{mjs → esm}/effects/expiration.js +0 -0
  26. /package/dist/{mjs → esm}/effects/index.js +0 -0
  27. /package/dist/{mjs → esm}/effects/indexedDb.js +0 -0
  28. /package/dist/{mjs → esm}/effects/localStorage.js +0 -0
  29. /package/dist/{mjs → esm}/effects/migration.js +0 -0
  30. /package/dist/{mjs → esm}/index.js +0 -0
  31. /package/dist/{mjs → esm}/utils/Expiration.js +0 -0
  32. /package/dist/{mjs → esm}/utils/LocalStorage.js +0 -0
  33. /package/dist/{mjs → esm}/utils/Queue.js +0 -0
  34. /package/dist/{mjs → esm}/utils/Store.js +0 -0
@@ -0,0 +1,13 @@
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,5 +1,6 @@
1
- type Callback<Value> = (value: Value, previous: Value) => void;
2
- export declare class Stateful<Value = unknown> {
1
+ import { Destroyable } from "./Destroyable";
2
+ export type Callback<Value> = (value: Value, previous: Value) => void;
3
+ export declare class Stateful<Value = unknown> extends Destroyable {
3
4
  protected value: Value;
4
5
  /** Promise that resolves when the states initialization was finished. */
5
6
  didInit: PromiseLike<void> | boolean;
@@ -17,8 +18,10 @@ export declare class Stateful<Value = unknown> {
17
18
  * @returns A callback to unsubscribe the passed callback.
18
19
  */
19
20
  subscribe(callback: Callback<Value>): () => boolean;
21
+ /** Make this atom unusable and remove all references.
22
+ **/
23
+ destroy(): void;
20
24
  private emit;
21
25
  protected update(value: Value): void;
22
26
  protected setDidInit(didInit: boolean | PromiseLike<void>): void;
23
27
  }
24
- export {};
@@ -1,4 +1,4 @@
1
- import { EffectAtomCallback } from "../effects/createEffect";
1
+ import type { EffectAtomCallback } from "../effects/createEffect";
2
2
  interface Config {
3
3
  /** Global name to make internal keys unique
4
4
  * among UIs on the same domain.
@@ -1,5 +1,5 @@
1
- import { Atom } from "./createAtom";
2
- import { SettableDerive } from "./createDerived";
1
+ import type { Atom } from "./createAtom";
2
+ import type { SettableDerive } from "./createDerived";
3
3
  type Reducer<State> = (state: State, ...payloadArgs: any[]) => State;
4
4
  export type Reducers<State> = Record<string, Reducer<State>>;
5
5
  type Payload<R extends Reducer<any>> = Parameters<R> extends [
@@ -1,6 +1,6 @@
1
1
  import { Updater } from "@yaasl/utils";
2
2
  import { Stateful } from "./Stateful";
3
- import { EffectAtomCallback } from "../effects/createEffect";
3
+ import type { EffectAtomCallback } from "../effects/createEffect";
4
4
  export interface AtomConfig<Value> {
5
5
  /** Value that will be used initially. */
6
6
  defaultValue: Value;
@@ -1,5 +1,5 @@
1
1
  import { Updater } from "@yaasl/utils";
2
- import { Atom } from "./createAtom";
2
+ import type { Atom } from "./createAtom";
3
3
  import { Stateful } from "./Stateful";
4
4
  type GetterFn<Value> = (context: {
5
5
  get: <V>(dep: Stateful<V>) => V;
@@ -1,5 +1,5 @@
1
- import { ActionType, EffectAtomCallback } from "./createEffect";
2
- import { Atom } from "../base/createAtom";
1
+ import type { ActionType, EffectAtomCallback } from "./createEffect";
2
+ import type { Atom } from "../base/createAtom";
3
3
  export declare class EffectActions<Value> {
4
4
  private readonly atom;
5
5
  private options;
@@ -1,5 +1,5 @@
1
- import { Updater, Dispatch } from "@yaasl/utils";
2
- import { Atom } from "../base";
1
+ import type { Updater, Dispatch } from "@yaasl/utils";
2
+ import type { Atom } from "../base";
3
3
  export type ActionType = "init" | "didInit" | "set";
4
4
  export interface EffectPayload<Options = undefined, AtomValue = any> {
5
5
  /** Current value of the atom */
@@ -0,0 +1,47 @@
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
+ const throwOnCall = () => {
20
+ const name = "name" in this ? this.name : undefined;
21
+ throw new Error((0, utils_1.consoleMessage)("The methods of a destroyed atom cannot be called.", {
22
+ scope: name,
23
+ }));
24
+ };
25
+ Object.assign(this, {
26
+ set: throwOnCall,
27
+ get: throwOnCall,
28
+ subscribe: throwOnCall,
29
+ });
30
+ }
31
+ /** Subscribe to another atom and automatically destroy the atom instance, if the parent is destroyed */
32
+ subscribeTo(parent, callback) {
33
+ parent.addDependent(this);
34
+ const unsubscribe = parent.subscribe(callback);
35
+ this.unsubscribers.add(() => {
36
+ unsubscribe();
37
+ parent.removeDependent(this);
38
+ });
39
+ }
40
+ addDependent(dependent) {
41
+ this.dependents.add(dependent);
42
+ }
43
+ removeDependent(dependent) {
44
+ this.dependents.delete(dependent);
45
+ }
46
+ }
47
+ exports.Destroyable = Destroyable;
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Stateful = void 0;
4
- class Stateful {
4
+ const Destroyable_1 = require("./Destroyable");
5
+ class Stateful extends Destroyable_1.Destroyable {
5
6
  constructor(value) {
7
+ super();
6
8
  this.value = value;
7
9
  /** Promise that resolves when the states initialization was finished. */
8
10
  this.didInit = false;
@@ -25,6 +27,13 @@ class Stateful {
25
27
  this.listeners.add(callback);
26
28
  return () => this.listeners.delete(callback);
27
29
  }
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
+ }
28
37
  emit(value, previous) {
29
38
  this.listeners.forEach(listener => listener(value, previous));
30
39
  }
@@ -21,7 +21,7 @@ class Derive extends Stateful_1.Stateful {
21
21
  }
22
22
  addGetDependency(dependency) {
23
23
  if (!this.getterDependencies.has(dependency)) {
24
- dependency.subscribe(() => this.deriveUpdate());
24
+ this.subscribeTo(dependency, () => this.deriveUpdate());
25
25
  this.getterDependencies.add(dependency);
26
26
  }
27
27
  return dependency.get();
@@ -9,7 +9,7 @@ const selectPath = (state, path) => path
9
9
  class PathSelector extends Stateful_1.Stateful {
10
10
  constructor(atom, path) {
11
11
  super(selectPath(atom.get(), path));
12
- atom.subscribe(state => this.update(selectPath(state, path)));
12
+ this.subscribeTo(atom, state => this.update(selectPath(state, path)));
13
13
  this.setDidInit(atom.didInit);
14
14
  }
15
15
  }
@@ -28,7 +28,7 @@ class CombinerSelector extends Stateful_1.Stateful {
28
28
  return combiner(...values);
29
29
  };
30
30
  super(selectState());
31
- atoms.forEach(atom => atom.subscribe(() => this.update(selectState())));
31
+ atoms.forEach(atom => this.subscribeTo(atom, () => this.update(selectState())));
32
32
  this.setDidInit(allDidInit(atoms));
33
33
  }
34
34
  }
@@ -0,0 +1,41 @@
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
+ const throwOnCall = () => {
15
+ const name = "name" in this ? this.name : undefined;
16
+ throw new Error(consoleMessage("The methods of a destroyed atom cannot be called.", {
17
+ scope: name,
18
+ }));
19
+ };
20
+ Object.assign(this, {
21
+ set: throwOnCall,
22
+ get: throwOnCall,
23
+ subscribe: throwOnCall,
24
+ });
25
+ }
26
+ /** Subscribe to another atom and automatically destroy the atom instance, if the parent is destroyed */
27
+ subscribeTo(parent, callback) {
28
+ parent.addDependent(this);
29
+ const unsubscribe = parent.subscribe(callback);
30
+ this.unsubscribers.add(() => {
31
+ unsubscribe();
32
+ parent.removeDependent(this);
33
+ });
34
+ }
35
+ addDependent(dependent) {
36
+ this.dependents.add(dependent);
37
+ }
38
+ removeDependent(dependent) {
39
+ this.dependents.delete(dependent);
40
+ }
41
+ }
@@ -1,9 +1,11 @@
1
- export class Stateful {
1
+ import { Destroyable } from "./Destroyable";
2
+ export class Stateful extends Destroyable {
2
3
  value;
3
4
  /** Promise that resolves when the states initialization was finished. */
4
5
  didInit = false;
5
6
  listeners = new Set();
6
7
  constructor(value) {
8
+ super();
7
9
  this.value = value;
8
10
  }
9
11
  /** Read the value of state.
@@ -23,6 +25,13 @@ export class Stateful {
23
25
  this.listeners.add(callback);
24
26
  return () => this.listeners.delete(callback);
25
27
  }
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
+ }
26
35
  emit(value, previous) {
27
36
  this.listeners.forEach(listener => listener(value, previous));
28
37
  }
@@ -18,7 +18,7 @@ export class Derive extends Stateful {
18
18
  }
19
19
  addGetDependency(dependency) {
20
20
  if (!this.getterDependencies.has(dependency)) {
21
- dependency.subscribe(() => this.deriveUpdate());
21
+ this.subscribeTo(dependency, () => this.deriveUpdate());
22
22
  this.getterDependencies.add(dependency);
23
23
  }
24
24
  return dependency.get();
@@ -6,7 +6,7 @@ const selectPath = (state, path) => path
6
6
  export class PathSelector extends Stateful {
7
7
  constructor(atom, path) {
8
8
  super(selectPath(atom.get(), path));
9
- atom.subscribe(state => this.update(selectPath(state, path)));
9
+ this.subscribeTo(atom, state => this.update(selectPath(state, path)));
10
10
  this.setDidInit(atom.didInit);
11
11
  }
12
12
  }
@@ -24,7 +24,7 @@ export class CombinerSelector extends Stateful {
24
24
  return combiner(...values);
25
25
  };
26
26
  super(selectState());
27
- atoms.forEach(atom => atom.subscribe(() => this.update(selectState())));
27
+ atoms.forEach(atom => this.subscribeTo(atom, () => this.update(selectState())));
28
28
  this.setDidInit(allDidInit(atoms));
29
29
  }
30
30
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yaasl/core",
3
- "version": "0.10.2",
3
+ "version": "0.11.0-alpha.1",
4
4
  "description": "yet another atomic store library (vanilla-js)",
5
5
  "author": "PrettyCoffee",
6
6
  "license": "MIT",
@@ -17,18 +17,20 @@
17
17
  "bugs": {
18
18
  "url": "https://github.com/PrettyCoffee/yaasl/issues"
19
19
  },
20
+ "sideEffects": false,
20
21
  "main": "./dist/cjs/index.js",
22
+ "module": "./dist/esm/index.js",
21
23
  "types": "./dist/@types/index.d.ts",
22
24
  "exports": {
23
25
  "types": "./dist/@types/index.d.ts",
24
- "import": "./dist/mjs/index.js",
25
- "require": "./dist/cjs/index.js"
26
+ "require": "./dist/cjs/index.js",
27
+ "default": "./dist/esm/index.js"
26
28
  },
27
29
  "scripts": {
28
30
  "build": "run-s clean compile",
29
31
  "clean": "rimraf ./dist",
30
- "compile": "run-p compile:mjs compile:cjs compile:types",
31
- "compile:mjs": "tsc -p ./tsconfig.node.json --outDir ./dist/mjs -m esnext -t esnext",
32
+ "compile": "run-p compile:esm compile:cjs compile:types",
33
+ "compile:esm": "tsc -p ./tsconfig.node.json --outDir ./dist/esm -m esnext -t esnext",
32
34
  "compile:cjs": "tsc -p ./tsconfig.node.json --outDir ./dist/cjs -m commonjs -t es2015",
33
35
  "compile:types": "tsc -p ./tsconfig.node.json --outDir ./dist/@types --declaration --emitDeclarationOnly",
34
36
  "test": "vitest run",
@@ -38,7 +40,7 @@
38
40
  "validate": "run-s lint test build"
39
41
  },
40
42
  "dependencies": {
41
- "@yaasl/utils": "0.10.2"
43
+ "@yaasl/utils": "0.11.0-alpha.1"
42
44
  },
43
45
  "eslintConfig": {
44
46
  "extends": [
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes