atom.io 0.34.1 → 0.35.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 (53) hide show
  1. package/dist/internal/index.d.ts +32 -41
  2. package/dist/internal/index.d.ts.map +1 -1
  3. package/dist/internal/index.js +106 -128
  4. package/dist/internal/index.js.map +1 -1
  5. package/dist/json/index.d.ts +19 -7
  6. package/dist/json/index.d.ts.map +1 -1
  7. package/dist/json/index.js +4 -0
  8. package/dist/json/index.js.map +1 -1
  9. package/dist/main/index.d.ts +704 -788
  10. package/dist/main/index.d.ts.map +1 -1
  11. package/dist/main/index.js +61 -33
  12. package/dist/main/index.js.map +1 -1
  13. package/dist/react-devtools/index.js +10 -10
  14. package/dist/react-devtools/index.js.map +1 -1
  15. package/dist/realtime/index.d.ts.map +1 -1
  16. package/dist/realtime/index.js +3 -5
  17. package/dist/realtime/index.js.map +1 -1
  18. package/dist/realtime-client/index.js +10 -10
  19. package/dist/realtime-client/index.js.map +1 -1
  20. package/dist/realtime-server/index.d.ts.map +1 -1
  21. package/dist/realtime-server/index.js +8 -10
  22. package/dist/realtime-server/index.js.map +1 -1
  23. package/package.json +12 -12
  24. package/src/internal/atom/create-regular-atom.ts +1 -0
  25. package/src/internal/atom/index.ts +0 -1
  26. package/src/internal/families/index.ts +0 -1
  27. package/src/internal/index.ts +111 -89
  28. package/src/internal/join/join-internal.ts +3 -4
  29. package/src/internal/mutable/create-mutable-atom-family.ts +0 -1
  30. package/src/internal/mutable/create-mutable-atom.ts +1 -1
  31. package/src/internal/selector/register-selector.ts +2 -2
  32. package/src/json/entries.ts +10 -3
  33. package/src/json/index.ts +40 -17
  34. package/src/main/atom.ts +68 -115
  35. package/src/main/dispose-state.ts +0 -2
  36. package/src/main/find-state.ts +3 -9
  37. package/src/main/get-state.ts +0 -2
  38. package/src/main/index.ts +1 -176
  39. package/src/main/join.ts +12 -20
  40. package/src/main/reset-state.ts +0 -2
  41. package/src/main/selector.ts +5 -72
  42. package/src/main/set-state.ts +1 -4
  43. package/src/main/silo.ts +14 -5
  44. package/src/main/subscribe.ts +0 -7
  45. package/src/main/timeline.ts +24 -32
  46. package/src/main/tokens.ts +247 -0
  47. package/src/main/transaction.ts +17 -55
  48. package/src/main/validators.ts +1 -1
  49. package/src/react-devtools/store.ts +61 -45
  50. package/src/realtime/shared-room-store.ts +3 -5
  51. package/src/realtime-server/realtime-server-stores/server-user-store.ts +3 -5
  52. package/src/internal/atom/create-standalone-atom.ts +0 -39
  53. package/src/internal/families/create-atom-family.ts +0 -38
@@ -1,4 +1,3 @@
1
- import type { findState, getState, setState } from "atom.io"
2
1
  import type { EnvironmentData, Func, Transceiver } from "atom.io/internal"
3
2
  import {
4
3
  actUponStore,
@@ -8,65 +7,51 @@ import {
8
7
  } from "atom.io/internal"
9
8
  import type { Canonical, Json, stringified } from "atom.io/json"
10
9
 
10
+ import type { disposeState } from "./dispose-state"
11
+ import type { findState } from "./find-state"
12
+ import type { getState } from "./get-state"
13
+ import type { resetState } from "./reset-state"
14
+ import type { setState } from "./set-state"
15
+ import type { KeyedStateUpdate } from "./subscribe"
11
16
  import type {
12
- disposeState,
13
- KeyedStateUpdate,
14
17
  MutableAtomToken,
15
18
  ReadableToken,
16
- TokenType,
19
+ TransactionToken,
17
20
  WritablePureSelectorToken,
18
- } from "."
19
- import type { resetState } from "./reset-state"
21
+ } from "./tokens"
22
+ import type { TokenType } from "./validators"
20
23
 
21
- /** @public */
22
- export type TransactionToken<F extends Func> = {
23
- /** The unique identifier of the transaction */
24
- key: string
25
- /** Discriminator */
26
- type: `transaction`
27
- /** Never present. This is a marker that preserves the type of the transaction function */
28
- __F?: F
29
- }
30
-
31
- /** @public */
32
24
  export type StateCreation<Token extends ReadableToken<any>> = {
33
25
  type: `state_creation`
34
26
  token: Token
35
27
  }
36
- /** @public */
28
+ export type StateDisposal<Token extends ReadableToken<any>> =
29
+ | AtomDisposal<Token>
30
+ | SelectorDisposal<Token>
31
+
37
32
  export type AtomDisposal<Token extends ReadableToken<any>> = {
38
33
  type: `state_disposal`
39
34
  subType: `atom`
40
35
  token: Token
41
36
  value: TokenType<Token>
42
37
  }
43
- /** @public */
44
38
  export type SelectorDisposal<Token extends ReadableToken<any>> = {
45
39
  type: `state_disposal`
46
40
  subType: `selector`
47
41
  token: Token
48
42
  }
49
- /** @public */
50
- export type StateDisposal<Token extends ReadableToken<any>> =
51
- | AtomDisposal<Token>
52
- | SelectorDisposal<Token>
53
43
 
54
- /** @public */
55
44
  export type MoleculeCreation = {
56
45
  type: `molecule_creation`
57
46
  key: Canonical
58
47
  provenance: Canonical
59
48
  }
60
-
61
- /** @public */
62
49
  export type MoleculeDisposal = {
63
50
  type: `molecule_disposal`
64
51
  key: Canonical
65
52
  provenance: stringified<Canonical>[]
66
53
  values: [key: string, value: any][]
67
54
  }
68
-
69
- /** @public */
70
55
  export type MoleculeTransfer = {
71
56
  type: `molecule_transfer`
72
57
  key: Canonical
@@ -74,7 +59,6 @@ export type MoleculeTransfer = {
74
59
  to: Canonical[]
75
60
  }
76
61
 
77
- /** @public */
78
62
  export type TransactionUpdateContent =
79
63
  | KeyedStateUpdate<unknown>
80
64
  | MoleculeCreation
@@ -84,7 +68,6 @@ export type TransactionUpdateContent =
84
68
  | StateDisposal<ReadableToken<unknown>>
85
69
  | TransactionUpdate<Func>
86
70
 
87
- /** @public */
88
71
  export type TransactionUpdate<F extends Func> = {
89
72
  type: `transaction_update`
90
73
  key: string
@@ -95,18 +78,8 @@ export type TransactionUpdate<F extends Func> = {
95
78
  output: ReturnType<F>
96
79
  }
97
80
 
98
- /** @public */
99
- export type GetterToolkit = Pick<SetterToolkit, `find` | `get` | `json`>
100
- /** @public */
101
- export type SetterToolkit = Readonly<{
102
- get: typeof getState
103
- set: typeof setState
104
- find: typeof findState
105
- json: <T extends Transceiver<any>, J extends Json.Serializable>(
106
- state: MutableAtomToken<T, J>,
107
- ) => WritablePureSelectorToken<J>
108
- }>
109
- /** @public */
81
+ export type ReaderToolkit = Pick<ActorToolkit, `find` | `get` | `json`>
82
+ export type WriterToolkit = Pick<ActorToolkit, `find` | `get` | `json` | `set`>
110
83
  export type ActorToolkit = Readonly<{
111
84
  get: typeof getState
112
85
  set: typeof setState
@@ -120,29 +93,20 @@ export type ActorToolkit = Readonly<{
120
93
  env: () => EnvironmentData
121
94
  }>
122
95
 
123
- /** @public */
124
96
  export type Read<F extends Func> = (
125
- toolkit: GetterToolkit,
97
+ toolkit: ReaderToolkit,
126
98
  ...parameters: Parameters<F>
127
99
  ) => ReturnType<F>
128
-
129
- /** @public */
130
100
  export type Write<F extends Func> = (
131
- toolkit: SetterToolkit,
101
+ toolkit: WriterToolkit,
132
102
  ...parameters: Parameters<F>
133
103
  ) => ReturnType<F>
134
-
135
- /** @public */
136
104
  export type Transact<F extends Func> = (
137
105
  toolkit: ActorToolkit,
138
106
  ...parameters: Parameters<F>
139
107
  ) => ReturnType<F>
140
-
141
- /** @public */
142
108
  export type TransactionIO<Token extends TransactionToken<any>> =
143
109
  Token extends TransactionToken<infer F> ? F : never
144
-
145
- /** @public */
146
110
  export type TransactionOptions<F extends Func> = {
147
111
  /** The unique identifier of the transaction */
148
112
  key: string
@@ -151,7 +115,6 @@ export type TransactionOptions<F extends Func> = {
151
115
  }
152
116
 
153
117
  /**
154
- * @public
155
118
  * Create a transaction, a mechanism for batching updates multiple states in a single, all-or-nothing operation
156
119
  * @param options - {@link TransactionOptions}
157
120
  * @returns A reference to the transaction created: a {@link TransactionToken}
@@ -163,7 +126,6 @@ export function transaction<F extends Func>(
163
126
  }
164
127
 
165
128
  /**
166
- * @public
167
129
  * Execute a {@link transaction}
168
130
  * @param token - A {@link TransactionToken}
169
131
  * @param id - A unique identifier for the transaction. If not provided, a random identifier will be generated
@@ -11,7 +11,7 @@ import type {
11
11
  WritablePureSelectorFamilyToken,
12
12
  WritablePureSelectorToken,
13
13
  WritableToken,
14
- } from "atom.io"
14
+ } from "./tokens"
15
15
 
16
16
  export type TokenType<
17
17
  Comparison extends ReadableFamilyToken<any, any> | ReadableToken<any>,
@@ -6,8 +6,8 @@ import type {
6
6
  TransactionToken,
7
7
  } from "atom.io"
8
8
  import {
9
- createAtomFamily,
10
- createStandaloneAtom,
9
+ createRegularAtom,
10
+ createRegularAtomFamily,
11
11
  createTransaction,
12
12
  type Store,
13
13
  } from "atom.io/internal"
@@ -39,53 +39,69 @@ export function attachDevtoolsStates(
39
39
  ): DevtoolsStates & IntrospectionStates & { store: Store } {
40
40
  const introspectionStates = attachIntrospectionStates(store)
41
41
 
42
- const devtoolsAreHiddenAtom = createStandaloneAtom<boolean>(store, {
43
- key: `🔍 Devtools Are Hidden`,
44
- default: hideByDefault,
45
- effects:
46
- typeof window === `undefined`
47
- ? []
48
- : [
49
- persistSync(window.localStorage, JSON, `🔍 Devtools Are Hidden`),
50
- ({ setSelf }) => {
51
- window.addEventListener(`keydown`, (e) => {
52
- if (e.ctrlKey && e.shiftKey && e.key.toLowerCase() === `a`) {
53
- e.preventDefault()
54
- setSelf((state) => !state)
55
- }
56
- })
57
- },
58
- ],
59
- })
42
+ const devtoolsAreHiddenAtom = createRegularAtom<boolean>(
43
+ store,
44
+ {
45
+ key: `🔍 Devtools Are Hidden`,
46
+ default: hideByDefault,
47
+ effects:
48
+ typeof window === `undefined`
49
+ ? []
50
+ : [
51
+ persistSync(window.localStorage, JSON, `🔍 Devtools Are Hidden`),
52
+ ({ setSelf }) => {
53
+ window.addEventListener(`keydown`, (e) => {
54
+ if (e.ctrlKey && e.shiftKey && e.key.toLowerCase() === `a`) {
55
+ e.preventDefault()
56
+ setSelf((state) => !state)
57
+ }
58
+ })
59
+ },
60
+ ],
61
+ },
62
+ undefined,
63
+ )
60
64
 
61
- const devtoolsAreOpenAtom = createStandaloneAtom<boolean>(store, {
62
- key: `🔍 Devtools Are Open`,
63
- default: true,
64
- effects:
65
- typeof window === `undefined`
66
- ? []
67
- : [persistSync(window.localStorage, JSON, `🔍 Devtools Are Open`)],
68
- })
65
+ const devtoolsAreOpenAtom = createRegularAtom<boolean>(
66
+ store,
67
+ {
68
+ key: `🔍 Devtools Are Open`,
69
+ default: true,
70
+ effects:
71
+ typeof window === `undefined`
72
+ ? []
73
+ : [persistSync(window.localStorage, JSON, `🔍 Devtools Are Open`)],
74
+ },
75
+ undefined,
76
+ )
69
77
 
70
- const devtoolsViewSelectionAtom = createStandaloneAtom<DevtoolsView>(store, {
71
- key: `🔍 Devtools View Selection`,
72
- default: `atoms`,
73
- effects:
74
- typeof window === `undefined`
75
- ? []
76
- : [persistSync(window.localStorage, JSON, `🔍 Devtools View`)],
77
- })
78
+ const devtoolsViewSelectionAtom = createRegularAtom<DevtoolsView>(
79
+ store,
80
+ {
81
+ key: `🔍 Devtools View Selection`,
82
+ default: `atoms`,
83
+ effects:
84
+ typeof window === `undefined`
85
+ ? []
86
+ : [persistSync(window.localStorage, JSON, `🔍 Devtools View`)],
87
+ },
88
+ undefined,
89
+ )
78
90
 
79
- const devtoolsViewOptionsAtom = createStandaloneAtom<DevtoolsView[]>(store, {
80
- key: `🔍 Devtools View Options`,
81
- default: [`atoms`, `selectors`, `transactions`, `timelines`],
82
- effects:
83
- typeof window === `undefined`
84
- ? []
85
- : [persistSync(window.localStorage, JSON, `🔍 Devtools View Options`)],
86
- })
91
+ const devtoolsViewOptionsAtom = createRegularAtom<DevtoolsView[]>(
92
+ store,
93
+ {
94
+ key: `🔍 Devtools View Options`,
95
+ default: [`atoms`, `selectors`, `transactions`, `timelines`],
96
+ effects:
97
+ typeof window === `undefined`
98
+ ? []
99
+ : [persistSync(window.localStorage, JSON, `🔍 Devtools View Options`)],
100
+ },
101
+ undefined,
102
+ )
87
103
 
88
- const viewIsOpenAtoms = createAtomFamily<
104
+ const viewIsOpenAtoms = createRegularAtomFamily<
89
105
  boolean,
90
106
  readonly (number | string)[]
91
107
  >(store, {
@@ -3,16 +3,15 @@ import type {
3
3
  MutableAtomToken,
4
4
  ReadonlyPureSelectorFamilyToken,
5
5
  } from "atom.io"
6
- import { atom, getInternalRelations, join, selectorFamily } from "atom.io"
6
+ import { getInternalRelations, join, mutableAtom, selectorFamily } from "atom.io"
7
7
  import type { SetRTXJson } from "atom.io/transceivers/set-rtx"
8
8
  import { SetRTX } from "atom.io/transceivers/set-rtx"
9
9
 
10
10
  export const usersInThisRoomIndex: MutableAtomToken<
11
11
  SetRTX<string>,
12
12
  SetRTXJson<string>
13
- > = atom<SetRTX<string>, SetRTXJson<string>>({
13
+ > = mutableAtom<SetRTX<string>, SetRTXJson<string>>({
14
14
  key: `usersInRoomIndex`,
15
- mutable: true,
16
15
  default: () => new SetRTX<string>(),
17
16
  toJson: (set) => set.toJSON(),
18
17
  fromJson: (json) => SetRTX.fromJSON(json),
@@ -21,10 +20,9 @@ export const usersInThisRoomIndex: MutableAtomToken<
21
20
  export const roomIndex: MutableAtomToken<
22
21
  SetRTX<string>,
23
22
  SetRTXJson<string>
24
- > = atom<SetRTX<string>, SetRTXJson<string>>({
23
+ > = mutableAtom<SetRTX<string>, SetRTXJson<string>>({
25
24
  key: `roomIndex`,
26
25
  default: () => new SetRTX<string>(),
27
- mutable: true,
28
26
  toJson: (set) => set.toJSON(),
29
27
  fromJson: (json) => SetRTX.fromJSON(json),
30
28
  })
@@ -4,7 +4,7 @@ import type {
4
4
  MutableAtomToken,
5
5
  RegularAtomFamilyToken,
6
6
  } from "atom.io"
7
- import { atom, atomFamily, join } from "atom.io"
7
+ import { atomFamily, join, mutableAtom } from "atom.io"
8
8
  import type { SetRTXJson } from "atom.io/transceivers/set-rtx"
9
9
  import { SetRTX } from "atom.io/transceivers/set-rtx"
10
10
 
@@ -32,9 +32,8 @@ export const socketAtoms: RegularAtomFamilyToken<Socket | null, SocketKey> =
32
32
  export const socketIndex: MutableAtomToken<
33
33
  SetRTX<SocketKey>,
34
34
  SetRTXJson<SocketKey>
35
- > = atom<SetRTX<SocketKey>, SetRTXJson<SocketKey>>({
35
+ > = mutableAtom<SetRTX<SocketKey>, SetRTXJson<SocketKey>>({
36
36
  key: `socketsIndex`,
37
- mutable: true,
38
37
  default: () => new SetRTX(),
39
38
  toJson: (set) => set.toJSON(),
40
39
  fromJson: (json) => SetRTX.fromJSON(json),
@@ -42,9 +41,8 @@ export const socketIndex: MutableAtomToken<
42
41
  export const userIndex: MutableAtomToken<
43
42
  SetRTX<UserKey>,
44
43
  SetRTXJson<UserKey>
45
- > = atom<SetRTX<UserKey>, SetRTXJson<UserKey>>({
44
+ > = mutableAtom<SetRTX<UserKey>, SetRTXJson<UserKey>>({
46
45
  key: `usersIndex`,
47
- mutable: true,
48
46
  default: () => new SetRTX(),
49
47
  toJson: (set) => set.toJSON(),
50
48
  fromJson: (json) => SetRTX.fromJSON(json),
@@ -1,39 +0,0 @@
1
- import type {
2
- AtomToken,
3
- MutableAtomOptions,
4
- MutableAtomToken,
5
- RegularAtomOptions,
6
- RegularAtomToken,
7
- } from "atom.io"
8
- import type { Json } from "atom.io/json"
9
-
10
- import type { Transceiver } from "../mutable"
11
- import { createMutableAtom } from "../mutable"
12
- import type { Store } from "../store"
13
- import { createRegularAtom } from "./create-regular-atom"
14
-
15
- export function createStandaloneAtom<T>(
16
- store: Store,
17
- options: RegularAtomOptions<T>,
18
- ): RegularAtomToken<T>
19
-
20
- export function createStandaloneAtom<
21
- T extends Transceiver<any>,
22
- J extends Json.Serializable,
23
- >(store: Store, options: MutableAtomOptions<T, J>): MutableAtomToken<T, J>
24
-
25
- export function createStandaloneAtom<T>(
26
- store: Store,
27
- options: MutableAtomOptions<any, any> | RegularAtomOptions<T>,
28
- ): AtomToken<T> {
29
- const isMutable = `mutable` in options
30
-
31
- if (isMutable) {
32
- const state = createMutableAtom(store, options, undefined)
33
- store.on.atomCreation.next(state)
34
- return state
35
- }
36
- const state = createRegularAtom(store, options, undefined)
37
- store.on.atomCreation.next(state)
38
- return state
39
- }
@@ -1,38 +0,0 @@
1
- import type {
2
- AtomFamilyToken,
3
- MutableAtomFamilyOptions,
4
- MutableAtomFamilyToken,
5
- RegularAtomFamilyOptions,
6
- RegularAtomFamilyToken,
7
- } from "atom.io"
8
- import type { Canonical, Json } from "atom.io/json"
9
-
10
- import { createMutableAtomFamily, type Transceiver } from "../mutable"
11
- import type { Store } from "../store"
12
- import { createRegularAtomFamily } from "./create-regular-atom-family"
13
-
14
- export function createAtomFamily<
15
- T extends Transceiver<any>,
16
- J extends Json.Serializable,
17
- K extends Canonical,
18
- >(
19
- store: Store,
20
- options: MutableAtomFamilyOptions<T, J, K>,
21
- ): MutableAtomFamilyToken<T, J, K>
22
- export function createAtomFamily<T, K extends Canonical>(
23
- store: Store,
24
- options: RegularAtomFamilyOptions<T, K>,
25
- ): RegularAtomFamilyToken<T, K>
26
- export function createAtomFamily<T, K extends Canonical>(
27
- store: Store,
28
- options:
29
- | MutableAtomFamilyOptions<any, any, any>
30
- | RegularAtomFamilyOptions<T, K>,
31
- ): AtomFamilyToken<any, any> {
32
- const isMutable = `mutable` in options
33
-
34
- if (isMutable) {
35
- return createMutableAtomFamily(store, options)
36
- }
37
- return createRegularAtomFamily<T, K>(store, options)
38
- }