atom.io 0.24.5 → 0.24.7

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,265 +0,0 @@
1
- import type {
2
- AtomToken,
3
- Func,
4
- TimelineUpdate,
5
- TransactionToken,
6
- TransactionUpdate,
7
- } from "atom.io"
8
-
9
- import { newest } from "../lineage"
10
- import { getUpdateToken } from "../mutable"
11
- import type { Store } from "../store"
12
- import { withdraw } from "../store"
13
- import type {
14
- Timeline,
15
- TimelineAtomUpdate,
16
- TimelineTransactionUpdate,
17
- } from "./create-timeline"
18
-
19
- export const addAtomToTimeline = (
20
- atomToken: AtomToken<any>,
21
- tl: Timeline<any>,
22
- store: Store,
23
- ): void => {
24
- let maybeAtom = withdraw(atomToken, store)
25
- if (maybeAtom.type === `mutable_atom`) {
26
- const updateToken = getUpdateToken(maybeAtom)
27
- maybeAtom = withdraw(updateToken, store)
28
- }
29
- const atom = maybeAtom
30
- store.timelineAtoms.set({ atomKey: atom.key, timelineKey: tl.key })
31
-
32
- tl.subscriptions.set(
33
- atom.key,
34
- atom.subject.subscribe(`timeline`, (update) => {
35
- const target = newest(store)
36
- const currentSelectorKey =
37
- store.operation.open && store.operation.token.type === `selector`
38
- ? store.operation.token.key
39
- : null
40
- const currentSelectorTime =
41
- store.operation.open && store.operation.token.type === `selector`
42
- ? store.operation.time
43
- : null
44
- const { transactionApplying } = target.on
45
- const currentTransactionKey = transactionApplying.state?.update.key
46
- const currentTransactionInstanceId = transactionApplying.state?.update.id
47
-
48
- store.logger.info(
49
- `⏳`,
50
- `timeline`,
51
- tl.key,
52
- `atom`,
53
- atomToken.key,
54
- `went`,
55
- update.oldValue,
56
- `->`,
57
- update.newValue,
58
- currentTransactionKey
59
- ? `in transaction "${currentTransactionKey}"`
60
- : currentSelectorKey
61
- ? `in selector "${currentSelectorKey}"`
62
- : ``,
63
- )
64
- if (tl.timeTraveling === null) {
65
- if (tl.selectorTime && tl.selectorTime !== currentSelectorTime) {
66
- const mostRecentUpdate: TimelineUpdate<any> | undefined =
67
- tl.history.at(-1)
68
- if (mostRecentUpdate === undefined) {
69
- throw new Error(
70
- `Timeline "${tl.key}" has a selectorTime, but no history. This is most likely a bug in AtomIO.`,
71
- )
72
- }
73
- }
74
- if (currentTransactionKey) {
75
- const txToken: TransactionToken<any> = {
76
- key: currentTransactionKey,
77
- type: `transaction`,
78
- }
79
- const currentTransaction = withdraw(txToken, store)
80
- if (tl.transactionKey !== currentTransactionKey) {
81
- if (tl.transactionKey) {
82
- store.logger.error(
83
- `🐞`,
84
- `timeline`,
85
- tl.key,
86
- `unable to resolve transaction "${tl.transactionKey}. This is probably a bug in AtomIO.`,
87
- )
88
- }
89
- tl.transactionKey = currentTransactionKey
90
- const unsubscribe = currentTransaction.subject.subscribe(
91
- `timeline:${tl.key}`,
92
- (transactionUpdate) => {
93
- unsubscribe()
94
- if (tl.timeTraveling === null && currentTransactionInstanceId) {
95
- if (tl.at !== tl.history.length) {
96
- tl.history.splice(tl.at)
97
- }
98
-
99
- const filterUpdates = (
100
- updates: TransactionUpdate<Func>[`updates`],
101
- ) =>
102
- updates
103
- .filter((updateFromTx) => {
104
- const newestStore = newest(store)
105
- if (`updates` in updateFromTx) {
106
- return true
107
- }
108
- const atomOrFamilyKeys =
109
- newestStore.timelineAtoms.getRelatedKeys(tl.key)
110
-
111
- if (!atomOrFamilyKeys) {
112
- return false
113
- }
114
- let key: string | undefined
115
- let familyKey: string | undefined
116
- switch (updateFromTx.type) {
117
- case `state_creation`:
118
- case `state_disposal`:
119
- key = updateFromTx.token.key
120
- familyKey = updateFromTx.token.family?.key
121
- break
122
- case `molecule_creation`:
123
- case `molecule_disposal`:
124
- break
125
- default:
126
- key = updateFromTx.key
127
- familyKey = updateFromTx.family?.key
128
- break
129
- }
130
- if (key === undefined) {
131
- return false
132
- }
133
- if (atomOrFamilyKeys.has(key)) {
134
- return true
135
- }
136
- if (familyKey !== undefined) {
137
- return atomOrFamilyKeys.has(familyKey)
138
- }
139
- return false
140
- })
141
- .map((updateFromTx) => {
142
- if (`updates` in updateFromTx) {
143
- return {
144
- ...updateFromTx,
145
- updates: filterUpdates(updateFromTx.updates),
146
- }
147
- }
148
- return updateFromTx
149
- })
150
-
151
- const updates = filterUpdates(transactionUpdate.updates)
152
-
153
- const timelineTransactionUpdate: TimelineTransactionUpdate = {
154
- timestamp: Date.now(),
155
- ...transactionUpdate,
156
- updates,
157
- }
158
- const willCapture =
159
- tl.shouldCapture?.(timelineTransactionUpdate, tl) ?? true
160
- if (willCapture) {
161
- tl.history.push(timelineTransactionUpdate)
162
- tl.at = tl.history.length
163
- tl.subject.next(timelineTransactionUpdate)
164
- }
165
- }
166
- tl.transactionKey = null
167
- store.logger.info(
168
- `⌛`,
169
- `timeline`,
170
- tl.key,
171
- `got a transaction_update "${transactionUpdate.key}"`,
172
- )
173
- },
174
- )
175
- }
176
- } else if (currentSelectorKey && currentSelectorTime) {
177
- let latestUpdate: TimelineUpdate<any> | undefined = tl.history.at(-1)
178
-
179
- if (currentSelectorTime !== tl.selectorTime) {
180
- latestUpdate = {
181
- type: `selector_update`,
182
- timestamp: currentSelectorTime,
183
- key: currentSelectorKey,
184
- atomUpdates: [],
185
- }
186
- latestUpdate.atomUpdates.push({
187
- key: atom.key,
188
- type: `atom_update`,
189
- ...update,
190
- })
191
- if (tl.at !== tl.history.length) {
192
- tl.history.splice(tl.at)
193
- }
194
-
195
- tl.history.push(latestUpdate)
196
-
197
- store.logger.info(
198
- `⌛`,
199
- `timeline`,
200
- tl.key,
201
- `got a selector_update "${currentSelectorKey}" with`,
202
- latestUpdate.atomUpdates.map((atomUpdate) => atomUpdate.key),
203
- )
204
-
205
- tl.at = tl.history.length
206
- tl.selectorTime = currentSelectorTime
207
- } else {
208
- if (latestUpdate?.type === `selector_update`) {
209
- latestUpdate.atomUpdates.push({
210
- key: atom.key,
211
- type: `atom_update`,
212
- ...update,
213
- })
214
- store.logger.info(
215
- `⌛`,
216
- `timeline`,
217
- tl.key,
218
- `set selector_update "${currentSelectorKey}" to`,
219
- latestUpdate?.atomUpdates.map((atomUpdate) => atomUpdate.key),
220
- )
221
- }
222
- }
223
- if (latestUpdate) {
224
- const willCaptureSelectorUpdate =
225
- tl.shouldCapture?.(latestUpdate, tl) ?? true
226
- if (willCaptureSelectorUpdate) {
227
- tl.subject.next(latestUpdate)
228
- } else {
229
- tl.history.pop()
230
- tl.at = tl.history.length
231
- }
232
- }
233
- } else {
234
- const timestamp = Date.now()
235
- tl.selectorTime = null
236
- if (tl.at !== tl.history.length) {
237
- tl.history.splice(tl.at)
238
- }
239
- const atomUpdate: TimelineAtomUpdate<any> = {
240
- type: `atom_update`,
241
- timestamp,
242
- key: atom.key,
243
- oldValue: update.oldValue,
244
- newValue: update.newValue,
245
- }
246
- if (atom.family) {
247
- atomUpdate.family = atom.family
248
- }
249
- const willCapture = tl.shouldCapture?.(atomUpdate, tl) ?? true
250
- store.logger.info(
251
- `⌛`,
252
- `timeline`,
253
- tl.key,
254
- `got an atom_update to "${atom.key}"`,
255
- )
256
- if (willCapture) {
257
- tl.history.push(atomUpdate)
258
- tl.at = tl.history.length
259
- tl.subject.next(atomUpdate)
260
- }
261
- }
262
- }
263
- }),
264
- )
265
- }