@exodus/atoms 7.3.2 → 7.4.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,18 @@
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
+ ## [7.4.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/atoms@7.3.3...@exodus/atoms@7.4.0) (2024-05-24)
7
+
8
+ ### Features
9
+
10
+ - add storage cache atom enhancer ([#7083](https://github.com/ExodusMovement/exodus-hydra/issues/7083)) ([3983ac6](https://github.com/ExodusMovement/exodus-hydra/commit/3983ac610808a0d70b106829998362dd3b52c263))
11
+
12
+ ## [7.3.3](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/atoms@7.3.2...@exodus/atoms@7.3.3) (2024-05-17)
13
+
14
+ ### Bug Fixes
15
+
16
+ - relax type for atom supplied to compute ([#6995](https://github.com/ExodusMovement/exodus-hydra/issues/6995)) ([929c5aa](https://github.com/ExodusMovement/exodus-hydra/commit/929c5aad1a2a3b05369068bdeeac7f5e84184696))
17
+
6
18
  ## [7.3.2](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/atoms@7.3.1...@exodus/atoms@7.3.2) (2024-05-09)
7
19
 
8
20
  ### Bug Fixes
@@ -1,6 +1,6 @@
1
1
  import { Atom, ReadonlyAtom } from '../utils/types.js';
2
2
  type Params<T, V> = {
3
- atom: Atom<T>;
3
+ atom: Atom<T> | ReadonlyAtom<T>;
4
4
  selector: (value: T) => V;
5
5
  };
6
6
  declare const compute: <T, V>({ atom, selector }: Params<T, V>) => ReadonlyAtom<V>;
@@ -18,11 +18,12 @@ const compute = ({ atom, selector }) => {
18
18
  return callback(selected);
19
19
  });
20
20
  };
21
- return {
21
+ const computed = {
22
22
  ...atom,
23
23
  get,
24
24
  set,
25
25
  observe,
26
26
  };
27
+ return computed;
27
28
  };
28
29
  export default compute;
@@ -0,0 +1,10 @@
1
+ import { Atom } from '../utils/types.js';
2
+ import { Storage } from '@exodus/storage-interface';
3
+ type Params<T> = {
4
+ atom: Atom<T>;
5
+ storage: Storage<T>;
6
+ key: string;
7
+ logger: Pick<Console, 'warn'>;
8
+ };
9
+ declare const withStorageCache: <T>(params: Params<T>) => Atom<T>;
10
+ export default withStorageCache;
@@ -0,0 +1,49 @@
1
+ import createStorageAtomFactory from '../factories/storage.js';
2
+ const getCacheAtom = ({ storage, key }) => {
3
+ const createStorageAtom = createStorageAtomFactory({ storage });
4
+ return createStorageAtom({ key });
5
+ };
6
+ const enhanceAtom = (params) => {
7
+ const { atom, logger } = params;
8
+ const cacheAtom = getCacheAtom(params);
9
+ let hasCache = false;
10
+ const setCache = async (fresh) => {
11
+ try {
12
+ await cacheAtom.set(fresh);
13
+ hasCache = true;
14
+ }
15
+ catch (error) {
16
+ logger.warn('Failed to write to cache atom', error);
17
+ }
18
+ };
19
+ const readCache = async () => {
20
+ try {
21
+ return (await cacheAtom.get());
22
+ }
23
+ catch (error) {
24
+ logger.warn('Failed to read from cache atom, returning fresh', error);
25
+ return readFresh();
26
+ }
27
+ };
28
+ const readFresh = async () => {
29
+ const fresh = await atom.get();
30
+ await setCache(fresh);
31
+ return fresh;
32
+ };
33
+ const set = async (value) => {
34
+ await atom.set(value);
35
+ hasCache = false;
36
+ };
37
+ const get = async () => {
38
+ return hasCache ? readCache() : readFresh();
39
+ };
40
+ const reset = async () => {
41
+ await atom.reset();
42
+ hasCache = false;
43
+ };
44
+ return { ...atom, get, set, reset };
45
+ };
46
+ const withStorageCache = (params) => {
47
+ return enhanceAtom(params);
48
+ };
49
+ export default withStorageCache;
@@ -1,6 +1,6 @@
1
- import { Atom, Port } from '../utils/types.js';
1
+ import { ReadonlyAtom, Port } from '../utils/types.js';
2
2
  type Params<T> = {
3
- atom: Atom<T>;
3
+ atom: ReadonlyAtom<T>;
4
4
  port: Port<T>;
5
5
  event: string;
6
6
  immediateRegister?: boolean;
package/lib/index.d.ts CHANGED
@@ -17,6 +17,7 @@ export { default as swallowObserverErrors } from './enhancers/swallow-observer-e
17
17
  export { default as timeoutObservers } from './enhancers/timeout-observers.js';
18
18
  export { default as optimisticNotifier } from './enhancers/optimistic-notifier.js';
19
19
  export { default as mergeWithValue } from './enhancers/merge-with-value.js';
20
+ export { default as withStorageCache } from './enhancers/with-storage-cache.js';
20
21
  export { default as waitUntil } from './effects/wait-until.js';
21
22
  export { default as enforceObservableRules } from './enforce-rules.js';
22
23
  export { default as fromEventEmitter } from './event-emitter.js';
package/lib/index.js CHANGED
@@ -17,6 +17,7 @@ export { default as swallowObserverErrors } from './enhancers/swallow-observer-e
17
17
  export { default as timeoutObservers } from './enhancers/timeout-observers.js';
18
18
  export { default as optimisticNotifier } from './enhancers/optimistic-notifier.js';
19
19
  export { default as mergeWithValue } from './enhancers/merge-with-value.js';
20
+ export { default as withStorageCache } from './enhancers/with-storage-cache.js';
20
21
  export { default as waitUntil } from './effects/wait-until.js';
21
22
  export { default as enforceObservableRules } from './enforce-rules.js';
22
23
  export { default as fromEventEmitter } from './event-emitter.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/atoms",
3
- "version": "7.3.2",
3
+ "version": "7.4.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",
@@ -45,5 +45,5 @@
45
45
  "@types/lodash": "^4.14.200",
46
46
  "@types/minimalistic-assert": "^1.0.2"
47
47
  },
48
- "gitHead": "d826264da11cdb907877d8796d882f8e488abcea"
48
+ "gitHead": "240b0901d2a97ec1fe86be7c450cc289e28c98bf"
49
49
  }