atom.io 0.6.2 → 0.6.4

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 (117) hide show
  1. package/dist/index.d.mts +27 -15
  2. package/dist/index.d.ts +27 -15
  3. package/dist/index.js +44 -24
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +44 -24
  6. package/dist/index.mjs.map +1 -1
  7. package/json/dist/index.d.mts +18 -0
  8. package/json/dist/index.d.ts +18 -0
  9. package/json/dist/index.js +51 -0
  10. package/json/dist/index.js.map +1 -0
  11. package/json/dist/index.mjs +15 -0
  12. package/json/dist/index.mjs.map +1 -0
  13. package/json/package.json +13 -13
  14. package/package.json +31 -12
  15. package/react/dist/index.d.mts +24 -0
  16. package/react/dist/index.d.ts +24 -0
  17. package/react/dist/index.js +87 -0
  18. package/react/dist/index.js.map +1 -0
  19. package/react/dist/index.mjs +45 -0
  20. package/react/dist/index.mjs.map +1 -0
  21. package/react/package.json +13 -13
  22. package/react-devtools/dist/index.css +26 -0
  23. package/react-devtools/dist/index.css.map +1 -0
  24. package/react-devtools/dist/index.d.mts +15 -0
  25. package/react-devtools/dist/index.d.ts +15 -0
  26. package/react-devtools/dist/index.js +2108 -0
  27. package/react-devtools/dist/index.js.map +1 -0
  28. package/react-devtools/dist/index.mjs +2080 -0
  29. package/react-devtools/dist/index.mjs.map +1 -0
  30. package/react-devtools/package.json +13 -13
  31. package/realtime/dist/index.d.mts +27 -0
  32. package/realtime/dist/index.d.ts +27 -0
  33. package/realtime/dist/index.js +191 -0
  34. package/realtime/dist/index.js.map +1 -0
  35. package/realtime/dist/index.mjs +152 -0
  36. package/realtime/dist/index.mjs.map +1 -0
  37. package/realtime/package.json +13 -13
  38. package/realtime-react/dist/index.d.mts +45 -0
  39. package/realtime-react/dist/index.d.ts +45 -0
  40. package/realtime-react/dist/index.js +217 -0
  41. package/realtime-react/dist/index.js.map +1 -0
  42. package/realtime-react/dist/index.mjs +172 -0
  43. package/realtime-react/dist/index.mjs.map +1 -0
  44. package/realtime-react/package.json +13 -13
  45. package/realtime-testing/dist/index.d.mts +49 -0
  46. package/realtime-testing/dist/index.d.ts +49 -0
  47. package/realtime-testing/dist/index.js +165 -0
  48. package/realtime-testing/dist/index.js.map +1 -0
  49. package/realtime-testing/dist/index.mjs +129 -0
  50. package/realtime-testing/dist/index.mjs.map +1 -0
  51. package/realtime-testing/package.json +15 -0
  52. package/src/atom.ts +16 -17
  53. package/src/index.ts +59 -59
  54. package/src/internal/atom-internal.ts +37 -37
  55. package/src/internal/families-internal.ts +115 -116
  56. package/src/internal/get.ts +83 -83
  57. package/src/internal/index.ts +1 -0
  58. package/src/internal/is-default.ts +17 -17
  59. package/src/internal/meta/attach-meta.ts +7 -7
  60. package/src/internal/meta/meta-state.ts +115 -115
  61. package/src/internal/operation.ts +93 -93
  62. package/src/internal/selector/create-read-write-selector.ts +47 -47
  63. package/src/internal/selector/create-readonly-selector.ts +38 -38
  64. package/src/internal/selector/lookup-selector-sources.ts +9 -9
  65. package/src/internal/selector/register-selector.ts +44 -44
  66. package/src/internal/selector/trace-selector-atoms.ts +30 -30
  67. package/src/internal/selector/update-selector-atoms.ts +25 -25
  68. package/src/internal/selector-internal.ts +38 -39
  69. package/src/internal/set.ts +78 -78
  70. package/src/internal/store.ts +119 -119
  71. package/src/internal/subject.ts +24 -0
  72. package/src/internal/subscribe-internal.ts +62 -62
  73. package/src/internal/time-travel-internal.ts +76 -76
  74. package/src/internal/timeline/add-atom-to-timeline.ts +158 -153
  75. package/src/internal/timeline-internal.ts +81 -82
  76. package/src/internal/transaction/abort-transaction.ts +8 -8
  77. package/src/internal/transaction/apply-transaction.ts +41 -41
  78. package/src/internal/transaction/build-transaction.ts +28 -28
  79. package/src/internal/transaction/index.ts +7 -7
  80. package/src/internal/transaction/redo-transaction.ts +13 -13
  81. package/src/internal/transaction/undo-transaction.ts +13 -13
  82. package/src/internal/transaction-internal.ts +49 -49
  83. package/src/json/select-json.ts +12 -12
  84. package/src/logger.ts +30 -30
  85. package/src/react/store-context.tsx +5 -6
  86. package/src/react/store-hooks.ts +19 -20
  87. package/src/react-devtools/AtomIODevtools.tsx +85 -85
  88. package/src/react-devtools/StateEditor.tsx +54 -55
  89. package/src/react-devtools/TokenList.tsx +49 -45
  90. package/src/react-explorer/AtomIOExplorer.tsx +198 -187
  91. package/src/react-explorer/explorer-effects.ts +11 -11
  92. package/src/react-explorer/explorer-states.ts +186 -193
  93. package/src/react-explorer/index.ts +11 -11
  94. package/src/react-explorer/space-states.ts +48 -50
  95. package/src/react-explorer/view-states.ts +25 -25
  96. package/src/realtime/hook-composition/expose-family.ts +81 -81
  97. package/src/realtime/hook-composition/expose-single.ts +26 -26
  98. package/src/realtime/hook-composition/expose-timeline.ts +60 -0
  99. package/src/realtime/hook-composition/index.ts +2 -2
  100. package/src/realtime/hook-composition/receive-state.ts +18 -18
  101. package/src/realtime/hook-composition/receive-transaction.ts +8 -8
  102. package/src/realtime-react/realtime-context.tsx +18 -19
  103. package/src/realtime-react/realtime-hooks.ts +17 -17
  104. package/src/realtime-react/realtime-state.ts +4 -4
  105. package/src/realtime-react/use-pull-family-member.ts +16 -17
  106. package/src/realtime-react/use-pull-family.ts +14 -15
  107. package/src/realtime-react/use-pull.ts +13 -14
  108. package/src/realtime-react/use-push.ts +16 -17
  109. package/src/realtime-react/use-server-action.ts +22 -23
  110. package/src/realtime-testing/index.ts +1 -0
  111. package/src/realtime-testing/setup-realtime-test.tsx +159 -0
  112. package/src/selector.ts +26 -27
  113. package/src/silo.ts +38 -38
  114. package/src/subscribe.ts +68 -68
  115. package/src/timeline.ts +13 -13
  116. package/src/transaction.ts +28 -28
  117. package/src/web-effects/storage.ts +17 -17
@@ -1,106 +1,105 @@
1
1
  import HAMT from "hamt_plus"
2
- import * as Rx from "rxjs"
3
2
 
4
3
  import type { ƒn } from "~/packages/anvl/src/function"
5
4
 
6
5
  import type { Store } from "."
7
- import { target, IMPLICIT } from "."
6
+ import { Subject, target, IMPLICIT } from "."
8
7
  import { addAtomToTimeline } from "./timeline/add-atom-to-timeline"
9
8
  import type {
10
- StateUpdate,
11
- TimelineOptions,
12
- TimelineToken,
13
- TimelineUpdate,
14
- TransactionUpdate,
9
+ StateUpdate,
10
+ TimelineOptions,
11
+ TimelineToken,
12
+ TimelineUpdate,
13
+ TransactionUpdate,
15
14
  } from ".."
16
15
 
17
16
  export type TimelineAtomUpdate = StateUpdate<unknown> & {
18
- key: string
19
- type: `atom_update`
20
- timestamp: number
17
+ key: string
18
+ type: `atom_update`
19
+ timestamp: number
21
20
  }
22
21
  export type TimelineSelectorUpdate = {
23
- key: string
24
- type: `selector_update`
25
- timestamp: number
26
- atomUpdates: Omit<TimelineAtomUpdate, `timestamp`>[]
22
+ key: string
23
+ type: `selector_update`
24
+ timestamp: number
25
+ atomUpdates: Omit<TimelineAtomUpdate, `timestamp`>[]
27
26
  }
28
27
  export type TimelineTransactionUpdate = TransactionUpdate<ƒn> & {
29
- key: string
30
- type: `transaction_update`
31
- timestamp: number
28
+ key: string
29
+ type: `transaction_update`
30
+ timestamp: number
32
31
  }
33
32
 
34
33
  export type Timeline = {
35
- key: string
36
- at: number
37
- timeTraveling: boolean
38
- history: TimelineUpdate[]
39
- selectorTime: number | null
40
- transactionKey: string | null
41
- install: (store: Store) => void
42
- subject: Rx.Subject<
43
- TimelineAtomUpdate | TimelineSelectorUpdate | TimelineTransactionUpdate
44
- >
34
+ key: string
35
+ at: number
36
+ timeTraveling: boolean
37
+ history: TimelineUpdate[]
38
+ selectorTime: number | null
39
+ transactionKey: string | null
40
+ install: (store: Store) => void
41
+ subject: Subject<
42
+ TimelineAtomUpdate | TimelineSelectorUpdate | TimelineTransactionUpdate
43
+ >
45
44
  }
46
45
 
47
46
  export function timeline__INTERNAL(
48
- options: TimelineOptions,
49
- store: Store = IMPLICIT.STORE,
50
- data: Timeline | null = null
47
+ options: TimelineOptions,
48
+ store: Store = IMPLICIT.STORE,
49
+ data: Timeline | null = null,
51
50
  ): TimelineToken {
52
- const tl: Timeline = {
53
- key: options.key,
54
- at: 0,
55
- timeTraveling: false,
56
- selectorTime: null,
57
- transactionKey: null,
58
- ...data,
59
- history: data?.history.map((update) => ({ ...update })) ?? [],
60
- install: (store) => timeline__INTERNAL(options, store, tl),
61
- subject: new Rx.Subject(),
62
- }
51
+ const tl: Timeline = {
52
+ key: options.key,
53
+ at: 0,
54
+ timeTraveling: false,
55
+ selectorTime: null,
56
+ transactionKey: null,
57
+ ...data,
58
+ history: data?.history.map((update) => ({ ...update })) ?? [],
59
+ install: (store) => timeline__INTERNAL(options, store, tl),
60
+ subject: new Subject(),
61
+ }
63
62
 
64
- const core = target(store)
65
- for (const tokenOrFamily of options.atoms) {
66
- const timelineKey = core.timelineAtoms.getRelatedId(tokenOrFamily.key)
67
- if (timelineKey) {
68
- store.config.logger?.error(
69
- `❌ Failed to add atom "${tokenOrFamily.key}" to timeline "${options.key}" because it belongs to timeline "${timelineKey}"`
70
- )
71
- continue
72
- }
73
- if (tokenOrFamily.type === `atom_family`) {
74
- const family = tokenOrFamily
75
- family.subject.subscribe((token) =>
76
- addAtomToTimeline(token, options.atoms, tl, store)
77
- )
78
- } else {
79
- const token = tokenOrFamily
80
- if (`family` in token && token.family) {
81
- const familyTimelineKey = core.timelineAtoms.getRelatedId(
82
- token.family.key
83
- )
84
- if (familyTimelineKey) {
85
- store.config.logger?.error(
86
- `❌ Failed to add atom "${token.key}" to timeline "${options.key}" because its family "${token.family.key}" belongs to timeline "${familyTimelineKey}"`
87
- )
88
- continue
89
- }
90
- }
91
- addAtomToTimeline(token, options.atoms, tl, store)
92
- }
93
- core.timelineAtoms = core.timelineAtoms.set({
94
- atomKey: tokenOrFamily.key,
95
- timelineKey: options.key,
96
- })
97
- }
63
+ const core = target(store)
64
+ for (const tokenOrFamily of options.atoms) {
65
+ const timelineKey = core.timelineAtoms.getRelatedId(tokenOrFamily.key)
66
+ if (timelineKey) {
67
+ store.config.logger?.error(
68
+ `❌ Failed to add atom "${tokenOrFamily.key}" to timeline "${options.key}" because it belongs to timeline "${timelineKey}"`,
69
+ )
70
+ continue
71
+ }
72
+ if (tokenOrFamily.type === `atom_family`) {
73
+ const family = tokenOrFamily
74
+ family.subject.subscribe((token) =>
75
+ addAtomToTimeline(token, options.atoms, tl, store),
76
+ )
77
+ } else {
78
+ const token = tokenOrFamily
79
+ if (`family` in token && token.family) {
80
+ const familyTimelineKey = core.timelineAtoms.getRelatedId(
81
+ token.family.key,
82
+ )
83
+ if (familyTimelineKey) {
84
+ store.config.logger?.error(
85
+ `❌ Failed to add atom "${token.key}" to timeline "${options.key}" because its family "${token.family.key}" belongs to timeline "${familyTimelineKey}"`,
86
+ )
87
+ continue
88
+ }
89
+ }
90
+ addAtomToTimeline(token, options.atoms, tl, store)
91
+ }
92
+ core.timelineAtoms = core.timelineAtoms.set({
93
+ atomKey: tokenOrFamily.key,
94
+ timelineKey: options.key,
95
+ })
96
+ }
98
97
 
99
- store.timelines = HAMT.set(options.key, tl, store.timelines)
100
- const token: TimelineToken = {
101
- key: options.key,
102
- type: `timeline`,
103
- }
104
- store.subject.timelineCreation.next(token)
105
- return token
98
+ store.timelines = HAMT.set(options.key, tl, store.timelines)
99
+ const token: TimelineToken = {
100
+ key: options.key,
101
+ type: `timeline`,
102
+ }
103
+ store.subject.timelineCreation.next(token)
104
+ return token
106
105
  }
@@ -1,12 +1,12 @@
1
1
  import type { Store } from ".."
2
2
 
3
3
  export const abortTransaction = (store: Store): void => {
4
- if (store.transactionStatus.phase === `idle`) {
5
- store.config.logger?.warn(
6
- `abortTransaction called outside of a transaction. This is probably a bug.`
7
- )
8
- return
9
- }
10
- store.transactionStatus = { phase: `idle` }
11
- store.config.logger?.info(`🪂`, `transaction fail`)
4
+ if (store.transactionStatus.phase === `idle`) {
5
+ store.config.logger?.warn(
6
+ `abortTransaction called outside of a transaction. This is probably a bug.`,
7
+ )
8
+ return
9
+ }
10
+ store.transactionStatus = { phase: `idle` }
11
+ store.config.logger?.info(`🪂`, `transaction fail`)
12
12
  }
@@ -8,47 +8,47 @@ import type { AtomToken } from "../.."
8
8
  import { setState } from "../.."
9
9
 
10
10
  export const applyTransaction = <ƒ extends ƒn>(
11
- output: ReturnType<ƒ>,
12
- store: Store
11
+ output: ReturnType<ƒ>,
12
+ store: Store,
13
13
  ): void => {
14
- if (store.transactionStatus.phase !== `building`) {
15
- store.config.logger?.warn(
16
- `abortTransaction called outside of a transaction. This is probably a bug.`
17
- )
18
- return
19
- }
20
- store.config.logger?.info(
21
- `🛃 apply transaction "${store.transactionStatus.key}"`
22
- )
23
- store.transactionStatus.phase = `applying`
24
- store.transactionStatus.output = output
25
- const { atomUpdates } = store.transactionStatus
14
+ if (store.transactionStatus.phase !== `building`) {
15
+ store.config.logger?.warn(
16
+ `abortTransaction called outside of a transaction. This is probably a bug.`,
17
+ )
18
+ return
19
+ }
20
+ store.config.logger?.info(
21
+ `🛃 apply transaction "${store.transactionStatus.key}"`,
22
+ )
23
+ store.transactionStatus.phase = `applying`
24
+ store.transactionStatus.output = output
25
+ const { atomUpdates } = store.transactionStatus
26
26
 
27
- for (const { key, newValue } of atomUpdates) {
28
- const token: AtomToken<unknown> = { key, type: `atom` }
29
- if (!HAMT.has(token.key, store.valueMap)) {
30
- const newAtom = HAMT.get(token.key, store.transactionStatus.core.atoms)
31
- store.atoms = HAMT.set(newAtom.key, newAtom, store.atoms)
32
- store.valueMap = HAMT.set(newAtom.key, newAtom.default, store.valueMap)
33
- store.config.logger?.info(`🔧`, `add atom "${newAtom.key}"`)
34
- }
35
- setState(token, newValue, store)
36
- }
37
- const myTransaction = withdraw<ƒ>(
38
- { key: store.transactionStatus.key, type: `transaction` },
39
- store
40
- )
41
- if (myTransaction === null) {
42
- throw new Error(
43
- `Transaction "${store.transactionStatus.key}" not found. Absurd. How is this running?`
44
- )
45
- }
46
- myTransaction.subject.next({
47
- key: store.transactionStatus.key,
48
- atomUpdates,
49
- output,
50
- params: store.transactionStatus.params as Parameters<ƒ>,
51
- })
52
- store.transactionStatus = { phase: `idle` }
53
- store.config.logger?.info(`🛬`, `transaction done`)
27
+ for (const { key, newValue } of atomUpdates) {
28
+ const token: AtomToken<unknown> = { key, type: `atom` }
29
+ if (!HAMT.has(token.key, store.valueMap)) {
30
+ const newAtom = HAMT.get(token.key, store.transactionStatus.core.atoms)
31
+ store.atoms = HAMT.set(newAtom.key, newAtom, store.atoms)
32
+ store.valueMap = HAMT.set(newAtom.key, newAtom.default, store.valueMap)
33
+ store.config.logger?.info(`🔧`, `add atom "${newAtom.key}"`)
34
+ }
35
+ setState(token, newValue, store)
36
+ }
37
+ const myTransaction = withdraw<ƒ>(
38
+ { key: store.transactionStatus.key, type: `transaction` },
39
+ store,
40
+ )
41
+ if (myTransaction === null) {
42
+ throw new Error(
43
+ `Transaction "${store.transactionStatus.key}" not found. Absurd. How is this running?`,
44
+ )
45
+ }
46
+ myTransaction.subject.next({
47
+ key: store.transactionStatus.key,
48
+ atomUpdates,
49
+ output,
50
+ params: store.transactionStatus.params as Parameters<ƒ>,
51
+ })
52
+ store.transactionStatus = { phase: `idle` }
53
+ store.config.logger?.info(`🛬`, `transaction done`)
54
54
  }
@@ -1,33 +1,33 @@
1
1
  import type { Store } from ".."
2
2
 
3
3
  export const buildTransaction = (
4
- key: string,
5
- params: any[],
6
- store: Store
4
+ key: string,
5
+ params: any[],
6
+ store: Store,
7
7
  ): void => {
8
- store.transactionStatus = {
9
- key,
10
- phase: `building`,
11
- time: Date.now(),
12
- core: {
13
- atoms: store.atoms,
14
- atomsThatAreDefault: store.atomsThatAreDefault,
15
- operation: { open: false },
16
- readonlySelectors: store.readonlySelectors,
17
- timelines: store.timelines,
18
- timelineAtoms: store.timelineAtoms,
19
- transactions: store.transactions,
20
- selectorAtoms: store.selectorAtoms,
21
- selectorGraph: store.selectorGraph,
22
- selectors: store.selectors,
23
- valueMap: store.valueMap,
24
- },
25
- atomUpdates: [],
26
- params,
27
- output: undefined,
28
- }
29
- store.config.logger?.info(
30
- `🛫`,
31
- `transaction "${key}" started in store "${store.config.name}"`
32
- )
8
+ store.transactionStatus = {
9
+ key,
10
+ phase: `building`,
11
+ time: Date.now(),
12
+ core: {
13
+ atoms: store.atoms,
14
+ atomsThatAreDefault: store.atomsThatAreDefault,
15
+ operation: { open: false },
16
+ readonlySelectors: store.readonlySelectors,
17
+ timelines: store.timelines,
18
+ timelineAtoms: store.timelineAtoms,
19
+ transactions: store.transactions,
20
+ selectorAtoms: store.selectorAtoms,
21
+ selectorGraph: store.selectorGraph,
22
+ selectors: store.selectors,
23
+ valueMap: store.valueMap,
24
+ },
25
+ atomUpdates: [],
26
+ params,
27
+ output: undefined,
28
+ }
29
+ store.config.logger?.info(
30
+ `🛫`,
31
+ `transaction "${key}" started in store "${store.config.name}"`,
32
+ )
33
33
  }
@@ -10,16 +10,16 @@ export * from "./redo-transaction"
10
10
  export * from "./undo-transaction"
11
11
 
12
12
  export const TRANSACTION_PHASES = [`idle`, `building`, `applying`] as const
13
- export type TransactionPhase = (typeof TRANSACTION_PHASES)[number]
13
+ export type TransactionPhase = typeof TRANSACTION_PHASES[number]
14
14
 
15
15
  export type TransactionUpdateInProgress<ƒ extends ƒn> = TransactionUpdate<ƒ> & {
16
- phase: `applying` | `building`
17
- time: number
18
- core: StoreCore
16
+ phase: `applying` | `building`
17
+ time: number
18
+ core: StoreCore
19
19
  }
20
20
  export type TransactionIdle = {
21
- phase: `idle`
21
+ phase: `idle`
22
22
  }
23
23
  export type TransactionStatus<ƒ extends ƒn> =
24
- | TransactionIdle
25
- | TransactionUpdateInProgress<ƒ>
24
+ | TransactionIdle
25
+ | TransactionUpdateInProgress<ƒ>
@@ -6,18 +6,18 @@ import type { AtomToken, TransactionUpdate } from "../.."
6
6
  import { setState } from "../.."
7
7
 
8
8
  export const redoTransactionUpdate = <ƒ extends ƒn>(
9
- update: TransactionUpdate<ƒ>,
10
- store: Store
9
+ update: TransactionUpdate<ƒ>,
10
+ store: Store,
11
11
  ): void => {
12
- store.config.logger?.info(` ⏭ redo transaction "${update.key}" (redo)`)
13
- for (const { key, newValue } of update.atomUpdates) {
14
- const token: AtomToken<unknown> = { key, type: `atom` }
15
- const state = withdraw(token, store)
16
- if (state === null) {
17
- throw new Error(
18
- `State "${token.key}" not found in this store. This is surprising, because we are navigating the history of the store.`
19
- )
20
- }
21
- setState(state, newValue, store)
22
- }
12
+ store.config.logger?.info(` ⏭ redo transaction "${update.key}" (redo)`)
13
+ for (const { key, newValue } of update.atomUpdates) {
14
+ const token: AtomToken<unknown> = { key, type: `atom` }
15
+ const state = withdraw(token, store)
16
+ if (state === null) {
17
+ throw new Error(
18
+ `State "${token.key}" not found in this store. This is surprising, because we are navigating the history of the store.`,
19
+ )
20
+ }
21
+ setState(state, newValue, store)
22
+ }
23
23
  }
@@ -6,18 +6,18 @@ import type { AtomToken, TransactionUpdate } from "../.."
6
6
  import { setState } from "../.."
7
7
 
8
8
  export const undoTransactionUpdate = <ƒ extends ƒn>(
9
- update: TransactionUpdate<ƒ>,
10
- store: Store
9
+ update: TransactionUpdate<ƒ>,
10
+ store: Store,
11
11
  ): void => {
12
- store.config.logger?.info(` ⏮ undo transaction "${update.key}" (undo)`)
13
- for (const { key, oldValue } of update.atomUpdates) {
14
- const token: AtomToken<unknown> = { key, type: `atom` }
15
- const state = withdraw(token, store)
16
- if (state === null) {
17
- throw new Error(
18
- `State "${token.key}" not found in this store. This is surprising, because we are navigating the history of the store.`
19
- )
20
- }
21
- setState(state, oldValue, store)
22
- }
12
+ store.config.logger?.info(` ⏮ undo transaction "${update.key}" (undo)`)
13
+ for (const { key, oldValue } of update.atomUpdates) {
14
+ const token: AtomToken<unknown> = { key, type: `atom` }
15
+ const state = withdraw(token, store)
16
+ if (state === null) {
17
+ throw new Error(
18
+ `State "${token.key}" not found in this store. This is surprising, because we are navigating the history of the store.`,
19
+ )
20
+ }
21
+ setState(state, oldValue, store)
22
+ }
23
23
  }
@@ -1,67 +1,67 @@
1
1
  import HAMT from "hamt_plus"
2
- import * as Rx from "rxjs"
3
2
 
4
3
  import type { ƒn } from "~/packages/anvl/src/function"
5
4
 
6
5
  import type { Store, StoreCore } from "."
7
6
  import {
8
- abortTransaction,
9
- applyTransaction,
10
- buildTransaction,
11
- deposit,
12
- IMPLICIT,
7
+ Subject,
8
+ abortTransaction,
9
+ applyTransaction,
10
+ buildTransaction,
11
+ deposit,
12
+ IMPLICIT,
13
13
  } from "."
14
14
  import type { TransactionOptions, TransactionToken, TransactionUpdate } from ".."
15
15
  import { getState, setState } from ".."
16
16
 
17
17
  export type Transaction<ƒ extends ƒn> = {
18
- key: string
19
- type: `transaction`
20
- install: (store: Store) => void
21
- subject: Rx.Subject<TransactionUpdate<ƒ>>
22
- run: (...parameters: Parameters<ƒ>) => ReturnType<ƒ>
18
+ key: string
19
+ type: `transaction`
20
+ install: (store: Store) => void
21
+ subject: Subject<TransactionUpdate<ƒ>>
22
+ run: (...parameters: Parameters<ƒ>) => ReturnType<ƒ>
23
23
  }
24
24
 
25
25
  export function transaction__INTERNAL<ƒ extends ƒn>(
26
- options: TransactionOptions<ƒ>,
27
- store: Store = IMPLICIT.STORE
26
+ options: TransactionOptions<ƒ>,
27
+ store: Store = IMPLICIT.STORE,
28
28
  ): TransactionToken<ƒ> {
29
- const newTransaction: Transaction<ƒ> = {
30
- key: options.key,
31
- type: `transaction`,
32
- run: (...params: Parameters<ƒ>) => {
33
- buildTransaction(options.key, params, store)
34
- try {
35
- const output = options.do(
36
- {
37
- get: (token) => getState(token, store),
38
- set: (token, value) => setState(token, value, store),
39
- },
40
- ...params
41
- )
42
- applyTransaction(output, store)
43
- return output
44
- } catch (thrown) {
45
- abortTransaction(store)
46
- store.config.logger?.error(`Transaction ${options.key} failed`, thrown)
47
- throw thrown
48
- }
49
- },
50
- install: (store) => transaction__INTERNAL(options, store),
51
- subject: new Rx.Subject(),
52
- }
53
- const core = target(store)
54
- core.transactions = HAMT.set(
55
- newTransaction.key,
56
- newTransaction,
57
- core.transactions
58
- )
59
- const token = deposit(newTransaction)
60
- store.subject.transactionCreation.next(token)
61
- return token
29
+ const newTransaction: Transaction<ƒ> = {
30
+ key: options.key,
31
+ type: `transaction`,
32
+ run: (...params: Parameters<ƒ>) => {
33
+ buildTransaction(options.key, params, store)
34
+ try {
35
+ const output = options.do(
36
+ {
37
+ get: (token) => getState(token, store),
38
+ set: (token, value) => setState(token, value, store),
39
+ },
40
+ ...params,
41
+ )
42
+ applyTransaction(output, store)
43
+ return output
44
+ } catch (thrown) {
45
+ abortTransaction(store)
46
+ store.config.logger?.error(`Transaction ${options.key} failed`, thrown)
47
+ throw thrown
48
+ }
49
+ },
50
+ install: (store) => transaction__INTERNAL(options, store),
51
+ subject: new Subject(),
52
+ }
53
+ const core = target(store)
54
+ core.transactions = HAMT.set(
55
+ newTransaction.key,
56
+ newTransaction,
57
+ core.transactions,
58
+ )
59
+ const token = deposit(newTransaction)
60
+ store.subject.transactionCreation.next(token)
61
+ return token
62
62
  }
63
63
 
64
64
  export const target = (store: Store = IMPLICIT.STORE): StoreCore =>
65
- store.transactionStatus.phase === `building`
66
- ? store.transactionStatus.core
67
- : store
65
+ store.transactionStatus.phase === `building`
66
+ ? store.transactionStatus.core
67
+ : store
@@ -3,16 +3,16 @@ import * as AtomIO from "atom.io"
3
3
  import type { Json, JsonInterface } from "~/packages/anvl/src/json"
4
4
 
5
5
  export const selectJson = <T, J extends Json>(
6
- atom: AtomIO.AtomToken<T>,
7
- transform: JsonInterface<T, J>,
8
- store: AtomIO.Store = AtomIO.__INTERNAL__.IMPLICIT.STORE
6
+ atom: AtomIO.AtomToken<T>,
7
+ transform: JsonInterface<T, J>,
8
+ store: AtomIO.Store = AtomIO.__INTERNAL__.IMPLICIT.STORE,
9
9
  ): AtomIO.SelectorToken<J> =>
10
- AtomIO.__INTERNAL__.selector__INTERNAL(
11
- {
12
- key: `${atom.key}JSON`,
13
- get: ({ get }) => transform.toJson(get(atom)),
14
- set: ({ set }, newValue) => set(atom, transform.fromJson(newValue)),
15
- },
16
- undefined,
17
- store
18
- )
10
+ AtomIO.__INTERNAL__.selector__INTERNAL(
11
+ {
12
+ key: `${atom.key}JSON`,
13
+ get: ({ get }) => transform.toJson(get(atom)),
14
+ set: ({ set }, newValue) => set(atom, transform.fromJson(newValue)),
15
+ },
16
+ undefined,
17
+ store,
18
+ )