atom.io 0.4.1 → 0.6.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.
- package/README.md +38 -10
- package/dist/index.d.mts +614 -0
- package/dist/index.d.ts +130 -77
- package/dist/index.js +584 -347
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +582 -347
- package/dist/index.mjs.map +1 -1
- package/json/dist/index.d.mts +18 -0
- package/json/dist/index.d.ts +18 -0
- package/json/dist/index.js +51 -0
- package/json/dist/index.js.map +1 -0
- package/json/dist/index.mjs +15 -0
- package/json/dist/index.mjs.map +1 -0
- package/json/package.json +15 -0
- package/package.json +43 -9
- package/react/dist/index.d.mts +24 -0
- package/react/dist/index.d.ts +18 -11
- package/react/dist/index.js +45 -21
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs +31 -21
- package/react/dist/index.mjs.map +1 -1
- package/react-devtools/dist/index.d.mts +15 -0
- package/react-devtools/dist/index.d.ts +4 -4
- package/react-devtools/dist/index.js +1 -1
- package/react-devtools/dist/index.js.map +1 -1
- package/react-devtools/dist/index.mjs +1 -1
- package/react-devtools/dist/index.mjs.map +1 -1
- package/realtime/dist/index.d.mts +27 -0
- package/realtime/dist/index.d.ts +27 -0
- package/realtime/dist/index.js +191 -0
- package/realtime/dist/index.js.map +1 -0
- package/realtime/dist/index.mjs +152 -0
- package/realtime/dist/index.mjs.map +1 -0
- package/realtime/package.json +15 -0
- package/realtime-react/dist/index.d.mts +45 -0
- package/realtime-react/dist/index.d.ts +45 -0
- package/realtime-react/dist/index.js +213 -0
- package/realtime-react/dist/index.js.map +1 -0
- package/realtime-react/dist/index.mjs +168 -0
- package/realtime-react/dist/index.mjs.map +1 -0
- package/realtime-react/package.json +15 -0
- package/src/index.ts +21 -5
- package/src/internal/atom-internal.ts +1 -1
- package/src/internal/families-internal.ts +3 -3
- package/src/internal/get.ts +39 -15
- package/src/internal/index.ts +2 -0
- package/src/internal/meta/meta-state.ts +1 -1
- package/src/internal/operation.ts +3 -1
- package/src/internal/selector/create-read-write-selector.ts +62 -0
- package/src/internal/selector/create-readonly-selector.ts +52 -0
- package/src/internal/selector/index.ts +4 -0
- package/src/internal/selector/lookup-selector-sources.ts +16 -0
- package/src/internal/selector/register-selector.ts +57 -0
- package/src/internal/selector/trace-selector-atoms.ts +43 -0
- package/src/internal/selector/update-selector-atoms.ts +33 -0
- package/src/internal/selector-internal.ts +9 -197
- package/src/internal/set.ts +1 -1
- package/src/internal/store.ts +44 -17
- package/src/internal/subscribe-internal.ts +6 -1
- package/src/internal/time-travel-internal.ts +7 -7
- package/src/internal/timeline/add-atom-to-timeline.ts +164 -0
- package/src/internal/timeline/index.ts +1 -0
- package/src/internal/timeline-internal.ts +39 -146
- package/src/internal/transaction/abort-transaction.ts +12 -0
- package/src/internal/transaction/apply-transaction.ts +54 -0
- package/src/internal/transaction/build-transaction.ts +33 -0
- package/src/internal/transaction/index.ts +25 -0
- package/src/internal/transaction/redo-transaction.ts +23 -0
- package/src/internal/transaction/undo-transaction.ts +23 -0
- package/src/internal/transaction-internal.ts +16 -133
- package/src/json/index.ts +1 -0
- package/src/json/select-json.ts +18 -0
- package/src/react/index.ts +2 -46
- package/src/react/store-context.tsx +14 -0
- package/src/react/store-hooks.ts +48 -0
- package/src/react-devtools/AtomIODevtools.tsx +1 -1
- package/src/react-explorer/AtomIOExplorer.tsx +2 -2
- package/src/react-explorer/explorer-states.ts +5 -5
- package/src/react-explorer/index.ts +1 -1
- package/src/react-explorer/space-states.ts +8 -9
- package/src/realtime/README.md +33 -0
- package/src/realtime/hook-composition/expose-family.ts +101 -0
- package/src/realtime/hook-composition/expose-single.ts +38 -0
- package/src/realtime/hook-composition/index.ts +12 -0
- package/src/realtime/hook-composition/receive-state.ts +29 -0
- package/src/realtime/hook-composition/receive-transaction.ts +18 -0
- package/src/realtime/index.ts +1 -0
- package/src/realtime-react/index.ts +3 -0
- package/src/realtime-react/realtime-context.tsx +31 -0
- package/src/realtime-react/realtime-hooks.ts +39 -0
- package/src/realtime-react/realtime-state.ts +10 -0
- package/src/realtime-react/use-pull-family-member.ts +27 -0
- package/src/realtime-react/use-pull-family.ts +25 -0
- package/src/realtime-react/use-pull.ts +23 -0
- package/src/realtime-react/use-push.ts +26 -0
- package/src/realtime-react/use-server-action.ts +34 -0
- package/src/selector.ts +9 -6
- package/src/silo.ts +53 -0
- package/src/subscribe.ts +42 -2
- package/src/timeline.ts +10 -0
- package/src/transaction.ts +24 -12
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
|
|
3
|
+
import * as AR from "atom.io/react"
|
|
4
|
+
import type { Socket } from "socket.io-client"
|
|
5
|
+
import { io } from "socket.io-client"
|
|
6
|
+
|
|
7
|
+
import { myIdState__INTERNAL } from "./realtime-state"
|
|
8
|
+
|
|
9
|
+
export const RealtimeContext = React.createContext<{ socket: Socket }>({
|
|
10
|
+
socket: io(),
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
export const RealtimeProvider: React.FC<{
|
|
14
|
+
children: React.ReactNode
|
|
15
|
+
socket: Socket
|
|
16
|
+
}> = ({ children, socket }) => {
|
|
17
|
+
const setMyId = AR.useI(myIdState__INTERNAL)
|
|
18
|
+
React.useEffect(() => {
|
|
19
|
+
socket.on(`connect`, () => {
|
|
20
|
+
setMyId(socket.id)
|
|
21
|
+
})
|
|
22
|
+
socket.on(`disconnect`, () => {
|
|
23
|
+
setMyId(null)
|
|
24
|
+
})
|
|
25
|
+
}, [socket, setMyId])
|
|
26
|
+
return (
|
|
27
|
+
<RealtimeContext.Provider value={{ socket }}>
|
|
28
|
+
{children}
|
|
29
|
+
</RealtimeContext.Provider>
|
|
30
|
+
)
|
|
31
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type * as AtomIO from "atom.io"
|
|
2
|
+
|
|
3
|
+
import type { ƒn } from "~/packages/anvl/src/function"
|
|
4
|
+
import type { Json } from "~/packages/anvl/src/json"
|
|
5
|
+
|
|
6
|
+
import { usePull } from "./use-pull"
|
|
7
|
+
import { usePullFamily } from "./use-pull-family"
|
|
8
|
+
import { usePullFamilyMember } from "./use-pull-family-member"
|
|
9
|
+
import { usePush } from "./use-push"
|
|
10
|
+
import { useServerAction } from "./use-server-action"
|
|
11
|
+
|
|
12
|
+
export type RealtimeHooks = {
|
|
13
|
+
usePull: <J extends Json>(token: AtomIO.StateToken<J>) => void
|
|
14
|
+
usePullFamily: <J extends Json>(
|
|
15
|
+
family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>
|
|
16
|
+
) => void
|
|
17
|
+
usePullFamilyMember: <J extends Json>(
|
|
18
|
+
family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,
|
|
19
|
+
subKey: string
|
|
20
|
+
) => void
|
|
21
|
+
usePush: <J extends Json>(token: AtomIO.StateToken<J>) => void
|
|
22
|
+
useServerAction: <ƒ extends ƒn>(
|
|
23
|
+
token: AtomIO.TransactionToken<ƒ>
|
|
24
|
+
) => (...parameters: Parameters<ƒ>) => ReturnType<ƒ>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const realtimeHooks: RealtimeHooks = {
|
|
28
|
+
usePull,
|
|
29
|
+
usePullFamily,
|
|
30
|
+
usePullFamilyMember,
|
|
31
|
+
usePush,
|
|
32
|
+
useServerAction,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export * from "./use-pull"
|
|
36
|
+
export * from "./use-pull-family"
|
|
37
|
+
export * from "./use-pull-family-member"
|
|
38
|
+
export * from "./use-push"
|
|
39
|
+
export * from "./use-server-action"
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as AtomIO from "atom.io"
|
|
2
|
+
|
|
3
|
+
export const myIdState__INTERNAL = AtomIO.atom<string | null>({
|
|
4
|
+
key: `myId__INTERNAL`,
|
|
5
|
+
default: null,
|
|
6
|
+
})
|
|
7
|
+
export const myIdState = AtomIO.selector<string | null>({
|
|
8
|
+
key: `myId`,
|
|
9
|
+
get: ({ get }) => get(myIdState__INTERNAL),
|
|
10
|
+
})
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
|
|
3
|
+
import * as AtomIO from "atom.io"
|
|
4
|
+
|
|
5
|
+
import type { Json } from "~/packages/anvl/src/json"
|
|
6
|
+
|
|
7
|
+
import { RealtimeContext } from "./realtime-context"
|
|
8
|
+
import { StoreContext } from "../react"
|
|
9
|
+
|
|
10
|
+
export function usePullFamilyMember<J extends Json>(
|
|
11
|
+
family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>,
|
|
12
|
+
subKey: AtomIO.Serializable
|
|
13
|
+
): void {
|
|
14
|
+
const token = family(subKey)
|
|
15
|
+
const { socket } = React.useContext(RealtimeContext)
|
|
16
|
+
const store = React.useContext(StoreContext)
|
|
17
|
+
React.useEffect(() => {
|
|
18
|
+
socket?.on(`serve:${token.key}`, (data: J) => {
|
|
19
|
+
AtomIO.setState(family(subKey), data, store)
|
|
20
|
+
})
|
|
21
|
+
socket?.emit(`sub:${family.key}`, subKey)
|
|
22
|
+
return () => {
|
|
23
|
+
socket?.off(`serve:${token.key}`)
|
|
24
|
+
socket?.emit(`unsub:${token.key}`)
|
|
25
|
+
}
|
|
26
|
+
}, [family.key])
|
|
27
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
|
|
3
|
+
import * as AtomIO from "atom.io"
|
|
4
|
+
|
|
5
|
+
import type { Json } from "~/packages/anvl/src/json"
|
|
6
|
+
|
|
7
|
+
import { RealtimeContext } from "./realtime-context"
|
|
8
|
+
import { StoreContext } from "../react"
|
|
9
|
+
|
|
10
|
+
export function usePullFamily<J extends Json>(
|
|
11
|
+
family: AtomIO.AtomFamily<J> | AtomIO.SelectorFamily<J>
|
|
12
|
+
): void {
|
|
13
|
+
const { socket } = React.useContext(RealtimeContext)
|
|
14
|
+
const store = React.useContext(StoreContext)
|
|
15
|
+
React.useEffect(() => {
|
|
16
|
+
socket.on(`serve:${family.key}`, (key: Json, data: J) => {
|
|
17
|
+
AtomIO.setState(family(key), data, store)
|
|
18
|
+
})
|
|
19
|
+
socket?.emit(`sub:${family.key}`)
|
|
20
|
+
return () => {
|
|
21
|
+
socket?.off(`serve:${family.key}`)
|
|
22
|
+
socket?.emit(`unsub:${family.key}`)
|
|
23
|
+
}
|
|
24
|
+
}, [family.key])
|
|
25
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
|
|
3
|
+
import * as AtomIO from "atom.io"
|
|
4
|
+
|
|
5
|
+
import type { Json } from "~/packages/anvl/src/json"
|
|
6
|
+
|
|
7
|
+
import { RealtimeContext } from "./realtime-context"
|
|
8
|
+
import { StoreContext } from "../react"
|
|
9
|
+
|
|
10
|
+
export function usePull<J extends Json>(token: AtomIO.StateToken<J>): void {
|
|
11
|
+
const { socket } = React.useContext(RealtimeContext)
|
|
12
|
+
const store = React.useContext(StoreContext)
|
|
13
|
+
React.useEffect(() => {
|
|
14
|
+
socket.on(`serve:${token.key}`, (data: J) => {
|
|
15
|
+
AtomIO.setState(token, data, store)
|
|
16
|
+
})
|
|
17
|
+
socket.emit(`sub:${token.key}`)
|
|
18
|
+
return () => {
|
|
19
|
+
socket.off(`serve:${token.key}`)
|
|
20
|
+
socket.emit(`unsub:${token.key}`)
|
|
21
|
+
}
|
|
22
|
+
}, [token.key])
|
|
23
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
|
|
3
|
+
import * as AtomIO from "atom.io"
|
|
4
|
+
|
|
5
|
+
import type { Json } from "~/packages/anvl/src/json"
|
|
6
|
+
|
|
7
|
+
import { RealtimeContext } from "./realtime-context"
|
|
8
|
+
import { StoreContext } from "../react"
|
|
9
|
+
|
|
10
|
+
export function usePush<J extends Json>(token: AtomIO.StateToken<J>): void {
|
|
11
|
+
const { socket } = React.useContext(RealtimeContext)
|
|
12
|
+
const store = React.useContext(StoreContext)
|
|
13
|
+
React.useEffect(() => {
|
|
14
|
+
socket.emit(`claim:${token.key}`)
|
|
15
|
+
AtomIO.subscribe(
|
|
16
|
+
token,
|
|
17
|
+
({ newValue }) => {
|
|
18
|
+
socket.emit(`pub:${token.key}`, newValue)
|
|
19
|
+
},
|
|
20
|
+
store
|
|
21
|
+
)
|
|
22
|
+
return () => {
|
|
23
|
+
socket.emit(`unclaim:${token.key}`)
|
|
24
|
+
}
|
|
25
|
+
}, [token.key])
|
|
26
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
|
|
3
|
+
import * as AtomIO from "atom.io"
|
|
4
|
+
import { StoreContext } from "atom.io/react"
|
|
5
|
+
|
|
6
|
+
import type { ƒn } from "~/packages/anvl/src/function"
|
|
7
|
+
|
|
8
|
+
import { RealtimeContext } from "./realtime-context"
|
|
9
|
+
|
|
10
|
+
const TX_SUBS = new Map<string, number>()
|
|
11
|
+
export function useServerAction<ƒ extends ƒn>(
|
|
12
|
+
token: AtomIO.TransactionToken<ƒ>
|
|
13
|
+
): (...parameters: Parameters<ƒ>) => ReturnType<ƒ> {
|
|
14
|
+
const store = React.useContext(StoreContext)
|
|
15
|
+
const { socket } = React.useContext(RealtimeContext)
|
|
16
|
+
React.useEffect(() => {
|
|
17
|
+
const count = TX_SUBS.get(token.key) ?? 0
|
|
18
|
+
TX_SUBS.set(token.key, count + 1)
|
|
19
|
+
const unsubscribe =
|
|
20
|
+
count === 0
|
|
21
|
+
? AtomIO.subscribeToTransaction(
|
|
22
|
+
token,
|
|
23
|
+
(update) => socket.emit(`tx:${token.key}`, update),
|
|
24
|
+
store
|
|
25
|
+
)
|
|
26
|
+
: () => null
|
|
27
|
+
return () => {
|
|
28
|
+
const newCount = TX_SUBS.get(token.key) ?? 0
|
|
29
|
+
TX_SUBS.set(token.key, newCount - 1)
|
|
30
|
+
unsubscribe()
|
|
31
|
+
}
|
|
32
|
+
}, [token.key])
|
|
33
|
+
return AtomIO.runTransaction(token, store)
|
|
34
|
+
}
|
package/src/selector.ts
CHANGED
|
@@ -11,12 +11,15 @@ export type SelectorOptions<T> = {
|
|
|
11
11
|
get: Read<() => T>
|
|
12
12
|
set: Write<(newValue: T) => void>
|
|
13
13
|
}
|
|
14
|
-
export type ReadonlySelectorOptions<T> =
|
|
14
|
+
export type ReadonlySelectorOptions<T> = {
|
|
15
|
+
key: string
|
|
16
|
+
get: Read<() => T>
|
|
17
|
+
}
|
|
15
18
|
|
|
19
|
+
export function selector<T>(options: SelectorOptions<T>): SelectorToken<T>
|
|
16
20
|
export function selector<T>(
|
|
17
21
|
options: ReadonlySelectorOptions<T>
|
|
18
22
|
): ReadonlySelectorToken<T>
|
|
19
|
-
export function selector<T>(options: SelectorOptions<T>): SelectorToken<T>
|
|
20
23
|
export function selector<T>(
|
|
21
24
|
options: ReadonlySelectorOptions<T> | SelectorOptions<T>
|
|
22
25
|
): ReadonlySelectorToken<T> | SelectorToken<T> {
|
|
@@ -28,10 +31,10 @@ export type SelectorFamilyOptions<T, K extends Serializable> = {
|
|
|
28
31
|
get: (key: K) => Read<() => T>
|
|
29
32
|
set: (key: K) => Write<(newValue: T) => void>
|
|
30
33
|
}
|
|
31
|
-
export type ReadonlySelectorFamilyOptions<T, K extends Serializable> =
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
export type ReadonlySelectorFamilyOptions<T, K extends Serializable> = {
|
|
35
|
+
key: string
|
|
36
|
+
get: (key: K) => Read<() => T>
|
|
37
|
+
}
|
|
35
38
|
|
|
36
39
|
export type SelectorFamily<T, K extends Serializable = Serializable> = ((
|
|
37
40
|
key: K
|
package/src/silo.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
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
|
+
import {
|
|
6
|
+
atomFamily__INTERNAL,
|
|
7
|
+
atom__INTERNAL,
|
|
8
|
+
createStore,
|
|
9
|
+
redo__INTERNAL,
|
|
10
|
+
selectorFamily__INTERNAL,
|
|
11
|
+
selector__INTERNAL,
|
|
12
|
+
timeline__INTERNAL,
|
|
13
|
+
transaction__INTERNAL,
|
|
14
|
+
undo__INTERNAL,
|
|
15
|
+
} from "./internal"
|
|
16
|
+
import type { selector, selectorFamily } from "./selector"
|
|
17
|
+
import type { transaction } from "./transaction"
|
|
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),
|
|
52
|
+
}
|
|
53
|
+
}
|
package/src/subscribe.ts
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type { ƒn } from "~/packages/anvl/src/function"
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
ReadonlySelectorToken,
|
|
5
|
+
StateToken,
|
|
6
|
+
TimelineToken,
|
|
7
|
+
TimelineUpdate,
|
|
8
|
+
TransactionToken,
|
|
9
|
+
TransactionUpdate,
|
|
10
|
+
} from "."
|
|
11
|
+
import type { Store } from "./internal"
|
|
3
12
|
import { IMPLICIT, subscribeToRootAtoms, withdraw } from "./internal"
|
|
4
13
|
|
|
5
14
|
export type StateUpdate<T> = { newValue: T; oldValue: T }
|
|
15
|
+
export type KeyedStateUpdate<T> = StateUpdate<T> & { key: string }
|
|
6
16
|
export type UpdateHandler<T> = (update: StateUpdate<T>) => void
|
|
7
17
|
|
|
8
18
|
export const subscribe = <T>(
|
|
@@ -11,6 +21,11 @@ export const subscribe = <T>(
|
|
|
11
21
|
store: Store = IMPLICIT.STORE
|
|
12
22
|
): (() => void) => {
|
|
13
23
|
const state = withdraw<T>(token, store)
|
|
24
|
+
if (state === null) {
|
|
25
|
+
throw new Error(
|
|
26
|
+
`State "${token.key}" not found in this store. Did you forget to initialize with the "atom" or "selector" function?`
|
|
27
|
+
)
|
|
28
|
+
}
|
|
14
29
|
const subscription = state.subject.subscribe(handleUpdate)
|
|
15
30
|
store.config.logger?.info(`👀 subscribe to "${state.key}"`)
|
|
16
31
|
const dependencySubscriptions =
|
|
@@ -45,6 +60,11 @@ export const subscribeToTransaction = <ƒ extends ƒn>(
|
|
|
45
60
|
store = IMPLICIT.STORE
|
|
46
61
|
): (() => void) => {
|
|
47
62
|
const tx = withdraw(token, store)
|
|
63
|
+
if (tx === null) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`Cannot subscribe to transaction "${token.key}": transaction not found in store "${store.config.name}".`
|
|
66
|
+
)
|
|
67
|
+
}
|
|
48
68
|
store.config.logger?.info(`👀 subscribe to transaction "${token.key}"`)
|
|
49
69
|
const subscription = tx.subject.subscribe(handleUpdate)
|
|
50
70
|
const unsubscribe = () => {
|
|
@@ -53,3 +73,23 @@ export const subscribeToTransaction = <ƒ extends ƒn>(
|
|
|
53
73
|
}
|
|
54
74
|
return unsubscribe
|
|
55
75
|
}
|
|
76
|
+
|
|
77
|
+
export const subscribeToTimeline = (
|
|
78
|
+
token: TimelineToken,
|
|
79
|
+
handleUpdate: (update: TimelineUpdate) => void,
|
|
80
|
+
store = IMPLICIT.STORE
|
|
81
|
+
): (() => void) => {
|
|
82
|
+
const tl = withdraw(token, store)
|
|
83
|
+
if (tl === null) {
|
|
84
|
+
throw new Error(
|
|
85
|
+
`Cannot subscribe to timeline "${token.key}": timeline not found in store "${store.config.name}".`
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
store.config.logger?.info(`👀 subscribe to timeline "${token.key}"`)
|
|
89
|
+
const subscription = tl.subject.subscribe(handleUpdate)
|
|
90
|
+
const unsubscribe = () => {
|
|
91
|
+
store.config.logger?.info(`🙈 unsubscribe from timeline "${token.key}"`)
|
|
92
|
+
subscription.unsubscribe()
|
|
93
|
+
}
|
|
94
|
+
return unsubscribe
|
|
95
|
+
}
|
package/src/timeline.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import type { AtomFamily, AtomToken } from "."
|
|
2
|
+
import type {
|
|
3
|
+
TimelineAtomUpdate,
|
|
4
|
+
TimelineSelectorUpdate,
|
|
5
|
+
TimelineTransactionUpdate,
|
|
6
|
+
} from "./internal"
|
|
2
7
|
import { IMPLICIT } from "./internal"
|
|
3
8
|
import { redo__INTERNAL, timeline__INTERNAL, undo__INTERNAL } from "./internal/"
|
|
4
9
|
|
|
@@ -12,6 +17,11 @@ export type TimelineOptions = {
|
|
|
12
17
|
atoms: (AtomFamily<any> | AtomToken<any>)[]
|
|
13
18
|
}
|
|
14
19
|
|
|
20
|
+
export type TimelineUpdate =
|
|
21
|
+
| TimelineAtomUpdate
|
|
22
|
+
| TimelineSelectorUpdate
|
|
23
|
+
| TimelineTransactionUpdate
|
|
24
|
+
|
|
15
25
|
export const timeline = (options: TimelineOptions): TimelineToken => {
|
|
16
26
|
return timeline__INTERNAL(options)
|
|
17
27
|
}
|
package/src/transaction.ts
CHANGED
|
@@ -1,10 +1,21 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import type { ƒn } from "~/packages/anvl/src/function"
|
|
2
2
|
|
|
3
|
-
import type { ReadonlySelectorToken, StateToken
|
|
4
|
-
import type { Store
|
|
3
|
+
import type { KeyedStateUpdate, ReadonlySelectorToken, StateToken } from "."
|
|
4
|
+
import type { Store } from "./internal"
|
|
5
5
|
import { IMPLICIT, transaction__INTERNAL, withdraw } from "./internal"
|
|
6
6
|
|
|
7
|
-
export type
|
|
7
|
+
export type TransactionToken<_> = {
|
|
8
|
+
key: string
|
|
9
|
+
type: `transaction`
|
|
10
|
+
__brand?: _
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type TransactionUpdate<ƒ extends ƒn> = {
|
|
14
|
+
key: string
|
|
15
|
+
atomUpdates: KeyedStateUpdate<unknown>[]
|
|
16
|
+
params: Parameters<ƒ>
|
|
17
|
+
output: ReturnType<ƒ>
|
|
18
|
+
}
|
|
8
19
|
|
|
9
20
|
export type Transactors = {
|
|
10
21
|
get: <S>(state: ReadonlySelectorToken<S> | StateToken<S>) => S
|
|
@@ -27,12 +38,6 @@ export type TransactionOptions<ƒ extends ƒn> = {
|
|
|
27
38
|
do: Write<ƒ>
|
|
28
39
|
}
|
|
29
40
|
|
|
30
|
-
export type Transaction<ƒ extends ƒn> = {
|
|
31
|
-
key: string
|
|
32
|
-
type: `transaction`
|
|
33
|
-
run: (...parameters: Parameters<ƒ>) => ReturnType<ƒ>
|
|
34
|
-
subject: Rx.Subject<TransactionUpdate<ƒ>>
|
|
35
|
-
}
|
|
36
41
|
export type TransactionIO<Token extends TransactionToken<any>> =
|
|
37
42
|
Token extends TransactionToken<infer ƒ> ? ƒ : never
|
|
38
43
|
|
|
@@ -44,5 +49,12 @@ export function transaction<ƒ extends ƒn>(
|
|
|
44
49
|
|
|
45
50
|
export const runTransaction =
|
|
46
51
|
<ƒ extends ƒn>(token: TransactionToken<ƒ>, store: Store = IMPLICIT.STORE) =>
|
|
47
|
-
(...parameters: Parameters<ƒ>): ReturnType<ƒ> =>
|
|
48
|
-
withdraw(token, store)
|
|
52
|
+
(...parameters: Parameters<ƒ>): ReturnType<ƒ> => {
|
|
53
|
+
const tx = withdraw(token, store)
|
|
54
|
+
if (tx) {
|
|
55
|
+
return tx.run(...parameters)
|
|
56
|
+
}
|
|
57
|
+
throw new Error(
|
|
58
|
+
`Cannot run transaction "${token.key}": transaction not found in store "${store.config.name}".`
|
|
59
|
+
)
|
|
60
|
+
}
|