atom.io 0.15.6 → 0.16.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 (141) hide show
  1. package/data/dist/index.cjs +49 -37
  2. package/data/dist/index.cjs.map +1 -1
  3. package/data/dist/index.d.ts +6 -6
  4. package/data/dist/index.js +50 -38
  5. package/data/dist/index.js.map +1 -1
  6. package/data/src/dict.ts +6 -7
  7. package/data/src/join.ts +52 -38
  8. package/data/src/struct-family.ts +2 -2
  9. package/data/src/struct.ts +4 -5
  10. package/dist/index.cjs +12 -5
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.ts +67 -60
  13. package/dist/index.js +13 -6
  14. package/dist/index.js.map +1 -1
  15. package/internal/dist/index.cjs +911 -888
  16. package/internal/dist/index.cjs.map +1 -1
  17. package/internal/dist/index.d.ts +157 -162
  18. package/internal/dist/index.js +486 -462
  19. package/internal/dist/index.js.map +1 -1
  20. package/internal/src/atom/create-regular-atom.ts +9 -12
  21. package/internal/src/atom/create-standalone-atom.ts +33 -0
  22. package/internal/src/atom/delete-atom.ts +5 -2
  23. package/internal/src/atom/index.ts +1 -16
  24. package/internal/src/atom/is-default.ts +0 -1
  25. package/internal/src/caching.ts +6 -3
  26. package/internal/src/families/create-atom-family.ts +11 -7
  27. package/internal/src/families/create-readonly-selector-family.ts +4 -3
  28. package/internal/src/families/create-regular-atom-family.ts +12 -27
  29. package/internal/src/families/create-selector-family.ts +13 -49
  30. package/internal/src/families/create-writable-selector-family.ts +51 -0
  31. package/internal/src/index.ts +44 -3
  32. package/internal/src/lineage.ts +0 -7
  33. package/internal/src/mutable/create-mutable-atom-family.ts +61 -15
  34. package/internal/src/mutable/create-mutable-atom.ts +70 -25
  35. package/internal/src/mutable/get-json-family.ts +4 -5
  36. package/internal/src/mutable/get-json-token.ts +6 -3
  37. package/internal/src/mutable/get-update-token.ts +3 -3
  38. package/internal/src/mutable/index.ts +1 -7
  39. package/internal/src/mutable/is-mutable.ts +6 -7
  40. package/internal/src/mutable/tracker-family.ts +4 -4
  41. package/internal/src/mutable/tracker.ts +6 -6
  42. package/internal/src/read-or-compute-value.ts +6 -3
  43. package/internal/src/selector/create-readonly-selector.ts +4 -5
  44. package/internal/src/selector/create-standalone-selector.ts +32 -0
  45. package/internal/src/selector/{create-read-write-selector.ts → create-writable-selector.ts} +13 -10
  46. package/internal/src/selector/delete-selector.ts +2 -2
  47. package/internal/src/selector/index.ts +3 -1
  48. package/internal/src/selector/trace-selector-atoms.ts +3 -2
  49. package/internal/src/selector/update-selector-atoms.ts +1 -1
  50. package/internal/src/set-state/copy-mutable-if-needed.ts +5 -8
  51. package/internal/src/set-state/copy-mutable-in-transaction.ts +4 -36
  52. package/internal/src/set-state/emit-update.ts +2 -3
  53. package/internal/src/set-state/evict-downstream.ts +1 -1
  54. package/internal/src/set-state/set-atom-or-selector.ts +3 -3
  55. package/internal/src/set-state/set-atom.ts +1 -2
  56. package/internal/src/set-state/stow-update.ts +1 -1
  57. package/internal/src/store/deposit.ts +25 -13
  58. package/internal/src/store/store.ts +20 -21
  59. package/internal/src/store/withdraw-new-family-member.ts +16 -9
  60. package/internal/src/store/withdraw.ts +43 -19
  61. package/internal/src/subscribe/recall-state.ts +2 -6
  62. package/internal/src/subscribe/subscribe-to-root-atoms.ts +39 -41
  63. package/internal/src/subscribe/subscribe-to-state.ts +3 -1
  64. package/internal/src/timeline/add-atom-to-timeline.ts +5 -5
  65. package/internal/src/timeline/create-timeline.ts +19 -22
  66. package/introspection/dist/index.cjs +4 -8
  67. package/introspection/dist/index.cjs.map +1 -1
  68. package/introspection/dist/index.d.ts +5 -5
  69. package/introspection/dist/index.js +5 -9
  70. package/introspection/dist/index.js.map +1 -1
  71. package/introspection/src/attach-atom-index.ts +4 -5
  72. package/introspection/src/attach-selector-index.ts +4 -5
  73. package/introspection/src/attach-timeline-index.ts +6 -3
  74. package/introspection/src/attach-transaction-index.ts +6 -3
  75. package/introspection/src/index.ts +9 -5
  76. package/json/dist/index.cjs +3 -4
  77. package/json/dist/index.cjs.map +1 -1
  78. package/json/dist/index.d.ts +4 -3
  79. package/json/dist/index.js +4 -5
  80. package/json/dist/index.js.map +1 -1
  81. package/json/src/select-json-family.ts +24 -4
  82. package/json/src/select-json.ts +3 -4
  83. package/package.json +11 -11
  84. package/react-devtools/dist/index.cjs +0 -2
  85. package/react-devtools/dist/index.cjs.map +1 -1
  86. package/react-devtools/dist/index.d.ts +9 -9
  87. package/react-devtools/dist/index.js +0 -2
  88. package/react-devtools/dist/index.js.map +1 -1
  89. package/react-devtools/src/StateIndex.tsx +18 -10
  90. package/react-devtools/src/TimelineIndex.tsx +6 -2
  91. package/react-devtools/src/TransactionIndex.tsx +2 -2
  92. package/realtime-client/dist/index.cjs +27 -13
  93. package/realtime-client/dist/index.cjs.map +1 -1
  94. package/realtime-client/dist/index.d.ts +6 -4
  95. package/realtime-client/dist/index.js +20 -7
  96. package/realtime-client/dist/index.js.map +1 -1
  97. package/realtime-client/src/index.ts +4 -3
  98. package/realtime-client/src/server-action.ts +2 -55
  99. package/realtime-client/src/sync-server-action.ts +75 -0
  100. package/realtime-react/dist/index.cjs +22 -13
  101. package/realtime-react/dist/index.cjs.map +1 -1
  102. package/realtime-react/dist/index.d.ts +3 -1
  103. package/realtime-react/dist/index.js +20 -12
  104. package/realtime-react/dist/index.js.map +1 -1
  105. package/realtime-react/src/index.ts +1 -0
  106. package/realtime-react/src/use-server-action.ts +2 -4
  107. package/realtime-react/src/use-sync-server-action.ts +19 -0
  108. package/realtime-server/dist/index.cjs +83 -81
  109. package/realtime-server/dist/index.cjs.map +1 -1
  110. package/realtime-server/dist/index.d.ts +8 -8
  111. package/realtime-server/dist/index.js +58 -56
  112. package/realtime-server/dist/index.js.map +1 -1
  113. package/realtime-server/src/index.ts +15 -1
  114. package/realtime-server/src/{hook-composition/receive-transaction.ts → realtime-action-receiver.ts} +6 -2
  115. package/realtime-server/src/{hook-composition/sync-transaction.ts → realtime-action-synchronizer.ts} +10 -43
  116. package/realtime-server/src/{hook-composition/expose-family.ts → realtime-family-provider.ts} +9 -23
  117. package/realtime-server/src/{hook-composition/expose-mutable-family.ts → realtime-mutable-family-provider.ts} +4 -4
  118. package/realtime-server/src/{hook-composition/expose-mutable.ts → realtime-mutable-provider.ts} +4 -5
  119. package/realtime-server/src/realtime-server-store.ts +39 -0
  120. package/realtime-server/src/{hook-composition/expose-single.ts → realtime-state-provider.ts} +7 -8
  121. package/realtime-server/src/{hook-composition/receive-state.ts → realtime-state-receiver.ts} +7 -4
  122. package/src/atom.ts +39 -24
  123. package/src/find-state.ts +20 -19
  124. package/src/index.ts +41 -28
  125. package/src/logger.ts +1 -0
  126. package/src/selector.ts +31 -16
  127. package/src/silo.ts +45 -6
  128. package/src/subscribe.ts +1 -0
  129. package/src/validators.ts +35 -25
  130. package/transceivers/set-rtx/dist/index.cjs +2 -2
  131. package/transceivers/set-rtx/dist/index.cjs.map +1 -1
  132. package/transceivers/set-rtx/dist/index.js +2 -2
  133. package/transceivers/set-rtx/dist/index.js.map +1 -1
  134. package/transceivers/set-rtx/src/set-rtx.ts +2 -2
  135. package/internal/src/atom/create-atom.ts +0 -21
  136. package/internal/src/mutable/get-update-family.ts +0 -23
  137. package/internal/src/selector/create-selector.ts +0 -65
  138. package/realtime-server/src/hook-composition/index.ts +0 -15
  139. /package/realtime-client/src/{pull.ts → pull-state.ts} +0 -0
  140. /package/realtime-client/src/{push.ts → push-state.ts} +0 -0
  141. /package/realtime-client/src/{realtime-state.ts → realtime-client-store.ts} +0 -0
@@ -1,15 +1,14 @@
1
1
  import * as AtomIO from "atom.io"
2
- import * as Internal from "atom.io/internal"
2
+ import { IMPLICIT, subscribeToState } from "atom.io/internal"
3
3
  import type { Json } from "atom.io/json"
4
4
 
5
- import type { ServerConfig } from ".."
5
+ import type { ServerConfig } from "."
6
6
 
7
- /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
8
- export const useExposeSingle = ({
7
+ export function realtimeStateProvider({
9
8
  socket,
10
- store = Internal.IMPLICIT.STORE,
11
- }: ServerConfig) => {
12
- return function exposeSingle<J extends Json.Serializable>(
9
+ store = IMPLICIT.STORE,
10
+ }: ServerConfig) {
11
+ return function stateProvider<J extends Json.Serializable>(
13
12
  token: AtomIO.WritableToken<J>,
14
13
  ): () => void {
15
14
  let unsubscribeFromStateUpdates: (() => void) | null = null
@@ -22,7 +21,7 @@ export const useExposeSingle = ({
22
21
 
23
22
  const fillSubRequest = () => {
24
23
  socket.emit(`serve:${token.key}`, AtomIO.getState(token, store))
25
- unsubscribeFromStateUpdates = Internal.subscribeToState(
24
+ unsubscribeFromStateUpdates = subscribeToState(
26
25
  token,
27
26
  ({ newValue }) => {
28
27
  socket.emit(`serve:${token.key}`, newValue)
@@ -1,12 +1,15 @@
1
1
  import { setState } from "atom.io"
2
2
  import type { WritableToken } from "atom.io"
3
+ import { IMPLICIT } from "atom.io/internal"
3
4
  import type { Json } from "atom.io/json"
4
5
 
5
- import type { ServerConfig } from ".."
6
+ import type { ServerConfig } from "."
6
7
 
7
- /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
8
- export const useReceiveState = ({ socket, store }: ServerConfig) => {
9
- return function receiveState<J extends Json.Serializable>(
8
+ export function realtimeStateReceiver({
9
+ socket,
10
+ store = IMPLICIT.STORE,
11
+ }: ServerConfig) {
12
+ return function stateReceiver<J extends Json.Serializable>(
10
13
  token: WritableToken<J>,
11
14
  ): () => void {
12
15
  const publish = (newValue: J) => setState(token, newValue, store)
package/src/atom.ts CHANGED
@@ -1,8 +1,12 @@
1
1
  import type { Store, Subject, Transceiver } from "atom.io/internal"
2
- import { IMPLICIT, createAtom, createAtomFamily } from "atom.io/internal"
2
+ import {
3
+ IMPLICIT,
4
+ createAtomFamily,
5
+ createStandaloneAtom,
6
+ } from "atom.io/internal"
3
7
  import type { Json, JsonInterface } from "atom.io/json"
4
8
 
5
- import type { AtomToken, MutableAtomToken } from "."
9
+ import type { AtomToken, MutableAtomToken, RegularAtomToken } from "."
6
10
 
7
11
  export type Effectors<T> = {
8
12
  setSelf: <V extends T>(next: V | ((oldValue: T) => V)) => void
@@ -11,7 +15,7 @@ export type Effectors<T> = {
11
15
 
12
16
  export type AtomEffect<T> = (tools: Effectors<T>) => (() => void) | void
13
17
 
14
- export type AtomOptions<T> = {
18
+ export type RegularAtomOptions<T> = {
15
19
  key: string
16
20
  default: T | (() => T)
17
21
  effects?: AtomEffect<T>[]
@@ -19,7 +23,7 @@ export type AtomOptions<T> = {
19
23
  // biome-ignore format: complex intersection
20
24
  export type MutableAtomOptions<T extends Transceiver<any>, J extends Json.Serializable> =
21
25
  & JsonInterface<T, J>
22
- & Omit<AtomOptions<T>, `default`>
26
+ & Omit<RegularAtomOptions<T>, `default`>
23
27
  & {
24
28
  default: () => T
25
29
  mutable: true
@@ -28,31 +32,31 @@ export type MutableAtomOptions<T extends Transceiver<any>, J extends Json.Serial
28
32
  export function atom<T extends Transceiver<any>, J extends Json.Serializable>(
29
33
  options: MutableAtomOptions<T, J>,
30
34
  ): MutableAtomToken<T, J>
31
- export function atom<T>(options: AtomOptions<T>): AtomToken<T>
35
+ export function atom<T>(options: RegularAtomOptions<T>): RegularAtomToken<T>
32
36
  export function atom(
33
- options: AtomOptions<any> | MutableAtomOptions<any, any>,
37
+ options: MutableAtomOptions<any, any> | RegularAtomOptions<any>,
34
38
  ): AtomToken<any> {
35
- return createAtom(options, undefined, IMPLICIT.STORE)
39
+ return createStandaloneAtom(options, IMPLICIT.STORE)
36
40
  }
37
41
 
38
- export type AtomFamilyOptions<T, K extends Json.Serializable> = {
42
+ export type RegularAtomFamilyOptions<T, K extends Json.Serializable> = {
39
43
  key: string
40
44
  default: T | ((key: K) => T)
41
45
  effects?: (key: K) => AtomEffect<T>[]
42
46
  }
43
47
 
44
- export type AtomFamily<T, K extends Json.Serializable = Json.Serializable> = ((
45
- key: K,
46
- ) => AtomToken<T>) & {
48
+ export type RegularAtomFamily<
49
+ T,
50
+ K extends Json.Serializable = Json.Serializable,
51
+ > = ((key: K) => RegularAtomToken<T>) & {
47
52
  key: string
48
53
  type: `atom_family`
49
- subject: Subject<AtomToken<T>>
50
- mutable?: boolean
54
+ subject: Subject<RegularAtomToken<T>>
51
55
  install: (store: Store) => void
52
56
  __T?: T
53
57
  __K?: K
54
58
  }
55
- export type AtomFamilyToken<T, K extends Json.Serializable> = {
59
+ export type RegularAtomFamilyToken<T, K extends Json.Serializable> = {
56
60
  key: string
57
61
  type: `atom_family`
58
62
  __T?: T
@@ -65,9 +69,13 @@ export type MutableAtomFamilyOptions<
65
69
  J extends Json.Serializable,
66
70
  K extends Json.Serializable,
67
71
  > =
68
- & AtomFamilyOptions<T, K>
69
72
  & JsonInterface<T, J>
70
- & { mutable: true }
73
+ & {
74
+ key: string
75
+ default: (key: K) => T
76
+ effects?: (key: K) => AtomEffect<T>[]
77
+ mutable: true,
78
+ }
71
79
 
72
80
  // biome-ignore format: intersection
73
81
  export type MutableAtomFamily<
@@ -79,9 +87,8 @@ export type MutableAtomFamily<
79
87
  & ((key: K) => MutableAtomToken<T, J>)
80
88
  & {
81
89
  key: string
82
- type: `atom_family`
90
+ type: `mutable_atom_family`
83
91
  subject: Subject<MutableAtomToken<T, J>>
84
- mutable: true
85
92
  install: (store: Store) => void
86
93
  __T?: T
87
94
  __J?: J
@@ -93,23 +100,31 @@ export type MutableAtomFamilyToken<
93
100
  K extends Json.Serializable,
94
101
  > = {
95
102
  key: string
96
- type: `atom_family`
97
- mutable: true
103
+ type: `mutable_atom_family`
98
104
  __T?: T
99
105
  __J?: J
100
106
  __K?: K
101
107
  }
102
108
 
109
+ export type AtomFamily<T, K extends Json.Serializable = Json.Serializable> =
110
+ | MutableAtomFamily<T extends Transceiver<any> ? T : never, any, K>
111
+ | RegularAtomFamily<T, K>
112
+ export type AtomFamilyToken<T, K extends Json.Serializable = Json.Serializable> =
113
+ | MutableAtomFamilyToken<T extends Transceiver<any> ? T : never, any, K>
114
+ | RegularAtomFamilyToken<T, K>
115
+
103
116
  export function atomFamily<
104
117
  T extends Transceiver<any>,
105
118
  J extends Json.Serializable,
106
119
  K extends Json.Serializable,
107
120
  >(options: MutableAtomFamilyOptions<T, J, K>): MutableAtomFamily<T, J, K>
108
121
  export function atomFamily<T, K extends Json.Serializable>(
109
- options: AtomFamilyOptions<T, K>,
110
- ): AtomFamily<T, K>
122
+ options: RegularAtomFamilyOptions<T, K>,
123
+ ): RegularAtomFamily<T, K>
111
124
  export function atomFamily<T, K extends Json.Serializable>(
112
- options: AtomFamilyOptions<T, K> | MutableAtomFamilyOptions<any, any, any>,
113
- ): AtomFamily<T, K> | MutableAtomFamily<any, any, any> {
125
+ options:
126
+ | MutableAtomFamilyOptions<any, any, any>
127
+ | RegularAtomFamilyOptions<T, K>,
128
+ ): MutableAtomFamily<any, any, any> | RegularAtomFamily<T, K> {
114
129
  return createAtomFamily(options, IMPLICIT.STORE)
115
130
  }
package/src/find-state.ts CHANGED
@@ -1,15 +1,15 @@
1
1
  import type {
2
- AtomFamilyToken,
3
- AtomToken,
4
2
  MutableAtomFamilyToken,
5
3
  MutableAtomToken,
6
4
  ReadableFamilyToken,
7
5
  ReadableToken,
8
6
  ReadonlySelectorFamilyToken,
9
7
  ReadonlySelectorToken,
10
- SelectorFamilyToken,
11
- SelectorToken,
8
+ RegularAtomFamilyToken,
9
+ RegularAtomToken,
12
10
  WritableFamilyToken,
11
+ WritableSelectorFamilyToken,
12
+ WritableSelectorToken,
13
13
  WritableToken,
14
14
  } from "atom.io"
15
15
  import type { Store, Transceiver } from "atom.io/internal"
@@ -34,26 +34,27 @@ export function findState<
34
34
  T extends Transceiver<any>,
35
35
  J extends Json.Serializable,
36
36
  K extends Json.Serializable,
37
- >(token: MutableAtomFamilyToken<T, J, K>, key: K): MutableAtomToken<T, J>
38
- export function findState<T, K extends Json.Serializable>(
39
- token: AtomFamilyToken<T, K>,
40
- key: K,
41
- ): AtomToken<T>
42
- export function findState<T, K extends Json.Serializable>(
43
- token: SelectorFamilyToken<T, K>,
44
- key: K,
45
- ): SelectorToken<T>
46
- export function findState<T, K extends Json.Serializable>(
37
+ Key extends K,
38
+ >(token: MutableAtomFamilyToken<T, J, K>, key: Key): MutableAtomToken<T, J>
39
+ export function findState<T, K extends Json.Serializable, Key extends K>(
40
+ token: RegularAtomFamilyToken<T, K>,
41
+ key: Key,
42
+ ): RegularAtomToken<T>
43
+ export function findState<T, K extends Json.Serializable, Key extends K>(
44
+ token: WritableSelectorFamilyToken<T, K>,
45
+ key: Key,
46
+ ): WritableSelectorToken<T>
47
+ export function findState<T, K extends Json.Serializable, Key extends K>(
47
48
  token: ReadonlySelectorFamilyToken<T, K>,
48
- key: K,
49
+ key: Key,
49
50
  ): ReadonlySelectorToken<T>
50
- export function findState<T, K extends Json.Serializable>(
51
+ export function findState<T, K extends Json.Serializable, Key extends K>(
51
52
  token: WritableFamilyToken<T, K>,
52
- key: K,
53
+ key: Key,
53
54
  ): WritableToken<T>
54
- export function findState<T, K extends Json.Serializable>(
55
+ export function findState<T, K extends Json.Serializable, Key extends K>(
55
56
  token: ReadableFamilyToken<T, K>,
56
- key: K,
57
+ key: Key,
57
58
  ): ReadableToken<T>
58
59
  export function findState(
59
60
  token: ReadableFamilyToken<any, any>,
package/src/index.ts CHANGED
@@ -1,11 +1,18 @@
1
1
  import type { Transceiver } from "atom.io/internal"
2
2
  import type { Json } from "atom.io/json"
3
- import type { AtomFamily, AtomFamilyToken } from "./atom"
3
+ import type {
4
+ AtomFamily,
5
+ AtomFamilyToken,
6
+ RegularAtomFamily,
7
+ RegularAtomFamilyToken,
8
+ } from "./atom"
4
9
  import type {
5
10
  ReadonlySelectorFamily,
6
11
  ReadonlySelectorFamilyToken,
7
12
  SelectorFamily,
8
13
  SelectorFamilyToken,
14
+ WritableSelectorFamily,
15
+ WritableSelectorFamilyToken,
9
16
  } from "./selector"
10
17
 
11
18
  export * from "./atom"
@@ -23,51 +30,57 @@ export * from "./validators"
23
30
 
24
31
  export type ƒn = (...parameters: any[]) => any
25
32
 
26
- export type AtomToken<_> = {
33
+ export type RegularAtomToken<T> = {
27
34
  key: string
28
35
  type: `atom`
29
36
  family?: FamilyMetadata
30
- __brand?: _
37
+ __T?: T
31
38
  }
32
- export interface MutableAtomToken<
39
+ export type MutableAtomToken<
33
40
  T extends Transceiver<any>,
34
41
  J extends Json.Serializable,
35
- > extends AtomToken<T> {
36
- __asJSON?: J
37
- __update?: T extends Transceiver<infer Update> ? Update : never
42
+ > = {
43
+ key: string
44
+ type: `mutable_atom`
45
+ family?: FamilyMetadata
46
+ __J?: J
47
+ __U?: T extends Transceiver<infer Update> ? Update : never
38
48
  }
39
- export type SelectorToken<_> = {
49
+ export type AtomToken<T> =
50
+ | MutableAtomToken<T extends Transceiver<any> ? T : never, any>
51
+ | RegularAtomToken<T>
52
+
53
+ export type WritableSelectorToken<T> = {
40
54
  key: string
41
55
  type: `selector`
42
56
  family?: FamilyMetadata
43
- __brand?: _
57
+ __T?: T
58
+ }
59
+ export type ReadonlySelectorToken<T> = {
60
+ key: string
61
+ type: `readonly_selector`
62
+ family?: FamilyMetadata
63
+ __T?: T
44
64
  }
45
- /**
46
- * @deprecated Prefer `WritableToken`.
47
- */
48
- export type StateToken<T> = AtomToken<T> | SelectorToken<T>
49
- export type WritableToken<T> = AtomToken<T> | SelectorToken<T>
50
- export type ReadableToken<T> = ReadonlySelectorToken<T> | WritableToken<T>
65
+ export type SelectorToken<T> =
66
+ | ReadonlySelectorToken<T>
67
+ | WritableSelectorToken<T>
68
+
69
+ export type WritableToken<T> = AtomToken<T> | WritableSelectorToken<T>
70
+ export type ReadableToken<T> = AtomToken<T> | SelectorToken<T>
51
71
 
52
72
  export type WritableFamily<T, K extends Json.Serializable> =
53
73
  | AtomFamily<T, K>
54
- | SelectorFamily<T, K>
74
+ | WritableSelectorFamily<T, K>
55
75
  export type ReadableFamily<T, K extends Json.Serializable> =
56
- | ReadonlySelectorFamily<T, K>
57
- | WritableFamily<T, K>
76
+ | AtomFamily<T, K>
77
+ | SelectorFamily<T, K>
58
78
 
59
79
  export type WritableFamilyToken<T, K extends Json.Serializable> =
60
80
  | AtomFamilyToken<T, K>
61
- | SelectorFamilyToken<T, K>
81
+ | WritableSelectorFamilyToken<T, K>
62
82
  export type ReadableFamilyToken<T, K extends Json.Serializable> =
63
- | ReadonlySelectorFamilyToken<T, K>
64
- | WritableFamilyToken<T, K>
65
-
66
- export type ReadonlySelectorToken<_> = {
67
- key: string
68
- type: `readonly_selector`
69
- family?: FamilyMetadata
70
- __brand?: _
71
- }
83
+ | AtomFamilyToken<T, K>
84
+ | SelectorFamilyToken<T, K>
72
85
 
73
86
  export type FamilyMetadata = { key: string; subKey: string }
package/src/logger.ts CHANGED
@@ -47,6 +47,7 @@ export type LogFn = (
47
47
  icon: LoggerIcon,
48
48
  tokenType:
49
49
  | `atom`
50
+ | `mutable_atom`
50
51
  | `readonly_selector`
51
52
  | `selector`
52
53
  | `state`
package/src/selector.ts CHANGED
@@ -1,11 +1,15 @@
1
1
  import type { Store, Subject } from "atom.io/internal"
2
- import { IMPLICIT, createSelector, createSelectorFamily } from "atom.io/internal"
2
+ import {
3
+ IMPLICIT,
4
+ createSelectorFamily,
5
+ createStandaloneSelector,
6
+ } from "atom.io/internal"
3
7
  import type { Json } from "atom.io/json"
4
8
 
5
- import type { ReadonlySelectorToken, SelectorToken } from "."
9
+ import type { ReadonlySelectorToken, WritableSelectorToken } from "."
6
10
  import type { Read, Write } from "./transaction"
7
11
 
8
- export type SelectorOptions<T> = {
12
+ export type WritableSelectorOptions<T> = {
9
13
  key: string
10
14
  get: Read<() => T>
11
15
  set: Write<(newValue: T) => void>
@@ -15,17 +19,19 @@ export type ReadonlySelectorOptions<T> = {
15
19
  get: Read<() => T>
16
20
  }
17
21
 
18
- export function selector<T>(options: SelectorOptions<T>): SelectorToken<T>
22
+ export function selector<T>(
23
+ options: WritableSelectorOptions<T>,
24
+ ): WritableSelectorToken<T>
19
25
  export function selector<T>(
20
26
  options: ReadonlySelectorOptions<T>,
21
27
  ): ReadonlySelectorToken<T>
22
28
  export function selector<T>(
23
- options: ReadonlySelectorOptions<T> | SelectorOptions<T>,
24
- ): ReadonlySelectorToken<T> | SelectorToken<T> {
25
- return createSelector(options, undefined, IMPLICIT.STORE)
29
+ options: ReadonlySelectorOptions<T> | WritableSelectorOptions<T>,
30
+ ): ReadonlySelectorToken<T> | WritableSelectorToken<T> {
31
+ return createStandaloneSelector(options, IMPLICIT.STORE)
26
32
  }
27
33
 
28
- export type SelectorFamilyOptions<T, K extends Json.Serializable> = {
34
+ export type WritableSelectorFamilyOptions<T, K extends Json.Serializable> = {
29
35
  key: string
30
36
  get: (key: K) => Read<() => T>
31
37
  set: (key: K) => Write<(newValue: T) => void>
@@ -35,18 +41,18 @@ export type ReadonlySelectorFamilyOptions<T, K extends Json.Serializable> = {
35
41
  get: (key: K) => Read<() => T>
36
42
  }
37
43
 
38
- export type SelectorFamily<
44
+ export type WritableSelectorFamily<
39
45
  T,
40
46
  K extends Json.Serializable = Json.Serializable,
41
- > = ((key: K) => SelectorToken<T>) & {
47
+ > = ((key: K) => WritableSelectorToken<T>) & {
42
48
  key: string
43
49
  type: `selector_family`
44
- subject: Subject<SelectorToken<T>>
50
+ subject: Subject<WritableSelectorToken<T>>
45
51
  install: (store: Store) => void
46
52
  __T?: T
47
53
  __K?: K
48
54
  }
49
- export type SelectorFamilyToken<T, K extends Json.Serializable> = {
55
+ export type WritableSelectorFamilyToken<T, K extends Json.Serializable> = {
50
56
  key: string
51
57
  type: `selector_family`
52
58
  __T?: T
@@ -71,14 +77,23 @@ export type ReadonlySelectorFamilyToken<T, K extends Json.Serializable> = {
71
77
  __K?: K
72
78
  }
73
79
 
80
+ export type SelectorFamily<T, K extends Json.Serializable> =
81
+ | ReadonlySelectorFamily<T, K>
82
+ | WritableSelectorFamily<T, K>
83
+ export type SelectorFamilyToken<T, K extends Json.Serializable> =
84
+ | ReadonlySelectorFamilyToken<T, K>
85
+ | WritableSelectorFamilyToken<T, K>
86
+
74
87
  export function selectorFamily<T, K extends Json.Serializable>(
75
- options: SelectorFamilyOptions<T, K>,
76
- ): SelectorFamily<T, K>
88
+ options: WritableSelectorFamilyOptions<T, K>,
89
+ ): WritableSelectorFamily<T, K>
77
90
  export function selectorFamily<T, K extends Json.Serializable>(
78
91
  options: ReadonlySelectorFamilyOptions<T, K>,
79
92
  ): ReadonlySelectorFamily<T, K>
80
93
  export function selectorFamily<T, K extends Json.Serializable>(
81
- options: ReadonlySelectorFamilyOptions<T, K> | SelectorFamilyOptions<T, K>,
82
- ): ReadonlySelectorFamily<T, K> | SelectorFamily<T, K> {
94
+ options:
95
+ | ReadonlySelectorFamilyOptions<T, K>
96
+ | WritableSelectorFamilyOptions<T, K>,
97
+ ): ReadonlySelectorFamily<T, K> | WritableSelectorFamily<T, K> {
83
98
  return createSelectorFamily(options, IMPLICIT.STORE)
84
99
  }
package/src/silo.ts CHANGED
@@ -1,15 +1,30 @@
1
+ import type { Transceiver } from "atom.io/internal"
1
2
  import {
2
3
  Store,
3
- createAtom,
4
4
  createAtomFamily,
5
- createSelector,
6
5
  createSelectorFamily,
6
+ createStandaloneAtom,
7
+ createStandaloneSelector,
7
8
  createTimeline,
8
9
  createTransaction,
9
10
  timeTravel,
10
11
  } from "atom.io/internal"
12
+ import type { Json } from "atom.io/json"
11
13
 
12
- import type { redo, timeline, undo } from "."
14
+ import type {
15
+ AtomToken,
16
+ MutableAtomFamily,
17
+ MutableAtomFamilyOptions,
18
+ MutableAtomOptions,
19
+ MutableAtomToken,
20
+ RegularAtomFamily,
21
+ RegularAtomFamilyOptions,
22
+ RegularAtomOptions,
23
+ RegularAtomToken,
24
+ redo,
25
+ timeline,
26
+ undo,
27
+ } from "."
13
28
  import { getState, setState, subscribe } from "."
14
29
  import type { atom, atomFamily } from "./atom"
15
30
  import type { selector, selectorFamily } from "./selector"
@@ -30,10 +45,34 @@ export class Silo {
30
45
  public redo: typeof redo
31
46
  public constructor(name: string, fromStore: Store | null = null) {
32
47
  const s = new Store(name, fromStore)
48
+ function _atom<T>(options: RegularAtomOptions<T>): RegularAtomToken<T>
49
+ function _atom<T extends Transceiver<any>, J extends Json.Serializable>(
50
+ options: MutableAtomOptions<T, J>,
51
+ ): MutableAtomToken<T, J>
52
+ function _atom<T>(
53
+ options: MutableAtomOptions<any, any> | RegularAtomOptions<T>,
54
+ ): AtomToken<T> {
55
+ return createStandaloneAtom(options, s)
56
+ }
57
+ function _atomFamily<
58
+ T extends Transceiver<any>,
59
+ J extends Json.Serializable,
60
+ K extends Json.Serializable,
61
+ >(options: MutableAtomFamilyOptions<T, J, K>): MutableAtomFamily<T, J, K>
62
+ function _atomFamily<T, K extends Json.Serializable>(
63
+ options: RegularAtomFamilyOptions<T, K>,
64
+ ): RegularAtomFamily<T, K>
65
+ function _atomFamily<T, K extends Json.Serializable>(
66
+ options:
67
+ | MutableAtomFamilyOptions<any, any, any>
68
+ | RegularAtomFamilyOptions<T, K>,
69
+ ): MutableAtomFamily<any, any, any> | RegularAtomFamily<T, K> {
70
+ return createAtomFamily(options, s)
71
+ }
33
72
  this.store = s
34
- this.atom = (options) => createAtom(options, undefined, s)
35
- this.atomFamily = (options) => createAtomFamily(options, s)
36
- this.selector = (options) => createSelector(options, undefined, s) as any
73
+ this.atom = _atom
74
+ this.atomFamily = _atomFamily
75
+ this.selector = (options) => createStandaloneSelector(options, s) as any
37
76
  this.selectorFamily = (options) => createSelectorFamily(options, s) as any
38
77
  this.transaction = (options) => createTransaction(options, s)
39
78
  this.timeline = (options) => createTimeline(options, s)
package/src/subscribe.ts CHANGED
@@ -54,6 +54,7 @@ export function subscribe(
54
54
  ): () => void {
55
55
  switch (token.type) {
56
56
  case `atom`:
57
+ case `mutable_atom`:
57
58
  case `readonly_selector`:
58
59
  case `selector`:
59
60
  return subscribeToState(token, handleUpdate, key, store)
package/src/validators.ts CHANGED
@@ -1,13 +1,15 @@
1
1
  import type {
2
- AtomFamily,
3
- AtomToken,
2
+ MutableAtomFamily,
3
+ MutableAtomToken,
4
4
  ReadableFamily,
5
5
  ReadableToken,
6
6
  ReadonlySelectorFamily,
7
7
  ReadonlySelectorToken,
8
- SelectorFamily,
9
- SelectorToken,
8
+ RegularAtomFamily,
9
+ RegularAtomToken,
10
10
  WritableFamily,
11
+ WritableSelectorFamily,
12
+ WritableSelectorToken,
11
13
  WritableToken,
12
14
  } from "atom.io"
13
15
 
@@ -19,56 +21,64 @@ export type TokenType<
19
21
  ? RepresentedValue
20
22
  : never
21
23
 
22
- export function isToken<KnownToken extends AtomToken<any>>(
24
+ export function isToken<KnownToken extends RegularAtomToken<any>>(
23
25
  knownToken: KnownToken,
24
- unknownToken: ReadableToken<unknown>,
25
- ): unknownToken is AtomToken<TokenType<KnownToken>>
26
- export function isToken<KnownToken extends SelectorToken<any>>(
26
+ unknownToken: ReadableToken<any>,
27
+ ): unknownToken is RegularAtomToken<TokenType<KnownToken>>
28
+ export function isToken<KnownToken extends MutableAtomToken<any, any>>(
27
29
  knownToken: KnownToken,
28
- unknownToken: ReadableToken<unknown>,
29
- ): unknownToken is SelectorToken<TokenType<KnownToken>>
30
+ unknownToken: ReadableToken<any>,
31
+ ): unknownToken is MutableAtomToken<TokenType<KnownToken>, any>
32
+ export function isToken<KnownToken extends WritableSelectorToken<any>>(
33
+ knownToken: KnownToken,
34
+ unknownToken: ReadableToken<any>,
35
+ ): unknownToken is WritableSelectorToken<TokenType<KnownToken>>
30
36
  export function isToken<KnownToken extends ReadonlySelectorToken<any>>(
31
37
  knownToken: KnownToken,
32
- unknownToken: ReadableToken<unknown>,
38
+ unknownToken: ReadableToken<any>,
33
39
  ): unknownToken is ReadonlySelectorToken<TokenType<KnownToken>>
34
40
  export function isToken<KnownToken extends WritableToken<any>>(
35
41
  knownToken: KnownToken,
36
- unknownToken: ReadableToken<unknown>,
42
+ unknownToken: ReadableToken<any>,
37
43
  ): unknownToken is WritableToken<TokenType<KnownToken>>
38
- export function isToken<KnownToken extends WritableToken<any>>(
44
+ export function isToken<KnownToken extends ReadableToken<any>>(
39
45
  knownToken: KnownToken,
40
- unknownToken: ReadableToken<unknown>,
46
+ unknownToken: ReadableToken<any>,
41
47
  ): unknownToken is ReadableToken<TokenType<KnownToken>>
42
48
  export function isToken<KnownToken extends ReadableToken<any>>(
43
49
  knownToken: KnownToken,
44
- unknownToken: ReadableToken<unknown>,
50
+ unknownToken: ReadableToken<any>,
45
51
  ): unknownToken is ReadableToken<TokenType<KnownToken>> {
46
52
  return knownToken.key === unknownToken.key
47
53
  }
48
54
 
49
- export function belongsTo<Family extends AtomFamily<any, any>>(
55
+ export function belongsTo<Family extends RegularAtomFamily<any, any>>(
56
+ family: Family,
57
+ unknownToken: ReadableToken<any>,
58
+ ): unknownToken is RegularAtomToken<TokenType<Family>>
59
+ export function belongsTo<Family extends MutableAtomFamily<any, any, any>>(
50
60
  family: Family,
51
- unknownToken: ReadableToken<unknown>,
52
- ): unknownToken is AtomToken<TokenType<Family>>
53
- export function belongsTo<Family extends SelectorFamily<any, any>>(
61
+ unknownToken: ReadableToken<any>,
62
+ ): unknownToken is MutableAtomToken<TokenType<Family>, any>
63
+ export function belongsTo<Family extends WritableSelectorFamily<any, any>>(
54
64
  family: Family,
55
- unknownToken: ReadableToken<unknown>,
56
- ): unknownToken is SelectorToken<TokenType<Family>>
65
+ unknownToken: ReadableToken<any>,
66
+ ): unknownToken is WritableSelectorToken<TokenType<Family>>
57
67
  export function belongsTo<Family extends ReadonlySelectorFamily<any, any>>(
58
68
  family: Family,
59
- unknownToken: ReadableToken<unknown>,
69
+ unknownToken: ReadableToken<any>,
60
70
  ): unknownToken is ReadonlySelectorToken<TokenType<Family>>
61
71
  export function belongsTo<Family extends WritableFamily<any, any>>(
62
72
  family: Family,
63
- unknownToken: ReadableToken<unknown>,
73
+ unknownToken: ReadableToken<any>,
64
74
  ): unknownToken is WritableToken<TokenType<Family>>
65
75
  export function belongsTo<Family extends ReadableFamily<any, any>>(
66
76
  family: Family,
67
- unknownToken: ReadableToken<unknown>,
77
+ unknownToken: ReadableToken<any>,
68
78
  ): unknownToken is ReadableToken<TokenType<Family>>
69
79
  export function belongsTo<Family extends ReadableFamily<any, any>>(
70
80
  family: Family,
71
- unknownToken: ReadableToken<unknown>,
81
+ unknownToken: ReadableToken<any>,
72
82
  ): unknownToken is ReadableToken<TokenType<Family>> {
73
83
  return family.key === unknownToken.family?.key
74
84
  }
@@ -80,11 +80,11 @@ var SetRTX = class _SetRTX extends Set {
80
80
  try {
81
81
  const shouldCommit = run(this.child);
82
82
  if (shouldCommit) {
83
- this.cacheUpdateNumber++;
84
- this.emit(`tx:${this.transactionUpdates.join(`;`)}`);
85
83
  for (const update of this.transactionUpdates) {
86
84
  this.doStep(update);
87
85
  }
86
+ this.cacheUpdateNumber++;
87
+ this.emit(`tx:${this.transactionUpdates.join(`;`)}`);
88
88
  }
89
89
  } catch (thrown) {
90
90
  console.error(`Failed to apply transaction to SetRTX: ${thrown}`);