@collective-intelligence/external-apps-client-sdk-react 0.2.8 → 0.2.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.
- package/dist/effect.d.ts +71 -0
- package/dist/effect.js +1 -1
- package/docs/effect.md +27 -0
- package/package.json +2 -2
package/dist/effect.d.ts
CHANGED
|
@@ -20,6 +20,77 @@ export type EffectRunner<R> = Pick<ManagedRuntime.ManagedRuntime<R, never>, "run
|
|
|
20
20
|
export declare const fromEffect: <A, E, R, I extends {
|
|
21
21
|
readonly runtime: EffectRunner<R>;
|
|
22
22
|
}>(run: (input: I) => Effect.Effect<A, E, R>) => PromiseActorLogic<A, I>;
|
|
23
|
+
/** The input of an `effectTask` machine: the runtime its effect runs on. */
|
|
24
|
+
export type EffectTaskInput<R> = Readonly<{
|
|
25
|
+
runtime: EffectRunner<R>;
|
|
26
|
+
}>;
|
|
27
|
+
/** The command of an `effectTask` machine: run the effect with `input`. */
|
|
28
|
+
export type EffectTaskEvent<Input> = Readonly<{
|
|
29
|
+
input: Input;
|
|
30
|
+
type: "RUN";
|
|
31
|
+
}>;
|
|
32
|
+
/** What an `effectTask` machine emits when its run ends. */
|
|
33
|
+
export type EffectTaskEmitted<A> = Readonly<{
|
|
34
|
+
output: A;
|
|
35
|
+
type: "done";
|
|
36
|
+
}> | Readonly<{
|
|
37
|
+
error: unknown;
|
|
38
|
+
type: "failed";
|
|
39
|
+
}>;
|
|
40
|
+
/** The context of an `effectTask` machine: its runtime and its last result. */
|
|
41
|
+
export type EffectTaskContext<A, R> = Readonly<{
|
|
42
|
+
/** The typed error or the defect of the last run, while the task is `failed`. */
|
|
43
|
+
error: unknown;
|
|
44
|
+
/** The value of the last run, while the task is `done`. */
|
|
45
|
+
output: A | undefined;
|
|
46
|
+
runtime: EffectRunner<R>;
|
|
47
|
+
}>;
|
|
48
|
+
/**
|
|
49
|
+
* The machine of one single action of a view, such as a download, a share or
|
|
50
|
+
* a sign-out: `RUN` runs `run(input)` in a `fromEffect` actor on the runtime
|
|
51
|
+
* of the machine input. The machine is `idle`, then `running` until the run
|
|
52
|
+
* settles, then `done` (with `output`) or `failed` (with `error`) until the
|
|
53
|
+
* next `RUN`; it emits `done` or `failed` with the same result. A `RUN` while
|
|
54
|
+
* it runs is ignored. A stop of the machine, as when its view unmounts,
|
|
55
|
+
* interrupts the run.
|
|
56
|
+
*
|
|
57
|
+
* A flow with steps of its own, such as a form that saves and then confirms,
|
|
58
|
+
* is its own machine with `fromEffect` actors, not a task.
|
|
59
|
+
*/
|
|
60
|
+
export declare const effectTask: <Input, A, E, R = never>(run: (input: Input) => Effect.Effect<A, E, R>) => import("xstate").StateMachine<Readonly<{
|
|
61
|
+
/** The typed error or the defect of the last run, while the task is `failed`. */
|
|
62
|
+
error: unknown;
|
|
63
|
+
/** The value of the last run, while the task is `done`. */
|
|
64
|
+
output: A | undefined;
|
|
65
|
+
runtime: EffectRunner<R>;
|
|
66
|
+
}>, Readonly<{
|
|
67
|
+
input: Input;
|
|
68
|
+
type: "RUN";
|
|
69
|
+
}>, {
|
|
70
|
+
[x: string]: import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<A, Readonly<{
|
|
71
|
+
runtime: EffectRunner<R>;
|
|
72
|
+
task: Input;
|
|
73
|
+
}>>> | undefined;
|
|
74
|
+
}, {
|
|
75
|
+
src: "run";
|
|
76
|
+
logic: import("xstate").PromiseActorLogic<A, Readonly<{
|
|
77
|
+
runtime: EffectRunner<R>;
|
|
78
|
+
task: Input;
|
|
79
|
+
}>>;
|
|
80
|
+
id: string | undefined;
|
|
81
|
+
}, never, never, never, "done" | "failed" | "idle" | "running", string, Readonly<{
|
|
82
|
+
runtime: EffectRunner<R>;
|
|
83
|
+
}>, undefined, EffectTaskEmitted<A>, import("xstate").MetaObject, {
|
|
84
|
+
id: "effectTask";
|
|
85
|
+
states: {
|
|
86
|
+
readonly done: {};
|
|
87
|
+
readonly failed: {};
|
|
88
|
+
readonly idle: {};
|
|
89
|
+
readonly running: {};
|
|
90
|
+
};
|
|
91
|
+
}, import("xstate").MetaObject>;
|
|
92
|
+
/** The machine `effectTask(run)` makes. */
|
|
93
|
+
export type EffectTask<Input, A, E = never, R = never> = ReturnType<typeof effectTask<Input, A, E, R>>;
|
|
23
94
|
export type QueryCacheFetchOptions<Data> = Readonly<{
|
|
24
95
|
queryKey: QueryKey;
|
|
25
96
|
/** Runs when the cache holds no fresh value for `queryKey`. */
|
package/dist/effect.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import{__export}from"./index-6ra894v2.js";var exports_effect={};__export(exports_effect,{EffectRuntimeProvider:()=>EffectRuntimeProvider,QueryCache:()=>QueryCache,fromEffect:()=>fromEffect,useEffectRuntime:()=>useEffectRuntime});import{fromPromise}from"xstate";var unsettled=()=>Promise.race([]);var fromEffect=(run)=>fromPromise(async({input,signal})=>{try{const value=await input.runtime.runPromise(run(input),{signal});return signal.aborted?unsettled():value}catch(error){if(signal.aborted){return unsettled()}throw error}});import{CiError,CiTransportError}from"@collective-intelligence/external-apps-client-sdk/effect";import{isCancelledError,QueryObserver}from"@tanstack/react-query";import*as Context from"effect/Context";import*as Effect from"effect/Effect";import*as Exit from"effect/Exit";import{constVoid}from"effect/Function";import*as Layer from"effect/Layer";class QueryCache extends Context.Service()("@collective-intelligence/external-apps-client-sdk-react/QueryCache"){}var make=(queryClient)=>{const fetchExit=async({query,...options},context)=>{try{return Exit.succeed(await queryClient.fetchQuery({...options,queryFn:({signal})=>Effect.runPromiseWith(context)(query,{signal}),retry:false}))}catch(error){if(isCancelledError(error)){return Exit.interrupt()}return error instanceof CiError||error instanceof CiTransportError?Exit.fail(error):Exit.die(error)}};const waitOn=(queryKey)=>Effect.sync(()=>new QueryObserver(queryClient,{enabled:false,queryKey}).subscribe(constVoid));return QueryCache.of({fetch:(options)=>Effect.flatMap(Effect.context(),(context)=>Effect.acquireUseRelease(waitOn(options.queryKey),()=>Effect.flatten(Effect.promise(()=>fetchExit(options,context))),(stopWaiting)=>Effect.sync(stopWaiting))),invalidate:(queryKey)=>Effect.promise(()=>queryClient.invalidateQueries({queryKey})),remove:(queryKey)=>Effect.sync(()=>{queryClient.removeQueries({queryKey})}),setData:(queryKey,data)=>Effect.sync(()=>{queryClient.setQueryData(queryKey,data)}),update:(queryKey,updater)=>Effect.sync(()=>{queryClient.setQueriesData({queryKey},updater)})})};var layer=(queryClient)=>Layer.succeed(QueryCache,make(queryClient));import{useQueryClient}from"@tanstack/react-query";import*as ManagedRuntime from"effect/ManagedRuntime";import{createContext,useContext,useMemo}from"react";import{jsx}from"react/jsx-runtime";var RuntimeContext=createContext(null);var EffectRuntimeProvider=({children})=>{const queryClient=useQueryClient();const runtime=useMemo(()=>ManagedRuntime.make(layer(queryClient)),[queryClient]);return jsx(RuntimeContext.Provider,{value:runtime,children})};var useEffectRuntime=()=>{const runtime=useContext(RuntimeContext);if(runtime===null){throw new Error("useEffectRuntime requires an EffectRuntimeProvider")}return runtime};var{EffectRuntimeProvider:EffectRuntimeProvider2,QueryCache:QueryCache2,fromEffect:fromEffect2,useEffectRuntime:useEffectRuntime2}=exports_effect;export{EffectRuntimeProvider2 as EffectRuntimeProvider,QueryCache2 as QueryCache,fromEffect2 as fromEffect,useEffectRuntime2 as useEffectRuntime};
|
|
2
|
+
import{__export}from"./index-6ra894v2.js";var exports_effect={};__export(exports_effect,{EffectRuntimeProvider:()=>EffectRuntimeProvider,QueryCache:()=>QueryCache,effectTask:()=>effectTask,fromEffect:()=>fromEffect,useEffectRuntime:()=>useEffectRuntime});import{assign,emit,setup}from"xstate";import{fromPromise}from"xstate";var unsettled=()=>Promise.race([]);var fromEffect=(run)=>fromPromise(async({input,signal})=>{try{const value=await input.runtime.runPromise(run(input),{signal});return signal.aborted?unsettled():value}catch(error){if(signal.aborted){return unsettled()}throw error}});var effectTask=(run)=>{const actors={run:fromEffect(({task})=>run(task))};return setup({actors}).createMachine({context:({input})=>({error:undefined,output:undefined,runtime:input.runtime}),id:"effectTask",initial:"idle",on:{RUN:{target:".running"}},states:{done:{},failed:{},idle:{},running:{entry:assign({error:undefined,output:undefined}),invoke:{input:({context,event})=>({runtime:context.runtime,task:event.input}),onDone:{actions:[assign({output:({event})=>event.output}),emit(({event})=>({output:event.output,type:"done"}))],target:"done"},onError:{actions:[assign({error:({event})=>event.error}),emit(({event})=>({error:event.error,type:"failed"}))],target:"failed"},src:"run"},on:{RUN:{}}}}})};import{CiError,CiTransportError}from"@collective-intelligence/external-apps-client-sdk/effect";import{isCancelledError,QueryObserver}from"@tanstack/react-query";import*as Context from"effect/Context";import*as Effect from"effect/Effect";import*as Exit from"effect/Exit";import{constVoid}from"effect/Function";import*as Layer from"effect/Layer";class QueryCache extends Context.Service()("@collective-intelligence/external-apps-client-sdk-react/QueryCache"){}var make=(queryClient)=>{const fetchExit=async({query,...options},context)=>{try{return Exit.succeed(await queryClient.fetchQuery({...options,queryFn:({signal})=>Effect.runPromiseWith(context)(query,{signal}),retry:false}))}catch(error){if(isCancelledError(error)){return Exit.interrupt()}return error instanceof CiError||error instanceof CiTransportError?Exit.fail(error):Exit.die(error)}};const waitOn=(queryKey)=>Effect.sync(()=>new QueryObserver(queryClient,{enabled:false,queryKey}).subscribe(constVoid));return QueryCache.of({fetch:(options)=>Effect.flatMap(Effect.context(),(context)=>Effect.acquireUseRelease(waitOn(options.queryKey),()=>Effect.flatten(Effect.promise(()=>fetchExit(options,context))),(stopWaiting)=>Effect.sync(stopWaiting))),invalidate:(queryKey)=>Effect.promise(()=>queryClient.invalidateQueries({queryKey})),remove:(queryKey)=>Effect.sync(()=>{queryClient.removeQueries({queryKey})}),setData:(queryKey,data)=>Effect.sync(()=>{queryClient.setQueryData(queryKey,data)}),update:(queryKey,updater)=>Effect.sync(()=>{queryClient.setQueriesData({queryKey},updater)})})};var layer=(queryClient)=>Layer.succeed(QueryCache,make(queryClient));import{useQueryClient}from"@tanstack/react-query";import*as ManagedRuntime from"effect/ManagedRuntime";import{createContext,useContext,useMemo}from"react";import{jsx}from"react/jsx-runtime";var RuntimeContext=createContext(null);var EffectRuntimeProvider=({children})=>{const queryClient=useQueryClient();const runtime=useMemo(()=>ManagedRuntime.make(layer(queryClient)),[queryClient]);return jsx(RuntimeContext.Provider,{value:runtime,children})};var useEffectRuntime=()=>{const runtime=useContext(RuntimeContext);if(runtime===null){throw new Error("useEffectRuntime requires an EffectRuntimeProvider")}return runtime};var{EffectRuntimeProvider:EffectRuntimeProvider2,QueryCache:QueryCache2,effectTask:effectTask2,fromEffect:fromEffect2,useEffectRuntime:useEffectRuntime2}=exports_effect;export{EffectRuntimeProvider2 as EffectRuntimeProvider,QueryCache2 as QueryCache,effectTask2 as effectTask,fromEffect2 as fromEffect,useEffectRuntime2 as useEffectRuntime};
|
package/docs/effect.md
CHANGED
|
@@ -39,6 +39,33 @@ is an actor; each actor is a `fromEffect` actor.
|
|
|
39
39
|
`["crm", "leads"]` or `["crm"]`. `setData` writes `queryKey` only.
|
|
40
40
|
- Views read the same keys with `useQuery`.
|
|
41
41
|
|
|
42
|
+
- `effectTask(run)` is the machine of one single action of a view, such as
|
|
43
|
+
a download, a share or a sign-out. `RUN` with `input` runs `run(input)` on
|
|
44
|
+
the runtime of the machine input; the machine is `idle`, `running`, then
|
|
45
|
+
`done` (`context.output`) or `failed` (`context.error`) until the next
|
|
46
|
+
`RUN`, and emits `done` or `failed` with the same result. A `RUN` while it
|
|
47
|
+
runs is ignored; a stop, as when the view unmounts, interrupts the run. A
|
|
48
|
+
flow with steps of its own is its own machine with `fromEffect` actors.
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
const shareTask = effectTask((page: string) => sharePage(page))
|
|
52
|
+
|
|
53
|
+
const ShareButton = ({ page }: { page: string }) => {
|
|
54
|
+
const share = useActorRef(shareTask, {
|
|
55
|
+
input: { runtime: useEffectRuntime() }
|
|
56
|
+
})
|
|
57
|
+
const running = useSelector(share, (snapshot) => snapshot.matches("running"))
|
|
58
|
+
return (
|
|
59
|
+
<button
|
|
60
|
+
disabled={running}
|
|
61
|
+
onClick={() => share.send({ input: page, type: "RUN" })}
|
|
62
|
+
>
|
|
63
|
+
Share
|
|
64
|
+
</button>
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
42
69
|
A write refreshes the reads through `QueryCache`: `invalidate` to read again
|
|
43
70
|
from CI, `setData` or `update` to show a known value, `remove` to drop values
|
|
44
71
|
that the next user must not get. Machine input holds no QueryClient.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@collective-intelligence/external-apps-client-sdk-react",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9",
|
|
4
4
|
"description": "React bindings for Collective Intelligence external applications.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -88,5 +88,5 @@
|
|
|
88
88
|
"vitest": "4.1.10",
|
|
89
89
|
"xstate": "5.33.3"
|
|
90
90
|
},
|
|
91
|
-
"gitHead": "
|
|
91
|
+
"gitHead": "5e694db7a00e52f9be053815a39cafd7418e56cf"
|
|
92
92
|
}
|