atom.io 0.6.6 → 0.6.8

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 (76) hide show
  1. package/dist/index.d.mts +34 -25
  2. package/dist/index.d.ts +34 -25
  3. package/dist/index.js +94 -115
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +94 -105
  6. package/dist/index.mjs.map +1 -1
  7. package/introspection/dist/index.d.mts +272 -0
  8. package/introspection/dist/index.d.ts +272 -0
  9. package/introspection/dist/index.js +41 -3
  10. package/introspection/dist/index.js.map +1 -1
  11. package/introspection/dist/index.mjs +41 -3
  12. package/introspection/dist/index.mjs.map +1 -1
  13. package/json/dist/index.d.mts +5 -7
  14. package/json/dist/index.d.ts +5 -7
  15. package/json/dist/index.js.map +1 -1
  16. package/json/dist/index.mjs.map +1 -1
  17. package/package.json +22 -14
  18. package/react-devtools/dist/index.css +9 -1
  19. package/react-devtools/dist/index.css.map +1 -1
  20. package/react-devtools/dist/index.d.mts +22 -25
  21. package/react-devtools/dist/index.d.ts +22 -25
  22. package/react-devtools/dist/index.js +845 -326
  23. package/react-devtools/dist/index.js.map +1 -1
  24. package/react-devtools/dist/index.mjs +830 -306
  25. package/react-devtools/dist/index.mjs.map +1 -1
  26. package/realtime/dist/index.d.mts +6 -8
  27. package/realtime/dist/index.d.ts +6 -8
  28. package/realtime/dist/index.js +0 -1
  29. package/realtime/dist/index.js.map +1 -1
  30. package/realtime/dist/index.mjs +0 -1
  31. package/realtime/dist/index.mjs.map +1 -1
  32. package/realtime-react/dist/index.d.mts +11 -13
  33. package/realtime-react/dist/index.d.ts +11 -13
  34. package/realtime-react/dist/index.js.map +1 -1
  35. package/realtime-react/dist/index.mjs.map +1 -1
  36. package/src/atom.ts +4 -4
  37. package/src/index.ts +1 -1
  38. package/src/internal/atom-internal.ts +5 -6
  39. package/src/internal/families-internal.ts +23 -20
  40. package/src/internal/get.ts +7 -9
  41. package/src/internal/index.ts +1 -1
  42. package/src/internal/operation.ts +14 -21
  43. package/src/internal/selector/create-read-write-selector.ts +11 -5
  44. package/src/internal/selector/create-readonly-selector.ts +4 -8
  45. package/src/internal/selector-internal.ts +1 -3
  46. package/src/internal/set.ts +1 -4
  47. package/src/internal/store.ts +19 -22
  48. package/src/internal/subscribe-internal.ts +7 -1
  49. package/src/internal/time-travel-internal.ts +4 -4
  50. package/src/internal/timeline/add-atom-to-timeline.ts +2 -2
  51. package/src/internal/timeline-internal.ts +3 -5
  52. package/src/internal/transaction/apply-transaction.ts +9 -6
  53. package/src/internal/transaction/build-transaction.ts +6 -6
  54. package/src/internal/transaction-internal.ts +1 -7
  55. package/src/introspection/attach-timeline-family.ts +14 -4
  56. package/src/introspection/attach-transaction-logs.ts +1 -1
  57. package/src/json/select-json.ts +1 -1
  58. package/src/react-devtools/AtomIODevtools.tsx +1 -2
  59. package/src/react-devtools/StateEditor.tsx +5 -1
  60. package/src/react-devtools/StateIndex.tsx +4 -1
  61. package/src/react-devtools/devtools.scss +0 -1
  62. package/src/react-explorer/AtomIOExplorer.tsx +3 -3
  63. package/src/realtime/hook-composition/expose-family.ts +2 -2
  64. package/src/realtime/hook-composition/expose-single.ts +1 -1
  65. package/src/realtime/hook-composition/receive-state.ts +1 -1
  66. package/src/realtime-react/realtime-hooks.ts +4 -4
  67. package/src/realtime-react/use-pull-family-member.ts +2 -2
  68. package/src/realtime-react/use-pull-family.ts +2 -2
  69. package/src/realtime-react/use-pull.ts +3 -1
  70. package/src/realtime-react/use-push.ts +3 -1
  71. package/src/selector.ts +14 -12
  72. package/src/subscribe.ts +1 -1
  73. package/src/tracker/index.ts +3 -0
  74. package/src/tracker/tracker.ts +61 -0
  75. package/src/web-effects/storage.ts +1 -1
  76. package/src/internal/subject.ts +0 -24
@@ -10,15 +10,15 @@ import { usePush } from "./use-push"
10
10
  import { useServerAction } from "./use-server-action"
11
11
 
12
12
  export type RealtimeHooks = {
13
- usePull: <J extends Json>(token: AtomIO.StateToken<J>) => void
14
- usePullFamily: <J extends Json>(
13
+ usePull: <J extends Json.Serializable>(token: AtomIO.StateToken<J>) => void
14
+ usePullFamily: <J extends Json.Serializable>(
15
15
  family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,
16
16
  ) => void
17
- usePullFamilyMember: <J extends Json>(
17
+ usePullFamilyMember: <J extends Json.Serializable>(
18
18
  family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,
19
19
  subKey: string,
20
20
  ) => void
21
- usePush: <J extends Json>(token: AtomIO.StateToken<J>) => void
21
+ usePush: <J extends Json.Serializable>(token: AtomIO.StateToken<J>) => void
22
22
  useServerAction: <ƒ extends ƒn>(
23
23
  token: AtomIO.TransactionToken<ƒ>,
24
24
  ) => (...parameters: Parameters<ƒ>) => ReturnType<ƒ>
@@ -6,9 +6,9 @@ import type { Json } from "~/packages/anvl/src/json"
6
6
  import { RealtimeContext } from "./realtime-context"
7
7
  import { StoreContext } from "../react"
8
8
 
9
- export function usePullFamilyMember<J extends Json>(
9
+ export function usePullFamilyMember<J extends Json.Serializable>(
10
10
  family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,
11
- subKey: AtomIO.Serializable,
11
+ subKey: AtomIO.Json.Serializable,
12
12
  ): void {
13
13
  const token = family(subKey)
14
14
  const { socket } = React.useContext(RealtimeContext)
@@ -6,13 +6,13 @@ import type { Json } from "~/packages/anvl/src/json"
6
6
  import { RealtimeContext } from "./realtime-context"
7
7
  import { StoreContext } from "../react"
8
8
 
9
- export function usePullFamily<J extends Json>(
9
+ export function usePullFamily<J extends Json.Serializable>(
10
10
  family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,
11
11
  ): void {
12
12
  const { socket } = React.useContext(RealtimeContext)
13
13
  const store = React.useContext(StoreContext)
14
14
  React.useEffect(() => {
15
- socket.on(`serve:${family.key}`, (key: Json, data: J) => {
15
+ socket.on(`serve:${family.key}`, (key: Json.Serializable, data: J) => {
16
16
  AtomIO.setState(family(key), data, store)
17
17
  })
18
18
  socket?.emit(`sub:${family.key}`)
@@ -6,7 +6,9 @@ import type { Json } from "~/packages/anvl/src/json"
6
6
  import { RealtimeContext } from "./realtime-context"
7
7
  import { StoreContext } from "../react"
8
8
 
9
- export function usePull<J extends Json>(token: AtomIO.StateToken<J>): void {
9
+ export function usePull<J extends Json.Serializable>(
10
+ token: AtomIO.StateToken<J>,
11
+ ): void {
10
12
  const { socket } = React.useContext(RealtimeContext)
11
13
  const store = React.useContext(StoreContext)
12
14
  React.useEffect(() => {
@@ -6,7 +6,9 @@ import type { Json } from "~/packages/anvl/src/json"
6
6
  import { RealtimeContext } from "./realtime-context"
7
7
  import { StoreContext } from "../react"
8
8
 
9
- export function usePush<J extends Json>(token: AtomIO.StateToken<J>): void {
9
+ export function usePush<J extends Json.Serializable>(
10
+ token: AtomIO.StateToken<J>,
11
+ ): void {
10
12
  const { socket } = React.useContext(RealtimeContext)
11
13
  const store = React.useContext(StoreContext)
12
14
  React.useEffect(() => {
package/src/selector.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Serializable } from "~/packages/anvl/src/json"
1
+ import type { Json } from "~/packages/anvl/src/json"
2
2
 
3
3
  import type { ReadonlySelectorToken, SelectorToken } from "."
4
4
  import type { Subject } from "./internal"
@@ -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/subscribe.ts CHANGED
@@ -76,7 +76,7 @@ export const subscribeToTransaction = <ƒ extends ƒn>(
76
76
 
77
77
  export const subscribeToTimeline = (
78
78
  token: TimelineToken,
79
- handleUpdate: (update: TimelineUpdate) => void,
79
+ handleUpdate: (update: TimelineUpdate | `redo` | `undo`) => void,
80
80
  store = IMPLICIT.STORE,
81
81
  ): (() => void) => {
82
82
  const tl = withdraw(token, store)
@@ -0,0 +1,3 @@
1
+ export * from "./tracker"
2
+
3
+ export * from "~/packages/anvl/reactivity/transceiver"
@@ -0,0 +1,61 @@
1
+ import * as AtomIO from "atom.io"
2
+
3
+ import type { Transceiver } from "."
4
+
5
+ export type CacheOptions<Core extends Transceiver<any>> = {
6
+ key: string
7
+ default: Core
8
+ }
9
+
10
+ export const tracker = <Core extends Transceiver<any>>(
11
+ coreState: AtomIO.AtomToken<Core>,
12
+ store: AtomIO.__INTERNAL__.Store = AtomIO.__INTERNAL__.IMPLICIT.STORE,
13
+ ): AtomIO.AtomToken<
14
+ (Core extends Transceiver<infer Signal> ? Signal : never) | null
15
+ > => {
16
+ const trackerKey = `${coreState.key}:signal`
17
+ const trackerState = AtomIO.atom<
18
+ (Core extends Transceiver<infer Signal> ? Signal : never) | null
19
+ >({
20
+ key: trackerKey,
21
+ default: null,
22
+ effects: [
23
+ ({ setSelf }) => {
24
+ AtomIO.getState(coreState).observe((update) => setSelf(update))
25
+ },
26
+ ({ onSet }) => {
27
+ onSet(({ newValue, oldValue }) => {
28
+ const timelineId = store.timelineAtoms.getRelatedId(trackerKey)
29
+ if (timelineId) {
30
+ const timelineData = store.timelines.get(timelineId)
31
+ if (timelineData?.timeTraveling) {
32
+ const unsubscribe = AtomIO.subscribeToTimeline(
33
+ { key: timelineId, type: `timeline` },
34
+ (update) => {
35
+ unsubscribe()
36
+ AtomIO.setState(coreState, (core) => {
37
+ if (update === `redo` && newValue) {
38
+ core.do(newValue)
39
+ } else if (update === `undo` && oldValue) {
40
+ core.undo(oldValue)
41
+ }
42
+ return core
43
+ })
44
+ },
45
+ )
46
+ return
47
+ }
48
+ }
49
+
50
+ const { unsubscribe } = store.subject.operationStatus.subscribe(() => {
51
+ unsubscribe()
52
+ if (newValue) {
53
+ AtomIO.setState(coreState, (core) => (core.do(newValue), core))
54
+ }
55
+ })
56
+ })
57
+ },
58
+ ],
59
+ })
60
+ return trackerState
61
+ }
@@ -25,6 +25,6 @@ export const persistAtom =
25
25
  })
26
26
  }
27
27
 
28
- export const lazyLocalStorageEffect: <J extends Json>(
28
+ export const lazyLocalStorageEffect: <J extends Json.Serializable>(
29
29
  key: string,
30
30
  ) => AtomEffect<J> = persistAtom(localStorage)(JSON)
@@ -1,24 +0,0 @@
1
- type Subscriber<T> = (value: T) => void
2
-
3
- export class Subject<T> {
4
- public subscribers: Subscriber<T>[] = []
5
-
6
- public subscribe(subscriber: Subscriber<T>): { unsubscribe: () => void } {
7
- this.subscribers.push(subscriber)
8
- const unsubscribe = () => this.unsubscribe(subscriber)
9
- return { unsubscribe }
10
- }
11
-
12
- private unsubscribe(subscriber: Subscriber<T>) {
13
- const subscriberIndex = this.subscribers.indexOf(subscriber)
14
- if (subscriberIndex !== -1) {
15
- this.subscribers.splice(subscriberIndex, 1)
16
- }
17
- }
18
-
19
- public next(value: T): void {
20
- for (const subscriber of this.subscribers) {
21
- subscriber(value)
22
- }
23
- }
24
- }