atom.io 0.33.21 → 0.34.1

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 (54) hide show
  1. package/dist/eslint-plugin/index.js +1 -2
  2. package/dist/eslint-plugin/index.js.map +1 -1
  3. package/dist/internal/index.d.ts +10 -6
  4. package/dist/internal/index.d.ts.map +1 -1
  5. package/dist/internal/index.js +1262 -1253
  6. package/dist/internal/index.js.map +1 -1
  7. package/dist/json/index.d.ts +3 -0
  8. package/dist/json/index.d.ts.map +1 -1
  9. package/dist/json/index.js.map +1 -1
  10. package/dist/main/index.d.ts +405 -23
  11. package/dist/main/index.d.ts.map +1 -1
  12. package/dist/main/index.js +107 -11
  13. package/dist/main/index.js.map +1 -1
  14. package/dist/realtime-client/index.js +6 -10
  15. package/dist/realtime-client/index.js.map +1 -1
  16. package/dist/realtime-server/index.js +1 -1
  17. package/dist/realtime-server/index.js.map +1 -1
  18. package/dist/realtime-testing/index.d.ts.map +1 -1
  19. package/dist/realtime-testing/index.js +0 -1
  20. package/dist/realtime-testing/index.js.map +1 -1
  21. package/package.json +6 -6
  22. package/src/internal/atom/create-regular-atom.ts +6 -7
  23. package/src/internal/caching.ts +9 -9
  24. package/src/internal/future.ts +3 -0
  25. package/src/internal/get-state/read-or-compute-value.ts +12 -7
  26. package/src/internal/join/create-join.ts +27 -0
  27. package/src/internal/join/index.ts +1 -0
  28. package/src/internal/junction.ts +2 -0
  29. package/src/internal/mutable/create-mutable-atom.ts +2 -4
  30. package/src/internal/selector/create-readonly-held-selector.ts +10 -0
  31. package/src/internal/selector/create-readonly-pure-selector.ts +13 -4
  32. package/src/internal/selector/create-writable-held-selector.ts +11 -2
  33. package/src/internal/selector/create-writable-pure-selector.ts +11 -1
  34. package/src/internal/selector/trace-selector-atoms.ts +18 -40
  35. package/src/internal/selector/update-selector-atoms.ts +5 -5
  36. package/src/internal/set-state/evict-downstream.ts +2 -2
  37. package/src/internal/set-state/reset-atom-or-selector.ts +3 -3
  38. package/src/internal/store/store.ts +0 -1
  39. package/src/internal/subscribe/subscribe-to-root-atoms.ts +42 -38
  40. package/src/internal/subscribe/subscribe-to-state.ts +23 -11
  41. package/src/json/index.ts +3 -0
  42. package/src/main/atom.ts +54 -13
  43. package/src/main/dispose-state.ts +8 -2
  44. package/src/main/find-state.ts +44 -14
  45. package/src/main/get-state.ts +2 -2
  46. package/src/main/index.ts +8 -0
  47. package/src/main/join.ts +79 -22
  48. package/src/main/realm.ts +50 -4
  49. package/src/main/selector.ts +116 -6
  50. package/src/main/subscribe.ts +31 -1
  51. package/src/main/timeline.ts +17 -0
  52. package/src/main/transaction.ts +39 -3
  53. package/src/realtime-client/realtime-client-stores/client-sync-store.ts +2 -2
  54. package/src/realtime-testing/setup-realtime-test.tsx +0 -5
@@ -11,7 +11,9 @@ import type {
11
11
  TransactionUpdate,
12
12
  } from "."
13
13
 
14
+ /** @public */
14
15
  export type StateUpdate<T> = { newValue: T; oldValue: T }
16
+ /** @public */
15
17
  export type KeyedStateUpdate<T> = Flat<
16
18
  StateUpdate<T> & {
17
19
  key: string
@@ -19,22 +21,50 @@ export type KeyedStateUpdate<T> = Flat<
19
21
  family?: FamilyMetadata
20
22
  }
21
23
  >
24
+ /** @public */
22
25
  export type UpdateHandler<T> = (update: StateUpdate<T>) => void
23
-
26
+ /** @public */
24
27
  export type TransactionUpdateHandler<F extends Func> = (
25
28
  data: TransactionUpdate<F>,
26
29
  ) => void
27
30
 
31
+ /**
32
+ * @public
33
+ * Subscribe to a state in the implicit store
34
+ * @param token - The token of the state to subscribe to
35
+ * @param handleUpdate - A function that will be called when the state is updated
36
+ * @param key - A unique key for the subscription. If not provided, a random key will be generated.
37
+ * @returns A function that can be called to unsubscribe from the state
38
+ * @overload State
39
+ */
28
40
  export function subscribe<T>(
29
41
  token: ReadableToken<T>,
30
42
  handleUpdate: UpdateHandler<T>,
31
43
  key?: string,
32
44
  ): () => void
45
+ /**
46
+ * @public
47
+ * Subscribe to a transaction in the implicit store
48
+ * @param token - The token of the transaction to subscribe to
49
+ * @param handleUpdate - A function that will be called when the transaction succeeds
50
+ * @param key - A unique key for the subscription. If not provided, a random key will be generated.
51
+ * @returns A function that can be called to unsubscribe from the transaction
52
+ * @overload Transaction
53
+ */
33
54
  export function subscribe<F extends Func>(
34
55
  token: TransactionToken<F>,
35
56
  handleUpdate: TransactionUpdateHandler<F>,
36
57
  key?: string,
37
58
  ): () => void
59
+ /**
60
+ * @public
61
+ * Subscribe to a timeline in the implicit store
62
+ * @param token - The token of the timeline to subscribe to
63
+ * @param handleUpdate - A function that will be called when a new update is available
64
+ * @param key - A unique key for the subscription. If not provided, a random key will be generated.
65
+ * @returns A function that can be called to unsubscribe from the timeline
66
+ * @overload Timeline
67
+ */
38
68
  export function subscribe<M extends TimelineManageable>(
39
69
  token: TimelineToken<M>,
40
70
  handleUpdate: (update: TimelineUpdate<M> | `redo` | `undo`) => void,
@@ -12,7 +12,9 @@ import { createTimeline, IMPLICIT, timeTravel } from "atom.io/internal"
12
12
 
13
13
  import type { AtomFamilyToken, AtomToken } from "."
14
14
 
15
+ /** @public */
15
16
  export type TimelineManageable = AtomFamilyToken<any, any> | AtomToken<any>
17
+ /** @public */
16
18
  export type AtomOnly<M extends TimelineManageable> = M extends AtomFamilyToken<
17
19
  any,
18
20
  any
@@ -22,21 +24,30 @@ export type AtomOnly<M extends TimelineManageable> = M extends AtomFamilyToken<
22
24
  ? M
23
25
  : never
24
26
 
27
+ /** @public */
25
28
  export type TimelineToken<M> = {
29
+ /** The unique identifier of the timeline */
26
30
  key: string
31
+ /** Discriminator */
27
32
  type: `timeline`
33
+ /** Never present. This is a marker that preserves the type of the managed atoms */
28
34
  __M?: M
29
35
  }
30
36
 
37
+ /** @public */
31
38
  export type TimelineOptions<ManagedAtom extends TimelineManageable> = {
39
+ /** The unique identifier of the timeline */
32
40
  key: string
41
+ /** The managed atoms (and families of atoms) to record */
33
42
  scope: ManagedAtom[]
43
+ /** A function that determines whether a given update should be recorded */
34
44
  shouldCapture?: (
35
45
  update: TimelineUpdate<ManagedAtom>,
36
46
  timeline: Timeline<TimelineManageable>,
37
47
  ) => boolean
38
48
  }
39
49
 
50
+ /** @public */
40
51
  export type TimelineUpdate<ManagedAtom extends TimelineManageable> =
41
52
  | TimelineAtomUpdate<ManagedAtom>
42
53
  | TimelineMoleculeCreation
@@ -46,6 +57,12 @@ export type TimelineUpdate<ManagedAtom extends TimelineManageable> =
46
57
  | TimelineStateDisposal<AtomOnly<ManagedAtom>>
47
58
  | TimelineTransactionUpdate
48
59
 
60
+ /**
61
+ * @public
62
+ * Create a timeline, a mechanism for recording, undoing, and replaying changes to groups of atoms.
63
+ * @param options - {@link TimelineOptions}
64
+ * @returns A reference to the timeline created: a {@link TimelineToken}
65
+ */
49
66
  export const timeline = <ManagedAtom extends TimelineManageable>(
50
67
  options: TimelineOptions<ManagedAtom>,
51
68
  ): TimelineToken<ManagedAtom> => {
@@ -18,37 +18,47 @@ import type {
18
18
  } from "."
19
19
  import type { resetState } from "./reset-state"
20
20
 
21
+ /** @public */
21
22
  export type TransactionToken<F extends Func> = {
23
+ /** The unique identifier of the transaction */
22
24
  key: string
25
+ /** Discriminator */
23
26
  type: `transaction`
27
+ /** Never present. This is a marker that preserves the type of the transaction function */
24
28
  __F?: F
25
29
  }
26
30
 
31
+ /** @public */
27
32
  export type StateCreation<Token extends ReadableToken<any>> = {
28
33
  type: `state_creation`
29
34
  token: Token
30
35
  }
36
+ /** @public */
31
37
  export type AtomDisposal<Token extends ReadableToken<any>> = {
32
38
  type: `state_disposal`
33
39
  subType: `atom`
34
40
  token: Token
35
41
  value: TokenType<Token>
36
42
  }
43
+ /** @public */
37
44
  export type SelectorDisposal<Token extends ReadableToken<any>> = {
38
45
  type: `state_disposal`
39
46
  subType: `selector`
40
47
  token: Token
41
48
  }
49
+ /** @public */
42
50
  export type StateDisposal<Token extends ReadableToken<any>> =
43
51
  | AtomDisposal<Token>
44
52
  | SelectorDisposal<Token>
45
53
 
54
+ /** @public */
46
55
  export type MoleculeCreation = {
47
56
  type: `molecule_creation`
48
57
  key: Canonical
49
58
  provenance: Canonical
50
59
  }
51
60
 
61
+ /** @public */
52
62
  export type MoleculeDisposal = {
53
63
  type: `molecule_disposal`
54
64
  key: Canonical
@@ -56,6 +66,7 @@ export type MoleculeDisposal = {
56
66
  values: [key: string, value: any][]
57
67
  }
58
68
 
69
+ /** @public */
59
70
  export type MoleculeTransfer = {
60
71
  type: `molecule_transfer`
61
72
  key: Canonical
@@ -63,6 +74,7 @@ export type MoleculeTransfer = {
63
74
  to: Canonical[]
64
75
  }
65
76
 
77
+ /** @public */
66
78
  export type TransactionUpdateContent =
67
79
  | KeyedStateUpdate<unknown>
68
80
  | MoleculeCreation
@@ -72,6 +84,7 @@ export type TransactionUpdateContent =
72
84
  | StateDisposal<ReadableToken<unknown>>
73
85
  | TransactionUpdate<Func>
74
86
 
87
+ /** @public */
75
88
  export type TransactionUpdate<F extends Func> = {
76
89
  type: `transaction_update`
77
90
  key: string
@@ -82,7 +95,9 @@ export type TransactionUpdate<F extends Func> = {
82
95
  output: ReturnType<F>
83
96
  }
84
97
 
98
+ /** @public */
85
99
  export type GetterToolkit = Pick<SetterToolkit, `find` | `get` | `json`>
100
+ /** @public */
86
101
  export type SetterToolkit = Readonly<{
87
102
  get: typeof getState
88
103
  set: typeof setState
@@ -91,6 +106,7 @@ export type SetterToolkit = Readonly<{
91
106
  state: MutableAtomToken<T, J>,
92
107
  ) => WritablePureSelectorToken<J>
93
108
  }>
109
+ /** @public */
94
110
  export type ActorToolkit = Readonly<{
95
111
  get: typeof getState
96
112
  set: typeof setState
@@ -104,35 +120,55 @@ export type ActorToolkit = Readonly<{
104
120
  env: () => EnvironmentData
105
121
  }>
106
122
 
123
+ /** @public */
107
124
  export type Read<F extends Func> = (
108
125
  toolkit: GetterToolkit,
109
126
  ...parameters: Parameters<F>
110
127
  ) => ReturnType<F>
111
128
 
129
+ /** @public */
112
130
  export type Write<F extends Func> = (
113
131
  toolkit: SetterToolkit,
114
132
  ...parameters: Parameters<F>
115
133
  ) => ReturnType<F>
116
134
 
135
+ /** @public */
117
136
  export type Transact<F extends Func> = (
118
137
  toolkit: ActorToolkit,
119
138
  ...parameters: Parameters<F>
120
139
  ) => ReturnType<F>
121
140
 
141
+ /** @public */
142
+ export type TransactionIO<Token extends TransactionToken<any>> =
143
+ Token extends TransactionToken<infer F> ? F : never
144
+
145
+ /** @public */
122
146
  export type TransactionOptions<F extends Func> = {
147
+ /** The unique identifier of the transaction */
123
148
  key: string
149
+ /** The operation to perform */
124
150
  do: Transact<F>
125
151
  }
126
152
 
127
- export type TransactionIO<Token extends TransactionToken<any>> =
128
- Token extends TransactionToken<infer F> ? F : never
129
-
153
+ /**
154
+ * @public
155
+ * Create a transaction, a mechanism for batching updates multiple states in a single, all-or-nothing operation
156
+ * @param options - {@link TransactionOptions}
157
+ * @returns A reference to the transaction created: a {@link TransactionToken}
158
+ */
130
159
  export function transaction<F extends Func>(
131
160
  options: TransactionOptions<F>,
132
161
  ): TransactionToken<F> {
133
162
  return createTransaction(IMPLICIT.STORE, options)
134
163
  }
135
164
 
165
+ /**
166
+ * @public
167
+ * Execute a {@link transaction}
168
+ * @param token - A {@link TransactionToken}
169
+ * @param id - A unique identifier for the transaction. If not provided, a random identifier will be generated
170
+ * @returns A function that can be called to run the transaction with its {@link TransactionIO} parameters
171
+ */
136
172
  export function runTransaction<F extends Func>(
137
173
  token: TransactionToken<F>,
138
174
  id: string = arbitrary(),
@@ -4,12 +4,12 @@ export const optimisticUpdateQueue: AtomIO.RegularAtomToken<
4
4
  AtomIO.TransactionUpdate<any>[]
5
5
  > = AtomIO.atom<AtomIO.TransactionUpdate<any>[]>({
6
6
  key: `updateQueue`,
7
- default: [],
7
+ default: () => [],
8
8
  })
9
9
 
10
10
  export const confirmedUpdateQueue: AtomIO.RegularAtomToken<
11
11
  AtomIO.TransactionUpdate<any>[]
12
12
  > = AtomIO.atom<AtomIO.TransactionUpdate<any>[]>({
13
13
  key: `serverConfirmedUpdateQueue`,
14
- default: [],
14
+ default: () => [],
15
15
  })
@@ -192,11 +192,6 @@ export const setupRealtimeTestClient = (
192
192
  auth: { token: `test`, username: `${name}-${testNumber}` },
193
193
  })
194
194
  const silo = new AtomIO.Silo({ name, lifespan: `ephemeral` }, IMPLICIT.STORE)
195
- for (const [key, value] of silo.store.valueMap.entries()) {
196
- if (Array.isArray(value)) {
197
- silo.store.valueMap.set(key, [...value])
198
- }
199
- }
200
195
  silo.setState(RTC.myUsernameState, `${name}-${testNumber}`)
201
196
 
202
197
  const { document } = new Happy.Window()