@exodus/atoms 8.1.1 → 9.0.0

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/CHANGELOG.md CHANGED
@@ -3,6 +3,25 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [9.0.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/atoms@8.1.1...@exodus/atoms@9.0.0) (2024-09-26)
7
+
8
+ ### ⚠ BREAKING CHANGES
9
+
10
+ - remove isSoleWriter from storage atom factory (#9423)
11
+
12
+ ### Features
13
+
14
+ - don't allow get to proceed after unsubscribe ([#9441](https://github.com/ExodusMovement/exodus-hydra/issues/9441)) ([33bc642](https://github.com/ExodusMovement/exodus-hydra/commit/33bc642ad6ec32e1f31711f9dfe435d235eaca56))
15
+ - remove isSoleWriter from storage atom factory ([#9423](https://github.com/ExodusMovement/exodus-hydra/issues/9423)) ([ab90ee1](https://github.com/ExodusMovement/exodus-hydra/commit/ab90ee13a819058c0f23c37008da2bebf4439157))
16
+
17
+ ### Bug Fixes
18
+
19
+ - storage atom race condition ([#9403](https://github.com/ExodusMovement/exodus-hydra/issues/9403)) ([bf30d0c](https://github.com/ExodusMovement/exodus-hydra/commit/bf30d0cc90632459b6b0b9fd76fac191d20ddd0b))
20
+
21
+ ### Performance Improvements
22
+
23
+ - reduce underlying storage reads ([#9418](https://github.com/ExodusMovement/exodus-hydra/issues/9418)) ([f3c0c23](https://github.com/ExodusMovement/exodus-hydra/commit/f3c0c232b120977a27565636f4d1a6ca9fc90b4a))
24
+
6
25
  ## [8.1.1](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/atoms@8.1.0...@exodus/atoms@8.1.1) (2024-09-09)
7
26
 
8
27
  ### Bug Fixes
@@ -1,4 +1,4 @@
1
- import { Atom } from '../utils/types.js';
1
+ import type { Atom } from '../utils/types.js';
2
2
  type Params<T> = {
3
3
  atom: Atom<T>;
4
4
  predicate: (value: T) => boolean;
@@ -1,9 +1,9 @@
1
- import { Atom } from './utils/types.js';
1
+ import type { Atom } from './utils/types.js';
2
2
  type Params<T> = {
3
- makeGetConcurrent?: boolean;
3
+ makeGetNonConcurrent?: boolean;
4
4
  getInitialized?: () => boolean;
5
5
  defaultValue?: T;
6
6
  set: (value: T) => Promise<void>;
7
7
  } & Omit<Atom<T>, 'set' | 'reset'>;
8
- declare const enforceObservableRules: <T>({ defaultValue, makeGetConcurrent, getInitialized, ...atom }: Params<T>) => Atom<T>;
8
+ declare const enforceObservableRules: <T>({ defaultValue, makeGetNonConcurrent, getInitialized, ...atom }: Params<T>) => Atom<T>;
9
9
  export default enforceObservableRules;
@@ -14,26 +14,31 @@ const withChangeDetection = (listener) => {
14
14
  await listener(currentValue);
15
15
  };
16
16
  };
17
- const enforceObservableRules = ({ defaultValue, makeGetConcurrent = false, getInitialized = () => true, ...atom }) => {
17
+ const enforceObservableRules = ({ defaultValue, makeGetNonConcurrent = false, getInitialized = () => true, ...atom }) => {
18
18
  const enqueue = makeConcurrent((fn) => fn(), { concurrency: 1 });
19
19
  const postProcessValue = (value = defaultValue) => value && typeof value === 'object' ? freeze(value) : value;
20
- const _get = makeGetConcurrent ? makeConcurrent(atom.get) : atom.get;
21
- const get = () => _get().then(postProcessValue);
20
+ const nonConcurrentGet = makeGetNonConcurrent
21
+ ? makeConcurrent(atom.get)
22
+ : atom.get;
23
+ const get = () => nonConcurrentGet().then(postProcessValue);
22
24
  const observe = (listener) => {
23
25
  let called = false;
24
26
  let valueEmittedFromGet;
27
+ let subscribed = true;
25
28
  listener = withChangeDetection(listener);
26
29
  const publishSerially = (value) => {
27
30
  called = true;
28
31
  return enqueue(() => listener(value));
29
32
  };
30
- atom.get().then((value) => {
33
+ nonConcurrentGet().then((value) => {
34
+ if (!subscribed)
35
+ return;
31
36
  if (!called) {
32
37
  valueEmittedFromGet = value;
33
38
  publishSerially(postProcessValue(value));
34
39
  }
35
40
  });
36
- return atom.observe((value) => {
41
+ const unsubscribe = atom.observe((value) => {
37
42
  if (valueEmittedFromGet !== undefined) {
38
43
  const isAlreadyEmitted = value === valueEmittedFromGet;
39
44
  valueEmittedFromGet = undefined;
@@ -43,6 +48,10 @@ const enforceObservableRules = ({ defaultValue, makeGetConcurrent = false, getIn
43
48
  }
44
49
  return publishSerially(postProcessValue(value));
45
50
  });
51
+ return () => {
52
+ unsubscribe();
53
+ subscribed = false;
54
+ };
46
55
  };
47
56
  const set = makeConcurrent(async (value) => {
48
57
  if (isSetter(value)) {
@@ -1,4 +1,4 @@
1
- import { Atom, Listener } from '../utils/types.js';
1
+ import type { Atom, Listener } from '../utils/types.js';
2
2
  type Params<T> = {
3
3
  atom: Atom<T>;
4
4
  unblock: () => Promise<unknown>;
@@ -1,4 +1,4 @@
1
- import { Atom, ReadonlyAtom } from '../utils/types.js';
1
+ import type { Atom, ReadonlyAtom } from '../utils/types.js';
2
2
  type CombinedValue<T> = {
3
3
  [Key in keyof T]: T[Key] extends Atom<infer U> ? U : never;
4
4
  };
@@ -1,4 +1,4 @@
1
- import { Atom, ReadonlyAtom } from '../utils/types.js';
1
+ import type { Atom, ReadonlyAtom } from '../utils/types.js';
2
2
  type Params<T, V> = {
3
3
  atom: Atom<T> | ReadonlyAtom<T>;
4
4
  selector: (value: T) => V | Promise<V>;
@@ -1,3 +1,3 @@
1
- import { Atom } from '../utils/types.js';
1
+ import type { Atom } from '../utils/types.js';
2
2
  declare const dedupe: <T>(atom: Atom<T>) => Atom<T>;
3
3
  export default dedupe;
@@ -1,4 +1,4 @@
1
- import { Atom } from '../utils/types.js';
1
+ import type { Atom } from '../utils/types.js';
2
2
  type Diff<T> = {
3
3
  previous?: T;
4
4
  current: T;
@@ -1,2 +1,2 @@
1
- import { Atom } from '../utils/types.js';
1
+ import type { Atom } from '../utils/types.js';
2
2
  export default function filter<T>(atom: Atom<T>, predicate: (value: T) => boolean): Atom<T>;
@@ -1,4 +1,4 @@
1
- import { Atom } from '../utils/types.js';
1
+ import type { Atom } from '../utils/types.js';
2
2
  type Params<T extends object> = {
3
3
  atom: Atom<T>;
4
4
  value: T;
@@ -1,3 +1,3 @@
1
- import { Atom } from '../utils/types.js';
1
+ import type { Atom } from '../utils/types.js';
2
2
  declare const optimisticNotifier: <T>(atom: Atom<T>) => Atom<T | undefined>;
3
3
  export default optimisticNotifier;
@@ -1,3 +1,3 @@
1
- import { Atom, ReadonlyAtom } from '../utils/types.js';
1
+ import type { Atom, ReadonlyAtom } from '../utils/types.js';
2
2
  declare const readOnly: <T>(atom: Atom<T>) => ReadonlyAtom<T>;
3
3
  export default readOnly;
@@ -1,4 +1,4 @@
1
- import { Atom, Listener, Logger } from '../utils/types.js';
1
+ import type { Atom, Listener, Logger } from '../utils/types.js';
2
2
  type Params<T> = {
3
3
  atom: Atom<T>;
4
4
  logger: Logger;
@@ -1,4 +1,4 @@
1
- import { Atom } from '../utils/types.js';
1
+ import type { Atom } from '../utils/types.js';
2
2
  type Params<T> = {
3
3
  atom: Atom<T>;
4
4
  timeout: number;
@@ -1,4 +1,4 @@
1
- import { Atom, Logger } from '../utils/types.js';
1
+ import type { Atom, Logger } from '../utils/types.js';
2
2
  type Params<T> = {
3
3
  atom: Atom<T>;
4
4
  logger: Logger;
@@ -1,4 +1,4 @@
1
- import { Atom } from '../utils/types.js';
1
+ import type { Atom } from '../utils/types.js';
2
2
  type Params<T, S> = {
3
3
  atom: Atom<S>;
4
4
  serialize: (value: T) => S;
@@ -1,5 +1,5 @@
1
- import { Atom } from '../utils/types.js';
2
- import { Storage } from '@exodus/storage-interface';
1
+ import type { Atom } from '../utils/types.js';
2
+ import type { Storage } from '@exodus/storage-interface';
3
3
  type Params<T> = {
4
4
  atom: Atom<T>;
5
5
  storage: Storage<T>;
@@ -1,7 +1,7 @@
1
1
  import createStorageAtomFactory from '../factories/storage.js';
2
2
  const getCacheAtom = ({ storage, key }) => {
3
3
  const createStorageAtom = createStorageAtomFactory({ storage });
4
- return createStorageAtom({ key, isSoleWriter: true });
4
+ return createStorageAtom({ key });
5
5
  };
6
6
  const enhanceAtom = (params) => {
7
7
  const { atom, logger } = params;
@@ -1,4 +1,4 @@
1
- import { EventEmitter } from './utils/types.js';
1
+ import type { EventEmitter } from './utils/types.js';
2
2
  type Params<T> = {
3
3
  emitter: EventEmitter;
4
4
  event: string;
@@ -1,4 +1,4 @@
1
- import { Atom, Keystore, KeystoreValue } from '../utils/types.js';
1
+ import type { Atom, Keystore, KeystoreValue } from '../utils/types.js';
2
2
  type Params = {
3
3
  keystore: Keystore;
4
4
  config: {
@@ -1,4 +1,4 @@
1
- import { Atom } from '../utils/types.js';
1
+ import type { Atom } from '../utils/types.js';
2
2
  type Params<T> = {
3
3
  defaultValue?: T;
4
4
  };
@@ -1,4 +1,4 @@
1
- import { ReadonlyAtom, Port } from '../utils/types.js';
1
+ import type { ReadonlyAtom, Port } from '../utils/types.js';
2
2
  type Params<T> = {
3
3
  atom: ReadonlyAtom<T>;
4
4
  port: Port<T>;
@@ -1,4 +1,4 @@
1
- import { Keystore, KeystoreValue } from '../utils/types.js';
1
+ import type { Keystore, KeystoreValue } from '../utils/types.js';
2
2
  type Params = {
3
3
  keystore: Keystore;
4
4
  config: {
@@ -1,12 +1,11 @@
1
- import { Atom } from '../utils/types.js';
2
- import { Storage } from '@exodus/storage-interface';
1
+ import type { Atom } from '../utils/types.js';
2
+ import type { Storage } from '@exodus/storage-interface';
3
3
  type FactoryParams<T> = {
4
4
  storage: Storage<T>;
5
5
  };
6
6
  type Params<D> = {
7
7
  key: string;
8
8
  defaultValue?: D;
9
- isSoleWriter?: boolean;
10
9
  };
11
10
  declare const createStorageAtomFactory: <T>({ storage }: FactoryParams<T>) => {
12
11
  (opts: Omit<Params<unknown>, "defaultValue">): Atom<T | undefined>;
@@ -1,43 +1,43 @@
1
1
  import createSimpleObserver from '../simple-observer.js';
2
2
  import enforceObservableRules from '../enforce-rules.js';
3
- import pDefer from 'p-defer';
4
3
  const createStorageAtomFactory = ({ storage }) => {
5
- function createStorageAtom({ key, defaultValue, isSoleWriter, }) {
6
- const { notify, observe } = createSimpleObserver({ enable: isSoleWriter });
4
+ function createStorageAtom({ key, defaultValue }) {
5
+ let version = 0;
6
+ const { notify, observe } = createSimpleObserver({ enable: true });
7
7
  let canUseCached = false;
8
8
  let cached;
9
- let writePromiseDefer;
9
+ let pendingWrite;
10
10
  const set = async (value) => {
11
- if (value === undefined) {
12
- writePromiseDefer = pDefer();
13
- await storage.delete(key);
14
- }
15
- else {
16
- writePromiseDefer = pDefer();
17
- await storage.set(key, value);
18
- }
19
- writePromiseDefer.resolve();
20
- if (isSoleWriter) {
11
+ version++;
12
+ pendingWrite = (async () => {
13
+ if (value === undefined) {
14
+ await storage.delete(key);
15
+ canUseCached = false;
16
+ }
17
+ else {
18
+ await storage.set(key, value);
19
+ canUseCached = true;
20
+ }
21
21
  cached = value;
22
- canUseCached = value !== undefined;
23
- await notify(value);
24
- }
22
+ pendingWrite = undefined;
23
+ })();
24
+ await pendingWrite;
25
+ await notify(value);
25
26
  };
26
27
  const get = async () => {
27
- if (canUseCached) {
28
- return cached;
29
- }
30
- if (writePromiseDefer) {
31
- await writePromiseDefer.promise;
28
+ if (pendingWrite) {
29
+ await pendingWrite;
32
30
  }
33
31
  if (canUseCached) {
34
32
  return cached;
35
33
  }
34
+ const currentVersion = version;
36
35
  const value = await storage.get(key);
37
- if (isSoleWriter) {
38
- cached = value;
39
- canUseCached = true;
36
+ if (currentVersion !== version) {
37
+ return get();
40
38
  }
39
+ cached = value;
40
+ canUseCached = true;
41
41
  return value;
42
42
  };
43
43
  return enforceObservableRules({
@@ -45,7 +45,7 @@ const createStorageAtomFactory = ({ storage }) => {
45
45
  set,
46
46
  observe,
47
47
  defaultValue,
48
- makeGetConcurrent: isSoleWriter,
48
+ makeGetNonConcurrent: true,
49
49
  });
50
50
  }
51
51
  return createStorageAtom;
@@ -1,4 +1,4 @@
1
- import { Listener } from './utils/types.js';
1
+ import type { Listener } from './utils/types.js';
2
2
  declare const createSimpleObserver: <T>({ enable }?: {
3
3
  enable?: boolean | undefined;
4
4
  }) => {
@@ -1,2 +1,2 @@
1
- import { Setter } from './types.js';
1
+ import type { Setter } from './types.js';
2
2
  export declare function isSetter<T>(value: T | Setter<T>): value is Setter<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/atoms",
3
- "version": "8.1.1",
3
+ "version": "9.0.0",
4
4
  "description": "Abstraction for encapsulating a piece of data behind a simple unified interface: get, set, observe",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -40,12 +40,13 @@
40
40
  },
41
41
  "devDependencies": {
42
42
  "@exodus/atom-tests": "^1.0.0",
43
- "@exodus/storage-memory": "^2.2.0",
43
+ "@exodus/storage-encrypted": "^1.4.1",
44
+ "@exodus/storage-memory": "^2.2.1",
44
45
  "@types/jest": "^29.5.11",
45
46
  "@types/json-stringify-safe": "^5.0.3",
46
47
  "@types/lodash": "^4.14.200",
47
48
  "@types/minimalistic-assert": "^1.0.2",
48
49
  "events": "^3.3.0"
49
50
  },
50
- "gitHead": "d64de7579b06afe91fbded3d1f7eb29950f82e5a"
51
+ "gitHead": "bbcb4c47a53d1770a36213ba77fa1f46bccc8d64"
51
52
  }