atom.io 0.33.14 → 0.33.16

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.
@@ -12,7 +12,6 @@ import type { Store } from "../store"
12
12
  import { deposit } from "../store"
13
13
  import { Subject } from "../subject"
14
14
  import { subscribeToState } from "../subscribe"
15
- import { markAtomAsDefault } from "./is-default"
16
15
 
17
16
  export function createRegularAtom<T>(
18
17
  store: Store,
@@ -56,7 +55,6 @@ export function createRegularAtom<T>(
56
55
  initialValue = def()
57
56
  }
58
57
  target.atoms.set(key, newAtom)
59
- markAtomAsDefault(store, key)
60
58
  cacheValue(target, key, initialValue, subject)
61
59
  const token = deposit(newAtom)
62
60
  if (options.effects) {
@@ -1,4 +1,3 @@
1
1
  export * from "./create-regular-atom"
2
2
  export * from "./create-standalone-atom"
3
3
  export * from "./dispose-atom"
4
- export * from "./is-default"
@@ -51,7 +51,7 @@ export function createRegularAtomFamily<T, K extends Canonical>(
51
51
  const def = options.default
52
52
  const individualOptions: RegularAtomOptions<T> = {
53
53
  key: fullKey,
54
- default: def instanceof Function ? def(key) : def,
54
+ default: def instanceof Function ? () => def(key) : def,
55
55
  }
56
56
  if (options.effects) {
57
57
  individualOptions.effects = options.effects(key)
@@ -9,7 +9,6 @@ import { selectJson } from "atom.io/json"
9
9
 
10
10
  import type { MutableAtom } from ".."
11
11
  import { cacheValue, setIntoStore } from ".."
12
- import { markAtomAsDefault } from "../atom"
13
12
  import { newest } from "../lineage"
14
13
  import { deposit, type Store } from "../store"
15
14
  import { Subject } from "../subject"
@@ -59,7 +58,6 @@ export function createMutableAtom<
59
58
  }
60
59
  const initialValue = def()
61
60
  target.atoms.set(newAtom.key, newAtom)
62
- markAtomAsDefault(store, key)
63
61
  cacheValue(target, key, initialValue, subject)
64
62
  const token = deposit(newAtom)
65
63
  if (options.effects) {
@@ -1,4 +1,4 @@
1
- import type { Selector, Store } from ".."
1
+ import type { Atom, Selector, Store } from ".."
2
2
  import type { AtomKey, StateKey } from "../keys"
3
3
  import { isAtomKey } from "../keys"
4
4
  import { getSelectorDependencyKeys } from "./get-selector-dependency-keys"
@@ -37,13 +37,15 @@ export const traceSelectorAtoms = (
37
37
  export const traceAllSelectorAtoms = (
38
38
  selector: Selector<any>,
39
39
  store: Store,
40
- ): AtomKey<unknown>[] => {
40
+ ): Atom<unknown>[] => {
41
41
  const selectorKey = selector.key
42
42
  const directDependencyKeys = getSelectorDependencyKeys(store, selectorKey)
43
43
  const covered = new Set<string>()
44
- return directDependencyKeys.flatMap((depKey) =>
45
- isAtomKey(store, depKey)
46
- ? depKey
47
- : traceSelectorAtoms(store, depKey, covered),
48
- )
44
+ return directDependencyKeys
45
+ .flatMap((depKey) =>
46
+ isAtomKey(store, depKey)
47
+ ? depKey
48
+ : traceSelectorAtoms(store, depKey, covered),
49
+ )
50
+ .map((atomKey) => store.atoms.get(atomKey) as Atom<unknown>)
49
51
  }
@@ -1,3 +1,5 @@
1
1
  export * from "./become"
2
+ export * from "./reset-atom-or-selector"
3
+ export * from "./reset-in-store"
2
4
  export * from "./set-atom-or-selector"
3
5
  export * from "./set-into-store"
@@ -0,0 +1,32 @@
1
+ import { type Atom, traceAllSelectorAtoms, type WritableState } from ".."
2
+ import type { Store } from "../store"
3
+ import { setAtom } from "./set-atom"
4
+
5
+ function resetAtom(store: Store, state: Atom<any>) {
6
+ let def = state.default
7
+ if (def instanceof Function) {
8
+ def = def()
9
+ }
10
+ setAtom(store, state, def)
11
+ }
12
+
13
+ export function resetAtomOrSelector(
14
+ store: Store,
15
+ state: WritableState<any>,
16
+ ): void {
17
+ switch (state.type) {
18
+ case `atom`:
19
+ case `mutable_atom`:
20
+ resetAtom(store, state)
21
+ break
22
+ case `writable_pure_selector`:
23
+ case `writable_held_selector`:
24
+ {
25
+ const atoms = traceAllSelectorAtoms(state, store)
26
+ for (const atom of atoms) {
27
+ resetAtom(store, atom)
28
+ }
29
+ }
30
+ break
31
+ }
32
+ }
@@ -0,0 +1,78 @@
1
+ import type { WritableFamilyToken, WritableToken } from "atom.io"
2
+ import { type Canonical, parseJson } from "atom.io/json"
3
+
4
+ import { findInStore } from "../families"
5
+ import { getFamilyOfToken } from "../families/get-family-of-token"
6
+ import { closeOperation, openOperation } from "../operation"
7
+ import type { Store } from "../store"
8
+ import { withdraw } from "../store"
9
+ import { resetAtomOrSelector } from "./reset-atom-or-selector"
10
+
11
+ export function resetInStore(store: Store, token: WritableToken<any>): void
12
+
13
+ export function resetInStore<K extends Canonical>(
14
+ store: Store,
15
+ token: WritableFamilyToken<any, K>,
16
+ key: K,
17
+ ): void
18
+
19
+ export function resetInStore<T>(
20
+ store: Store,
21
+ ...params:
22
+ | [token: WritableFamilyToken<T, Canonical>, key: Canonical]
23
+ | [token: WritableToken<T>]
24
+ ): void {
25
+ let token: WritableToken<T>
26
+ let family: WritableFamilyToken<T, Canonical> | null
27
+ let key: Canonical | null
28
+ if (params.length === 1) {
29
+ token = params[0]
30
+ family = getFamilyOfToken(store, token) ?? null
31
+ if (family) {
32
+ key = token.family ? parseJson(token.family.subKey) : null
33
+ token = findInStore(store, family, key)
34
+ }
35
+ } else {
36
+ family = params[0]
37
+ key = params[1]
38
+ token = findInStore(store, family, key)
39
+ }
40
+
41
+ if (`counterfeit` in token && `family` in token) {
42
+ const subKey = token.family.subKey
43
+ const disposal = store.disposalTraces.buffer.find(
44
+ (item) => item?.key === subKey,
45
+ )
46
+ store.logger.error(
47
+ `❌`,
48
+ token.type,
49
+ token.key,
50
+ `could not be reset because it was not found in the store "${store.config.name}".`,
51
+ disposal
52
+ ? `This state was previously disposed:\n${disposal.trace}`
53
+ : `No previous disposal trace was found.`,
54
+ )
55
+ return
56
+ }
57
+
58
+ const rejectionTime = openOperation(store, token)
59
+ if (rejectionTime) {
60
+ const unsubscribe = store.on.operationClose.subscribe(
61
+ `waiting to reset "${token.key}" at T-${rejectionTime}`,
62
+ () => {
63
+ unsubscribe()
64
+ store.logger.info(
65
+ `🟢`,
66
+ token.type,
67
+ token.key,
68
+ `resuming deferred resetState from T-${rejectionTime}`,
69
+ )
70
+ resetInStore(store, token)
71
+ },
72
+ )
73
+ return
74
+ }
75
+ const state = withdraw(store, token)
76
+ resetAtomOrSelector(store, state)
77
+ closeOperation(store)
78
+ }
@@ -1,7 +1,6 @@
1
1
  import type { KeyedStateUpdate } from "atom.io"
2
2
 
3
3
  import type { Atom, Store } from ".."
4
- import { isAtomDefault, markAtomAsNotDefault } from "../atom"
5
4
  import { cacheValue } from "../caching"
6
5
  import { readOrComputeValue } from "../get-state/read-or-compute-value"
7
6
  import { isTransceiver, type Transceiver } from "../mutable"
@@ -27,9 +26,6 @@ export const setAtom = <T>(
27
26
  newValue = become(next)(newValue)
28
27
  target.logger.info(`📝`, `atom`, atom.key, `set to`, newValue)
29
28
  newValue = cacheValue(target, atom.key, newValue, atom.subject)
30
- if (isAtomDefault(target, atom.key)) {
31
- markAtomAsNotDefault(target, atom.key)
32
- }
33
29
  markDone(target, atom.key)
34
30
  evictDownStream(target, atom)
35
31
  const update = { oldValue, newValue }
@@ -11,13 +11,7 @@ export const subscribeToRootAtoms = <T>(
11
11
  ): (() => void)[] => {
12
12
  const target = newest(store)
13
13
  const dependencySubscriptions = traceAllSelectorAtoms(selector, store).map(
14
- (atomKey) => {
15
- const atom = target.atoms.get(atomKey)
16
- if (atom === undefined) {
17
- throw new Error(
18
- `Atom "${atomKey}", a dependency of selector "${selector.key}", not found in store "${store.config.name}".`,
19
- )
20
- }
14
+ (atom) => {
21
15
  return atom.subject.subscribe(
22
16
  `${selector.type}:${selector.key}`,
23
17
  (atomChange) => {
@@ -26,7 +20,7 @@ export const subscribeToRootAtoms = <T>(
26
20
  selector.type,
27
21
  selector.key,
28
22
  `root`,
29
- atomKey,
23
+ atom.key,
30
24
  `went`,
31
25
  atomChange.oldValue,
32
26
  `->`,
@@ -1,4 +1,10 @@
1
- import type { disposeState, findState, getState, setState } from "atom.io"
1
+ import type {
2
+ disposeState,
3
+ findState,
4
+ getState,
5
+ resetState,
6
+ setState,
7
+ } from "atom.io"
2
8
 
3
9
  import { arbitrary } from "../arbitrary"
4
10
  import { disposeFromStore, findInStore } from "../families"
@@ -8,7 +14,7 @@ import { Junction } from "../junction"
8
14
  import { LazyMap } from "../lazy-map"
9
15
  import { newest } from "../lineage"
10
16
  import { getJsonToken } from "../mutable"
11
- import { setIntoStore } from "../set-state"
17
+ import { resetInStore, setIntoStore } from "../set-state"
12
18
  import type { Store } from "../store"
13
19
  import type { Func } from "../utility-types"
14
20
  import type { TransactionProgress } from "."
@@ -77,6 +83,9 @@ export const buildTransaction = (
77
83
  set: ((...ps: Parameters<typeof setState>) => {
78
84
  setIntoStore(child, ...ps)
79
85
  }) as typeof setState,
86
+ reset: ((...ps: Parameters<typeof resetState>) => {
87
+ resetInStore(child, ...ps)
88
+ }) as typeof resetState,
80
89
  run: (token, identifier = arbitrary()) =>
81
90
  actUponStore(child, token, identifier),
82
91
  find: ((...ps: Parameters<typeof findState>) =>
package/src/main/index.ts CHANGED
@@ -16,6 +16,7 @@ export * from "./get-state"
16
16
  export * from "./join"
17
17
  export * from "./logger"
18
18
  export * from "./realm"
19
+ export * from "./reset-state"
19
20
  export * from "./selector"
20
21
  export * from "./set-state"
21
22
  export * from "./silo"
@@ -0,0 +1,35 @@
1
+ import * as Internal from "atom.io/internal"
2
+ import type { Canonical } from "atom.io/json"
3
+
4
+ import type { WritableFamilyToken, WritableToken } from "."
5
+
6
+ /**
7
+ * @public
8
+ * Set the value of a state into the implicit store back to its default value.
9
+ * @param token - An atom or writable selector token.
10
+ * @overload Default
11
+ * @default
12
+ */
13
+ export function resetState(token: WritableToken<any>): void
14
+ /**
15
+ * @public
16
+ * Set the value of a state into the implicit store back to its default value.
17
+ * @param token - An atom family or writable selector family token.
18
+ * @param key - The unique key of the state to set.
19
+ * @overload Streamlined
20
+ */
21
+ export function resetState<K extends Canonical>(
22
+ token: WritableFamilyToken<any, K>,
23
+ key: K,
24
+ ): void
25
+ export function resetState(
26
+ ...params:
27
+ | [token: WritableFamilyToken<any, Canonical>, key: Canonical]
28
+ | [token: WritableToken<any>]
29
+ ): void {
30
+ if (params.length === 2) {
31
+ Internal.resetInStore(Internal.IMPLICIT.STORE, ...params)
32
+ } else {
33
+ Internal.resetInStore(Internal.IMPLICIT.STORE, ...params)
34
+ }
35
+ }
package/src/main/silo.ts CHANGED
@@ -13,6 +13,7 @@ import {
13
13
  getFromStore,
14
14
  IMPLICIT,
15
15
  installIntoStore,
16
+ resetInStore,
16
17
  setIntoStore,
17
18
  Store,
18
19
  subscribeInStore,
@@ -30,6 +31,7 @@ import type {
30
31
  undo,
31
32
  } from "."
32
33
  import type { atom, atomFamily } from "./atom"
34
+ import type { resetState } from "./reset-state"
33
35
  import type { selector, selectorFamily } from "./selector"
34
36
  import type { runTransaction, transaction } from "./transaction"
35
37
 
@@ -44,6 +46,7 @@ export class Silo {
44
46
  public findState: typeof findState
45
47
  public getState: typeof getState
46
48
  public setState: typeof setState
49
+ public resetState: typeof resetState
47
50
  public disposeState: typeof disposeState
48
51
  public subscribe: typeof subscribe
49
52
  public undo: typeof undo
@@ -70,6 +73,9 @@ export class Silo {
70
73
  this.setState = ((...params: Parameters<typeof setState>) => {
71
74
  setIntoStore(s, ...params)
72
75
  }) as typeof setState
76
+ this.resetState = ((...params: Parameters<typeof resetState>) => {
77
+ resetInStore(s, ...params)
78
+ }) as typeof resetState
73
79
  this.disposeState = ((...params: Parameters<typeof disposeState>) => {
74
80
  disposeFromStore(s, ...params)
75
81
  }) as typeof disposeState
@@ -16,6 +16,7 @@ import type {
16
16
  TokenType,
17
17
  WritablePureSelectorToken,
18
18
  } from "."
19
+ import type { resetState } from "./reset-state"
19
20
 
20
21
  export type TransactionToken<F extends Func> = {
21
22
  key: string
@@ -93,6 +94,7 @@ export type SetterToolkit = Readonly<{
93
94
  export type ActorToolkit = Readonly<{
94
95
  get: typeof getState
95
96
  set: typeof setState
97
+ reset: typeof resetState
96
98
  find: typeof findState
97
99
  json: <T extends Transceiver<any>, J extends Json.Serializable>(
98
100
  state: MutableAtomToken<T, J>,
@@ -1,18 +0,0 @@
1
- import { newest } from "../lineage"
2
- import type { Store } from "../store"
3
-
4
- export const isAtomDefault = (store: Store, key: string): boolean => {
5
- const core = newest(store)
6
- return core.atomsThatAreDefault.has(key)
7
- }
8
-
9
- export const markAtomAsDefault = (store: Store, key: string): void => {
10
- const core = newest(store)
11
- core.atomsThatAreDefault = new Set(core.atomsThatAreDefault).add(key)
12
- }
13
-
14
- export const markAtomAsNotDefault = (store: Store, key: string): void => {
15
- const core = newest(store)
16
- core.atomsThatAreDefault = new Set(newest(store).atomsThatAreDefault)
17
- core.atomsThatAreDefault.delete(key)
18
- }