atom.io 0.33.8 → 0.33.10

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.
@@ -1,9 +1,13 @@
1
1
  import type { StateUpdate } from "atom.io"
2
2
 
3
3
  import type { ReadableState } from "."
4
- import { isChildStore } from "."
4
+ import { closeOperation, isChildStore, openOperation } from "."
5
5
  import { Future } from "./future"
6
6
  import { copyMutableIfNeeded } from "./set-state/copy-mutable-if-needed"
7
+ import {
8
+ evictDownStream,
9
+ evictDownStreamFromSelector,
10
+ } from "./set-state/evict-downstream"
7
11
  import type { Store } from "./store"
8
12
  import type { Subject } from "./subject"
9
13
 
@@ -28,15 +32,37 @@ export function cacheValue<T>(
28
32
  const currentValue = target.valueMap.get(key)
29
33
  if (currentValue instanceof Future) {
30
34
  const future = currentValue
31
- future.use(value)
35
+ if (value instanceof Promise) {
36
+ return future
37
+ }
38
+ target.valueMap.set(key, value)
39
+ return value
32
40
  }
33
41
  if (value instanceof Promise) {
34
42
  const future = new Future<T>(value)
35
43
  target.valueMap.set(key, future)
36
44
  future
37
45
  .then((resolved) => {
38
- cacheValue(target, key, resolved, subject)
39
- subject.next({ newValue: resolved, oldValue: future })
46
+ const current = target.valueMap.get(key)
47
+ if (current === future) {
48
+ cacheValue(target, key, resolved, subject)
49
+ const atom = target.atoms.get(key)
50
+ if (atom) {
51
+ openOperation(target, atom)
52
+ evictDownStream(target, atom)
53
+ closeOperation(target)
54
+ } else {
55
+ const selector =
56
+ target.writableSelectors.get(key) ??
57
+ target.readonlySelectors.get(key)
58
+ if (selector) {
59
+ openOperation(target, selector)
60
+ evictDownStreamFromSelector(target, selector)
61
+ closeOperation(target)
62
+ }
63
+ }
64
+ subject.next({ newValue: resolved, oldValue: future })
65
+ }
40
66
  })
41
67
  .catch((thrown) => {
42
68
  target.logger.error(`💥`, `state`, key, `rejected:`, thrown)
@@ -1,4 +1,4 @@
1
- import type { WritableToken } from "atom.io"
1
+ import type { ReadableToken } from "atom.io"
2
2
 
3
3
  import type { Store } from "./store"
4
4
  import { isChildStore } from "./transaction/is-root-store"
@@ -12,12 +12,12 @@ export type OperationProgress =
12
12
  done: Set<string>
13
13
  prev: Map<string, any>
14
14
  time: number
15
- token: WritableToken<any>
15
+ token: ReadableToken<any>
16
16
  }
17
17
 
18
18
  export const openOperation = (
19
19
  store: Store,
20
- token: WritableToken<any>,
20
+ token: ReadableToken<any>,
21
21
  ): number | undefined => {
22
22
  if (store.operation.open) {
23
23
  const rejectionTime = performance.now()
@@ -1,10 +1,10 @@
1
- import type { Atom } from ".."
1
+ import type { Atom, Selector } from ".."
2
2
  import { evictCachedValue } from "../caching"
3
3
  import { newest } from "../lineage"
4
4
  import { isDone, markDone } from "../operation"
5
5
  import type { Store } from "../store"
6
6
 
7
- export const evictDownStream = <T>(store: Store, atom: Atom<T>): void => {
7
+ export function evictDownStream(store: Store, atom: Atom<any>): void {
8
8
  const target = newest(store)
9
9
  const downstreamKeys = target.selectorAtoms.getRelatedKeys(atom.key)
10
10
  target.logger.info(
@@ -34,3 +34,22 @@ export const evictDownStream = <T>(store: Store, atom: Atom<T>): void => {
34
34
  }
35
35
  }
36
36
  }
37
+
38
+ export function evictDownStreamFromSelector(
39
+ store: Store,
40
+ selector: Selector<any>,
41
+ ): void {
42
+ const target = newest(store)
43
+ const relationEntries = target.selectorGraph
44
+ .getRelationEntries({
45
+ upstreamSelectorKey: selector.key,
46
+ })
47
+ .filter(([_, { source }]) => source === selector.key)
48
+ for (const [downstreamSelectorKey] of relationEntries) {
49
+ if (isDone(target, downstreamSelectorKey)) {
50
+ continue
51
+ }
52
+ evictCachedValue(downstreamSelectorKey, target)
53
+ markDone(target, downstreamSelectorKey)
54
+ }
55
+ }