atom.io 0.17.0 → 0.18.0

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 (140) hide show
  1. package/data/dist/index.cjs +62 -40
  2. package/data/dist/index.cjs.map +1 -1
  3. package/data/dist/index.d.ts +8 -2
  4. package/data/dist/index.js +64 -42
  5. package/data/dist/index.js.map +1 -1
  6. package/data/src/dict.ts +8 -4
  7. package/data/src/join.ts +74 -33
  8. package/data/src/struct-family.ts +18 -17
  9. package/dist/chunk-OEVFAUPE.js +289 -0
  10. package/dist/chunk-OEVFAUPE.js.map +1 -0
  11. package/dist/index.cjs +4 -10
  12. package/dist/index.cjs.map +1 -1
  13. package/dist/index.d.ts +62 -51
  14. package/dist/index.js +5 -11
  15. package/dist/index.js.map +1 -1
  16. package/internal/dist/index.cjs +163 -64
  17. package/internal/dist/index.cjs.map +1 -1
  18. package/internal/dist/index.d.ts +94 -70
  19. package/internal/dist/index.js +155 -59
  20. package/internal/dist/index.js.map +1 -1
  21. package/internal/src/arbitrary.ts +3 -0
  22. package/internal/src/caching.ts +8 -6
  23. package/internal/src/families/find-in-store.ts +16 -0
  24. package/internal/src/get-environment-data.ts +4 -7
  25. package/internal/src/index.ts +6 -5
  26. package/internal/src/ingest-updates/ingest-transaction-update.ts +0 -1
  27. package/internal/src/selector/create-standalone-selector.ts +0 -2
  28. package/internal/src/set-state/set-atom.ts +14 -18
  29. package/internal/src/store/store.ts +14 -2
  30. package/internal/src/store/withdraw.ts +72 -2
  31. package/internal/src/subscribe/subscribe-to-timeline.ts +2 -2
  32. package/internal/src/subscribe/subscribe-to-transaction.ts +2 -2
  33. package/internal/src/timeline/create-timeline.ts +12 -1
  34. package/internal/src/transaction/act-upon-store.ts +19 -0
  35. package/internal/src/transaction/apply-transaction.ts +7 -1
  36. package/internal/src/transaction/assign-transaction-to-continuity.ts +18 -0
  37. package/internal/src/transaction/build-transaction.ts +7 -6
  38. package/internal/src/transaction/create-transaction.ts +1 -1
  39. package/internal/src/transaction/get-epoch-number.ts +40 -0
  40. package/internal/src/transaction/index.ts +10 -1
  41. package/internal/src/transaction/set-epoch-number.ts +31 -0
  42. package/introspection/dist/index.cjs.map +1 -1
  43. package/introspection/dist/index.d.ts +3 -3
  44. package/introspection/dist/index.js.map +1 -1
  45. package/introspection/src/attach-introspection-states.ts +6 -2
  46. package/introspection/src/attach-timeline-family.ts +5 -2
  47. package/introspection/src/attach-transaction-logs.ts +2 -2
  48. package/json/dist/index.d.ts +3 -1
  49. package/json/src/index.ts +6 -2
  50. package/package.json +24 -13
  51. package/react/dist/index.cjs.map +1 -1
  52. package/react/dist/index.d.ts +1 -1
  53. package/react/dist/index.js.map +1 -1
  54. package/react/src/use-json.ts +1 -1
  55. package/react-devtools/dist/index.cjs +131 -134
  56. package/react-devtools/dist/index.cjs.map +1 -1
  57. package/react-devtools/dist/index.css +2 -2
  58. package/react-devtools/dist/index.css.map +1 -1
  59. package/react-devtools/dist/index.d.ts +3 -3
  60. package/react-devtools/dist/index.js +103 -106
  61. package/react-devtools/dist/index.js.map +1 -1
  62. package/react-devtools/src/StateEditor.tsx +6 -6
  63. package/react-devtools/src/StateIndex.tsx +2 -5
  64. package/react-devtools/src/TimelineIndex.tsx +3 -3
  65. package/react-devtools/src/TransactionIndex.tsx +9 -8
  66. package/react-devtools/src/Updates.tsx +1 -1
  67. package/react-devtools/src/index.ts +4 -4
  68. package/realtime/dist/index.cjs +72 -0
  69. package/realtime/dist/index.cjs.map +1 -0
  70. package/realtime/dist/index.d.ts +39 -0
  71. package/realtime/dist/index.js +68 -0
  72. package/realtime/dist/index.js.map +1 -0
  73. package/realtime/package.json +16 -0
  74. package/realtime/src/index.ts +1 -0
  75. package/realtime/src/realtime-continuity.ts +152 -0
  76. package/realtime-client/dist/index.cjs +389 -48
  77. package/realtime-client/dist/index.cjs.map +1 -1
  78. package/realtime-client/dist/index.d.ts +16 -9
  79. package/realtime-client/dist/index.js +100 -37
  80. package/realtime-client/dist/index.js.map +1 -1
  81. package/realtime-client/src/index.ts +8 -5
  82. package/realtime-client/src/{pull-family-member.ts → pull-atom-family-member.ts} +2 -2
  83. package/realtime-client/src/{pull-state.ts → pull-atom.ts} +2 -2
  84. package/realtime-client/src/{pull-mutable-family-member.ts → pull-mutable-atom-family-member.ts} +1 -1
  85. package/realtime-client/src/{pull-mutable.ts → pull-mutable-atom.ts} +1 -1
  86. package/realtime-client/src/pull-selector-family-member.ts +42 -0
  87. package/realtime-client/src/pull-selector.ts +38 -0
  88. package/realtime-client/src/realtime-client-stores/client-main-store.ts +2 -2
  89. package/realtime-client/src/realtime-client-stores/client-sync-store.ts +7 -7
  90. package/realtime-client/src/sync-continuity.ts +321 -0
  91. package/realtime-client/src/sync-server-action.ts +18 -20
  92. package/realtime-react/dist/index.cjs +330 -15
  93. package/realtime-react/dist/index.cjs.map +1 -1
  94. package/realtime-react/dist/index.d.ts +26 -6
  95. package/realtime-react/dist/index.js +43 -12
  96. package/realtime-react/dist/index.js.map +1 -1
  97. package/realtime-react/src/index.ts +6 -3
  98. package/realtime-react/src/use-pull-atom-family-member.ts +21 -0
  99. package/realtime-react/src/{use-pull-family-member.ts → use-pull-atom.ts} +6 -5
  100. package/realtime-react/src/{use-pull-mutable.ts → use-pull-mutable-atom.ts} +4 -3
  101. package/realtime-react/src/use-pull-mutable-family-member.ts +9 -4
  102. package/realtime-react/src/use-pull-selector-family-member.ts +21 -0
  103. package/realtime-react/src/{use-pull.ts → use-pull-selector.ts} +7 -5
  104. package/realtime-react/src/use-push.ts +3 -2
  105. package/realtime-react/src/use-server-action.ts +3 -2
  106. package/realtime-react/src/use-sync-continuity.ts +12 -0
  107. package/realtime-react/src/use-sync-server-action.ts +3 -2
  108. package/realtime-server/dist/index.cjs +568 -242
  109. package/realtime-server/dist/index.cjs.map +1 -1
  110. package/realtime-server/dist/index.d.ts +124 -49
  111. package/realtime-server/dist/index.js +555 -238
  112. package/realtime-server/dist/index.js.map +1 -1
  113. package/realtime-server/src/index.ts +18 -2
  114. package/realtime-server/src/ipc-socket.ts +230 -0
  115. package/realtime-server/src/realtime-action-receiver.ts +8 -5
  116. package/realtime-server/src/realtime-action-synchronizer.ts +40 -28
  117. package/realtime-server/src/realtime-continuity-synchronizer.ts +247 -0
  118. package/realtime-server/src/realtime-family-provider.ts +30 -71
  119. package/realtime-server/src/realtime-mutable-family-provider.ts +24 -86
  120. package/realtime-server/src/realtime-server-stores/index.ts +3 -1
  121. package/realtime-server/src/realtime-server-stores/realtime-continuity-store.ts +90 -0
  122. package/realtime-server/src/realtime-server-stores/server-room-store.ts +97 -0
  123. package/realtime-server/src/realtime-server-stores/server-sync-store.ts +2 -72
  124. package/realtime-server/src/realtime-server-stores/server-user-store.ts +14 -29
  125. package/realtime-server/src/realtime-state-receiver.ts +0 -1
  126. package/realtime-testing/dist/index.cjs +28 -28
  127. package/realtime-testing/dist/index.cjs.map +1 -1
  128. package/realtime-testing/dist/index.js +28 -27
  129. package/realtime-testing/dist/index.js.map +1 -1
  130. package/realtime-testing/src/setup-realtime-test.tsx +38 -28
  131. package/src/atom.ts +49 -31
  132. package/src/logger.ts +10 -5
  133. package/src/selector.ts +44 -25
  134. package/src/subscribe.ts +2 -1
  135. package/src/timeline.ts +4 -4
  136. package/src/transaction.ts +13 -17
  137. package/src/validators.ts +15 -9
  138. package/dist/chunk-H4Q5FTPZ.js +0 -11
  139. package/dist/chunk-H4Q5FTPZ.js.map +0 -1
  140. package/internal/src/set-state/copy-mutable-in-transaction.ts +0 -19
@@ -1,8 +1,15 @@
1
1
  import * as http from "http"
2
2
 
3
- import { type RenderResult, prettyDOM, render } from "@testing-library/react"
3
+ import { prettyDOM, render } from "@testing-library/react"
4
+ import type { RenderResult } from "@testing-library/react"
4
5
  import * as AtomIO from "atom.io"
5
- import * as Internal from "atom.io/internal"
6
+ import {
7
+ IMPLICIT,
8
+ clearStore,
9
+ findInStore,
10
+ getFromStore,
11
+ setIntoStore,
12
+ } from "atom.io/internal"
6
13
  import * as AR from "atom.io/react"
7
14
  import * as RTR from "atom.io/realtime-react"
8
15
  import * as RTS from "atom.io/realtime-server"
@@ -14,6 +21,8 @@ import { io } from "socket.io-client"
14
21
 
15
22
  import { recordToEntries } from "~/packages/anvl/src/object"
16
23
 
24
+ let testNumber = 0
25
+
17
26
  export type TestSetupOptions = {
18
27
  server: (tools: { socket: SocketIO.Socket; silo: AtomIO.Silo }) => void
19
28
  }
@@ -61,36 +70,24 @@ export type RealtimeTestAPI__MultiClient<ClientNames extends string> =
61
70
  export const setupRealtimeTestServer = (
62
71
  options: TestSetupOptions,
63
72
  ): RealtimeTestServer => {
64
- const silo = new AtomIO.Silo(`SERVER`, Internal.IMPLICIT.STORE)
73
+ ++testNumber
74
+ const silo = new AtomIO.Silo(`SERVER-${testNumber}`, IMPLICIT.STORE)
65
75
 
66
76
  const httpServer = http.createServer((_, res) => res.end(`Hello World!`))
67
77
  const address = httpServer.listen().address()
68
78
  const port =
69
79
  typeof address === `string` ? 80 : address === null ? null : address.port
70
80
  if (port === null) throw new Error(`Could not determine port for test server`)
81
+
71
82
  const server = new SocketIO.Server(httpServer).use((socket, next) => {
72
83
  const { token, username } = socket.handshake.auth
73
84
  if (token === `test` && socket.id) {
74
- const socketRelatedKeysState = Internal.findInStore(
75
- RTS.usersOfSockets.core.findRelatedKeysState,
76
- socket.id,
77
- silo.store,
78
- )
79
- const clientRelatedKeysState = Internal.findInStore(
80
- RTS.usersOfSockets.core.findRelatedKeysState,
81
- username,
82
- silo.store,
83
- )
84
- Internal.setIntoStore(
85
- socketRelatedKeysState,
86
- (keys) => (keys.clear(), keys.add(username)),
87
- silo.store,
88
- )
89
- Internal.setIntoStore(
90
- clientRelatedKeysState,
91
- (keys) => (keys.clear(), keys.add(socket.id)),
92
- silo.store,
93
- )
85
+ const socketState = findInStore(RTS.socketAtoms, socket.id, silo.store)
86
+ setIntoStore(socketState, socket, silo.store)
87
+ const usersOfSockets = RTS.usersOfSockets.in(silo.store)
88
+ usersOfSockets.relations.set(socket.id, username)
89
+ setIntoStore(RTS.userIndex, (index) => index.add(username), silo.store)
90
+ setIntoStore(RTS.socketIndex, (index) => index.add(socket.id), silo.store)
94
91
  console.log(`${username} connected on ${socket.id}`)
95
92
  next()
96
93
  } else {
@@ -104,7 +101,15 @@ export const setupRealtimeTestServer = (
104
101
 
105
102
  const dispose = () => {
106
103
  server.close()
107
- Internal.clearStore(silo.store)
104
+ const roomKeys = getFromStore(RTS.roomIndex, silo.store)
105
+ for (const roomKey of roomKeys) {
106
+ const roomState = findInStore(RTS.roomSelectors, roomKey, silo.store)
107
+ const room = getFromStore(roomState, silo.store)
108
+ if (room && !(room instanceof Promise)) {
109
+ room.kill()
110
+ }
111
+ }
112
+ silo.store.valueMap.clear()
108
113
  }
109
114
 
110
115
  return {
@@ -122,9 +127,9 @@ export const setupRealtimeTestClient = (
122
127
  const testClient = { dispose: () => {} }
123
128
  const init = () => {
124
129
  const socket: ClientSocket = io(`http://localhost:${port}/`, {
125
- auth: { token: `test`, username: name },
130
+ auth: { token: `test`, username: `${name}-${testNumber}` },
126
131
  })
127
- const silo = new AtomIO.Silo(name, Internal.IMPLICIT.STORE)
132
+ const silo = new AtomIO.Silo(name, IMPLICIT.STORE)
128
133
  for (const [key, value] of silo.store.valueMap.entries()) {
129
134
  if (Array.isArray(value)) {
130
135
  silo.store.valueMap.set(key, [...value])
@@ -147,8 +152,9 @@ export const setupRealtimeTestClient = (
147
152
  const prettyPrint = () => console.log(prettyDOM(renderResult.container))
148
153
 
149
154
  const dispose = () => {
155
+ renderResult.unmount()
150
156
  socket.disconnect()
151
- Internal.clearStore(silo.store)
157
+ clearStore(silo.store)
152
158
  }
153
159
  testClient.dispose = dispose
154
160
 
@@ -167,7 +173,11 @@ export const singleClient = (
167
173
  options: TestSetupOptions__SingleClient,
168
174
  ): RealtimeTestAPI__SingleClient => {
169
175
  const server = setupRealtimeTestServer(options)
170
- const client = setupRealtimeTestClient(options, `CLIENT`, server.port)
176
+ const client = setupRealtimeTestClient(
177
+ options,
178
+ `CLIENT-${testNumber}`,
179
+ server.port,
180
+ )
171
181
 
172
182
  return {
173
183
  client,
package/src/atom.ts CHANGED
@@ -45,23 +45,30 @@ export type RegularAtomFamilyOptions<T, K extends Json.Serializable> = {
45
45
  effects?: (key: K) => AtomEffect<T>[]
46
46
  }
47
47
 
48
- export type RegularAtomFamily<
49
- T,
50
- K extends Json.Serializable = Json.Serializable,
51
- > = ((key: K) => RegularAtomToken<T>) & {
52
- key: string
53
- type: `atom_family`
54
- subject: Subject<RegularAtomToken<T>>
55
- install: (store: Store) => void
56
- __T?: T
57
- __K?: K
58
- }
59
48
  export type RegularAtomFamilyToken<T, K extends Json.Serializable> = {
60
49
  key: string
61
50
  type: `atom_family`
62
51
  __T?: T
63
52
  __K?: K
64
53
  }
54
+ // biome-ignore format: intersection
55
+ export type RegularAtomFamilyTokenWithCall<
56
+ T,
57
+ K extends Json.Serializable,
58
+ > =
59
+ & RegularAtomFamilyToken<T, K>
60
+ & {
61
+ /** @deprecated Prefer the `findState`, `findInStore`, or `find` functions. */
62
+ (key: K): RegularAtomToken<T>
63
+ }
64
+ // biome-ignore format: intersection
65
+ export type RegularAtomFamily<T, K extends Json.Serializable> =
66
+ & RegularAtomFamilyToken<T, K>
67
+ & {
68
+ (key: K): RegularAtomToken<T>
69
+ subject: Subject<RegularAtomToken<T>>
70
+ install: (store: Store) => void
71
+ }
65
72
 
66
73
  // biome-ignore format: intersection
67
74
  export type MutableAtomFamilyOptions<
@@ -77,23 +84,6 @@ export type MutableAtomFamilyOptions<
77
84
  mutable: true,
78
85
  }
79
86
 
80
- // biome-ignore format: intersection
81
- export type MutableAtomFamily<
82
- T extends Transceiver<any>,
83
- J extends Json.Serializable,
84
- K extends Json.Serializable,
85
- > =
86
- & JsonInterface<T, J>
87
- & ((key: K) => MutableAtomToken<T, J>)
88
- & {
89
- key: string
90
- type: `mutable_atom_family`
91
- subject: Subject<MutableAtomToken<T, J>>
92
- install: (store: Store) => void
93
- __T?: T
94
- __J?: J
95
- __K?: K
96
- }
97
87
  export type MutableAtomFamilyToken<
98
88
  T extends Transceiver<any>,
99
89
  J extends Json.Serializable,
@@ -105,6 +95,30 @@ export type MutableAtomFamilyToken<
105
95
  __J?: J
106
96
  __K?: K
107
97
  }
98
+ // biome-ignore format: intersection
99
+ export type MutableAtomFamilyTokenWithCall<
100
+ T extends Transceiver<any>,
101
+ J extends Json.Serializable,
102
+ K extends Json.Serializable,
103
+ > =
104
+ & MutableAtomFamilyToken<T, J, K>
105
+ & {
106
+ /** @deprecated Prefer the `findState`, `findInStore`, or `find` functions. */
107
+ (key: K): MutableAtomToken<T, J>
108
+ }
109
+ // biome-ignore format: intersection
110
+ export type MutableAtomFamily<
111
+ T extends Transceiver<any>,
112
+ J extends Json.Serializable,
113
+ K extends Json.Serializable,
114
+ > =
115
+ & JsonInterface<T, J>
116
+ & MutableAtomFamilyToken<T, J, K>
117
+ & {
118
+ (key: K): MutableAtomToken<T, J>
119
+ subject: Subject<MutableAtomToken<T, J>>
120
+ install: (store: Store) => void
121
+ }
108
122
 
109
123
  export type AtomFamily<T, K extends Json.Serializable = Json.Serializable> =
110
124
  | MutableAtomFamily<T extends Transceiver<any> ? T : never, any, K>
@@ -117,14 +131,18 @@ export function atomFamily<
117
131
  T extends Transceiver<any>,
118
132
  J extends Json.Serializable,
119
133
  K extends Json.Serializable,
120
- >(options: MutableAtomFamilyOptions<T, J, K>): MutableAtomFamily<T, J, K>
134
+ >(
135
+ options: MutableAtomFamilyOptions<T, J, K>,
136
+ ): MutableAtomFamilyTokenWithCall<T, J, K>
121
137
  export function atomFamily<T, K extends Json.Serializable>(
122
138
  options: RegularAtomFamilyOptions<T, K>,
123
- ): RegularAtomFamily<T, K>
139
+ ): RegularAtomFamilyTokenWithCall<T, K>
124
140
  export function atomFamily<T, K extends Json.Serializable>(
125
141
  options:
126
142
  | MutableAtomFamilyOptions<any, any, any>
127
143
  | RegularAtomFamilyOptions<T, K>,
128
- ): MutableAtomFamily<any, any, any> | RegularAtomFamily<T, K> {
144
+ ):
145
+ | MutableAtomFamilyTokenWithCall<any, any, any>
146
+ | RegularAtomFamilyTokenWithCall<T, K> {
129
147
  return createAtomFamily(options, IMPLICIT.STORE)
130
148
  }
package/src/logger.ts CHANGED
@@ -6,20 +6,23 @@ const LoggerIconDictionary = {
6
6
  "⏮️": `Transaction undo`,
7
7
  "⏳": `Timeline event partially captured`,
8
8
  "⏹️": `Time-travel complete`,
9
- "💁": `Notice`,
10
- "🔄": `Realtime transaction synchronized`,
11
9
  "✅": `Realtime transaction success`,
12
10
  "✨": `Computation complete`,
13
11
  "❌": `Conflict prevents attempted action`,
14
12
  "⭕": `Operation start`,
15
13
  "🐞": `Possible bug in AtomIO`,
16
14
  "👀": `Subscription added`,
15
+ "👋": `Greeting`,
16
+ "👍": `Realtime acknowledgment`,
17
17
  "👪": `Family member added`,
18
+ "💁": `Notice`,
19
+ "💥": `Caught`,
18
20
  "📁": `Stow update`,
19
21
  "📃": `Copy mutable`,
20
22
  "📖": `Read state`,
21
23
  "📝": `Write state`,
22
24
  "📢": `Notify subscribers`,
25
+ "🔄": `Realtime transaction synchronized`,
23
26
  "🔌": `Register dependency`,
24
27
  "🔍": `Discover root`,
25
28
  "🔥": `Delete state`,
@@ -27,8 +30,8 @@ const LoggerIconDictionary = {
27
30
  "🔨": `Create immutable atom`,
28
31
  "🔴": `Operation complete`,
29
32
  "🗑": `Evict cached value`,
30
- "💥": `Caught`,
31
33
  "🙈": `Subscription canceled`,
34
+ "🚀": `Performance measure`,
32
35
  "🛄": `Apply transaction`,
33
36
  "🛠️": `Install atom into store`,
34
37
  "🛫": `Begin transaction`,
@@ -36,14 +39,16 @@ const LoggerIconDictionary = {
36
39
  "🧮": `Computing selector`,
37
40
  "🧹": `Prepare to evict`,
38
41
  "🪂": `Abort transaction`,
39
- "🚀": `Performance measure`,
42
+ "🤞": `Realtime optimistic update enqueued`,
43
+ "👈": `Realtime confirmed update enqueued`,
44
+ "⚖️": `Realtime update beginning reconciliation`,
40
45
  } as const
41
46
  export type LoggerIcon = keyof typeof LoggerIconDictionary
42
47
  export type TokenDenomination =
43
48
  | `atom`
49
+ | `continuity`
44
50
  | `mutable_atom`
45
51
  | `readonly_selector`
46
- | `realtime_sync_group`
47
52
  | `selector`
48
53
  | `state`
49
54
  | `timeline`
package/src/selector.ts CHANGED
@@ -41,41 +41,58 @@ export type ReadonlySelectorFamilyOptions<T, K extends Json.Serializable> = {
41
41
  get: (key: K) => Read<() => T>
42
42
  }
43
43
 
44
- export type WritableSelectorFamily<
45
- T,
46
- K extends Json.Serializable = Json.Serializable,
47
- > = ((key: K) => WritableSelectorToken<T>) & {
48
- key: string
49
- type: `selector_family`
50
- subject: Subject<WritableSelectorToken<T>>
51
- install: (store: Store) => void
52
- __T?: T
53
- __K?: K
54
- }
55
44
  export type WritableSelectorFamilyToken<T, K extends Json.Serializable> = {
56
45
  key: string
57
46
  type: `selector_family`
58
47
  __T?: T
59
48
  __K?: K
60
49
  }
61
-
62
- export type ReadonlySelectorFamily<
50
+ // biome-ignore format: intersection
51
+ export type WritableSelectorFamilyTokenWithCall<
63
52
  T,
64
- K extends Json.Serializable = Json.Serializable,
65
- > = ((key: K) => ReadonlySelectorToken<T>) & {
66
- key: string
67
- type: `readonly_selector_family`
68
- subject: Subject<ReadonlySelectorToken<T>>
69
- install: (store: Store) => void
70
- __T?: T
71
- __K?: K
72
- }
53
+ K extends Json.Serializable,
54
+ > =
55
+ & WritableSelectorFamilyToken<T, K>
56
+ & {
57
+ /** @deprecated Prefer the `findState`, `findInStore`, or `find` functions. */
58
+ (key: K): WritableSelectorToken<T>
59
+ }
60
+ // biome-ignore format: intersection
61
+ export type WritableSelectorFamily<T, K extends Json.Serializable> =
62
+ & WritableSelectorFamilyToken<T, K>
63
+ & {
64
+ (key: K): WritableSelectorToken<T>
65
+ subject: Subject<WritableSelectorToken<T>>
66
+ install: (store: Store) => void
67
+ }
68
+
73
69
  export type ReadonlySelectorFamilyToken<T, K extends Json.Serializable> = {
74
70
  key: string
75
71
  type: `readonly_selector_family`
76
72
  __T?: T
77
73
  __K?: K
78
74
  }
75
+ // biome-ignore format: intersection
76
+ export type ReadonlySelectorFamilyTokenWithCall<
77
+ T,
78
+ K extends Json.Serializable,
79
+ > =
80
+ & ReadonlySelectorFamilyToken<T, K>
81
+ & {
82
+ /** @deprecated Prefer the `findState`, `findInStore`, or `find` functions. */
83
+ (key: K): ReadonlySelectorToken<T>
84
+ }
85
+ // biome-ignore format: intersection
86
+ export type ReadonlySelectorFamily<T, K extends Json.Serializable> =
87
+ & ((key: K) => ReadonlySelectorToken<T>)
88
+ & {
89
+ key: string
90
+ type: `readonly_selector_family`
91
+ subject: Subject<ReadonlySelectorToken<T>>
92
+ install: (store: Store) => void
93
+ __T?: T
94
+ __K?: K
95
+ }
79
96
 
80
97
  export type SelectorFamily<T, K extends Json.Serializable> =
81
98
  | ReadonlySelectorFamily<T, K>
@@ -86,14 +103,16 @@ export type SelectorFamilyToken<T, K extends Json.Serializable> =
86
103
 
87
104
  export function selectorFamily<T, K extends Json.Serializable>(
88
105
  options: WritableSelectorFamilyOptions<T, K>,
89
- ): WritableSelectorFamily<T, K>
106
+ ): WritableSelectorFamilyTokenWithCall<T, K>
90
107
  export function selectorFamily<T, K extends Json.Serializable>(
91
108
  options: ReadonlySelectorFamilyOptions<T, K>,
92
- ): ReadonlySelectorFamily<T, K>
109
+ ): ReadonlySelectorFamilyTokenWithCall<T, K>
93
110
  export function selectorFamily<T, K extends Json.Serializable>(
94
111
  options:
95
112
  | ReadonlySelectorFamilyOptions<T, K>
96
113
  | WritableSelectorFamilyOptions<T, K>,
97
- ): ReadonlySelectorFamily<T, K> | WritableSelectorFamily<T, K> {
114
+ ):
115
+ | ReadonlySelectorFamilyTokenWithCall<T, K>
116
+ | WritableSelectorFamilyTokenWithCall<T, K> {
98
117
  return createSelectorFamily(options, IMPLICIT.STORE)
99
118
  }
package/src/subscribe.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { Store } from "atom.io/internal"
2
2
  import {
3
3
  IMPLICIT,
4
+ arbitrary,
4
5
  subscribeToState,
5
6
  subscribeToTimeline,
6
7
  subscribeToTransaction,
@@ -49,7 +50,7 @@ export function subscribe<M extends TimelineManageable>(
49
50
  export function subscribe(
50
51
  token: ReadableToken<any> | TimelineToken<any> | TransactionToken<any>,
51
52
  handleUpdate: (update: any) => void,
52
- key: string = Math.random().toString(36).slice(2),
53
+ key: string = arbitrary(),
53
54
  store = IMPLICIT.STORE,
54
55
  ): () => void {
55
56
  switch (token.type) {
package/src/timeline.ts CHANGED
@@ -6,14 +6,14 @@ import type {
6
6
  } from "atom.io/internal"
7
7
  import { IMPLICIT, createTimeline, timeTravel } from "atom.io/internal"
8
8
 
9
- import type { AtomFamily, AtomToken } from "."
9
+ import type { AtomFamilyToken, AtomToken } from "."
10
10
 
11
- export type TimelineManageable = AtomFamily<any, any> | AtomToken<any>
11
+ export type TimelineManageable = AtomFamilyToken<any, any> | AtomToken<any>
12
12
 
13
- export type TimelineToken<_> = {
13
+ export type TimelineToken<M> = {
14
14
  key: string
15
15
  type: `timeline`
16
- __brand?: _
16
+ __M?: M
17
17
  }
18
18
 
19
19
  export type TimelineOptions<ManagedAtom extends TimelineManageable> = {
@@ -1,5 +1,10 @@
1
- import type { EnvironmentData, Store } from "atom.io/internal"
2
- import { IMPLICIT, createTransaction, withdraw } from "atom.io/internal"
1
+ import type { EnvironmentData } from "atom.io/internal"
2
+ import {
3
+ IMPLICIT,
4
+ actUponStore,
5
+ arbitrary,
6
+ createTransaction,
7
+ } from "atom.io/internal"
3
8
 
4
9
  import type {
5
10
  KeyedStateUpdate,
@@ -77,18 +82,9 @@ export function transaction<ƒ extends ƒn>(
77
82
  return createTransaction(options, IMPLICIT.STORE)
78
83
  }
79
84
 
80
- export const runTransaction =
81
- extends ƒn>(
82
- token: TransactionToken<ƒ>,
83
- id?: string,
84
- store: Store = IMPLICIT.STORE,
85
- ) =>
86
- (...parameters: Parameters<ƒ>): ReturnType<ƒ> => {
87
- const tx = withdraw(token, store)
88
- if (tx) {
89
- return tx.run(parameters, id)
90
- }
91
- throw new Error(
92
- `Cannot run transaction "${token.key}": transaction not found in store "${store.config.name}".`,
93
- )
94
- }
85
+ export function runTransaction extends ƒn>(
86
+ token: TransactionToken<ƒ>,
87
+ id = arbitrary(),
88
+ ): (...parameters: Parameters<ƒ>) => ReturnType<ƒ> {
89
+ return actUponStore(token, id, IMPLICIT.STORE)
90
+ }
package/src/validators.ts CHANGED
@@ -1,23 +1,29 @@
1
1
  import type {
2
2
  MutableAtomFamily,
3
+ MutableAtomFamilyToken,
3
4
  MutableAtomToken,
4
5
  ReadableFamily,
6
+ ReadableFamilyToken,
5
7
  ReadableToken,
6
8
  ReadonlySelectorFamily,
9
+ ReadonlySelectorFamilyToken,
7
10
  ReadonlySelectorToken,
8
11
  RegularAtomFamily,
12
+ RegularAtomFamilyToken,
9
13
  RegularAtomToken,
10
14
  WritableFamily,
15
+ WritableFamilyToken,
11
16
  WritableSelectorFamily,
17
+ WritableSelectorFamilyToken,
12
18
  WritableSelectorToken,
13
19
  WritableToken,
14
20
  } from "atom.io"
15
21
 
16
22
  export type TokenType<
17
- Comparison extends ReadableFamily<any, any> | ReadableToken<any>,
23
+ Comparison extends ReadableFamilyToken<any, any> | ReadableToken<any>,
18
24
  > = Comparison extends ReadableToken<infer RepresentedValue>
19
25
  ? RepresentedValue
20
- : Comparison extends ReadableFamily<infer RepresentedValue, any>
26
+ : Comparison extends ReadableFamilyToken<infer RepresentedValue, any>
21
27
  ? RepresentedValue
22
28
  : never
23
29
 
@@ -52,31 +58,31 @@ export function isToken<KnownToken extends ReadableToken<any>>(
52
58
  return knownToken.key === unknownToken.key
53
59
  }
54
60
 
55
- export function belongsTo<Family extends RegularAtomFamily<any, any>>(
61
+ export function belongsTo<Family extends RegularAtomFamilyToken<any, any>>(
56
62
  family: Family,
57
63
  unknownToken: ReadableToken<any>,
58
64
  ): unknownToken is RegularAtomToken<TokenType<Family>>
59
- export function belongsTo<Family extends MutableAtomFamily<any, any, any>>(
65
+ export function belongsTo<Family extends MutableAtomFamilyToken<any, any, any>>(
60
66
  family: Family,
61
67
  unknownToken: ReadableToken<any>,
62
68
  ): unknownToken is MutableAtomToken<TokenType<Family>, any>
63
- export function belongsTo<Family extends WritableSelectorFamily<any, any>>(
69
+ export function belongsTo<Family extends WritableSelectorFamilyToken<any, any>>(
64
70
  family: Family,
65
71
  unknownToken: ReadableToken<any>,
66
72
  ): unknownToken is WritableSelectorToken<TokenType<Family>>
67
- export function belongsTo<Family extends ReadonlySelectorFamily<any, any>>(
73
+ export function belongsTo<Family extends ReadonlySelectorFamilyToken<any, any>>(
68
74
  family: Family,
69
75
  unknownToken: ReadableToken<any>,
70
76
  ): unknownToken is ReadonlySelectorToken<TokenType<Family>>
71
- export function belongsTo<Family extends WritableFamily<any, any>>(
77
+ export function belongsTo<Family extends WritableFamilyToken<any, any>>(
72
78
  family: Family,
73
79
  unknownToken: ReadableToken<any>,
74
80
  ): unknownToken is WritableToken<TokenType<Family>>
75
- export function belongsTo<Family extends ReadableFamily<any, any>>(
81
+ export function belongsTo<Family extends ReadableFamilyToken<any, any>>(
76
82
  family: Family,
77
83
  unknownToken: ReadableToken<any>,
78
84
  ): unknownToken is ReadableToken<TokenType<Family>>
79
- export function belongsTo<Family extends ReadableFamily<any, any>>(
85
+ export function belongsTo<Family extends ReadableFamilyToken<any, any>>(
80
86
  family: Family,
81
87
  unknownToken: ReadableToken<any>,
82
88
  ): unknownToken is ReadableToken<TokenType<Family>> {
@@ -1,11 +0,0 @@
1
- // internal/src/transaction/is-root-store.ts
2
- function isRootStore(store) {
3
- return `epoch` in store.transactionMeta;
4
- }
5
- function isChildStore(store) {
6
- return `phase` in store.transactionMeta;
7
- }
8
-
9
- export { isChildStore, isRootStore };
10
- //# sourceMappingURL=out.js.map
11
- //# sourceMappingURL=chunk-H4Q5FTPZ.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../internal/src/transaction/is-root-store.ts"],"names":[],"mappings":";AAgBO,SAAS,YAAY,OAAkC;AAC7D,SAAO,WAAW,MAAM;AACzB;AAEO,SAAS,aAAa,OAAmC;AAC/D,SAAO,WAAW,MAAM;AACzB","sourcesContent":["import type { ƒn } from \"atom.io\"\n\nimport type { TransactionEpoch, TransactionProgress } from \".\"\nimport type { Store } from \"../store\"\n\nexport interface RootStore extends Store {\n\ttransactionMeta: TransactionEpoch\n\tparent: null\n\tchild: ChildStore | null\n}\nexport interface ChildStore extends Store {\n\ttransactionMeta: TransactionProgress<ƒn>\n\tparent: ChildStore | RootStore\n\tchild: ChildStore | null\n}\n\nexport function isRootStore(store: Store): store is RootStore {\n\treturn `epoch` in store.transactionMeta\n}\n\nexport function isChildStore(store: Store): store is ChildStore {\n\treturn `phase` in store.transactionMeta\n}\n"]}
@@ -1,19 +0,0 @@
1
- import { newest } from ".."
2
- import type { Atom, Store } from ".."
3
- import { copyMutableIfNeeded } from "./copy-mutable-if-needed"
4
-
5
- export function copyMutableIfWithinTransaction<T>(
6
- oldValue: T,
7
- atom: Atom<T>,
8
- store: Store,
9
- ): T {
10
- const target = newest(store)
11
- const parent = target.parent
12
- if (parent !== null) {
13
- if (atom.type === `mutable_atom`) {
14
- const copiedValue = copyMutableIfNeeded(atom, parent, target)
15
- return copiedValue
16
- }
17
- }
18
- return oldValue
19
- }