The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * Vue composables for reading and writing Effect atoms with an injected registry. |
| 3 | * |
| 4 | * @stability unstable |
| 5 | * @since 4.0.0 |
| 6 | */ |
| 7 | import * as Cause from "effect/Cause" |
| 8 | import * as Effect from "effect/Effect" |
| 9 | import * as Exit from "effect/Exit" |
| 10 | import type * as AsyncResult from "effect/reactivity/AsyncResult" |
| 11 | import type * as Atom from "effect/reactivity/Atom" |
| 12 | import type * as AtomRef from "effect/reactivity/AtomRef" |
| 13 | import * as AtomRegistry from "effect/reactivity/AtomRegistry" |
| 14 | import { computed, type ComputedRef, inject, type InjectionKey, type Ref, shallowRef, watchEffect } from "vue" |
| 15 | |
| 16 | /** |
| 17 | * Re-exports the atom registry API. |
| 18 | * |
| 19 | * @stability unstable |
| 20 | * @category re-exports |
| 21 | * @since 4.0.0 |
| 22 | */ |
| 23 | export * as AtomRegistry from "effect/reactivity/AtomRegistry" |
| 24 | |
| 25 | /** |
| 26 | * Re-exports the asynchronous atom result API. |
| 27 | * |
| 28 | * @stability unstable |
| 29 | * @category re-exports |
| 30 | * @since 4.0.0 |
| 31 | */ |
| 32 | export * as AsyncResult from "effect/reactivity/AsyncResult" |
| 33 | |
| 34 | /** |
| 35 | * Re-exports the atom API. |
| 36 | * |
| 37 | * @stability unstable |
| 38 | * @category re-exports |
| 39 | * @since 4.0.0 |
| 40 | */ |
| 41 | export * as Atom from "effect/reactivity/Atom" |
| 42 | |
| 43 | /** |
| 44 | * Re-exports the atom reference API. |
| 45 | * |
| 46 | * @stability unstable |
| 47 | * @category re-exports |
| 48 | * @since 4.0.0 |
| 49 | */ |
| 50 | export * as AtomRef from "effect/reactivity/AtomRef" |
| 51 | |
| 52 | /** |
| 53 | * Re-exports the HTTP API atom helpers. |
| 54 | * |
| 55 | * @stability unstable |
| 56 | * @category re-exports |
| 57 | * @since 4.0.0 |
| 58 | */ |
| 59 | export * as AtomHttpApi from "effect/reactivity/AtomHttpApi" |
| 60 | |
| 61 | /** |
| 62 | * Re-exports the RPC atom helpers. |
| 63 | * |
| 64 | * @stability unstable |
| 65 | * @category re-exports |
| 66 | * @since 4.0.0 |
| 67 | */ |
| 68 | export * as AtomRpc from "effect/reactivity/AtomRpc" |
| 69 | |
| 70 | /** |
| 71 | * Vue injection key for an atom registry. |
| 72 | * |
| 73 | * @stability unstable |
| 74 | * @category symbols |
| 75 | * @since 4.0.0 |
| 76 | */ |
| 77 | export const registryKey = Symbol.for("@effect/atom-vue/registryKey") as InjectionKey<AtomRegistry.AtomRegistry> |
| 78 | |
| 79 | /** |
| 80 | * Fallback atom registry used when no registry is provided. |
| 81 | * |
| 82 | * @stability unstable |
| 83 | * @category constants |
| 84 | * @since 4.0.0 |
| 85 | */ |
| 86 | export const defaultRegistry: AtomRegistry.AtomRegistry = AtomRegistry.make() |
| 87 | |
| 88 | /** |
| 89 | * Returns the injected atom registry, falling back to the default registry. |
| 90 | * |
| 91 | * @stability unstable |
| 92 | * @category accessors |
| 93 | * @since 4.0.0 |
| 94 | */ |
| 95 | export const injectRegistry = (): AtomRegistry.AtomRegistry => { |
| 96 | return inject(registryKey, defaultRegistry) |
| 97 | } |
| 98 | |
| 99 | const useAtomValueRef = <A extends Atom.Atom<any>>(atom: () => A) => { |
| 100 | const registry = injectRegistry() |
| 101 | const atomRef = computed(atom) |
| 102 | const value = shallowRef(undefined as any as A) |
| 103 | watchEffect((onCleanup) => { |
| 104 | onCleanup(registry.subscribe(atomRef.value, (nextValue: Atom.Type<A>) => { |
| 105 | value.value = nextValue |
| 106 | }, { immediate: true })) |
| 107 | }) |
| 108 | return [value as Readonly<Ref<Atom.Type<A>>>, atomRef, registry] as const |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns a reactive atom value and a setter with the requested write mode. |
| 113 | * |
| 114 | * **Gotchas** |
| 115 | * |
| 116 | * In `promise` and `promiseExit` modes, the setter observes the atom's next |
| 117 | * settled result, not a result correlated with that write. Overlapping writes |
| 118 | * can resolve with another write's result. Serialize writes when each caller |
| 119 | * needs its own result. |
| 120 | * |
| 121 | * @stability unstable |
| 122 | * @category composables |
| 123 | * @since 4.0.0 |
| 124 | */ |
| 125 | export const useAtom = <R, W, Mode extends "value" | "promise" | "promiseExit" = never>( |
| 126 | atom: () => Atom.Writable<R, W>, |
| 127 | options?: { |
| 128 | readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined |
| 129 | } |
| 130 | ): readonly [ |
| 131 | Readonly<Ref<R>>, |
| 132 | write: "promise" extends Mode ? ( |
| 133 | (value: W) => Promise<AsyncResult.AsyncResult.Success<R>> |
| 134 | ) : |
| 135 | "promiseExit" extends Mode ? ( |
| 136 | (value: W) => Promise<Exit.Exit<AsyncResult.AsyncResult.Success<R>, AsyncResult.AsyncResult.Failure<R>>> |
| 137 | ) : |
| 138 | ((value: W | ((value: R) => W)) => void) |
| 139 | ] => { |
| 140 | const [value, atomRef, registry] = useAtomValueRef(atom) |
| 141 | return [value as Readonly<Ref<R>>, setAtom(registry, atomRef, options)] |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns a read-only Vue ref that tracks an atom value. |
| 146 | * |
| 147 | * @stability unstable |
| 148 | * @category composables |
| 149 | * @since 4.0.0 |
| 150 | */ |
| 151 | export const useAtomValue = <A>(atom: () => Atom.Atom<A>): Readonly<Ref<A>> => useAtomValueRef(atom)[0] |
| 152 | |
| 153 | const flattenExit = <A, E>(exit: Exit.Exit<A, E>): A => { |
| 154 | if (Exit.isSuccess(exit)) return exit.value |
| 155 | throw Cause.squash(exit.cause) |
| 156 | } |
| 157 | |
| 158 | function setAtom<R, W, Mode extends "value" | "promise" | "promiseExit" = never>( |
| 159 | registry: AtomRegistry.AtomRegistry, |
| 160 | atomRef: ComputedRef<Atom.Writable<R, W>>, |
| 161 | options?: { |
| 162 | readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined |
| 163 | } |
| 164 | ): "promise" extends Mode ? ( |
| 165 | ( |
| 166 | value: W, |
| 167 | options?: { |
| 168 | readonly signal?: AbortSignal | undefined |
| 169 | } | undefined |
| 170 | ) => Promise<AsyncResult.AsyncResult.Success<R>> |
| 171 | ) : |
| 172 | "promiseExit" extends Mode ? ( |
| 173 | ( |
| 174 | value: W, |
| 175 | options?: { |
| 176 | readonly signal?: AbortSignal | undefined |
| 177 | } | undefined |
| 178 | ) => Promise<Exit.Exit<AsyncResult.AsyncResult.Success<R>, AsyncResult.AsyncResult.Failure<R>>> |
| 179 | ) : |
| 180 | ((value: W | ((value: R) => W)) => void) |
| 181 | { |
| 182 | if (options?.mode === "promise" || options?.mode === "promiseExit") { |
| 183 | return ((value: W, opts?: any) => { |
| 184 | registry.set(atomRef.value, value) |
| 185 | const promise = Effect.runPromiseExit( |
| 186 | AtomRegistry.getResult( |
| 187 | registry, |
| 188 | atomRef.value as Atom.Atom<AsyncResult.AsyncResult<any, any>>, |
| 189 | { suspendOnWaiting: true } |
| 190 | ), |
| 191 | opts |
| 192 | ) |
| 193 | return options!.mode === "promise" ? promise.then(flattenExit) : promise |
| 194 | }) as any |
| 195 | } |
| 196 | return ((value: W | ((value: R) => W)) => { |
| 197 | registry.set(atomRef.value, typeof value === "function" ? (value as any)(registry.get(atomRef.value)) : value) |
| 198 | }) as any |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Returns a setter for an atom, mounting it for the lifetime of the composable. |
| 203 | * |
| 204 | * **Gotchas** |
| 205 | * |
| 206 | * In `promise` and `promiseExit` modes, the setter observes the atom's next |
| 207 | * settled result, not a result correlated with that write. Overlapping writes |
| 208 | * can resolve with another write's result. Serialize writes when each caller |
| 209 | * needs its own result. |
| 210 | * |
| 211 | * @stability unstable |
| 212 | * @category composables |
| 213 | * @since 4.0.0 |
| 214 | */ |
| 215 | export const useAtomSet = < |
| 216 | R, |
| 217 | W, |
| 218 | Mode extends "value" | "promise" | "promiseExit" = never |
| 219 | >( |
| 220 | atom: () => Atom.Writable<R, W>, |
| 221 | options?: { |
| 222 | readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined |
| 223 | } |
| 224 | ): "promise" extends Mode ? ( |
| 225 | ( |
| 226 | value: W, |
| 227 | options?: { |
| 228 | readonly signal?: AbortSignal | undefined |
| 229 | } | undefined |
| 230 | ) => Promise<AsyncResult.AsyncResult.Success<R>> |
| 231 | ) : |
| 232 | "promiseExit" extends Mode ? ( |
| 233 | ( |
| 234 | value: W, |
| 235 | options?: { |
| 236 | readonly signal?: AbortSignal | undefined |
| 237 | } | undefined |
| 238 | ) => Promise<Exit.Exit<AsyncResult.AsyncResult.Success<R>, AsyncResult.AsyncResult.Failure<R>>> |
| 239 | ) : |
| 240 | ((value: W | ((value: R) => W)) => void) => |
| 241 | { |
| 242 | const registry = injectRegistry() |
| 243 | const atomRef = computed(atom) |
| 244 | watchEffect((onCleanup) => { |
| 245 | onCleanup(registry.mount(atomRef.value)) |
| 246 | }) |
| 247 | return setAtom(registry, atomRef, options) |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Returns a read-only Vue ref that tracks an atom reference. |
| 252 | * |
| 253 | * @stability unstable |
| 254 | * @category composables |
| 255 | * @since 4.0.0 |
| 256 | */ |
| 257 | export const useAtomRef = <A>(atomRef: () => AtomRef.ReadonlyRef<A>): Readonly<Ref<A>> => { |
| 258 | const atomRefRef = computed(atomRef) |
| 259 | const value = shallowRef<A>(atomRefRef.value.value) |
| 260 | watchEffect((onCleanup) => { |
| 261 | const ref = atomRefRef.value |
| 262 | onCleanup(ref.subscribe((next: A) => { |
| 263 | value.value = next |
| 264 | })) |
| 265 | value.value = ref.value |
| 266 | }) |
| 267 | return value as Readonly<Ref<A>> |
| 268 | } |