atom.io 0.6.7 → 0.6.9

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 (129) hide show
  1. package/dist/index.d.mts +241 -263
  2. package/dist/index.d.ts +241 -263
  3. package/dist/index.js +28 -1911
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +5 -1871
  6. package/dist/index.mjs.map +1 -1
  7. package/introspection/dist/index.d.mts +121 -176
  8. package/introspection/dist/index.d.ts +121 -176
  9. package/introspection/dist/index.js +6 -346
  10. package/introspection/dist/index.js.map +1 -1
  11. package/introspection/dist/index.mjs +5 -324
  12. package/introspection/dist/index.mjs.map +1 -1
  13. package/json/dist/index.d.mts +37 -8
  14. package/json/dist/index.d.ts +37 -8
  15. package/json/dist/index.js +31 -48
  16. package/json/dist/index.js.map +1 -1
  17. package/json/dist/index.mjs +6 -14
  18. package/json/dist/index.mjs.map +1 -1
  19. package/package.json +30 -14
  20. package/react/dist/index.js +34 -83
  21. package/react/dist/index.js.map +1 -1
  22. package/react/dist/index.mjs +7 -43
  23. package/react/dist/index.mjs.map +1 -1
  24. package/react-devtools/dist/index.css +1 -42
  25. package/react-devtools/dist/index.css.map +1 -1
  26. package/react-devtools/dist/index.d.mts +128 -193
  27. package/react-devtools/dist/index.d.ts +128 -193
  28. package/react-devtools/dist/index.js +56 -4154
  29. package/react-devtools/dist/index.js.map +1 -1
  30. package/react-devtools/dist/index.mjs +19 -4117
  31. package/react-devtools/dist/index.mjs.map +1 -1
  32. package/realtime/dist/index.d.mts +7 -11
  33. package/realtime/dist/index.d.ts +7 -11
  34. package/realtime/dist/index.js +26 -185
  35. package/realtime/dist/index.js.map +1 -1
  36. package/realtime/dist/index.mjs +4 -149
  37. package/realtime/dist/index.mjs.map +1 -1
  38. package/realtime-react/dist/index.d.mts +12 -16
  39. package/realtime-react/dist/index.d.ts +12 -16
  40. package/realtime-react/dist/index.js +41 -214
  41. package/realtime-react/dist/index.js.map +1 -1
  42. package/realtime-react/dist/index.mjs +9 -169
  43. package/realtime-react/dist/index.mjs.map +1 -1
  44. package/src/atom.ts +7 -6
  45. package/src/index.ts +12 -9
  46. package/src/logger.ts +5 -5
  47. package/src/selector.ts +16 -14
  48. package/src/silo.ts +36 -39
  49. package/src/subscribe.ts +25 -20
  50. package/src/timeline.ts +9 -4
  51. package/src/transaction.ts +3 -4
  52. package/src/internal/atom-internal.ts +0 -54
  53. package/src/internal/families-internal.ts +0 -141
  54. package/src/internal/get.ts +0 -129
  55. package/src/internal/index.ts +0 -15
  56. package/src/internal/is-default.ts +0 -35
  57. package/src/internal/operation.ts +0 -139
  58. package/src/internal/selector/create-read-write-selector.ts +0 -66
  59. package/src/internal/selector/create-readonly-selector.ts +0 -46
  60. package/src/internal/selector/index.ts +0 -4
  61. package/src/internal/selector/lookup-selector-sources.ts +0 -16
  62. package/src/internal/selector/register-selector.ts +0 -57
  63. package/src/internal/selector/trace-selector-atoms.ts +0 -43
  64. package/src/internal/selector/update-selector-atoms.ts +0 -33
  65. package/src/internal/selector-internal.ts +0 -58
  66. package/src/internal/set.ts +0 -99
  67. package/src/internal/store.ts +0 -151
  68. package/src/internal/subject.ts +0 -24
  69. package/src/internal/subscribe-internal.ts +0 -88
  70. package/src/internal/time-travel-internal.ts +0 -91
  71. package/src/internal/timeline/add-atom-to-timeline.ts +0 -168
  72. package/src/internal/timeline/index.ts +0 -1
  73. package/src/internal/timeline-internal.ts +0 -107
  74. package/src/internal/transaction/abort-transaction.ts +0 -12
  75. package/src/internal/transaction/apply-transaction.ts +0 -57
  76. package/src/internal/transaction/build-transaction.ts +0 -33
  77. package/src/internal/transaction/index.ts +0 -25
  78. package/src/internal/transaction/redo-transaction.ts +0 -23
  79. package/src/internal/transaction/undo-transaction.ts +0 -23
  80. package/src/internal/transaction-internal.ts +0 -61
  81. package/src/introspection/attach-atom-index.ts +0 -73
  82. package/src/introspection/attach-introspection-states.ts +0 -42
  83. package/src/introspection/attach-selector-index.ts +0 -77
  84. package/src/introspection/attach-timeline-family.ts +0 -59
  85. package/src/introspection/attach-timeline-index.ts +0 -36
  86. package/src/introspection/attach-transaction-index.ts +0 -38
  87. package/src/introspection/attach-transaction-logs.ts +0 -40
  88. package/src/introspection/index.ts +0 -20
  89. package/src/json/index.ts +0 -1
  90. package/src/json/select-json.ts +0 -18
  91. package/src/react/index.ts +0 -2
  92. package/src/react/store-context.tsx +0 -13
  93. package/src/react/store-hooks.ts +0 -47
  94. package/src/react-devtools/AtomIODevtools.tsx +0 -107
  95. package/src/react-devtools/Button.tsx +0 -24
  96. package/src/react-devtools/StateEditor.tsx +0 -70
  97. package/src/react-devtools/StateIndex.tsx +0 -153
  98. package/src/react-devtools/TimelineIndex.tsx +0 -92
  99. package/src/react-devtools/TransactionIndex.tsx +0 -70
  100. package/src/react-devtools/Updates.tsx +0 -145
  101. package/src/react-devtools/devtools.scss +0 -311
  102. package/src/react-devtools/index.ts +0 -72
  103. package/src/react-explorer/AtomIOExplorer.tsx +0 -218
  104. package/src/react-explorer/explorer-effects.ts +0 -20
  105. package/src/react-explorer/explorer-states.ts +0 -217
  106. package/src/react-explorer/index.ts +0 -23
  107. package/src/react-explorer/space-states.ts +0 -72
  108. package/src/react-explorer/view-states.ts +0 -41
  109. package/src/realtime/README.md +0 -33
  110. package/src/realtime/hook-composition/expose-family.ts +0 -101
  111. package/src/realtime/hook-composition/expose-single.ts +0 -38
  112. package/src/realtime/hook-composition/expose-timeline.ts +0 -60
  113. package/src/realtime/hook-composition/index.ts +0 -12
  114. package/src/realtime/hook-composition/receive-state.ts +0 -29
  115. package/src/realtime/hook-composition/receive-transaction.ts +0 -18
  116. package/src/realtime/index.ts +0 -1
  117. package/src/realtime-react/index.ts +0 -3
  118. package/src/realtime-react/realtime-context.tsx +0 -30
  119. package/src/realtime-react/realtime-hooks.ts +0 -39
  120. package/src/realtime-react/realtime-state.ts +0 -10
  121. package/src/realtime-react/use-pull-family-member.ts +0 -26
  122. package/src/realtime-react/use-pull-family.ts +0 -24
  123. package/src/realtime-react/use-pull.ts +0 -22
  124. package/src/realtime-react/use-push.ts +0 -25
  125. package/src/realtime-react/use-server-action.ts +0 -33
  126. package/src/realtime-testing/index.ts +0 -1
  127. package/src/realtime-testing/setup-realtime-test.tsx +0 -159
  128. package/src/web-effects/index.ts +0 -1
  129. package/src/web-effects/storage.ts +0 -30
package/src/index.ts CHANGED
@@ -1,17 +1,20 @@
1
- import { capitalize } from "~/packages/anvl/src/string/capitalize"
2
-
3
1
  import {
4
2
  IMPLICIT,
5
3
  closeOperation,
6
- openOperation,
7
4
  getState__INTERNAL,
8
- setState__INTERNAL,
9
5
  isAtomDefault,
10
6
  isSelectorDefault,
7
+ openOperation,
8
+ setState__INTERNAL,
11
9
  withdraw,
12
- } from "./internal"
13
- import * as __INTERNAL__ from "./internal"
14
- import type { Store } from "./internal/store"
10
+ } from "atom.io/internal"
11
+ import * as __INTERNAL__ from "atom.io/internal"
12
+ import type { Store } from "atom.io/internal"
13
+
14
+ import type { ƒn } from "~/packages/anvl/src/function"
15
+ import { capitalize } from "~/packages/anvl/src/string/capitalize"
16
+
17
+ export { ƒn }
15
18
 
16
19
  export * from "./atom"
17
20
  export * from "./logger"
@@ -21,8 +24,8 @@ export * from "./subscribe"
21
24
  export * from "./timeline"
22
25
  export * from "./transaction"
23
26
  export { __INTERNAL__ }
24
- export type { Store } from "./internal/store"
25
- export type { Serializable } from "~/packages/anvl/src/json"
27
+ export type { Store } from "atom.io/internal"
28
+ export type { Json } from "atom.io/json"
26
29
 
27
30
  export type AtomToken<_> = {
28
31
  key: string
package/src/logger.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { doNothing } from "~/packages/anvl/src/function"
1
+ import { IMPLICIT } from "atom.io/internal"
2
+ import type { Store } from "atom.io/internal"
2
3
 
3
- import type { Store } from "./internal/store"
4
- import { IMPLICIT } from "./internal/store"
4
+ import { doNothing } from "~/packages/anvl/src/function"
5
5
 
6
6
  export type Logger = Pick<Console, `error` | `info` | `warn`>
7
7
  export const LOG_LEVELS: ReadonlyArray<keyof Logger> = [
@@ -21,10 +21,10 @@ export const setLogLevel = (
21
21
  store.config.logger = { ...console }
22
22
  LOG_LEVELS.forEach((logLevel) => {
23
23
  if (LOG_LEVELS.indexOf(logLevel) < LOG_LEVELS.indexOf(preferredLevel)) {
24
- // rome-ignore lint/style/noNonNullAssertion: we just set it
24
+ // biome-ignore lint/style/noNonNullAssertion: we just set it
25
25
  store.config.logger![logLevel] = doNothing
26
26
  } else {
27
- // rome-ignore lint/style/noNonNullAssertion: we just set it
27
+ // biome-ignore lint/style/noNonNullAssertion: we just set it
28
28
  store.config.logger![logLevel] = logger__INTERNAL[logLevel]
29
29
  }
30
30
  })
package/src/selector.ts CHANGED
@@ -1,8 +1,8 @@
1
- import type { Serializable } from "~/packages/anvl/src/json"
1
+ import type { Subject } from "atom.io/internal"
2
+ import { selectorFamily__INTERNAL, selector__INTERNAL } from "atom.io/internal"
3
+ import type { Json } from "atom.io/json"
2
4
 
3
5
  import type { ReadonlySelectorToken, SelectorToken } from "."
4
- import type { Subject } from "./internal"
5
- import { selectorFamily__INTERNAL, selector__INTERNAL } from "./internal"
6
6
  import type { Read, Write } from "./transaction"
7
7
 
8
8
  export type SelectorOptions<T> = {
@@ -25,39 +25,41 @@ export function selector<T>(
25
25
  return selector__INTERNAL(options)
26
26
  }
27
27
 
28
- export type SelectorFamilyOptions<T, K extends Serializable> = {
28
+ export type SelectorFamilyOptions<T, K extends Json.Serializable> = {
29
29
  key: string
30
30
  get: (key: K) => Read<() => T>
31
31
  set: (key: K) => Write<(newValue: T) => void>
32
32
  }
33
- export type ReadonlySelectorFamilyOptions<T, K extends Serializable> = {
33
+ export type ReadonlySelectorFamilyOptions<T, K extends Json.Serializable> = {
34
34
  key: string
35
35
  get: (key: K) => Read<() => T>
36
36
  }
37
37
 
38
- export type SelectorFamily<T, K extends Serializable = Serializable> = ((
39
- key: K,
40
- ) => SelectorToken<T>) & {
38
+ export type SelectorFamily<
39
+ T,
40
+ K extends Json.Serializable = Json.Serializable,
41
+ > = ((key: K) => SelectorToken<T>) & {
41
42
  key: string
42
43
  type: `selector_family`
43
44
  subject: Subject<SelectorToken<T>>
44
45
  }
45
46
 
46
- export type ReadonlySelectorFamily<T, K extends Serializable = Serializable> = ((
47
- key: K,
48
- ) => ReadonlySelectorToken<T>) & {
47
+ export type ReadonlySelectorFamily<
48
+ T,
49
+ K extends Json.Serializable = Json.Serializable,
50
+ > = ((key: K) => ReadonlySelectorToken<T>) & {
49
51
  key: string
50
52
  type: `readonly_selector_family`
51
53
  subject: Subject<ReadonlySelectorToken<T>>
52
54
  }
53
55
 
54
- export function selectorFamily<T, K extends Serializable>(
56
+ export function selectorFamily<T, K extends Json.Serializable>(
55
57
  options: SelectorFamilyOptions<T, K>,
56
58
  ): SelectorFamily<T, K>
57
- export function selectorFamily<T, K extends Serializable>(
59
+ export function selectorFamily<T, K extends Json.Serializable>(
58
60
  options: ReadonlySelectorFamilyOptions<T, K>,
59
61
  ): ReadonlySelectorFamily<T, K>
60
- export function selectorFamily<T, K extends Serializable>(
62
+ export function selectorFamily<T, K extends Json.Serializable>(
61
63
  options: ReadonlySelectorFamilyOptions<T, K> | SelectorFamilyOptions<T, K>,
62
64
  ): ReadonlySelectorFamily<T, K> | SelectorFamily<T, K> {
63
65
  return selectorFamily__INTERNAL(options)
package/src/silo.ts CHANGED
@@ -1,53 +1,50 @@
1
- import type { redo, timeline, undo } from "."
2
- import { getState, setState, subscribe } from "."
3
- import type { atom, atomFamily } from "./atom"
4
- import type { Store } from "./internal"
5
1
  import {
2
+ Store,
6
3
  atomFamily__INTERNAL,
7
4
  atom__INTERNAL,
8
- createStore,
9
5
  redo__INTERNAL,
10
6
  selectorFamily__INTERNAL,
11
7
  selector__INTERNAL,
12
8
  timeline__INTERNAL,
13
9
  transaction__INTERNAL,
14
10
  undo__INTERNAL,
15
- } from "./internal"
11
+ } from "atom.io/internal"
12
+
13
+ import type { redo, timeline, undo } from "."
14
+ import { getState, setState, subscribe } from "."
15
+ import type { atom, atomFamily } from "./atom"
16
16
  import type { selector, selectorFamily } from "./selector"
17
17
  import type { transaction } from "./transaction"
18
18
 
19
- export type Silo = ReturnType<typeof silo>
20
-
21
- export const silo = (
22
- name: string,
23
- fromStore: Store | null = null,
24
- ): {
25
- store: Store
26
- atom: typeof atom
27
- atomFamily: typeof atomFamily
28
- selector: typeof selector
29
- selectorFamily: typeof selectorFamily
30
- transaction: typeof transaction
31
- timeline: typeof timeline
32
- getState: typeof getState
33
- setState: typeof setState
34
- subscribe: typeof subscribe
35
- undo: typeof undo
36
- redo: typeof redo
37
- } => {
38
- const store = createStore(name, fromStore)
39
- return {
40
- store,
41
- atom: (options) => atom__INTERNAL(options, undefined, store),
42
- atomFamily: (options) => atomFamily__INTERNAL(options, store),
43
- selector: (options) => selector__INTERNAL(options, undefined, store) as any,
44
- selectorFamily: (options) => selectorFamily__INTERNAL(options, store) as any,
45
- transaction: (options) => transaction__INTERNAL(options, store),
46
- timeline: (options) => timeline__INTERNAL(options, store),
47
- getState: (token) => getState(token, store),
48
- setState: (token, newValue) => setState(token, newValue, store),
49
- subscribe: (token, handler) => subscribe(token, handler, store),
50
- undo: (token) => undo__INTERNAL(token, store),
51
- redo: (token) => redo__INTERNAL(token, store),
19
+ export class Silo {
20
+ public store: Store
21
+ public atom: typeof atom
22
+ public atomFamily: typeof atomFamily
23
+ public selector: typeof selector
24
+ public selectorFamily: typeof selectorFamily
25
+ public transaction: typeof transaction
26
+ public timeline: typeof timeline
27
+ public getState: typeof getState
28
+ public setState: typeof setState
29
+ public subscribe: typeof subscribe
30
+ public undo: typeof undo
31
+ public redo: typeof redo
32
+ public constructor(name: string, fromStore: Store | null = null) {
33
+ const store = new Store(name, fromStore)
34
+ this.store = store
35
+ this.atom = (options) => atom__INTERNAL(options, undefined, store)
36
+ this.atomFamily = (options) => atomFamily__INTERNAL(options, store)
37
+ this.selector = (options) =>
38
+ selector__INTERNAL(options, undefined, store) as any
39
+ this.selectorFamily = (options) =>
40
+ selectorFamily__INTERNAL(options, store) as any
41
+ this.transaction = (options) => transaction__INTERNAL(options, store)
42
+ this.timeline = (options) => timeline__INTERNAL(options, store)
43
+ this.getState = (token) => getState(token, store)
44
+ this.setState = (token, newValue) => setState(token, newValue, store)
45
+ ;(this.subscribe = (token, handler, key) =>
46
+ subscribe(token, handler, key, store)),
47
+ (this.undo = (token) => undo__INTERNAL(token, store))
48
+ this.redo = (token) => redo__INTERNAL(token, store)
52
49
  }
53
50
  }
package/src/subscribe.ts CHANGED
@@ -1,23 +1,28 @@
1
- import type { ƒn } from "~/packages/anvl/src/function"
1
+ import type { Store } from "atom.io/internal"
2
+ import { IMPLICIT, subscribeToRootAtoms, withdraw } from "atom.io/internal"
2
3
 
3
4
  import type {
5
+ FamilyMetadata,
4
6
  ReadonlySelectorToken,
5
7
  StateToken,
6
8
  TimelineToken,
7
9
  TimelineUpdate,
8
10
  TransactionToken,
9
11
  TransactionUpdate,
12
+ ƒn,
10
13
  } from "."
11
- import type { Store } from "./internal"
12
- import { IMPLICIT, subscribeToRootAtoms, withdraw } from "./internal"
13
14
 
14
15
  export type StateUpdate<T> = { newValue: T; oldValue: T }
15
- export type KeyedStateUpdate<T> = StateUpdate<T> & { key: string }
16
+ export type KeyedStateUpdate<T> = StateUpdate<T> & {
17
+ key: string
18
+ family?: FamilyMetadata
19
+ }
16
20
  export type UpdateHandler<T> = (update: StateUpdate<T>) => void
17
21
 
18
22
  export const subscribe = <T>(
19
23
  token: ReadonlySelectorToken<T> | StateToken<T>,
20
24
  handleUpdate: UpdateHandler<T>,
25
+ key: string = Math.random().toString(36).slice(2),
21
26
  store: Store = IMPLICIT.STORE,
22
27
  ): (() => void) => {
23
28
  const state = withdraw<T>(token, store)
@@ -26,24 +31,24 @@ export const subscribe = <T>(
26
31
  `State "${token.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`,
27
32
  )
28
33
  }
29
- const subscription = state.subject.subscribe(handleUpdate)
34
+ const unsubFunction = state.subject.subscribe(key, handleUpdate)
30
35
  store.config.logger?.info(`👀 subscribe to "${state.key}"`)
31
- const dependencySubscriptions =
36
+ const dependencyUnsubFunctions =
32
37
  state.type !== `atom` ? subscribeToRootAtoms(state, store) : null
33
38
 
34
39
  const unsubscribe =
35
- dependencySubscriptions === null
40
+ dependencyUnsubFunctions === null
36
41
  ? () => {
37
42
  store.config.logger?.info(`🙈 unsubscribe from "${state.key}"`)
38
- subscription.unsubscribe()
43
+ unsubFunction()
39
44
  }
40
45
  : () => {
41
46
  store.config.logger?.info(
42
47
  `🙈 unsubscribe from "${state.key}" and its dependencies`,
43
48
  )
44
- subscription.unsubscribe()
45
- for (const dependencySubscription of dependencySubscriptions) {
46
- dependencySubscription.unsubscribe()
49
+ unsubFunction()
50
+ for (const unsubFromDependency of dependencyUnsubFunctions) {
51
+ unsubFromDependency()
47
52
  }
48
53
  }
49
54
 
@@ -57,6 +62,7 @@ export type TransactionUpdateHandler<ƒ extends ƒn> = (
57
62
  export const subscribeToTransaction = <ƒ extends ƒn>(
58
63
  token: TransactionToken<ƒ>,
59
64
  handleUpdate: TransactionUpdateHandler<ƒ>,
65
+ key: string = Math.random().toString(36).slice(2),
60
66
  store = IMPLICIT.STORE,
61
67
  ): (() => void) => {
62
68
  const tx = withdraw(token, store)
@@ -66,17 +72,17 @@ export const subscribeToTransaction = <ƒ extends ƒn>(
66
72
  )
67
73
  }
68
74
  store.config.logger?.info(`👀 subscribe to transaction "${token.key}"`)
69
- const subscription = tx.subject.subscribe(handleUpdate)
70
- const unsubscribe = () => {
75
+ const unsubscribe = tx.subject.subscribe(key, handleUpdate)
76
+ return () => {
71
77
  store.config.logger?.info(`🙈 unsubscribe from transaction "${token.key}"`)
72
- subscription.unsubscribe()
78
+ unsubscribe()
73
79
  }
74
- return unsubscribe
75
80
  }
76
81
 
77
82
  export const subscribeToTimeline = (
78
83
  token: TimelineToken,
79
- handleUpdate: (update: TimelineUpdate) => void,
84
+ handleUpdate: (update: TimelineUpdate | `redo` | `undo`) => void,
85
+ key: string = Math.random().toString(36).slice(2),
80
86
  store = IMPLICIT.STORE,
81
87
  ): (() => void) => {
82
88
  const tl = withdraw(token, store)
@@ -86,10 +92,9 @@ export const subscribeToTimeline = (
86
92
  )
87
93
  }
88
94
  store.config.logger?.info(`👀 subscribe to timeline "${token.key}"`)
89
- const subscription = tl.subject.subscribe(handleUpdate)
90
- const unsubscribe = () => {
95
+ const unsubscribe = tl.subject.subscribe(key, handleUpdate)
96
+ return () => {
91
97
  store.config.logger?.info(`🙈 unsubscribe from timeline "${token.key}"`)
92
- subscription.unsubscribe()
98
+ unsubscribe()
93
99
  }
94
- return unsubscribe
95
100
  }
package/src/timeline.ts CHANGED
@@ -1,11 +1,16 @@
1
- import type { AtomFamily, AtomToken } from "."
2
1
  import type {
3
2
  TimelineAtomUpdate,
4
3
  TimelineSelectorUpdate,
5
4
  TimelineTransactionUpdate,
6
- } from "./internal"
7
- import { IMPLICIT } from "./internal"
8
- import { redo__INTERNAL, timeline__INTERNAL, undo__INTERNAL } from "./internal/"
5
+ } from "atom.io/internal"
6
+ import { IMPLICIT } from "atom.io/internal"
7
+ import {
8
+ redo__INTERNAL,
9
+ timeline__INTERNAL,
10
+ undo__INTERNAL,
11
+ } from "atom.io/internal/"
12
+
13
+ import type { AtomFamily, AtomToken } from "."
9
14
 
10
15
  export type TimelineToken = {
11
16
  key: string
@@ -1,8 +1,7 @@
1
- import type { ƒn } from "~/packages/anvl/src/function"
1
+ import type { Store } from "atom.io/internal"
2
+ import { IMPLICIT, transaction__INTERNAL, withdraw } from "atom.io/internal"
2
3
 
3
- import type { KeyedStateUpdate, ReadonlySelectorToken, StateToken } from "."
4
- import type { Store } from "./internal"
5
- import { IMPLICIT, transaction__INTERNAL, withdraw } from "./internal"
4
+ import type { KeyedStateUpdate, ReadonlySelectorToken, StateToken, ƒn } from "."
6
5
 
7
6
  export type TransactionToken<_> = {
8
7
  key: string
@@ -1,54 +0,0 @@
1
- import { Subject } from "."
2
- import { deposit } from "./get"
3
- import { markAtomAsDefault } from "./is-default"
4
- import { cacheValue } from "./operation"
5
- import type { Store } from "./store"
6
- import { IMPLICIT } from "./store"
7
- import { target } from "./transaction-internal"
8
- import type { AtomToken, FamilyMetadata, UpdateHandler } from ".."
9
- import { setState, subscribe } from ".."
10
- import type { AtomOptions } from "../atom"
11
-
12
- export type Atom<T> = {
13
- key: string
14
- type: `atom`
15
- family?: FamilyMetadata
16
- subject: Subject<{ newValue: T; oldValue: T }>
17
- default: T
18
- }
19
-
20
- export function atom__INTERNAL<T>(
21
- options: AtomOptions<T>,
22
- family?: FamilyMetadata,
23
- store: Store = IMPLICIT.STORE,
24
- ): AtomToken<T> {
25
- const core = target(store)
26
- const existing = core.atoms.get(options.key)
27
- if (existing) {
28
- store.config.logger?.error?.(
29
- `Key "${options.key}" already exists in the store.`,
30
- )
31
- return deposit(existing)
32
- }
33
- const subject = new Subject<{ newValue: T; oldValue: T }>()
34
- const newAtom = {
35
- ...options,
36
- subject,
37
- type: `atom`,
38
- ...(family && { family }),
39
- } as const
40
- const initialValue =
41
- options.default instanceof Function ? options.default() : options.default
42
- core.atoms.set(newAtom.key, newAtom)
43
- markAtomAsDefault(options.key, store)
44
- cacheValue(options.key, initialValue, store)
45
- const token = deposit(newAtom)
46
- options.effects?.forEach((effect) =>
47
- effect({
48
- setSelf: (next) => setState(token, next, store),
49
- onSet: (handle: UpdateHandler<T>) => subscribe(token, handle, store),
50
- }),
51
- )
52
- store.subject.atomCreation.next(token)
53
- return token as AtomToken<T>
54
- }
@@ -1,141 +0,0 @@
1
- import type { Serializable } from "~/packages/anvl/src/json"
2
- import { stringifyJson } from "~/packages/anvl/src/json"
3
-
4
- import type { Store } from "."
5
- import {
6
- Subject,
7
- atom__INTERNAL,
8
- withdraw,
9
- selector__INTERNAL,
10
- target,
11
- deposit,
12
- IMPLICIT,
13
- } from "."
14
- import type {
15
- AtomFamily,
16
- AtomFamilyOptions,
17
- AtomToken,
18
- FamilyMetadata,
19
- ReadonlySelectorFamily,
20
- ReadonlySelectorFamilyOptions,
21
- ReadonlySelectorToken,
22
- SelectorFamily,
23
- SelectorFamilyOptions,
24
- SelectorToken,
25
- } from ".."
26
-
27
- export function atomFamily__INTERNAL<T, K extends Serializable>(
28
- options: AtomFamilyOptions<T, K>,
29
- store: Store = IMPLICIT.STORE,
30
- ): AtomFamily<T, K> {
31
- const subject = new Subject<AtomToken<T>>()
32
- return Object.assign(
33
- (key: K): AtomToken<T> => {
34
- const subKey = stringifyJson(key)
35
- const family: FamilyMetadata = { key: options.key, subKey }
36
- const fullKey = `${options.key}(${subKey})`
37
- const existing = withdraw({ key: fullKey, type: `atom` }, store)
38
- const token: AtomToken<any> = existing
39
- ? deposit(existing)
40
- : atom__INTERNAL<T>(
41
- {
42
- key: fullKey,
43
- default:
44
- options.default instanceof Function
45
- ? options.default(key)
46
- : options.default,
47
- effects: options.effects?.(key),
48
- },
49
- family,
50
- store,
51
- )
52
- subject.next(token)
53
- return token
54
- },
55
- {
56
- key: options.key,
57
- type: `atom_family`,
58
- subject,
59
- } as const,
60
- )
61
- }
62
-
63
- export function readonlySelectorFamily__INTERNAL<T, K extends Serializable>(
64
- options: ReadonlySelectorFamilyOptions<T, K>,
65
- store?: Store,
66
- ): ReadonlySelectorFamily<T, K> {
67
- const core = target(store)
68
- const subject = new Subject<ReadonlySelectorToken<T>>()
69
- return Object.assign(
70
- (key: K): ReadonlySelectorToken<T> => {
71
- const subKey = stringifyJson(key)
72
- const family: FamilyMetadata = { key: options.key, subKey }
73
- const fullKey = `${options.key}(${subKey})`
74
- const existing = core.readonlySelectors.get(fullKey)
75
- if (existing) {
76
- return deposit(existing)
77
- }
78
- return selector__INTERNAL<T>(
79
- {
80
- key: fullKey,
81
- get: options.get(key),
82
- },
83
- family,
84
- store,
85
- ) as ReadonlySelectorToken<T>
86
- },
87
- {
88
- key: options.key,
89
- type: `readonly_selector_family`,
90
- subject,
91
- } as const,
92
- ) as ReadonlySelectorFamily<T, K>
93
- }
94
-
95
- export function selectorFamily__INTERNAL<T, K extends Serializable>(
96
- options: SelectorFamilyOptions<T, K>,
97
- store?: Store,
98
- ): SelectorFamily<T, K>
99
- export function selectorFamily__INTERNAL<T, K extends Serializable>(
100
- options: ReadonlySelectorFamilyOptions<T, K>,
101
- store?: Store,
102
- ): ReadonlySelectorFamily<T, K>
103
- export function selectorFamily__INTERNAL<T, K extends Serializable>(
104
- options: ReadonlySelectorFamilyOptions<T, K> | SelectorFamilyOptions<T, K>,
105
- store: Store = IMPLICIT.STORE,
106
- ): ReadonlySelectorFamily<T, K> | SelectorFamily<T, K> {
107
- const isReadonly = !(`set` in options)
108
-
109
- if (isReadonly) {
110
- return readonlySelectorFamily__INTERNAL(options, store)
111
- }
112
- const core = target(store)
113
- const subject = new Subject<SelectorToken<T>>()
114
-
115
- return Object.assign(
116
- (key: K): SelectorToken<T> => {
117
- const subKey = stringifyJson(key)
118
- const family: FamilyMetadata = { key: options.key, subKey }
119
- const fullKey = `${options.key}(${subKey})`
120
- const existing = core.selectors.get(fullKey)
121
- if (existing) {
122
- return deposit(existing)
123
- }
124
- const token = selector__INTERNAL<T>(
125
- {
126
- key: fullKey,
127
- get: options.get(key),
128
- set: options.set(key),
129
- },
130
- family,
131
- store,
132
- )
133
- subject.next(token)
134
- return token
135
- },
136
- {
137
- key: options.key,
138
- type: `selector_family`,
139
- } as const,
140
- ) as SelectorFamily<T, K>
141
- }