@rxova/journey-react 0.7.0 → 1.0.0-rc.2
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 +101 -170
- package/dist/client.cjs +2 -0
- package/dist/client.cjs.map +7 -0
- package/dist/client.d.cts +1 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +2 -0
- package/dist/client.js.map +7 -0
- package/dist/createJourney.d.cts +15 -0
- package/dist/createJourney.d.ts +15 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.cts +3 -4
- package/dist/index.d.ts +3 -4
- package/dist/index.js +1 -1
- package/dist/index.js.map +4 -4
- package/dist/provider.d.cts +9 -0
- package/dist/provider.d.ts +9 -0
- package/dist/runtime-hooks.d.cts +18 -0
- package/dist/runtime-hooks.d.ts +18 -0
- package/dist/types.d.cts +80 -0
- package/dist/types.d.ts +69 -45
- package/package.json +17 -8
- package/dist/bindings/Provider.d.ts +0 -8
- package/dist/bindings/StepRenderer.d.ts +0 -13
- package/dist/bindings/index.d.ts +0 -6
- package/dist/bindings/useJourneyApi.d.ts +0 -16
- package/dist/bindings/useJourneyEvent.d.ts +0 -5
- package/dist/bindings/useJourneyMachine.d.ts +0 -4
- package/dist/bindings/useJourneySelector.d.ts +0 -5
- package/dist/bindings/useJourneySnapshot.d.ts +0 -5
package/README.md
CHANGED
|
@@ -1,226 +1,157 @@
|
|
|
1
1
|
# @rxova/journey-react
|
|
2
2
|
|
|
3
|
-
Typed React bindings for
|
|
3
|
+
Typed React bindings for multi-step UI flows.
|
|
4
|
+
|
|
5
|
+
<p>
|
|
6
|
+
<a href="https://www.npmjs.com/package/@rxova/journey-react">
|
|
7
|
+
<img src="https://img.shields.io/npm/v/@rxova/journey-react?color=0f8f6a" alt="npm" />
|
|
8
|
+
</a>
|
|
9
|
+
<img src="https://img.shields.io/badge/1.33%20kB-brotli-0f8f6a" alt="size" />
|
|
10
|
+
<img src="https://img.shields.io/badge/React%2018+-black" alt="React 18+" />
|
|
11
|
+
<img src="https://img.shields.io/badge/TypeScript-strict-3178C6?logo=typescript&logoColor=white" alt="TypeScript" />
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
`@rxova/journey-react` is approaching a `1.0.0-rc` contract freeze. The key runtime rule is unchanged:
|
|
15
|
+
one `createJourney(...)` call creates one machine instance immediately, and the returned hooks/components stay bound to that instance.
|
|
4
16
|
|
|
5
17
|
## Install
|
|
6
18
|
|
|
7
19
|
```bash
|
|
8
|
-
|
|
9
|
-
yarn add @rxova/journey-react
|
|
10
|
-
npm i @rxova/journey-react
|
|
11
|
-
bun add @rxova/journey-react
|
|
20
|
+
npm i @rxova/journey-react @rxova/journey-core
|
|
12
21
|
```
|
|
13
22
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
## API Style
|
|
17
|
-
|
|
18
|
-
`@rxova/journey-react` is bindings-first:
|
|
19
|
-
|
|
20
|
-
- `createJourneyBindings(journey)` returns a typed bundle that contains:
|
|
21
|
-
- `Provider`
|
|
22
|
-
- `StepRenderer`
|
|
23
|
-
- `useJourneyApi`
|
|
24
|
-
- `useJourneyEvent`
|
|
25
|
-
- `useJourneySelector`
|
|
26
|
-
- `useJourneySnapshot`
|
|
27
|
-
- `useJourneyMachine`
|
|
28
|
-
|
|
29
|
-
No per-hook generic arguments are needed at callsites.
|
|
23
|
+
Use the root entry for server-safe imports. When a Next.js App Router client boundary should be explicit,
|
|
24
|
+
import from `@rxova/journey-react/client`.
|
|
30
25
|
|
|
31
26
|
## Quickstart
|
|
32
27
|
|
|
33
28
|
```tsx
|
|
34
|
-
import
|
|
35
|
-
import {
|
|
29
|
+
import { createJourney, type JourneyViews } from "@rxova/journey-react";
|
|
30
|
+
import type { JourneyDefinition } from "@rxova/journey-core";
|
|
36
31
|
|
|
37
32
|
type StepId = "start" | "review";
|
|
38
|
-
type
|
|
39
|
-
|
|
40
|
-
let bindings: ReturnType<typeof createJourneyBindings<Ctx, StepId>>;
|
|
41
|
-
|
|
42
|
-
const Start = () => {
|
|
43
|
-
const api = bindings.useJourneyApi();
|
|
44
|
-
return <button onClick={() => void api.goToNextStep()}>Next</button>;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const Review = () => {
|
|
48
|
-
const api = bindings.useJourneyApi();
|
|
49
|
-
return <button onClick={() => void api.completeJourney()}>Submit</button>;
|
|
50
|
-
};
|
|
33
|
+
type Context = { name: string };
|
|
51
34
|
|
|
52
|
-
const
|
|
35
|
+
const definition: JourneyDefinition<Context, StepId> = {
|
|
53
36
|
initial: "start",
|
|
54
37
|
context: { name: "" },
|
|
55
|
-
steps: {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
{ from: "start", event: "goToNextStep", to: "review" },
|
|
61
|
-
{ from: "review", event: "completeJourney" }
|
|
62
|
-
]
|
|
38
|
+
steps: { start: {}, review: {} },
|
|
39
|
+
transitions: {
|
|
40
|
+
start: { goToNextStep: [{ to: "review" }] },
|
|
41
|
+
review: { completeJourney: true }
|
|
42
|
+
}
|
|
63
43
|
};
|
|
64
44
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
export const App = () => {
|
|
68
|
-
const Provider = bindings.Provider;
|
|
69
|
-
const StepRenderer = bindings.StepRenderer;
|
|
45
|
+
const signup = createJourney(definition);
|
|
70
46
|
|
|
47
|
+
const Start = () => {
|
|
48
|
+
const api = signup.useJourneyApi();
|
|
49
|
+
const snap = signup.useJourneySnapshot();
|
|
71
50
|
return (
|
|
72
|
-
<
|
|
73
|
-
<
|
|
74
|
-
|
|
51
|
+
<div>
|
|
52
|
+
<p>Hello, {snap.context.name || "stranger"}</p>
|
|
53
|
+
<button onClick={() => void api.goToNextStep()}>Next</button>
|
|
54
|
+
</div>
|
|
75
55
|
);
|
|
76
56
|
};
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
## Split Files Pattern (Hooks In Steps)
|
|
80
|
-
|
|
81
|
-
If step components live in separate files and call Journey hooks, export bindings as `let`:
|
|
82
|
-
|
|
83
|
-
```tsx
|
|
84
|
-
// journey-bindings.ts
|
|
85
|
-
import { createJourneyBindings, type JourneyReactDefinition } from "@rxova/journey-react";
|
|
86
|
-
import { Start, Review } from "./steps";
|
|
87
|
-
|
|
88
|
-
type StepId = "start" | "review";
|
|
89
|
-
type Ctx = { name: string };
|
|
90
|
-
|
|
91
|
-
export let bindings: ReturnType<typeof createJourneyBindings<Ctx, StepId>>;
|
|
92
57
|
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
steps: {
|
|
97
|
-
start: { component: Start },
|
|
98
|
-
review: { component: Review }
|
|
99
|
-
},
|
|
100
|
-
transitions: [
|
|
101
|
-
{ from: "start", event: "goToNextStep", to: "review" },
|
|
102
|
-
{ from: "review", event: "completeJourney" }
|
|
103
|
-
]
|
|
58
|
+
const Review = () => {
|
|
59
|
+
const api = signup.useJourneyApi();
|
|
60
|
+
return <button onClick={() => void api.completeJourney()}>Submit</button>;
|
|
104
61
|
};
|
|
105
62
|
|
|
106
|
-
|
|
63
|
+
const views: JourneyViews<StepId> = { start: Start, review: Review };
|
|
64
|
+
|
|
65
|
+
export const App = () => (
|
|
66
|
+
<signup.JourneyProvider views={views}>
|
|
67
|
+
<signup.StepRenderer />
|
|
68
|
+
</signup.JourneyProvider>
|
|
69
|
+
);
|
|
107
70
|
```
|
|
108
71
|
|
|
109
72
|
## Hooks
|
|
110
73
|
|
|
111
|
-
|
|
112
|
-
- `useJourneyEvent(listener)` subscribes to typed lifecycle events.
|
|
113
|
-
- `useJourneySelector(selector, equalityFn?)` subscribes to a selected slice and rerenders only when that selected value changes.
|
|
114
|
-
- `useJourneyApi()` returns typed commands.
|
|
115
|
-
- `useJourneyMachine()` returns the underlying core machine instance.
|
|
116
|
-
|
|
117
|
-
## Journey API Helpers
|
|
74
|
+
`createJourney()` returns a runtime with bound hooks:
|
|
118
75
|
|
|
119
|
-
|
|
76
|
+
- **`useJourneySnapshot()`** — full snapshot: `currentStepId`, `context`, `history`, `status`, `async`
|
|
77
|
+
- **`useJourneyApi()`** — runtime commands: `startJourney`, `goToNextStep`, `goToPreviousStep`, `completeJourney`, `send`, etc.
|
|
78
|
+
- **`useStepApi(stepId)`** — step-scoped command surface with `send(...)` narrowed to custom events handled by that step or `global`
|
|
79
|
+
- **`useJourneyComputed()`** — derived state: `mode`, `activeStepId`, `isLoading`, `isFirstStep`, `isLastStep`
|
|
80
|
+
- **`useJourneySelector(selector, eq?)`** — subscribe to a slice of the snapshot
|
|
81
|
+
- **`useJourneyEvent(listener)`** — stream lifecycle events for analytics
|
|
120
82
|
|
|
121
|
-
|
|
122
|
-
- `terminateJourney`
|
|
123
|
-
- `completeJourney`
|
|
124
|
-
- `send`
|
|
125
|
-
- `goToPreviousStep(steps?)`
|
|
126
|
-
- `goToLastVisitedStep()`
|
|
127
|
-
- `updateContext`
|
|
128
|
-
- `updateStepMetadata`
|
|
129
|
-
- `clearStepError`, `resetJourney`
|
|
130
|
-
|
|
131
|
-
Imperative jump is available through `send`:
|
|
83
|
+
## Navigation
|
|
132
84
|
|
|
133
85
|
```ts
|
|
134
|
-
|
|
86
|
+
const api = signup.useJourneyApi();
|
|
87
|
+
|
|
88
|
+
await api.startJourney();
|
|
89
|
+
await api.goToNextStep();
|
|
90
|
+
await api.goToPreviousStep();
|
|
91
|
+
await api.goToLastVisitedStep();
|
|
92
|
+
await api.completeJourney();
|
|
93
|
+
await api.terminateJourney();
|
|
94
|
+
await api.goToStepById("review");
|
|
95
|
+
|
|
96
|
+
api.updateContext((ctx) => ({ ...ctx, name: "Ada" }));
|
|
97
|
+
api.resetJourney();
|
|
135
98
|
```
|
|
136
99
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
`updateContext()` is immediate, but it follows core async timing rules: it does not retroactively change a transition already in `evaluating-when` or `running-effect`, and a running effect can later commit over that update. If the change must affect the current transition, apply it before `send(...)` or await the transition first.
|
|
140
|
-
|
|
141
|
-
## Provider Behavior
|
|
100
|
+
Transition failures resolve through `result.error` instead of rejecting, so `void api.goToNextStep()` is safe from unhandled promise rejections.
|
|
142
101
|
|
|
143
|
-
|
|
144
|
-
- `<Provider journey={...} />` lets you pass a different journey definition at runtime.
|
|
145
|
-
- Internal machine is preserved across `journey` and `persistence` prop changes by default.
|
|
146
|
-
- Set `resetOnJourneyChange` to rebuild internal machine when `journey` identity changes.
|
|
147
|
-
- Set `resetOnPersistenceChange` to rebuild internal machine when `persistence` identity changes.
|
|
148
|
-
- `<Provider machine={externalMachine} />` uses your machine directly.
|
|
149
|
-
- `persistence` applies only when Provider owns the internal machine.
|
|
150
|
-
- Internal Provider-owned machines default to completing on `goToNextStep()` when the current step declares no next transition.
|
|
151
|
-
- Set `completeOnNoNextStep={false}` to opt out.
|
|
152
|
-
- `onStart(event)` wraps `machine.subscribeStart(...)`.
|
|
153
|
-
- `onComplete(event)` wraps `machine.subscribeComplete(...)`.
|
|
154
|
-
- `onTerminate(event)` wraps `machine.subscribeTerminate(...)`.
|
|
155
|
-
- All three callback props work with internal and external machines.
|
|
156
|
-
- `onStart` replays startup on mount, matching core `journey.start` behavior.
|
|
157
|
-
- `onComplete` and `onTerminate` fire only for emitted terminal lifecycle events.
|
|
102
|
+
For step components, `useStepApi(stepId)` returns the same commands but narrows `send(...)` to custom events handled by that step or by `global` transitions:
|
|
158
103
|
|
|
159
104
|
```tsx
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
resetOnJourneyChange
|
|
163
|
-
onStart={() => console.log("started!")}
|
|
164
|
-
>
|
|
165
|
-
<bindings.StepRenderer />
|
|
166
|
-
</bindings.Provider>
|
|
105
|
+
const api = signup.useStepApi("start");
|
|
106
|
+
void api.send({ type: "submit" });
|
|
167
107
|
```
|
|
168
108
|
|
|
169
|
-
##
|
|
109
|
+
## Custom Step Renderer
|
|
170
110
|
|
|
171
|
-
|
|
111
|
+
`StepRenderer` is a convenience — it just looks up the current step's view from the `views` record and renders it. You can build your own if you need transitions, animations, or a different rendering strategy:
|
|
172
112
|
|
|
173
113
|
```tsx
|
|
174
|
-
const
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
if (
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
if (currentAsync.phase === "error") {
|
|
181
|
-
return (
|
|
182
|
-
<div>
|
|
183
|
-
<p>Something failed.</p>
|
|
184
|
-
<button onClick={() => api.clearStepError()}>Dismiss</button>
|
|
185
|
-
</div>
|
|
186
|
-
);
|
|
187
|
-
}
|
|
114
|
+
const MyStepRenderer = () => {
|
|
115
|
+
const { currentStepId } = signup.useJourneySnapshot();
|
|
116
|
+
const View = views[currentStepId];
|
|
117
|
+
if (!View) return <p>Unknown step</p>;
|
|
118
|
+
return <View />;
|
|
119
|
+
};
|
|
188
120
|
```
|
|
189
121
|
|
|
190
|
-
##
|
|
191
|
-
|
|
192
|
-
Use `useJourneyMachine()` and attach the devtools bridge from an effect:
|
|
122
|
+
## Plugins
|
|
193
123
|
|
|
194
124
|
```tsx
|
|
195
|
-
import
|
|
196
|
-
import { attachJourneyDevtools } from "@rxova/journey-devtools-bridge";
|
|
197
|
-
|
|
198
|
-
const JourneyDevtoolsBridge = () => {
|
|
199
|
-
const machine = bindings.useJourneyMachine();
|
|
125
|
+
import { createPersistencePlugin } from "@rxova/journey-core/persistence";
|
|
200
126
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
return null;
|
|
206
|
-
};
|
|
127
|
+
const signup = createJourney(definition, {
|
|
128
|
+
plugins: [createPersistencePlugin({ key: "signup", version: 1 })],
|
|
129
|
+
defaultTimeoutMs: 30_000
|
|
130
|
+
});
|
|
207
131
|
```
|
|
208
132
|
|
|
209
|
-
##
|
|
133
|
+
## Runtime Ownership
|
|
210
134
|
|
|
211
|
-
|
|
135
|
+
Each `createJourney()` call creates one machine instance. The returned hooks are permanently bound to it.
|
|
212
136
|
|
|
213
|
-
|
|
214
|
-
|
|
137
|
+
- Rendering multiple providers from the same runtime shares one journey state
|
|
138
|
+
- `JourneyProvider` auto-starts an `idled` runtime, but does not dispose it by default
|
|
139
|
+
- Provider-free flows can start manually through `useJourneyApi().startJourney()` or `machine.startJourney()`
|
|
140
|
+
- Provider-owned startup failures are reported through `onError(error, { phase: "start" })`
|
|
141
|
+
- Set `disposeOnUnmount` when a provider fully owns a component-scoped runtime
|
|
142
|
+
- Independent instances require separate `createJourney()` calls
|
|
143
|
+
- `createJourneyFactory()` returns a typed helper for producing fresh runtimes from the same definition/options pair and is the preferred path when request-scoped or boundary-scoped isolation matters
|
|
144
|
+
- `dispose()` tears down subscriptions when the runtime is no longer needed
|
|
215
145
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
)
|
|
220
|
-
|
|
146
|
+
## Documentation
|
|
147
|
+
|
|
148
|
+
- [Pre-1.0 Migration](https://rxova.org/docs/core/pre-1-0-migration)
|
|
149
|
+
- [Stability Contract](https://rxova.org/docs/core/stability)
|
|
150
|
+
- [React Quickstart](https://rxova.org/docs/react/quickstart)
|
|
151
|
+
- [Provider and Hooks](https://rxova.org/docs/react/provider-and-hooks)
|
|
152
|
+
- [Patterns](https://rxova.org/docs/react/patterns)
|
|
153
|
+
- [Core Docs](https://rxova.org/docs/core/getting-started)
|
|
221
154
|
|
|
222
|
-
##
|
|
155
|
+
## License
|
|
223
156
|
|
|
224
|
-
|
|
225
|
-
- In React Server Components environments, call bindings/hooks from client components.
|
|
226
|
-
- Server-side rendering is supported (Provider + StepRenderer render safely on the server).
|
|
157
|
+
MIT
|
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";"use client";var w=Object.create;var x=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var H=Object.getPrototypeOf,D=Object.prototype.hasOwnProperty;var j=(n,r)=>{for(var s in r)x(n,s,{get:r[s],enumerable:!0})},M=(n,r,s,y)=>{if(r&&typeof r=="object"||typeof r=="function")for(let c of F(r))!D.call(n,c)&&c!==s&&x(n,c,{get:()=>r[c],enumerable:!(y=k(r,c))||y.enumerable});return n};var E=(n,r,s)=>(s=n!=null?w(H(n)):{},M(r||!n||!n.__esModule?x(s,"default",{value:n,enumerable:!0}):s,n)),A=n=>M(x({},"__esModule",{value:!0}),n);var B={};j(B,{createJourney:()=>b,createJourneyFactory:()=>I});module.exports=A(B);var C=require("@rxova/journey-core");var d=E(require("react"),1),l=require("react/jsx-runtime"),m=typeof window>"u"?d.default.useEffect:d.default.useLayoutEffect,L=(n,r,s)=>{if(s){s(n,r);return}console.error(`JourneyProvider ${r.phase} failed.`,n)},h=(n,r)=>{let s=d.default.createContext(null),y=(a="hook")=>{let e=d.default.useContext(s);if(!e)throw new Error(`${a} must be used within JourneyProvider.`);return e},c=({runtimeMachine:a,onError:e,disposeOnUnmount:t})=>{let o=d.default.useRef(e),u=d.default.useRef(null);o.current=e,m(()=>{u.current!==null&&(globalThis.clearTimeout(u.current),u.current=null)});let J=d.default.useCallback(i=>i.status,[a]),S=d.default.useCallback(i=>a.subscribeSelector(J,()=>{i()}),[a,J]),v=d.default.useCallback(()=>a.getSnapshot().status,[a]),T=d.default.useSyncExternalStore(S,v,v);return m(()=>{T==="idled"&&a.startJourney().catch(i=>{L(i,{phase:"start"},o.current)})},[a,T]),m(()=>{if(t)return()=>{u.current=globalThis.setTimeout(()=>{u.current=null,a.dispose()},0)}},[a,t]),null};return{JourneyProvider:({views:a,onError:e,disposeOnUnmount:t=!1,children:o})=>{let u=n;return(0,l.jsxs)(s.Provider,{value:a,children:[o,(0,l.jsx)(c,{runtimeMachine:u,onError:e,disposeOnUnmount:t})]})},StepRenderer:({fallback:a=null})=>{let e=r(J=>J.currentStepId),o=y("StepRenderer")[e];if(!o)return(0,l.jsx)(l.Fragment,{children:a});let u=o;return(0,l.jsx)(d.default.Fragment,{children:(0,l.jsx)(u,{})},e)}}};var p=E(require("react"),1),q=typeof window>"u"?p.default.useEffect:p.default.useLayoutEffect,P=n=>{let r=()=>{let e=n,t=p.default.useCallback(()=>e.getSnapshot(),[e]),o=p.default.useCallback(u=>e.subscribe(u),[e]);return p.default.useSyncExternalStore(o,t,t)},s=()=>{let e=r(),t=n;return p.default.useMemo(()=>t.getComputed(),[t,e])},y=(e,t)=>{let o=n,u=t??Object.is,J=p.default.useRef(null),S=p.default.useCallback(()=>{let T=o.getSnapshot(),i=J.current;if(i&&Object.is(i.machine,o)&&Object.is(i.selector,e)&&Object.is(i.isEqual,u)&&Object.is(i.snapshot,T))return i.selected;let f=e(T);return i&&Object.is(i.machine,o)&&Object.is(i.selector,e)&&Object.is(i.isEqual,u)&&u(i.selected,f)?(J.current={machine:o,snapshot:T,selected:i.selected,selector:e,isEqual:u},i.selected):(J.current={machine:o,snapshot:T,selected:f,selector:e,isEqual:u},f)},[o,u,e]),v=p.default.useCallback(T=>o.subscribeSelector(e,()=>{T()},u),[o,u,e]);return p.default.useSyncExternalStore(v,S,S)},c=e=>{let t=n,o=p.default.useRef(e);o.current=e,q(()=>t.subscribeEvent(u=>{o.current(u)}),[t])};return{useJourneySnapshot:r,useJourneyComputed:s,useJourneySelector:y,useJourneyApi:()=>{let e=n;return p.default.useMemo(()=>({startJourney:e.startJourney,send:e.send,goToNextStep:e.goToNextStep,goToStepById:e.goToStepById,terminateJourney:e.terminateJourney,completeJourney:e.completeJourney,goToPreviousStep:e.goToPreviousStep,goToLastVisitedStep:e.goToLastVisitedStep,clearStepError:e.clearStepError,updateContext:e.updateContext,getStepMeta:e.getStepMeta,resetJourney:()=>e.resetJourney()}),[e])},useStepApi:e=>{let t=n;return p.default.useMemo(()=>({startJourney:t.startJourney,send:t.send,goToNextStep:t.goToNextStep,goToStepById:t.goToStepById,terminateJourney:t.terminateJourney,completeJourney:t.completeJourney,goToPreviousStep:t.goToPreviousStep,goToLastVisitedStep:t.goToLastVisitedStep,clearStepError:t.clearStepError,updateContext:t.updateContext,getStepMeta:t.getStepMeta,resetJourney:()=>t.resetJourney()}),[t])},useJourneyEvent:c,useJourneyStepLifecycle:(e,t)=>{c(o=>{o.type==="step.enter"&&o.stepId===e?t.onEnter?.({context:n.getSnapshot().context}):o.type==="step.exit"&&o.stepId===e&&t.onLeave?.({context:n.getSnapshot().context})})}}};var R=(n,r)=>{let y=(0,C.createJourneyMachine)(n,r),c=P(y),g=h(y,c.useJourneySelector);return{machine:y,dispose:()=>y.dispose(),...c,...g}};function b(n,r){return R(n,r)}function I(n,r){let s=n;return()=>R(s,r)}0&&(module.exports={createJourney,createJourneyFactory});
|
|
2
|
+
//# sourceMappingURL=client.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/client.ts", "../src/createJourney.tsx", "../src/provider.tsx", "../src/runtime-hooks.tsx"],
|
|
4
|
+
"sourcesContent": ["\"use client\";\n\nexport * from \"./index\";\n", "/* eslint-disable no-redeclare */\nimport { createJourneyMachine } from \"@rxova/journey-core\";\nimport type {\n JourneyDefinition,\n JourneyJsonObject,\n JourneyMachineOptions,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins\n} from \"@rxova/journey-core\";\nimport { createJourneyProviderArtifacts } from \"./provider\";\nimport { createJourneyHooks } from \"./runtime-hooks\";\nimport type {\n JourneyRuntime,\n JourneyRuntimeFactoryFromDefinition,\n JourneyRuntimeFromDefinition,\n JourneyRuntimeFactory\n} from \"./types\";\n\ntype JourneyOptionsInput<TPlugins extends readonly JourneyMachinePlugin[]> = JourneyMachineOptions<\n TPlugins extends [] ? readonly JourneyMachinePlugin[] : TPlugins\n>;\n\nconst createJourneyMachineRuntime = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> => {\n const machineOptions = options as JourneyMachineOptions<TPlugins> | undefined;\n const machine = createJourneyMachine<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(definition, machineOptions) as JourneyMachineWithPlugins<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >;\n const hooks = createJourneyHooks<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>(\n machine\n );\n const providerArtifacts = createJourneyProviderArtifacts<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(machine, hooks.useJourneySelector);\n\n return {\n machine,\n dispose: () => machine.dispose(),\n ...hooks,\n ...providerArtifacts\n };\n};\n\n/**\n * Creates a journey machine and returns React hooks/components bound to that machine.\n * Hooks work without a provider; `JourneyProvider` is only required for `StepRenderer`.\n */\nexport function createJourney<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []>(\n definition: TDefinition,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFromDefinition<TDefinition, TPlugins>;\nexport function createJourney<\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> {\n return createJourneyMachineRuntime(definition, options);\n}\n\n/**\n * Creates a typed factory for producing fresh React-bound journey runtimes.\n * Use this when a component or route boundary needs independent instances\n * from the same definition/options pair.\n */\nexport function createJourneyFactory<\n TDefinition,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: TDefinition,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFactoryFromDefinition<TDefinition, TPlugins>;\nexport function createJourneyFactory<\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFactory<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> {\n const runtimeDefinition = definition as JourneyDefinition<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers\n >;\n\n return () =>\n createJourneyMachineRuntime(\n runtimeDefinition,\n options as JourneyOptionsInput<TPlugins> | undefined\n );\n}\n", "import React from \"react\";\n\nimport type {\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneySelector,\n JourneyMachineWithPlugins,\n JourneyMachinePlugin\n} from \"@rxova/journey-core\";\nimport type { JourneyProviderErrorContext, JourneyProviderProps, JourneyViews } from \"./types\";\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nconst reportProviderError = (\n error: unknown,\n context: JourneyProviderErrorContext,\n listener?: ((error: unknown, context: JourneyProviderErrorContext) => void) | undefined\n) => {\n if (listener) {\n listener(error, context);\n return;\n }\n\n console.error(`JourneyProvider ${context.phase} failed.`, error);\n};\n\nexport const createJourneyProviderArtifacts = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>,\n useJourneySelector: <TSelected>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ) => TSelected\n) => {\n const ViewsContext = React.createContext<JourneyViews<TStepId> | null>(null);\n\n const useJourneyViews = (hookName = \"hook\") => {\n const views = React.useContext(ViewsContext);\n if (!views) {\n throw new Error(`${hookName} must be used within JourneyProvider.`);\n }\n return views;\n };\n\n const ProviderController = ({\n runtimeMachine,\n onError,\n disposeOnUnmount\n }: {\n runtimeMachine: typeof machine;\n onError: JourneyProviderProps<TStepId>[\"onError\"] | undefined;\n disposeOnUnmount: boolean;\n }) => {\n const onErrorRef = React.useRef(onError);\n const scheduledDisposeRef = React.useRef<ReturnType<typeof globalThis.setTimeout> | null>(null);\n onErrorRef.current = onError;\n\n useSafeLayoutEffect(() => {\n if (scheduledDisposeRef.current === null) {\n return;\n }\n\n globalThis.clearTimeout(scheduledDisposeRef.current);\n scheduledDisposeRef.current = null;\n });\n\n const selectStatus = React.useCallback(\n (snapshot: ReturnType<typeof runtimeMachine.getSnapshot>) => snapshot.status,\n [runtimeMachine]\n );\n const subscribeToStatus = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(selectStatus, () => {\n onStoreChange();\n }),\n [runtimeMachine, selectStatus]\n );\n const getStatus = React.useCallback(\n () => runtimeMachine.getSnapshot().status,\n [runtimeMachine]\n );\n const status = React.useSyncExternalStore(subscribeToStatus, getStatus, getStatus);\n\n useSafeLayoutEffect(() => {\n if (status === \"idled\") {\n void runtimeMachine.startJourney().catch((error) => {\n reportProviderError(error, { phase: \"start\" }, onErrorRef.current);\n });\n }\n }, [runtimeMachine, status]);\n\n useSafeLayoutEffect(() => {\n if (!disposeOnUnmount) {\n return;\n }\n\n return () => {\n scheduledDisposeRef.current = globalThis.setTimeout(() => {\n scheduledDisposeRef.current = null;\n runtimeMachine.dispose();\n }, 0);\n };\n }, [runtimeMachine, disposeOnUnmount]);\n\n return null;\n };\n\n const JourneyProvider = ({\n views,\n onError,\n disposeOnUnmount = false,\n children\n }: JourneyProviderProps<TStepId>) => {\n const runtimeMachine = machine;\n\n return (\n <ViewsContext.Provider value={views}>\n {children}\n <ProviderController\n runtimeMachine={runtimeMachine}\n onError={onError}\n disposeOnUnmount={disposeOnUnmount}\n />\n </ViewsContext.Provider>\n );\n };\n\n const StepRenderer = ({ fallback = null }: { fallback?: React.ReactNode }) => {\n const currentStepId = useJourneySelector((snapshot) => snapshot.currentStepId);\n const views = useJourneyViews(\"StepRenderer\");\n const StepComponent = views[currentStepId];\n\n if (!StepComponent) {\n return <>{fallback}</>;\n }\n\n const StepView = StepComponent as React.ComponentType;\n\n return (\n <React.Fragment key={currentStepId}>\n <StepView />\n </React.Fragment>\n );\n };\n\n return {\n JourneyProvider,\n StepRenderer\n };\n};\n", "import React from \"react\";\n\nimport type {\n JourneyBuilderCustomEventKey,\n JourneyComputed,\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins,\n JourneyObservationEvent,\n JourneySelector,\n JourneySnapshot\n} from \"@rxova/journey-core\";\nimport type { JourneyApi, StepScopedJourneyApi } from \"./types\";\n\ntype SelectorCache<TContext extends JourneyJsonObject, TStepId extends string, TSelected> = {\n machine: unknown;\n snapshot: JourneySnapshot<TContext, TStepId>;\n selected: TSelected;\n selector: unknown;\n isEqual: JourneyEqualityFn<TSelected>;\n};\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nexport const createJourneyHooks = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = [],\n TStepHandledCustomEventMap extends Record<TStepId, JourneyBuilderCustomEventKey<TEventMap>> =\n Record<TStepId, never>,\n TGlobalHandledCustomEventType extends JourneyBuilderCustomEventKey<TEventMap> = never\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>\n) => {\n const useJourneySnapshot = (): JourneySnapshot<TContext, TStepId> => {\n const runtimeMachine = machine;\n const getSnapshot = React.useCallback(() => runtimeMachine.getSnapshot(), [runtimeMachine]);\n const subscribe = React.useCallback(\n (onStoreChange: () => void) => runtimeMachine.subscribe(onStoreChange),\n [runtimeMachine]\n );\n\n return React.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n };\n\n const useJourneyComputed = (): JourneyComputed<TStepId> => {\n const snapshot = useJourneySnapshot();\n const runtimeMachine = machine;\n return React.useMemo(() => {\n void snapshot;\n return runtimeMachine.getComputed();\n }, [runtimeMachine, snapshot]);\n };\n\n const useJourneySelector = <TSelected,>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ): TSelected => {\n const runtimeMachine = machine;\n const isEqual = equalityFn ?? Object.is;\n const cacheRef = React.useRef<SelectorCache<TContext, TStepId, TSelected> | null>(null);\n\n const getSelectedSnapshot = React.useCallback(() => {\n const nextSnapshot = runtimeMachine.getSnapshot();\n const cached = cacheRef.current;\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n Object.is(cached.snapshot, nextSnapshot)\n ) {\n return cached.selected;\n }\n\n const nextSelected = selector(nextSnapshot);\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n isEqual(cached.selected, nextSelected)\n ) {\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: cached.selected,\n selector,\n isEqual\n };\n return cached.selected;\n }\n\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: nextSelected,\n selector,\n isEqual\n };\n return nextSelected;\n }, [runtimeMachine, isEqual, selector]);\n\n const subscribeToSelectedSnapshot = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(\n selector,\n () => {\n onStoreChange();\n },\n isEqual\n ),\n [runtimeMachine, isEqual, selector]\n );\n\n return React.useSyncExternalStore(\n subscribeToSelectedSnapshot,\n getSelectedSnapshot,\n getSelectedSnapshot\n );\n };\n\n const useJourneyEvent = (\n listener: (event: JourneyObservationEvent<TStepId, TEventMap>) => void\n ): void => {\n const runtimeMachine = machine;\n const listenerRef = React.useRef(listener);\n listenerRef.current = listener;\n\n useSafeLayoutEffect(() => {\n return runtimeMachine.subscribeEvent((event) => {\n listenerRef.current(event);\n });\n }, [runtimeMachine]);\n };\n\n const useJourneyStepLifecycle = (\n stepId: TStepId,\n callbacks: {\n onEnter?: (args: { context: TContext }) => void;\n onLeave?: (args: { context: TContext }) => void;\n }\n ): void => {\n useJourneyEvent((event) => {\n if (event.type === \"step.enter\" && event.stepId === stepId) {\n callbacks.onEnter?.({ context: machine.getSnapshot().context });\n } else if (event.type === \"step.exit\" && event.stepId === stepId) {\n callbacks.onLeave?.({ context: machine.getSnapshot().context });\n }\n });\n };\n\n const useJourneyApi = (): JourneyApi<TContext, TStepId, TEventMap, TStepMeta> => {\n const runtimeMachine = machine;\n return React.useMemo(\n () => ({\n startJourney: runtimeMachine.startJourney,\n send: runtimeMachine.send,\n goToNextStep: runtimeMachine.goToNextStep,\n goToStepById: runtimeMachine.goToStepById,\n terminateJourney: runtimeMachine.terminateJourney,\n completeJourney: runtimeMachine.completeJourney,\n goToPreviousStep: runtimeMachine.goToPreviousStep,\n goToLastVisitedStep: runtimeMachine.goToLastVisitedStep,\n clearStepError: runtimeMachine.clearStepError,\n updateContext: runtimeMachine.updateContext,\n getStepMeta: runtimeMachine.getStepMeta,\n resetJourney: () => runtimeMachine.resetJourney()\n }),\n [runtimeMachine]\n );\n };\n\n const useStepApi = <TStepKey extends TStepId>(\n stepId: TStepKey\n ): StepScopedJourneyApi<\n TContext,\n TStepId,\n TEventMap,\n Extract<\n TStepHandledCustomEventMap[TStepKey] | TGlobalHandledCustomEventType,\n keyof TEventMap & string\n >,\n TStepMeta\n > => {\n const runtimeMachine = machine;\n void stepId;\n return React.useMemo(\n () => ({\n startJourney: runtimeMachine.startJourney,\n send: runtimeMachine.send,\n goToNextStep: runtimeMachine.goToNextStep,\n goToStepById: runtimeMachine.goToStepById,\n terminateJourney: runtimeMachine.terminateJourney,\n completeJourney: runtimeMachine.completeJourney,\n goToPreviousStep: runtimeMachine.goToPreviousStep,\n goToLastVisitedStep: runtimeMachine.goToLastVisitedStep,\n clearStepError: runtimeMachine.clearStepError,\n updateContext: runtimeMachine.updateContext,\n getStepMeta: runtimeMachine.getStepMeta,\n resetJourney: () => runtimeMachine.resetJourney()\n }),\n [runtimeMachine]\n ) as StepScopedJourneyApi<\n TContext,\n TStepId,\n TEventMap,\n Extract<\n TStepHandledCustomEventMap[TStepKey] | TGlobalHandledCustomEventType,\n keyof TEventMap & string\n >,\n TStepMeta\n >;\n };\n\n return {\n useJourneySnapshot,\n useJourneyComputed,\n useJourneySelector,\n useJourneyApi,\n useStepApi,\n useJourneyEvent,\n useJourneyStepLifecycle\n };\n};\n"],
|
|
5
|
+
"mappings": "ukBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,mBAAAE,EAAA,yBAAAC,IAAA,eAAAC,EAAAJ,GCCA,IAAAK,EAAqC,+BCDrC,IAAAC,EAAkB,sBA0HZC,EAAA,6BA/GAC,EAAsB,OAAO,OAAW,IAAc,EAAAC,QAAM,UAAY,EAAAA,QAAM,gBAE9EC,EAAsB,CAC1BC,EACAC,EACAC,IACG,CACH,GAAIA,EAAU,CACZA,EAASF,EAAOC,CAAO,EACvB,MACF,CAEA,QAAQ,MAAM,mBAAmBA,EAAQ,KAAK,WAAYD,CAAK,CACjE,EAEaG,EAAiC,CAQ5CC,EACAC,IAIG,CACH,IAAMC,EAAe,EAAAR,QAAM,cAA4C,IAAI,EAErES,EAAkB,CAACC,EAAW,SAAW,CAC7C,IAAMC,EAAQ,EAAAX,QAAM,WAAWQ,CAAY,EAC3C,GAAI,CAACG,EACH,MAAM,IAAI,MAAM,GAAGD,CAAQ,uC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Q,EAAoBT,EAQ1B,MAAO,IACLD,EACEU,EACAR,CACF,CACJ",
|
|
6
|
+
"names": ["client_exports", "__export", "createJourney", "createJourneyFactory", "__toCommonJS", "import_journey_core", "import_react", "import_jsx_runtime", "useSafeLayoutEffect", "React", "reportProviderError", "error", "context", "listener", "createJourneyProviderArtifacts", "machine", "useJourneySelector", "ViewsContext", "useJourneyViews", "hookName", "views", "ProviderController", "runtimeMachine", "onError", "disposeOnUnmount", "onErrorRef", "scheduledDisposeRef", "selectStatus", "snapshot", "subscribeToStatus", "onStoreChange", "getStatus", "status", "children", "fallback", "currentStepId", "StepComponent", "StepView", "import_react", "useSafeLayoutEffect", "React", "createJourneyHooks", "machine", "useJourneySnapshot", "runtimeMachine", "getSnapshot", "subscribe", "onStoreChange", "useJourneyComputed", "snapshot", "useJourneySelector", "selector", "equalityFn", "isEqual", "cacheRef", "getSelectedSnapshot", "nextSnapshot", "cached", "nextSelected", "subscribeToSelectedSnapshot", "useJourneyEvent", "listener", "listenerRef", "event", "stepId", "callbacks", "createJourneyMachineRuntime", "definition", "options", "machine", "hooks", "createJourneyHooks", "providerArtifacts", "createJourneyProviderArtifacts", "createJourney", "createJourneyFactory", "runtimeDefinition"]
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./index";
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./index";
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use client";import{createJourneyMachine as I}from"@rxova/journey-core";import p from"react";import{Fragment as R,jsx as v,jsxs as C}from"react/jsx-runtime";var f=typeof window>"u"?p.useEffect:p.useLayoutEffect,P=(r,i,c)=>{if(c){c(r,i);return}console.error(`JourneyProvider ${i.phase} failed.`,r)},m=(r,i)=>{let c=p.createContext(null),y=(s="hook")=>{let e=p.useContext(c);if(!e)throw new Error(`${s} must be used within JourneyProvider.`);return e},l=({runtimeMachine:s,onError:e,disposeOnUnmount:n})=>{let t=p.useRef(e),o=p.useRef(null);t.current=e,f(()=>{o.current!==null&&(globalThis.clearTimeout(o.current),o.current=null)});let T=p.useCallback(u=>u.status,[s]),J=p.useCallback(u=>s.subscribeSelector(T,()=>{u()}),[s,T]),S=p.useCallback(()=>s.getSnapshot().status,[s]),d=p.useSyncExternalStore(J,S,S);return f(()=>{d==="idled"&&s.startJourney().catch(u=>{P(u,{phase:"start"},t.current)})},[s,d]),f(()=>{if(n)return()=>{o.current=globalThis.setTimeout(()=>{o.current=null,s.dispose()},0)}},[s,n]),null};return{JourneyProvider:({views:s,onError:e,disposeOnUnmount:n=!1,children:t})=>{let o=r;return C(c.Provider,{value:s,children:[t,v(l,{runtimeMachine:o,onError:e,disposeOnUnmount:n})]})},StepRenderer:({fallback:s=null})=>{let e=i(T=>T.currentStepId),t=y("StepRenderer")[e];if(!t)return v(R,{children:s});let o=t;return v(p.Fragment,{children:v(o,{})},e)}}};import a from"react";var b=typeof window>"u"?a.useEffect:a.useLayoutEffect,M=r=>{let i=()=>{let e=r,n=a.useCallback(()=>e.getSnapshot(),[e]),t=a.useCallback(o=>e.subscribe(o),[e]);return a.useSyncExternalStore(t,n,n)},c=()=>{let e=i(),n=r;return a.useMemo(()=>n.getComputed(),[n,e])},y=(e,n)=>{let t=r,o=n??Object.is,T=a.useRef(null),J=a.useCallback(()=>{let d=t.getSnapshot(),u=T.current;if(u&&Object.is(u.machine,t)&&Object.is(u.selector,e)&&Object.is(u.isEqual,o)&&Object.is(u.snapshot,d))return u.selected;let g=e(d);return u&&Object.is(u.machine,t)&&Object.is(u.selector,e)&&Object.is(u.isEqual,o)&&o(u.selected,g)?(T.current={machine:t,snapshot:d,selected:u.selected,selector:e,isEqual:o},u.selected):(T.current={machine:t,snapshot:d,selected:g,selector:e,isEqual:o},g)},[t,o,e]),S=a.useCallback(d=>t.subscribeSelector(e,()=>{d()},o),[t,o,e]);return a.useSyncExternalStore(S,J,J)},l=e=>{let n=r,t=a.useRef(e);t.current=e,b(()=>n.subscribeEvent(o=>{t.current(o)}),[n])};return{useJourneySnapshot:i,useJourneyComputed:c,useJourneySelector:y,useJourneyApi:()=>{let e=r;return a.useMemo(()=>({startJourney:e.startJourney,send:e.send,goToNextStep:e.goToNextStep,goToStepById:e.goToStepById,terminateJourney:e.terminateJourney,completeJourney:e.completeJourney,goToPreviousStep:e.goToPreviousStep,goToLastVisitedStep:e.goToLastVisitedStep,clearStepError:e.clearStepError,updateContext:e.updateContext,getStepMeta:e.getStepMeta,resetJourney:()=>e.resetJourney()}),[e])},useStepApi:e=>{let n=r;return a.useMemo(()=>({startJourney:n.startJourney,send:n.send,goToNextStep:n.goToNextStep,goToStepById:n.goToStepById,terminateJourney:n.terminateJourney,completeJourney:n.completeJourney,goToPreviousStep:n.goToPreviousStep,goToLastVisitedStep:n.goToLastVisitedStep,clearStepError:n.clearStepError,updateContext:n.updateContext,getStepMeta:n.getStepMeta,resetJourney:()=>n.resetJourney()}),[n])},useJourneyEvent:l,useJourneyStepLifecycle:(e,n)=>{l(t=>{t.type==="step.enter"&&t.stepId===e?n.onEnter?.({context:r.getSnapshot().context}):t.type==="step.exit"&&t.stepId===e&&n.onLeave?.({context:r.getSnapshot().context})})}}};var E=(r,i)=>{let y=I(r,i),l=M(y),x=m(y,l.useJourneySelector);return{machine:y,dispose:()=>y.dispose(),...l,...x}};function O(r,i){return E(r,i)}function w(r,i){let c=r;return()=>E(c,i)}export{O as createJourney,w as createJourneyFactory};
|
|
2
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/createJourney.tsx", "../src/provider.tsx", "../src/runtime-hooks.tsx"],
|
|
4
|
+
"sourcesContent": ["/* eslint-disable no-redeclare */\nimport { createJourneyMachine } from \"@rxova/journey-core\";\nimport type {\n JourneyDefinition,\n JourneyJsonObject,\n JourneyMachineOptions,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins\n} from \"@rxova/journey-core\";\nimport { createJourneyProviderArtifacts } from \"./provider\";\nimport { createJourneyHooks } from \"./runtime-hooks\";\nimport type {\n JourneyRuntime,\n JourneyRuntimeFactoryFromDefinition,\n JourneyRuntimeFromDefinition,\n JourneyRuntimeFactory\n} from \"./types\";\n\ntype JourneyOptionsInput<TPlugins extends readonly JourneyMachinePlugin[]> = JourneyMachineOptions<\n TPlugins extends [] ? readonly JourneyMachinePlugin[] : TPlugins\n>;\n\nconst createJourneyMachineRuntime = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> => {\n const machineOptions = options as JourneyMachineOptions<TPlugins> | undefined;\n const machine = createJourneyMachine<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(definition, machineOptions) as JourneyMachineWithPlugins<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >;\n const hooks = createJourneyHooks<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>(\n machine\n );\n const providerArtifacts = createJourneyProviderArtifacts<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers,\n TPlugins\n >(machine, hooks.useJourneySelector);\n\n return {\n machine,\n dispose: () => machine.dispose(),\n ...hooks,\n ...providerArtifacts\n };\n};\n\n/**\n * Creates a journey machine and returns React hooks/components bound to that machine.\n * Hooks work without a provider; `JourneyProvider` is only required for `StepRenderer`.\n */\nexport function createJourney<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []>(\n definition: TDefinition,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFromDefinition<TDefinition, TPlugins>;\nexport function createJourney<\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntime<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> {\n return createJourneyMachineRuntime(definition, options);\n}\n\n/**\n * Creates a typed factory for producing fresh React-bound journey runtimes.\n * Use this when a component or route boundary needs independent instances\n * from the same definition/options pair.\n */\nexport function createJourneyFactory<\n TDefinition,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: TDefinition,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFactoryFromDefinition<TDefinition, TPlugins>;\nexport function createJourneyFactory<\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n definition: JourneyDefinition<TContext, TStepId, TEventMap, TStepMeta, THandlers>,\n options?: JourneyOptionsInput<TPlugins>\n): JourneyRuntimeFactory<TContext, TStepId, TEventMap, TStepMeta, TPlugins, THandlers> {\n const runtimeDefinition = definition as JourneyDefinition<\n TContext,\n TStepId,\n TEventMap,\n TStepMeta,\n THandlers\n >;\n\n return () =>\n createJourneyMachineRuntime(\n runtimeDefinition,\n options as JourneyOptionsInput<TPlugins> | undefined\n );\n}\n", "import React from \"react\";\n\nimport type {\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneySelector,\n JourneyMachineWithPlugins,\n JourneyMachinePlugin\n} from \"@rxova/journey-core\";\nimport type { JourneyProviderErrorContext, JourneyProviderProps, JourneyViews } from \"./types\";\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nconst reportProviderError = (\n error: unknown,\n context: JourneyProviderErrorContext,\n listener?: ((error: unknown, context: JourneyProviderErrorContext) => void) | undefined\n) => {\n if (listener) {\n listener(error, context);\n return;\n }\n\n console.error(`JourneyProvider ${context.phase} failed.`, error);\n};\n\nexport const createJourneyProviderArtifacts = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = []\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>,\n useJourneySelector: <TSelected>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ) => TSelected\n) => {\n const ViewsContext = React.createContext<JourneyViews<TStepId> | null>(null);\n\n const useJourneyViews = (hookName = \"hook\") => {\n const views = React.useContext(ViewsContext);\n if (!views) {\n throw new Error(`${hookName} must be used within JourneyProvider.`);\n }\n return views;\n };\n\n const ProviderController = ({\n runtimeMachine,\n onError,\n disposeOnUnmount\n }: {\n runtimeMachine: typeof machine;\n onError: JourneyProviderProps<TStepId>[\"onError\"] | undefined;\n disposeOnUnmount: boolean;\n }) => {\n const onErrorRef = React.useRef(onError);\n const scheduledDisposeRef = React.useRef<ReturnType<typeof globalThis.setTimeout> | null>(null);\n onErrorRef.current = onError;\n\n useSafeLayoutEffect(() => {\n if (scheduledDisposeRef.current === null) {\n return;\n }\n\n globalThis.clearTimeout(scheduledDisposeRef.current);\n scheduledDisposeRef.current = null;\n });\n\n const selectStatus = React.useCallback(\n (snapshot: ReturnType<typeof runtimeMachine.getSnapshot>) => snapshot.status,\n [runtimeMachine]\n );\n const subscribeToStatus = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(selectStatus, () => {\n onStoreChange();\n }),\n [runtimeMachine, selectStatus]\n );\n const getStatus = React.useCallback(\n () => runtimeMachine.getSnapshot().status,\n [runtimeMachine]\n );\n const status = React.useSyncExternalStore(subscribeToStatus, getStatus, getStatus);\n\n useSafeLayoutEffect(() => {\n if (status === \"idled\") {\n void runtimeMachine.startJourney().catch((error) => {\n reportProviderError(error, { phase: \"start\" }, onErrorRef.current);\n });\n }\n }, [runtimeMachine, status]);\n\n useSafeLayoutEffect(() => {\n if (!disposeOnUnmount) {\n return;\n }\n\n return () => {\n scheduledDisposeRef.current = globalThis.setTimeout(() => {\n scheduledDisposeRef.current = null;\n runtimeMachine.dispose();\n }, 0);\n };\n }, [runtimeMachine, disposeOnUnmount]);\n\n return null;\n };\n\n const JourneyProvider = ({\n views,\n onError,\n disposeOnUnmount = false,\n children\n }: JourneyProviderProps<TStepId>) => {\n const runtimeMachine = machine;\n\n return (\n <ViewsContext.Provider value={views}>\n {children}\n <ProviderController\n runtimeMachine={runtimeMachine}\n onError={onError}\n disposeOnUnmount={disposeOnUnmount}\n />\n </ViewsContext.Provider>\n );\n };\n\n const StepRenderer = ({ fallback = null }: { fallback?: React.ReactNode }) => {\n const currentStepId = useJourneySelector((snapshot) => snapshot.currentStepId);\n const views = useJourneyViews(\"StepRenderer\");\n const StepComponent = views[currentStepId];\n\n if (!StepComponent) {\n return <>{fallback}</>;\n }\n\n const StepView = StepComponent as React.ComponentType;\n\n return (\n <React.Fragment key={currentStepId}>\n <StepView />\n </React.Fragment>\n );\n };\n\n return {\n JourneyProvider,\n StepRenderer\n };\n};\n", "import React from \"react\";\n\nimport type {\n JourneyBuilderCustomEventKey,\n JourneyComputed,\n JourneyEqualityFn,\n JourneyJsonObject,\n JourneyMachinePlugin,\n JourneyMachineWithPlugins,\n JourneyObservationEvent,\n JourneySelector,\n JourneySnapshot\n} from \"@rxova/journey-core\";\nimport type { JourneyApi, StepScopedJourneyApi } from \"./types\";\n\ntype SelectorCache<TContext extends JourneyJsonObject, TStepId extends string, TSelected> = {\n machine: unknown;\n snapshot: JourneySnapshot<TContext, TStepId>;\n selected: TSelected;\n selector: unknown;\n isEqual: JourneyEqualityFn<TSelected>;\n};\n\nconst useSafeLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nexport const createJourneyHooks = <\n TContext extends JourneyJsonObject,\n TStepId extends string,\n TEventMap extends Record<string, unknown> = Record<never, never>,\n TStepMeta = unknown,\n THandlers extends Record<string, unknown> = Record<never, never>,\n TPlugins extends readonly JourneyMachinePlugin[] = [],\n TStepHandledCustomEventMap extends Record<TStepId, JourneyBuilderCustomEventKey<TEventMap>> =\n Record<TStepId, never>,\n TGlobalHandledCustomEventType extends JourneyBuilderCustomEventKey<TEventMap> = never\n>(\n machine: JourneyMachineWithPlugins<TContext, TStepId, TEventMap, TStepMeta, THandlers, TPlugins>\n) => {\n const useJourneySnapshot = (): JourneySnapshot<TContext, TStepId> => {\n const runtimeMachine = machine;\n const getSnapshot = React.useCallback(() => runtimeMachine.getSnapshot(), [runtimeMachine]);\n const subscribe = React.useCallback(\n (onStoreChange: () => void) => runtimeMachine.subscribe(onStoreChange),\n [runtimeMachine]\n );\n\n return React.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n };\n\n const useJourneyComputed = (): JourneyComputed<TStepId> => {\n const snapshot = useJourneySnapshot();\n const runtimeMachine = machine;\n return React.useMemo(() => {\n void snapshot;\n return runtimeMachine.getComputed();\n }, [runtimeMachine, snapshot]);\n };\n\n const useJourneySelector = <TSelected,>(\n selector: JourneySelector<TContext, TStepId, TSelected>,\n equalityFn?: JourneyEqualityFn<TSelected>\n ): TSelected => {\n const runtimeMachine = machine;\n const isEqual = equalityFn ?? Object.is;\n const cacheRef = React.useRef<SelectorCache<TContext, TStepId, TSelected> | null>(null);\n\n const getSelectedSnapshot = React.useCallback(() => {\n const nextSnapshot = runtimeMachine.getSnapshot();\n const cached = cacheRef.current;\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n Object.is(cached.snapshot, nextSnapshot)\n ) {\n return cached.selected;\n }\n\n const nextSelected = selector(nextSnapshot);\n\n if (\n cached &&\n Object.is(cached.machine, runtimeMachine) &&\n Object.is(cached.selector, selector) &&\n Object.is(cached.isEqual, isEqual) &&\n isEqual(cached.selected, nextSelected)\n ) {\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: cached.selected,\n selector,\n isEqual\n };\n return cached.selected;\n }\n\n cacheRef.current = {\n machine: runtimeMachine,\n snapshot: nextSnapshot,\n selected: nextSelected,\n selector,\n isEqual\n };\n return nextSelected;\n }, [runtimeMachine, isEqual, selector]);\n\n const subscribeToSelectedSnapshot = React.useCallback(\n (onStoreChange: () => void) =>\n runtimeMachine.subscribeSelector(\n selector,\n () => {\n onStoreChange();\n },\n isEqual\n ),\n [runtimeMachine, isEqual, selector]\n );\n\n return React.useSyncExternalStore(\n subscribeToSelectedSnapshot,\n getSelectedSnapshot,\n getSelectedSnapshot\n );\n };\n\n const useJourneyEvent = (\n listener: (event: JourneyObservationEvent<TStepId, TEventMap>) => void\n ): void => {\n const runtimeMachine = machine;\n const listenerRef = React.useRef(listener);\n listenerRef.current = listener;\n\n useSafeLayoutEffect(() => {\n return runtimeMachine.subscribeEvent((event) => {\n listenerRef.current(event);\n });\n }, [runtimeMachine]);\n };\n\n const useJourneyStepLifecycle = (\n stepId: TStepId,\n callbacks: {\n onEnter?: (args: { context: TContext }) => void;\n onLeave?: (args: { context: TContext }) => void;\n }\n ): void => {\n useJourneyEvent((event) => {\n if (event.type === \"step.enter\" && event.stepId === stepId) {\n callbacks.onEnter?.({ context: machine.getSnapshot().context });\n } else if (event.type === \"step.exit\" && event.stepId === stepId) {\n callbacks.onLeave?.({ context: machine.getSnapshot().context });\n }\n });\n };\n\n const useJourneyApi = (): JourneyApi<TContext, TStepId, TEventMap, TStepMeta> => {\n const runtimeMachine = machine;\n return React.useMemo(\n () => ({\n startJourney: runtimeMachine.startJourney,\n send: runtimeMachine.send,\n goToNextStep: runtimeMachine.goToNextStep,\n goToStepById: runtimeMachine.goToStepById,\n terminateJourney: runtimeMachine.terminateJourney,\n completeJourney: runtimeMachine.completeJourney,\n goToPreviousStep: runtimeMachine.goToPreviousStep,\n goToLastVisitedStep: runtimeMachine.goToLastVisitedStep,\n clearStepError: runtimeMachine.clearStepError,\n updateContext: runtimeMachine.updateContext,\n getStepMeta: runtimeMachine.getStepMeta,\n resetJourney: () => runtimeMachine.resetJourney()\n }),\n [runtimeMachine]\n );\n };\n\n const useStepApi = <TStepKey extends TStepId>(\n stepId: TStepKey\n ): StepScopedJourneyApi<\n TContext,\n TStepId,\n TEventMap,\n Extract<\n TStepHandledCustomEventMap[TStepKey] | TGlobalHandledCustomEventType,\n keyof TEventMap & string\n >,\n TStepMeta\n > => {\n const runtimeMachine = machine;\n void stepId;\n return React.useMemo(\n () => ({\n startJourney: runtimeMachine.startJourney,\n send: runtimeMachine.send,\n goToNextStep: runtimeMachine.goToNextStep,\n goToStepById: runtimeMachine.goToStepById,\n terminateJourney: runtimeMachine.terminateJourney,\n completeJourney: runtimeMachine.completeJourney,\n goToPreviousStep: runtimeMachine.goToPreviousStep,\n goToLastVisitedStep: runtimeMachine.goToLastVisitedStep,\n clearStepError: runtimeMachine.clearStepError,\n updateContext: runtimeMachine.updateContext,\n getStepMeta: runtimeMachine.getStepMeta,\n resetJourney: () => runtimeMachine.resetJourney()\n }),\n [runtimeMachine]\n ) as StepScopedJourneyApi<\n TContext,\n TStepId,\n TEventMap,\n Extract<\n TStepHandledCustomEventMap[TStepKey] | TGlobalHandledCustomEventType,\n keyof TEventMap & string\n >,\n TStepMeta\n >;\n };\n\n return {\n useJourneySnapshot,\n useJourneyComputed,\n useJourneySelector,\n useJourneyApi,\n useStepApi,\n useJourneyEvent,\n useJourneyStepLifecycle\n };\n};\n"],
|
|
5
|
+
"mappings": "aACA,OAAS,wBAAAA,MAA4B,sBCDrC,OAAOC,MAAW,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,CAElE,CAAC,CACH,CAyEA,CACF,EFhNA,IAAMwB,EAA8B,CAQlCC,EACAC,IACiF,CAEjF,IAAMC,EAAUC,EAOdH,EARqBC,CAQK,EAQtBG,EAAQC,EACZH,CACF,EACMI,EAAoBC,EAOxBL,EAASE,EAAM,kBAAkB,EAEnC,MAAO,CACL,QAAAF,EACA,QAAS,IAAMA,EAAQ,QAAQ,EAC/B,GAAGE,EACH,GAAGE,CACL,CACF,EAUO,SAASE,EAQdR,EACAC,EAC8E,CAC9E,OAAOF,EAA4BC,EAAYC,CAAO,CACxD,CAcO,SAASQ,EAQdT,EACAC,EACqF,CACrF,IAAMS,EAAoBV,EAQ1B,MAAO,IACLD,EACEW,EACAT,CACF,CACJ",
|
|
6
|
+
"names": ["createJourneyMachine", "React", "Fragment", "jsx", "jsxs", "useSafeLayoutEffect", "reportProviderError", "error", "context", "listener", "createJourneyProviderArtifacts", "machine", "useJourneySelector", "ViewsContext", "useJourneyViews", "hookName", "views", "ProviderController", "runtimeMachine", "onError", "disposeOnUnmount", "onErrorRef", "scheduledDisposeRef", "selectStatus", "snapshot", "subscribeToStatus", "onStoreChange", "getStatus", "status", "children", "fallback", "currentStepId", "StepComponent", "StepView", "React", "useSafeLayoutEffect", "createJourneyHooks", "machine", "useJourneySnapshot", "runtimeMachine", "getSnapshot", "subscribe", "onStoreChange", "useJourneyComputed", "snapshot", "useJourneySelector", "selector", "equalityFn", "isEqual", "cacheRef", "getSelectedSnapshot", "nextSnapshot", "cached", "nextSelected", "subscribeToSelectedSnapshot", "useJourneyEvent", "listener", "listenerRef", "event", "stepId", "callbacks", "createJourneyMachineRuntime", "definition", "options", "machine", "createJourneyMachine", "hooks", "createJourneyHooks", "providerArtifacts", "createJourneyProviderArtifacts", "createJourney", "createJourneyFactory", "runtimeDefinition"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { JourneyMachineOptions, JourneyMachinePlugin } from "@rxova/journey-core";
|
|
2
|
+
import type { JourneyRuntimeFactoryFromDefinition, JourneyRuntimeFromDefinition } from "./types";
|
|
3
|
+
type JourneyOptionsInput<TPlugins extends readonly JourneyMachinePlugin[]> = JourneyMachineOptions<TPlugins extends [] ? readonly JourneyMachinePlugin[] : TPlugins>;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a journey machine and returns React hooks/components bound to that machine.
|
|
6
|
+
* Hooks work without a provider; `JourneyProvider` is only required for `StepRenderer`.
|
|
7
|
+
*/
|
|
8
|
+
export declare function createJourney<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []>(definition: TDefinition, options?: JourneyOptionsInput<TPlugins>): JourneyRuntimeFromDefinition<TDefinition, TPlugins>;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a typed factory for producing fresh React-bound journey runtimes.
|
|
11
|
+
* Use this when a component or route boundary needs independent instances
|
|
12
|
+
* from the same definition/options pair.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createJourneyFactory<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []>(definition: TDefinition, options?: JourneyOptionsInput<TPlugins>): JourneyRuntimeFactoryFromDefinition<TDefinition, TPlugins>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { JourneyMachineOptions, JourneyMachinePlugin } from "@rxova/journey-core";
|
|
2
|
+
import type { JourneyRuntimeFactoryFromDefinition, JourneyRuntimeFromDefinition } from "./types";
|
|
3
|
+
type JourneyOptionsInput<TPlugins extends readonly JourneyMachinePlugin[]> = JourneyMachineOptions<TPlugins extends [] ? readonly JourneyMachinePlugin[] : TPlugins>;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a journey machine and returns React hooks/components bound to that machine.
|
|
6
|
+
* Hooks work without a provider; `JourneyProvider` is only required for `StepRenderer`.
|
|
7
|
+
*/
|
|
8
|
+
export declare function createJourney<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []>(definition: TDefinition, options?: JourneyOptionsInput<TPlugins>): JourneyRuntimeFromDefinition<TDefinition, TPlugins>;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a typed factory for producing fresh React-bound journey runtimes.
|
|
11
|
+
* Use this when a component or route boundary needs independent instances
|
|
12
|
+
* from the same definition/options pair.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createJourneyFactory<TDefinition, TPlugins extends readonly JourneyMachinePlugin[] = []>(definition: TDefinition, options?: JourneyOptionsInput<TPlugins>): JourneyRuntimeFactoryFromDefinition<TDefinition, TPlugins>;
|
|
15
|
+
export {};
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";var w=Object.create;var x=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var H=Object.getPrototypeOf,D=Object.prototype.hasOwnProperty;var j=(n,r)=>{for(var s in r)x(n,s,{get:r[s],enumerable:!0})},M=(n,r,s,y)=>{if(r&&typeof r=="object"||typeof r=="function")for(let c of F(r))!D.call(n,c)&&c!==s&&x(n,c,{get:()=>r[c],enumerable:!(y=k(r,c))||y.enumerable});return n};var E=(n,r,s)=>(s=n!=null?w(H(n)):{},M(r||!n||!n.__esModule?x(s,"default",{value:n,enumerable:!0}):s,n)),A=n=>M(x({},"__esModule",{value:!0}),n);var B={};j(B,{createJourney:()=>b,createJourneyFactory:()=>I});module.exports=A(B);var C=require("@rxova/journey-core");var d=E(require("react"),1),l=require("react/jsx-runtime"),m=typeof window>"u"?d.default.useEffect:d.default.useLayoutEffect,L=(n,r,s)=>{if(s){s(n,r);return}console.error(`JourneyProvider ${r.phase} failed.`,n)},h=(n,r)=>{let s=d.default.createContext(null),y=(a="hook")=>{let e=d.default.useContext(s);if(!e)throw new Error(`${a} must be used within JourneyProvider.`);return e},c=({runtimeMachine:a,onError:e,disposeOnUnmount:t})=>{let o=d.default.useRef(e),u=d.default.useRef(null);o.current=e,m(()=>{u.current!==null&&(globalThis.clearTimeout(u.current),u.current=null)});let J=d.default.useCallback(i=>i.status,[a]),S=d.default.useCallback(i=>a.subscribeSelector(J,()=>{i()}),[a,J]),v=d.default.useCallback(()=>a.getSnapshot().status,[a]),T=d.default.useSyncExternalStore(S,v,v);return m(()=>{T==="idled"&&a.startJourney().catch(i=>{L(i,{phase:"start"},o.current)})},[a,T]),m(()=>{if(t)return()=>{u.current=globalThis.setTimeout(()=>{u.current=null,a.dispose()},0)}},[a,t]),null};return{JourneyProvider:({views:a,onError:e,disposeOnUnmount:t=!1,children:o})=>{let u=n;return(0,l.jsxs)(s.Provider,{value:a,children:[o,(0,l.jsx)(c,{runtimeMachine:u,onError:e,disposeOnUnmount:t})]})},StepRenderer:({fallback:a=null})=>{let e=r(J=>J.currentStepId),o=y("StepRenderer")[e];if(!o)return(0,l.jsx)(l.Fragment,{children:a});let u=o;return(0,l.jsx)(d.default.Fragment,{children:(0,l.jsx)(u,{})},e)}}};var p=E(require("react"),1),q=typeof window>"u"?p.default.useEffect:p.default.useLayoutEffect,P=n=>{let r=()=>{let e=n,t=p.default.useCallback(()=>e.getSnapshot(),[e]),o=p.default.useCallback(u=>e.subscribe(u),[e]);return p.default.useSyncExternalStore(o,t,t)},s=()=>{let e=r(),t=n;return p.default.useMemo(()=>t.getComputed(),[t,e])},y=(e,t)=>{let o=n,u=t??Object.is,J=p.default.useRef(null),S=p.default.useCallback(()=>{let T=o.getSnapshot(),i=J.current;if(i&&Object.is(i.machine,o)&&Object.is(i.selector,e)&&Object.is(i.isEqual,u)&&Object.is(i.snapshot,T))return i.selected;let f=e(T);return i&&Object.is(i.machine,o)&&Object.is(i.selector,e)&&Object.is(i.isEqual,u)&&u(i.selected,f)?(J.current={machine:o,snapshot:T,selected:i.selected,selector:e,isEqual:u},i.selected):(J.current={machine:o,snapshot:T,selected:f,selector:e,isEqual:u},f)},[o,u,e]),v=p.default.useCallback(T=>o.subscribeSelector(e,()=>{T()},u),[o,u,e]);return p.default.useSyncExternalStore(v,S,S)},c=e=>{let t=n,o=p.default.useRef(e);o.current=e,q(()=>t.subscribeEvent(u=>{o.current(u)}),[t])};return{useJourneySnapshot:r,useJourneyComputed:s,useJourneySelector:y,useJourneyApi:()=>{let e=n;return p.default.useMemo(()=>({startJourney:e.startJourney,send:e.send,goToNextStep:e.goToNextStep,goToStepById:e.goToStepById,terminateJourney:e.terminateJourney,completeJourney:e.completeJourney,goToPreviousStep:e.goToPreviousStep,goToLastVisitedStep:e.goToLastVisitedStep,clearStepError:e.clearStepError,updateContext:e.updateContext,getStepMeta:e.getStepMeta,resetJourney:()=>e.resetJourney()}),[e])},useStepApi:e=>{let t=n;return p.default.useMemo(()=>({startJourney:t.startJourney,send:t.send,goToNextStep:t.goToNextStep,goToStepById:t.goToStepById,terminateJourney:t.terminateJourney,completeJourney:t.completeJourney,goToPreviousStep:t.goToPreviousStep,goToLastVisitedStep:t.goToLastVisitedStep,clearStepError:t.clearStepError,updateContext:t.updateContext,getStepMeta:t.getStepMeta,resetJourney:()=>t.resetJourney()}),[t])},useJourneyEvent:c,useJourneyStepLifecycle:(e,t)=>{c(o=>{o.type==="step.enter"&&o.stepId===e?t.onEnter?.({context:n.getSnapshot().context}):o.type==="step.exit"&&o.stepId===e&&t.onLeave?.({context:n.getSnapshot().context})})}}};var R=(n,r)=>{let y=(0,C.createJourneyMachine)(n,r),c=P(y),g=h(y,c.useJourneySelector);return{machine:y,dispose:()=>y.dispose(),...c,...g}};function b(n,r){return R(n,r)}function I(n,r){let s=n;return()=>R(s,r)}0&&(module.exports={createJourney,createJourneyFactory});
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|