atom.io 0.22.0 → 0.23.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 (82) hide show
  1. package/data/dist/index.cjs +17 -1
  2. package/data/dist/index.js +1 -1
  3. package/data/src/join.ts +30 -1
  4. package/dist/chunk-6MLFYN32.js +18 -0
  5. package/dist/{chunk-JA4V7TJY.js → chunk-7DT3PVS3.js} +18 -2
  6. package/dist/chunk-OAYGID5B.js +27 -0
  7. package/dist/index.cjs +2 -11
  8. package/dist/index.d.ts +51 -23
  9. package/dist/index.js +2 -11
  10. package/ephemeral/dist/index.d.ts +12 -0
  11. package/eslint-plugin/dist/index.cjs +0 -1
  12. package/eslint-plugin/dist/index.d.ts +73 -0
  13. package/eslint-plugin/dist/index.js +0 -1
  14. package/eslint-plugin/src/rules/lifespan.ts +0 -1
  15. package/immortal/dist/index.cjs +180 -20
  16. package/immortal/dist/index.d.ts +103 -0
  17. package/immortal/dist/index.js +134 -19
  18. package/immortal/src/index.ts +1 -0
  19. package/immortal/src/make-molecule.ts +222 -0
  20. package/immortal/src/molecule.ts +49 -16
  21. package/immortal/src/seek-state.ts +15 -2
  22. package/internal/dist/index.cjs +1119 -754
  23. package/internal/dist/index.d.ts +109 -12
  24. package/internal/dist/index.js +1098 -760
  25. package/internal/src/atom/create-regular-atom.ts +0 -2
  26. package/internal/src/atom/create-standalone-atom.ts +6 -2
  27. package/internal/src/atom/dispose-atom.ts +22 -2
  28. package/internal/src/families/create-readonly-selector-family.ts +7 -2
  29. package/internal/src/families/create-regular-atom-family.ts +6 -2
  30. package/internal/src/families/create-writable-selector-family.ts +7 -2
  31. package/internal/src/families/dispose-from-store.ts +22 -0
  32. package/internal/src/families/find-in-store.ts +0 -1
  33. package/internal/src/families/index.ts +1 -0
  34. package/internal/src/families/init-family-member.ts +22 -1
  35. package/internal/src/families/seek-in-store.ts +23 -6
  36. package/internal/src/ingest-updates/index.ts +1 -0
  37. package/internal/src/ingest-updates/ingest-creation-disposal.ts +104 -0
  38. package/internal/src/ingest-updates/ingest-transaction-update.ts +26 -4
  39. package/internal/src/mutable/create-mutable-atom-family.ts +6 -2
  40. package/internal/src/mutable/create-mutable-atom.ts +0 -2
  41. package/internal/src/mutable/get-json-token.ts +0 -1
  42. package/internal/src/mutable/tracker-family.ts +7 -7
  43. package/internal/src/not-found-error.ts +5 -0
  44. package/internal/src/selector/create-readonly-selector.ts +2 -3
  45. package/internal/src/selector/create-standalone-selector.ts +6 -2
  46. package/internal/src/selector/create-writable-selector.ts +2 -3
  47. package/internal/src/selector/dispose-selector.ts +32 -5
  48. package/internal/src/selector/register-selector.ts +2 -0
  49. package/internal/src/set-state/stow-update.ts +5 -1
  50. package/internal/src/store/deposit.ts +41 -7
  51. package/internal/src/store/store.ts +11 -0
  52. package/internal/src/store/withdraw.ts +28 -1
  53. package/internal/src/timeline/add-atom-to-timeline.ts +206 -182
  54. package/internal/src/timeline/create-timeline.ts +181 -60
  55. package/internal/src/timeline/time-travel.ts +20 -0
  56. package/internal/src/transaction/apply-transaction.ts +2 -12
  57. package/internal/src/transaction/build-transaction.ts +11 -2
  58. package/introspection/dist/index.cjs +2 -1
  59. package/introspection/dist/index.js +2 -1
  60. package/introspection/src/attach-timeline-family.ts +1 -0
  61. package/json/dist/index.cjs +3 -3
  62. package/json/dist/index.js +6 -5
  63. package/json/src/select-json-family.ts +3 -4
  64. package/package.json +8 -5
  65. package/react-devtools/dist/index.cjs +58 -47
  66. package/react-devtools/dist/index.js +60 -48
  67. package/react-devtools/src/TimelineIndex.tsx +15 -13
  68. package/react-devtools/src/Updates.tsx +41 -32
  69. package/realtime-server/dist/index.cjs +21 -10
  70. package/realtime-server/dist/index.d.ts +1 -1
  71. package/realtime-server/dist/index.js +21 -11
  72. package/realtime-server/src/realtime-server-stores/server-sync-store.ts +21 -11
  73. package/realtime-testing/dist/index.cjs +1 -0
  74. package/realtime-testing/dist/index.js +1 -1
  75. package/src/atom.ts +9 -3
  76. package/src/dispose-state.ts +3 -12
  77. package/src/index.ts +4 -0
  78. package/src/selector.ts +3 -3
  79. package/src/subscribe.ts +8 -4
  80. package/src/timeline.ts +18 -1
  81. package/src/transaction.ts +56 -4
  82. package/dist/chunk-BF4MVQF6.js +0 -44
@@ -0,0 +1,222 @@
1
+ import type { Flat, MoleculeCreation, MoleculeDisposal } from "atom.io"
2
+ import * as Internal from "atom.io/internal"
3
+ import { type Json, stringifyJson } from "atom.io/json"
4
+
5
+ import { Molecule } from "./molecule"
6
+
7
+ export type MoleculeConstructor<
8
+ Key extends Json.Serializable,
9
+ Struct extends { [key: string]: any },
10
+ Params extends any[],
11
+ > = new (
12
+ context: Molecule<any>[],
13
+ token: MoleculeToken<Key, Struct, Params>,
14
+ ...params: Params
15
+ ) => Molecule<Key> & Struct
16
+
17
+ export type MoleculeFamilyToken<
18
+ Key extends Json.Serializable,
19
+ Struct extends { [key: string]: any },
20
+ Params extends any[],
21
+ > = {
22
+ key: string
23
+ type: `molecule_family`
24
+ __K?: Key
25
+ __S?: Struct
26
+ __P?: Params
27
+ }
28
+ export type MoleculeFamily<
29
+ Key extends Json.Serializable,
30
+ Struct extends { [key: string]: any },
31
+ Params extends any[],
32
+ > = Flat<
33
+ MoleculeFamilyToken<Key, Struct, Params> & {
34
+ subject: Internal.Subject<MoleculeCreation<Key> | MoleculeDisposal<Key>>
35
+ }
36
+ > &
37
+ MoleculeConstructor<Key, Struct, Params>
38
+
39
+ export type MoleculeToken<
40
+ Key extends Json.Serializable,
41
+ Struct extends { [key: string]: any },
42
+ Params extends any[],
43
+ > = {
44
+ key: Key
45
+ type: `molecule`
46
+ family?: MoleculeFamilyToken<Key, Struct, Params>
47
+ __S?: Struct
48
+ __P?: Params
49
+ }
50
+
51
+ export type MoleculeFamilyOptions<
52
+ Key extends Json.Serializable,
53
+ Struct extends { [key: string]: any },
54
+ Params extends any[],
55
+ > = {
56
+ key: string
57
+ new: (store: Internal.Store) => MoleculeConstructor<Key, Struct, Params>
58
+ }
59
+
60
+ export function createMoleculeFamily<
61
+ Key extends Json.Serializable,
62
+ Struct extends { [key: string]: any },
63
+ Params extends any[],
64
+ >(
65
+ options: MoleculeFamilyOptions<Key, Struct, Params>,
66
+ store: Internal.Store,
67
+ ): MoleculeFamilyToken<Key, Struct, Params> {
68
+ const subject = new Internal.Subject<
69
+ MoleculeCreation<Key> | MoleculeDisposal<Key>
70
+ >()
71
+ const token = {
72
+ key: options.key,
73
+ type: `molecule_family`,
74
+ } as const satisfies MoleculeFamilyToken<Key, Struct, Params>
75
+ const family = Object.assign(options.new(store), { ...token, subject })
76
+ store.moleculeFamilies.set(options.key, family)
77
+ return token
78
+ }
79
+
80
+ export function moleculeFamily<
81
+ Key extends Json.Serializable,
82
+ Struct extends { [key: string]: any },
83
+ Params extends any[],
84
+ >(
85
+ options: MoleculeFamilyOptions<Key, Struct, Params>,
86
+ ): MoleculeFamilyToken<Key, Struct, Params> {
87
+ return createMoleculeFamily(options, Internal.IMPLICIT.STORE)
88
+ }
89
+
90
+ export function makeMoleculeInStore<
91
+ Key extends Json.Serializable,
92
+ Struct extends { [key: string]: any },
93
+ Params extends any[],
94
+ >(
95
+ store: Internal.Store,
96
+ context: MoleculeToken<any, any, any> | MoleculeToken<any, any, any>[],
97
+ family: MoleculeFamilyToken<Key, Struct, Params>,
98
+ key: Key,
99
+ ...params: Params
100
+ ): MoleculeToken<Key, Struct, Params> {
101
+ const target = Internal.newest(store)
102
+
103
+ const token = {
104
+ type: `molecule`,
105
+ key,
106
+ family,
107
+ } as const satisfies MoleculeToken<Key, Struct, Params>
108
+
109
+ const contextArray = Array.isArray(context) ? context : [context]
110
+ const owners = contextArray
111
+ .map((ctx) => store.molecules.get(stringifyJson(ctx.key)))
112
+ .filter((m): m is Molecule<Key> => m !== undefined)
113
+ const Formula = Internal.withdraw(family, store)
114
+ const molecule = new Formula(owners, token, ...params)
115
+ target.molecules.set(stringifyJson(key), molecule)
116
+
117
+ const update = {
118
+ type: `molecule_creation`,
119
+ token,
120
+ family,
121
+ context: contextArray,
122
+ params,
123
+ } satisfies MoleculeCreation<Key>
124
+
125
+ const isTransaction =
126
+ Internal.isChildStore(target) && target.transactionMeta.phase === `building`
127
+ if (isTransaction) {
128
+ target.transactionMeta.update.updates.push(update)
129
+ } else {
130
+ Formula.subject.next(update)
131
+ }
132
+
133
+ return token
134
+ }
135
+ export function makeMolecule<
136
+ Key extends Json.Serializable,
137
+ Struct extends { [key: string]: any },
138
+ Params extends any[],
139
+ >(
140
+ context: MoleculeToken<any, any, any> | MoleculeToken<any, any, any>[],
141
+ family: MoleculeFamilyToken<Key, Struct, Params>,
142
+ key: Key,
143
+ ...params: Params
144
+ ): MoleculeToken<Key, Struct, Params> {
145
+ return makeMoleculeInStore(
146
+ Internal.IMPLICIT.STORE,
147
+ context,
148
+ family,
149
+ key,
150
+ ...params,
151
+ )
152
+ }
153
+
154
+ export function useMoleculeFromStore<
155
+ Key extends Json.Serializable,
156
+ Struct extends { [key: string]: any },
157
+ Params extends any[],
158
+ >(
159
+ token: MoleculeToken<Key, Struct, Params>,
160
+ store: Internal.Store,
161
+ ): (Molecule<Key> & Struct) | undefined {
162
+ const molecule = store.molecules.get(stringifyJson(token.key))
163
+ return molecule as Molecule<Key> & Struct
164
+ }
165
+ export function useMolecule<
166
+ Key extends Json.Serializable,
167
+ Struct extends { [key: string]: any },
168
+ Params extends any[],
169
+ >(
170
+ token: MoleculeToken<Key, Struct, Params>,
171
+ ): (Molecule<Key> & Struct) | undefined {
172
+ return useMoleculeFromStore(token, Internal.IMPLICIT.STORE)
173
+ }
174
+
175
+ export function disposeMolecule<
176
+ Key extends Json.Serializable,
177
+ Struct extends { [key: string]: any },
178
+ Params extends any[],
179
+ >(token: MoleculeToken<Key, Struct, Params>, store: Internal.Store): void {
180
+ const mole = useMoleculeFromStore(token, store)
181
+ if (!mole || !token.family) {
182
+ return // add error log
183
+ }
184
+ const { family } = token
185
+ const Formula = Internal.withdraw(family, store)
186
+ const disposalEvent: MoleculeDisposal<Key> = {
187
+ type: `molecule_disposal`,
188
+ token,
189
+ family,
190
+ context: mole.above,
191
+ familyKeys: mole.tokens
192
+ .map((t) => t.family?.key)
193
+ .filter((k): k is string => typeof k === `string`),
194
+ }
195
+ if (token.family) {
196
+ disposalEvent.family = token.family
197
+ }
198
+ const isTransaction =
199
+ Internal.isChildStore(store) && store.transactionMeta.phase === `building`
200
+ if (isTransaction) {
201
+ store.transactionMeta.update.updates.push(disposalEvent)
202
+ } else {
203
+ Formula.subject.next(disposalEvent)
204
+ }
205
+
206
+ mole.dispose()
207
+ }
208
+
209
+ export type MoleculeType<M extends MoleculeFamilyToken<any, any, any>> =
210
+ M extends MoleculeFamilyToken<any, infer T, any> ? T : never
211
+
212
+ export function makeRootMolecule(
213
+ key: Json.Serializable,
214
+ store: Internal.Store = Internal.IMPLICIT.STORE,
215
+ ): MoleculeToken<Json.Serializable, { [key: string]: unknown }, []> {
216
+ const molecule = new Molecule(store, undefined, { key, type: `molecule` })
217
+ store.molecules.set(stringifyJson(key), molecule)
218
+ return {
219
+ key,
220
+ type: `molecule`,
221
+ } as const
222
+ }
@@ -7,30 +7,58 @@ import type {
7
7
  ReadonlySelectorToken,
8
8
  RegularAtomFamilyToken,
9
9
  RegularAtomToken,
10
+ StateCreation,
11
+ StateDisposal,
10
12
  WritableFamilyToken,
11
13
  WritableSelectorFamilyToken,
12
14
  WritableSelectorToken,
13
15
  WritableToken,
14
16
  } from "atom.io"
15
- import { disposeState } from "atom.io"
16
17
  import type { Join, JoinToken } from "atom.io/data"
17
18
  import { getJoin } from "atom.io/data"
18
19
  import type { Store, Transceiver } from "atom.io/internal"
19
- import { getJsonFamily, IMPLICIT, initFamilyMember } from "atom.io/internal"
20
+ import {
21
+ disposeFromStore,
22
+ getJsonFamily,
23
+ initFamilyMember,
24
+ newest,
25
+ Subject,
26
+ } from "atom.io/internal"
20
27
  import { type Json, stringifyJson } from "atom.io/json"
21
28
 
29
+ import type { MoleculeFamilyToken, MoleculeToken } from "./make-molecule"
30
+
22
31
  export class Molecule<Key extends Json.Serializable> {
32
+ public readonly type = `molecule`
33
+ public get key(): Key {
34
+ return this.token.key
35
+ }
36
+ public readonly family?: MoleculeFamilyToken<Key, any, any>
37
+ public readonly above: Molecule<any>[]
23
38
  public readonly below: Molecule<any>[] = []
24
39
  public readonly tokens: ReadableToken<any>[] = []
25
40
  public readonly joins: Join<any, any, any, any>[] = []
41
+ public readonly subject = new Subject<
42
+ StateCreation<any> | StateDisposal<any>
43
+ >()
26
44
  public constructor(
27
- public readonly key: Key,
28
- public readonly above: Molecule<any>[] = [],
29
- public readonly store: Store = IMPLICIT.STORE,
45
+ public readonly store: Store,
46
+ above: Molecule<any> | Molecule<any>[] | undefined,
47
+ public readonly token: MoleculeToken<Key, any, any>,
30
48
  ) {
31
- store.molecules.set(stringifyJson(key), this)
32
- for (const parent of above) {
33
- parent.below.push(this)
49
+ // store.molecules.set(stringifyJson(key), this) // consider removing this
50
+ if (above) {
51
+ if (Array.isArray(above)) {
52
+ this.above = above
53
+ for (const parent of above) {
54
+ parent.below.push(this)
55
+ }
56
+ } else {
57
+ this.above = [above]
58
+ above.below.push(this)
59
+ }
60
+ } else {
61
+ this.above = []
34
62
  }
35
63
  }
36
64
 
@@ -55,24 +83,28 @@ export class Molecule<Key extends Json.Serializable> {
55
83
  token: ReadableFamilyToken<T, K>,
56
84
  ): ReadableToken<T>
57
85
  public bond(token: ReadableFamilyToken<any, any>): ReadableToken<any> {
58
- const state = initFamilyMember(token, this.key, this.store)
86
+ const state = initFamilyMember(token, this.token.key, this.store)
59
87
  if (token.type === `mutable_atom_family`) {
60
88
  const jsonFamily = getJsonFamily(token, this.store)
61
- const jsonState = initFamilyMember(jsonFamily, this.key, this.store)
89
+ const jsonState = initFamilyMember(jsonFamily, this.token.key, this.store)
62
90
  this.tokens.push(jsonState)
63
91
  }
64
92
  this.tokens.push(state)
93
+ this.subject.next({ type: `state_creation`, token: state })
65
94
  return state
66
95
  }
67
96
 
68
97
  public spawn<K extends Json.Serializable>(key: K): Molecule<K> {
69
- const child = new Molecule(key, [this], this.store)
98
+ const child = new Molecule(this.store, this, { key, type: `molecule` })
70
99
  return child
71
100
  }
72
101
 
73
102
  public with(molecule: Molecule<any>): (key: string) => Molecule<any> {
74
103
  return (key) => {
75
- const child = new Molecule(key, [this, molecule], this.store)
104
+ const child = new Molecule(this.store, [this, molecule], {
105
+ key,
106
+ type: `molecule`,
107
+ })
76
108
  return child
77
109
  }
78
110
  }
@@ -106,26 +138,27 @@ export class Molecule<Key extends Json.Serializable> {
106
138
  while (this.tokens.length > 0) {
107
139
  const token = this.tokens.pop()
108
140
  if (token) {
109
- disposeState(token, this.store)
141
+ disposeFromStore(token, this.store)
110
142
  }
111
143
  }
112
144
  while (this.joins.length > 0) {
113
145
  const join = this.joins.pop()
114
146
  if (join) {
115
- join.molecules.delete(stringifyJson(this.key))
147
+ join.molecules.delete(stringifyJson(this.token.key))
116
148
  }
117
149
  }
118
150
  }
119
151
 
120
152
  public join(token: JoinToken<any, any, any, any>): void {
121
153
  const join = getJoin(token, this.store)
122
- join.molecules.set(stringifyJson(this.key), this)
154
+ join.molecules.set(stringifyJson(this.token.key), this)
123
155
  this.joins.push(join)
124
156
  }
125
157
 
126
158
  private [Symbol.dispose](): void {
127
159
  this.clear()
128
- this.store.molecules.delete(stringifyJson(this.key))
160
+ const target = newest(this.store)
161
+ target.molecules.delete(stringifyJson(this.token.key))
129
162
  for (const parent of this.above) {
130
163
  parent.detach(this)
131
164
  }
@@ -16,6 +16,8 @@ import type { Transceiver } from "atom.io/internal"
16
16
  import { IMPLICIT, seekInStore } from "atom.io/internal"
17
17
  import type { Json } from "atom.io/json"
18
18
 
19
+ import type { MoleculeFamilyToken, MoleculeToken } from "./make-molecule"
20
+
19
21
  export function seekState<
20
22
  T extends Transceiver<any>,
21
23
  J extends Json.Serializable,
@@ -51,10 +53,21 @@ export function seekState<T, K extends Json.Serializable, Key extends K>(
51
53
  key: Key,
52
54
  ): ReadableToken<T> | undefined
53
55
 
56
+ export function seekState<
57
+ K extends Json.Serializable,
58
+ S extends { [key: string]: any },
59
+ >(
60
+ token: MoleculeFamilyToken<K, S, any[]>,
61
+ key: K,
62
+ ): MoleculeToken<K, S, any[]> | undefined
63
+
54
64
  export function seekState(
55
- token: ReadableFamilyToken<any, any>,
65
+ token: MoleculeFamilyToken<any, any, any> | ReadableFamilyToken<any, any>,
56
66
  key: Json.Serializable,
57
- ): ReadableToken<any> | undefined {
67
+ ): MoleculeToken<any, any, any> | ReadableToken<any> | undefined {
68
+ if (token.type === `molecule_family`) {
69
+ return seekInStore(token, key, IMPLICIT.STORE)
70
+ }
58
71
  const state = seekInStore(token, key, IMPLICIT.STORE)
59
72
  return state
60
73
  }