react-intent-engine-z 1.0.0 → 2.0.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 +78 -98
- package/build/index.cjs +1 -1
- package/build/index.esm.js +1 -1
- package/build/index.umd.min.js +1 -1
- package/build/react/IntentProvider.d.ts +7 -2
- package/build/react/useIntentState.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,71 +1,57 @@
|
|
|
1
1
|
## 📘 react-intent-engine-z
|
|
2
2
|
|
|
3
|
-
-
|
|
4
|
-
-
|
|
3
|
+
[](https://www.npmjs.com/package/react-intent-engine-z)
|
|
4
|
+

|
|
5
5
|
|
|
6
6
|
---
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
An **intent-first orchestration engine for React**.
|
|
9
|
+
Decouples UI from async flows, side effects, and business logic.
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
[Live Example](https://codesandbox.io/p/sandbox/kjstrf)
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
- Complex async flows spread across hooks
|
|
15
|
-
|
|
16
|
-
- Hard-to-test side effects
|
|
17
|
-
|
|
18
|
-
- Overloaded global stores
|
|
19
|
-
|
|
20
|
-
- react-intent-engine solves this by introducing an intent-driven architecture:
|
|
21
|
-
|
|
22
|
-
- Components declare what they want to do — the engine decides how it happens.
|
|
23
|
-
|
|
24
|
-
- Works great with:
|
|
25
|
-
|
|
26
|
-
- react-scoped-store
|
|
13
|
+
---
|
|
27
14
|
|
|
28
|
-
|
|
15
|
+
### ✨ Why Use It
|
|
29
16
|
|
|
30
|
-
|
|
17
|
+
- Separate business logic from UI
|
|
18
|
+
- Handle async flows declaratively
|
|
19
|
+
- Easy testing of intents
|
|
20
|
+
- Supports multiple engines & scoped state
|
|
21
|
+
- Works with React 17+, React Query, DDD-style architectures
|
|
31
22
|
|
|
32
23
|
---
|
|
33
24
|
|
|
34
|
-
|
|
35
|
-
|
|
25
|
+
### 📦 Installation
|
|
26
|
+
|
|
27
|
+
```bash
|
|
36
28
|
npm install react-intent-engine-z use-sync-external-store
|
|
37
29
|
```
|
|
38
|
-
|
|
39
30
|
- use-sync-external-store is required for React 17 compatibility.
|
|
40
31
|
|
|
41
32
|
---
|
|
42
33
|
|
|
43
|
-
|
|
44
|
-
```
|
|
45
|
-
UI
|
|
46
|
-
└─
|
|
47
|
-
|
|
34
|
+
### 🧠 Mental Model
|
|
35
|
+
```bash
|
|
36
|
+
UI Component
|
|
37
|
+
└─ emits intent
|
|
38
|
+
↓
|
|
48
39
|
Intent Engine
|
|
49
40
|
├─ async orchestration
|
|
50
41
|
├─ business rules
|
|
51
42
|
└─ side effects
|
|
52
|
-
|
|
43
|
+
```
|
|
53
44
|
|
|
54
45
|
---
|
|
55
46
|
|
|
56
|
-
|
|
57
|
-
##### 1️⃣ Create engine
|
|
47
|
+
### 🚀 Basic Usage
|
|
58
48
|
|
|
49
|
+
##### 1️⃣ Create Engine
|
|
59
50
|
```ts
|
|
60
51
|
import { createIntentEngine } from "react-intent-engine-z"
|
|
61
52
|
|
|
62
|
-
export const
|
|
63
|
-
initialState: {
|
|
64
|
-
auth: {
|
|
65
|
-
user: null,
|
|
66
|
-
loading: false,
|
|
67
|
-
},
|
|
68
|
-
},
|
|
53
|
+
export const authEngine = createIntentEngine({
|
|
54
|
+
initialState: { auth: { user: null, loading: false } },
|
|
69
55
|
effects: {
|
|
70
56
|
auth: {
|
|
71
57
|
login: async ({ email, password }) => {
|
|
@@ -77,29 +63,26 @@ export const engine = createIntentEngine({
|
|
|
77
63
|
})
|
|
78
64
|
```
|
|
79
65
|
|
|
80
|
-
##### 2️⃣ Register
|
|
66
|
+
##### 2️⃣ Register Handlers
|
|
81
67
|
```ts
|
|
82
|
-
|
|
68
|
+
authEngine.on("auth.login.submit", async (intent, ctx) => {
|
|
83
69
|
ctx.set("auth.loading", true)
|
|
84
|
-
|
|
85
70
|
const user = await ctx.effects.auth.login(intent.payload)
|
|
86
|
-
|
|
87
71
|
ctx.set("auth.user", user)
|
|
88
72
|
ctx.set("auth.loading", false)
|
|
89
73
|
})
|
|
90
74
|
```
|
|
75
|
+
##### 3️⃣ Provide Engine to React
|
|
91
76
|
|
|
92
|
-
##### 3️⃣ Provide engine to React
|
|
93
77
|
```ts
|
|
94
78
|
import { IntentProvider } from "react-intent-engine-z"
|
|
95
|
-
|
|
96
|
-
<
|
|
97
|
-
<App />
|
|
79
|
+
<IntentProvider engine={authEngine}>
|
|
80
|
+
<LoginForm />
|
|
98
81
|
</IntentProvider>
|
|
99
82
|
```
|
|
100
83
|
|
|
101
|
-
##### 4️⃣ Emit
|
|
102
|
-
```
|
|
84
|
+
##### 4️⃣ Emit Intent from UI
|
|
85
|
+
```tsx
|
|
103
86
|
import { useIntent, useIntentState } from "react-intent-engine-z"
|
|
104
87
|
|
|
105
88
|
function LoginButton() {
|
|
@@ -109,92 +92,89 @@ function LoginButton() {
|
|
|
109
92
|
return (
|
|
110
93
|
<button
|
|
111
94
|
disabled={loading}
|
|
112
|
-
onClick={() =>
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
})
|
|
117
|
-
}
|
|
95
|
+
onClick={() => emit({
|
|
96
|
+
type: "auth.login.submit",
|
|
97
|
+
payload: { email: "a@b.com", password: "123" }
|
|
98
|
+
})}
|
|
118
99
|
>
|
|
119
|
-
Login
|
|
100
|
+
{loading ? "Logging in..." : "Login"}
|
|
120
101
|
</button>
|
|
121
102
|
)
|
|
122
103
|
}
|
|
123
104
|
```
|
|
124
105
|
|
|
125
|
-
|
|
106
|
+
---
|
|
126
107
|
|
|
127
|
-
|
|
128
|
-
- Track lifecycle state of an intent:
|
|
108
|
+
### 🔄 Track Intent Status
|
|
129
109
|
```ts
|
|
130
110
|
import { useIntentStatus } from "react-intent-engine-z"
|
|
131
111
|
|
|
132
|
-
const status = useIntentStatus("auth.login.submit")
|
|
133
|
-
// idle | pending | success
|
|
112
|
+
const status = useIntentStatus("auth.login.submit") // idle | pending | success
|
|
134
113
|
```
|
|
135
114
|
|
|
136
115
|
---
|
|
137
116
|
|
|
138
|
-
|
|
117
|
+
### 🧩 Middleware Example
|
|
139
118
|
```ts
|
|
140
|
-
|
|
141
|
-
email: "",
|
|
142
|
-
password: "",
|
|
143
|
-
}))
|
|
119
|
+
import { Middleware, createIntentEngine } from "react-intent-engine-z"
|
|
144
120
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
121
|
+
// Logs every intent
|
|
122
|
+
const logger: Middleware = async (intent, next, ctx) => {
|
|
123
|
+
console.log("[Logger]", intent.type, intent.payload)
|
|
124
|
+
await next()
|
|
125
|
+
}
|
|
148
126
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
/>
|
|
158
|
-
)
|
|
127
|
+
// Blocks auth intents if not logged in
|
|
128
|
+
const authGuard: Middleware = async (intent, next, ctx) => {
|
|
129
|
+
const user = ctx.store.getState().auth.user
|
|
130
|
+
if (intent.type.startsWith("auth.") && !user) {
|
|
131
|
+
console.warn("[AuthGuard] Not logged in, blocking:", intent.type)
|
|
132
|
+
return
|
|
133
|
+
}
|
|
134
|
+
await next()
|
|
159
135
|
}
|
|
160
|
-
```
|
|
161
136
|
|
|
162
|
-
|
|
137
|
+
const engine = createIntentEngine({
|
|
138
|
+
initialState: { auth: { user: null, loading: false } },
|
|
139
|
+
effects: {
|
|
140
|
+
auth: { login: async () => ({ id: 1, email: "a@b.com" }) }
|
|
141
|
+
},
|
|
142
|
+
middleware: [logger, authGuard]
|
|
143
|
+
})
|
|
163
144
|
|
|
164
|
-
|
|
145
|
+
```
|
|
165
146
|
|
|
166
147
|
---
|
|
167
148
|
|
|
168
|
-
|
|
169
|
-
- Intent logic can be tested without React:
|
|
149
|
+
### 🧪 Testing (headless)
|
|
170
150
|
```ts
|
|
171
151
|
await engine.emit({
|
|
172
152
|
type: "auth.login.submit",
|
|
173
|
-
payload: { email: "a@b.com", password: "123" }
|
|
153
|
+
payload: { email: "a@b.com", password: "123" }
|
|
174
154
|
})
|
|
175
155
|
|
|
176
156
|
expect(engine.store.getState().auth.user).toBeDefined()
|
|
177
157
|
```
|
|
178
|
-
|
|
179
158
|
---
|
|
180
159
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
160
|
+
### ⚡ Multiple Engines
|
|
161
|
+
```tsx
|
|
162
|
+
import { IntentProvider } from "react-intent-engine-z"
|
|
184
163
|
|
|
185
|
-
|
|
164
|
+
<IntentProvider engine={authEngine}>
|
|
165
|
+
<LoginForm />
|
|
166
|
+
</IntentProvider>
|
|
186
167
|
|
|
187
|
-
|
|
168
|
+
<IntentProvider engine={notifEngine}>
|
|
169
|
+
<NotificationCenter />
|
|
170
|
+
</IntentProvider>
|
|
188
171
|
|
|
189
|
-
|
|
172
|
+
```
|
|
173
|
+
- Each engine is scoped
|
|
190
174
|
|
|
191
|
-
|
|
175
|
+
- Supports role-based UI and multiple async flows
|
|
192
176
|
|
|
193
|
-
- Hexagonal / DDD-inspired architecture
|
|
194
|
-
|
|
195
|
-
- This library focuses on behavior and flow, not data fetching or UI state.
|
|
196
177
|
---
|
|
197
178
|
|
|
198
|
-
|
|
199
|
-
|
|
179
|
+
### 📜 License
|
|
200
180
|
MIT
|
package/build/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react");function e(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var n=e(t);function r(t,e,n,r){return new(n||(n=Promise))(function(o,u){function i(t){try{c(r.next(t))}catch(t){u(t)}}function a(t){try{c(r.throw(t))}catch(t){u(t)}}function c(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(i,a)}c((r=r.apply(t,e||[])).next())})}function o(t,e){var n,r,o,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},i=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return i.next=a(0),i.throw=a(1),i.return=a(2),"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(a){return function(c){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,a[0]&&(u=0)),u;)try{if(n=1,r&&(o=2&a[0]?r.return:a[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,a[1])).done)return o;switch(r=0,o&&(a=[2&a[0],o.value]),a[0]){case 0:case 1:o=a;break;case 4:return u.label++,{value:a[1],done:!1};case 5:u.label++,r=a[1],a=[0];continue;case 7:a=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==a[0]&&2!==a[0])){u=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]<o[3])){u.label=a[1];break}if(6===a[0]&&u.label<o[1]){u.label=o[1],o=a;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(a);break}o[2]&&u.ops.pop(),u.trys.pop();continue}a=e.call(t,u)}catch(t){a=[6,t],r=0}finally{n=o=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}function u(t,e,n){if(n||2===arguments.length)for(var r,o=0,u=e.length;o<u;o++)!r&&o in e||(r||(r=Array.prototype.slice.call(e,0,o)),r[o]=e[o]);return t.concat(r||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var i=t.createContext(null)
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react");function e(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var n=e(t);function r(t,e,n,r){return new(n||(n=Promise))(function(o,u){function i(t){try{c(r.next(t))}catch(t){u(t)}}function a(t){try{c(r.throw(t))}catch(t){u(t)}}function c(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(i,a)}c((r=r.apply(t,e||[])).next())})}function o(t,e){var n,r,o,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},i=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return i.next=a(0),i.throw=a(1),i.return=a(2),"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(a){return function(c){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,a[0]&&(u=0)),u;)try{if(n=1,r&&(o=2&a[0]?r.return:a[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,a[1])).done)return o;switch(r=0,o&&(a=[2&a[0],o.value]),a[0]){case 0:case 1:o=a;break;case 4:return u.label++,{value:a[1],done:!1};case 5:u.label++,r=a[1],a=[0];continue;case 7:a=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==a[0]&&2!==a[0])){u=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]<o[3])){u.label=a[1];break}if(6===a[0]&&u.label<o[1]){u.label=o[1],o=a;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(a);break}o[2]&&u.ops.pop(),u.trys.pop();continue}a=e.call(t,u)}catch(t){a=[6,t],r=0}finally{n=o=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}function u(t,e,n){if(n||2===arguments.length)for(var r,o=0,u=e.length;o<u;o++)!r&&o in e||(r||(r=Array.prototype.slice.call(e,0,o)),r[o]=e[o]);return t.concat(r||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var i=t.createContext(null);function a(){var e=t.useContext(i);if(!e)throw new Error("Missing IntentProvider");return e}function c(t){var e={exports:{}};return t(e,e.exports),e.exports}var s="function"==typeof Object.is?Object.is:function(t,e){return t===e&&(0!==t||1/t==1/e)||t!=t&&e!=e},l=n.default.useState,f=n.default.useEffect,d=n.default.useLayoutEffect,p=n.default.useDebugValue;function v(t){var e=t.getSnapshot;t=t.value;try{var n=e();return!s(t,n)}catch(t){return!0}}var S="undefined"==typeof window||void 0===window.document||void 0===window.document.createElement?function(t,e){return e()}:function(t,e){var n=e(),r=l({inst:{value:n,getSnapshot:e}}),o=r[0].inst,u=r[1];return d(function(){o.value=n,o.getSnapshot=e,v(o)&&u({inst:o})},[t,n,e]),f(function(){return v(o)&&u({inst:o}),t(function(){v(o)&&u({inst:o})})},[t]),p(n),n},y={useSyncExternalStore:void 0!==n.default.useSyncExternalStore?n.default.useSyncExternalStore:S},h=c(function(t,e){"production"!==process.env.NODE_ENV&&function(){function t(t){var e=t.getSnapshot;t=t.value;try{var n=e();return!o(t,n)}catch(t){return!0}}"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());var r=n.default,o="function"==typeof Object.is?Object.is:function(t,e){return t===e&&(0!==t||1/t==1/e)||t!=t&&e!=e},u=r.useState,i=r.useEffect,a=r.useLayoutEffect,c=r.useDebugValue,s=!1,l=!1,f="undefined"==typeof window||void 0===window.document||void 0===window.document.createElement?function(t,e){return e()}:function(e,n){s||void 0===r.startTransition||(s=!0,console.error("You are using an outdated, pre-release alpha of React 18 that does not support useSyncExternalStore. The use-sync-external-store shim will not work correctly. Upgrade to a newer pre-release."));var f=n();if(!l){var d=n();o(f,d)||(console.error("The result of getSnapshot should be cached to avoid an infinite loop"),l=!0)}var p=(d=u({inst:{value:f,getSnapshot:n}}))[0].inst,v=d[1];return a(function(){p.value=f,p.getSnapshot=n,t(p)&&v({inst:p})},[e,f,n]),i(function(){return t(p)&&v({inst:p}),e(function(){t(p)&&v({inst:p})})},[e]),c(f),f};e.useSyncExternalStore=void 0!==r.useSyncExternalStore?r.useSyncExternalStore:f,"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error())}()}),_=c(function(t){"production"===process.env.NODE_ENV?t.exports=y:t.exports=h});exports.IntentProvider=function(t){var e=t.engine,r=t.children;return n.default.createElement(i.Provider,{value:e},r)},exports.createIntentEngine=function(t){var e,n,i,a,c,s,l=(i=t.initialState,a=structuredClone(i),c=new Set,{getState:function(){return a},setState:function(t){a=t(a),c.forEach(function(t){return t()})},subscribe:function(t){return c.add(t),function(){return c.delete(t)}}}),f=null!==(e=t.effects)&&void 0!==e?e:{},d=new Map,p=new Map,v=new Map,S=(s=null!==(n=t.middleware)&&void 0!==n?n:[],function(t,e){var n=-1;return function u(i){return r(this,void 0,void 0,function(){var r;return o(this,function(o){if(i<=n)throw new Error("next() called twice");return n=i,(r=s[i])?[2,r(t,function(){return u(i+1)})]:[2,e()]})})}(0)});return{emit:function t(e){var n;return r(this,void 0,void 0,function(){var u,i=this;return o(this,function(a){switch(a.label){case 0:return u=function(t,e,n){return{get:function(e){return e.split(".").reduce(function(t,e){return null==t?void 0:t[e]},t.getState())},set:function(e,n){t.setState(function(t){for(var r,o=structuredClone(t),u=e.split("."),i=o,a=0;a<u.length-1;a++)i=i[r=u[a]]||(i[r]={});return i[u.at(-1)]=n,o})},emit:e,effects:n}}(l,t,f),(null===(n=p.get(e.type))||void 0===n?void 0:n.some(function(t){return!t(u)}))?[2]:(v.set(e.type,"pending"),[4,S(e,function(){return r(i,void 0,void 0,function(){var t,n,r;return o(this,function(o){switch(o.label){case 0:t=0,n=null!==(r=d.get(e.type))&&void 0!==r?r:[],o.label=1;case 1:return t<n.length?[4,(0,n[t])(e,u)]:[3,4];case 2:o.sent(),o.label=3;case 3:return t++,[3,1];case 4:return[2]}})})})]);case 1:return a.sent(),v.set(e.type,"success"),[2]}})})},on:function(t,e){var n;d.set(t,u(u([],null!==(n=d.get(t))&&void 0!==n?n:[],!0),[e],!1))},guard:function(t,e){var n;p.set(t,u(u([],null!==(n=p.get(t))&&void 0!==n?n:[],!0),[e],!1))},store:l,getStatus:function(t){var e;return null!==(e=v.get(t))&&void 0!==e?e:"idle"}}},exports.useEngine=a,exports.useIntent=function(){var t=a();return function(e){return t.emit(e)}},exports.useIntentState=function(t){var e=a();return _.useSyncExternalStore(function(n){return e.store.subscribe(t,n)},function(){return e.store.getState(t)})},exports.useIntentStatus=function(t){return a().getStatus(t)};
|
package/build/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import t,{createContext as e,useContext as n}from"react";function r(t,e,n,r){return new(n||(n=Promise))(function(o,u){function i(t){try{a(r.next(t))}catch(t){u(t)}}function c(t){try{a(r.throw(t))}catch(t){u(t)}}function a(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(i,c)}a((r=r.apply(t,e||[])).next())})}function o(t,e){var n,r,o,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},i=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return i.next=c(0),i.throw=c(1),i.return=c(2),"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(c){return function(a){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,c[0]&&(u=0)),u;)try{if(n=1,r&&(o=2&c[0]?r.return:c[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,c[1])).done)return o;switch(r=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return u.label++,{value:c[1],done:!1};case 5:u.label++,r=c[1],c=[0];continue;case 7:c=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){u=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){u.label=c[1];break}if(6===c[0]&&u.label<o[1]){u.label=o[1],o=c;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(c);break}o[2]&&u.ops.pop(),u.trys.pop();continue}c=e.call(t,u)}catch(t){c=[6,t],r=0}finally{n=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,a])}}}function u(t,e,n){if(n||2===arguments.length)for(var r,o=0,u=e.length;o<u;o++)!r&&o in e||(r||(r=Array.prototype.slice.call(e,0,o)),r[o]=e[o]);return t.concat(r||Array.prototype.slice.call(e))}function i(t){var e,n,i,c,a,s,
|
|
1
|
+
import t,{createContext as e,useContext as n}from"react";function r(t,e,n,r){return new(n||(n=Promise))(function(o,u){function i(t){try{a(r.next(t))}catch(t){u(t)}}function c(t){try{a(r.throw(t))}catch(t){u(t)}}function a(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(i,c)}a((r=r.apply(t,e||[])).next())})}function o(t,e){var n,r,o,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},i=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return i.next=c(0),i.throw=c(1),i.return=c(2),"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(c){return function(a){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,c[0]&&(u=0)),u;)try{if(n=1,r&&(o=2&c[0]?r.return:c[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,c[1])).done)return o;switch(r=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return u.label++,{value:c[1],done:!1};case 5:u.label++,r=c[1],c=[0];continue;case 7:c=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){u=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){u.label=c[1];break}if(6===c[0]&&u.label<o[1]){u.label=o[1],o=c;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(c);break}o[2]&&u.ops.pop(),u.trys.pop();continue}c=e.call(t,u)}catch(t){c=[6,t],r=0}finally{n=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,a])}}}function u(t,e,n){if(n||2===arguments.length)for(var r,o=0,u=e.length;o<u;o++)!r&&o in e||(r||(r=Array.prototype.slice.call(e,0,o)),r[o]=e[o]);return t.concat(r||Array.prototype.slice.call(e))}function i(t){var e,n,i,c,a,s,l=(i=t.initialState,c=structuredClone(i),a=new Set,{getState:function(){return c},setState:function(t){c=t(c),a.forEach(function(t){return t()})},subscribe:function(t){return a.add(t),function(){return a.delete(t)}}}),f=null!==(e=t.effects)&&void 0!==e?e:{},d=new Map,p=new Map,v=new Map,S=(s=null!==(n=t.middleware)&&void 0!==n?n:[],function(t,e){var n=-1;return function u(i){return r(this,void 0,void 0,function(){var r;return o(this,function(o){if(i<=n)throw new Error("next() called twice");return n=i,(r=s[i])?[2,r(t,function(){return u(i+1)})]:[2,e()]})})}(0)});return{emit:function t(e){var n;return r(this,void 0,void 0,function(){var u,i=this;return o(this,function(c){switch(c.label){case 0:return u=function(t,e,n){return{get:function(e){return e.split(".").reduce(function(t,e){return null==t?void 0:t[e]},t.getState())},set:function(e,n){t.setState(function(t){for(var r,o=structuredClone(t),u=e.split("."),i=o,c=0;c<u.length-1;c++)i=i[r=u[c]]||(i[r]={});return i[u.at(-1)]=n,o})},emit:e,effects:n}}(l,t,f),(null===(n=p.get(e.type))||void 0===n?void 0:n.some(function(t){return!t(u)}))?[2]:(v.set(e.type,"pending"),[4,S(e,function(){return r(i,void 0,void 0,function(){var t,n,r;return o(this,function(o){switch(o.label){case 0:t=0,n=null!==(r=d.get(e.type))&&void 0!==r?r:[],o.label=1;case 1:return t<n.length?[4,(0,n[t])(e,u)]:[3,4];case 2:o.sent(),o.label=3;case 3:return t++,[3,1];case 4:return[2]}})})})]);case 1:return c.sent(),v.set(e.type,"success"),[2]}})})},on:function(t,e){var n;d.set(t,u(u([],null!==(n=d.get(t))&&void 0!==n?n:[],!0),[e],!1))},guard:function(t,e){var n;p.set(t,u(u([],null!==(n=p.get(t))&&void 0!==n?n:[],!0),[e],!1))},store:l,getStatus:function(t){var e;return null!==(e=v.get(t))&&void 0!==e?e:"idle"}}}"function"==typeof SuppressedError&&SuppressedError;var c=e(null);function a(e){var n=e.engine,r=e.children;return t.createElement(c.Provider,{value:n},r)}function s(){var t=n(c);if(!t)throw new Error("Missing IntentProvider");return t}function l(){var t=s();return function(e){return t.emit(e)}}function f(t){var e={exports:{}};return t(e,e.exports),e.exports}var d="function"==typeof Object.is?Object.is:function(t,e){return t===e&&(0!==t||1/t==1/e)||t!=t&&e!=e},p=t.useState,v=t.useEffect,S=t.useLayoutEffect,h=t.useDebugValue;function y(t){var e=t.getSnapshot;t=t.value;try{var n=e();return!d(t,n)}catch(t){return!0}}var _="undefined"==typeof window||void 0===window.document||void 0===window.document.createElement?function(t,e){return e()}:function(t,e){var n=e(),r=p({inst:{value:n,getSnapshot:e}}),o=r[0].inst,u=r[1];return S(function(){o.value=n,o.getSnapshot=e,y(o)&&u({inst:o})},[t,n,e]),v(function(){return y(o)&&u({inst:o}),t(function(){y(o)&&u({inst:o})})},[t]),h(n),n},E={useSyncExternalStore:void 0!==t.useSyncExternalStore?t.useSyncExternalStore:_},O=f(function(e,n){"production"!==process.env.NODE_ENV&&function(){function e(t){var e=t.getSnapshot;t=t.value;try{var n=e();return!o(t,n)}catch(t){return!0}}"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());var r=t,o="function"==typeof Object.is?Object.is:function(t,e){return t===e&&(0!==t||1/t==1/e)||t!=t&&e!=e},u=r.useState,i=r.useEffect,c=r.useLayoutEffect,a=r.useDebugValue,s=!1,l=!1,f="undefined"==typeof window||void 0===window.document||void 0===window.document.createElement?function(t,e){return e()}:function(t,n){s||void 0===r.startTransition||(s=!0,console.error("You are using an outdated, pre-release alpha of React 18 that does not support useSyncExternalStore. The use-sync-external-store shim will not work correctly. Upgrade to a newer pre-release."));var f=n();if(!l){var d=n();o(f,d)||(console.error("The result of getSnapshot should be cached to avoid an infinite loop"),l=!0)}var p=(d=u({inst:{value:f,getSnapshot:n}}))[0].inst,v=d[1];return c(function(){p.value=f,p.getSnapshot=n,e(p)&&v({inst:p})},[t,f,n]),i(function(){return e(p)&&v({inst:p}),t(function(){e(p)&&v({inst:p})})},[t]),a(f),f};n.useSyncExternalStore=void 0!==r.useSyncExternalStore?r.useSyncExternalStore:f,"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error())}()}),g=f(function(t){"production"===process.env.NODE_ENV?t.exports=E:t.exports=O});function w(t){var e=s();return g.useSyncExternalStore(function(n){return e.store.subscribe(t,n)},function(){return e.store.getState(t)})}function b(t){return s().getStatus(t)}export{a as IntentProvider,i as createIntentEngine,s as useEngine,l as useIntent,w as useIntentState,b as useIntentStatus};
|
package/build/index.umd.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactIntentEngine={},e.React)}(this,function(e,t){"use strict";function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=n(t);function o(e,t,n,r){return new(n||(n=Promise))(function(o,u){function i(e){try{c(r.next(e))}catch(e){u(e)}}function a(e){try{c(r.throw(e))}catch(e){u(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(i,a)}c((r=r.apply(e,t||[])).next())})}function u(e,t){var n,r,o,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},i=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return i.next=a(0),i.throw=a(1),i.return=a(2),"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(a){return function(c){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,a[0]&&(u=0)),u;)try{if(n=1,r&&(o=2&a[0]?r.return:a[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,a[1])).done)return o;switch(r=0,o&&(a=[2&a[0],o.value]),a[0]){case 0:case 1:o=a;break;case 4:return u.label++,{value:a[1],done:!1};case 5:u.label++,r=a[1],a=[0];continue;case 7:a=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==a[0]&&2!==a[0])){u=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]<o[3])){u.label=a[1];break}if(6===a[0]&&u.label<o[1]){u.label=o[1],o=a;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(a);break}o[2]&&u.ops.pop(),u.trys.pop();continue}a=t.call(e,u)}catch(e){a=[6,e],r=0}finally{n=o=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}function i(e,t,n){if(n||2===arguments.length)for(var r,o=0,u=t.length;o<u;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}"function"==typeof SuppressedError&&SuppressedError;var a=t.createContext(null);function c(){var e=t.useContext(a);if(!e)throw new Error("Missing IntentProvider");return e}function s(e){var t={exports:{}};return e(t,t.exports),t.exports}var f="function"==typeof Object.is?Object.is:function(e,t){return e===t&&(0!==e||1/e==1/t)||e!=e&&t!=t},l=r.default.useState,d=r.default.useEffect,p=r.default.useLayoutEffect,v=r.default.useDebugValue;function y(e){var t=e.getSnapshot;e=e.value;try{var n=t();return!f(e,n)}catch(e){return!0}}var h="undefined"==typeof window||void 0===window.document||void 0===window.document.createElement?function(e,t){return t()}:function(e,t){var n=t(),r=l({inst:{value:n,getSnapshot:t}}),o=r[0].inst,u=r[1];return p(function(){o.value=n,o.getSnapshot=t,y(o)&&u({inst:o})},[e,n,t]),d(function(){return y(o)&&u({inst:o}),e(function(){y(o)&&u({inst:o})})},[e]),v(n),n},S={useSyncExternalStore:void 0!==r.default.useSyncExternalStore?r.default.useSyncExternalStore:h},_=s(function(e,t){"production"!==process.env.NODE_ENV&&function(){function e(e){var t=e.getSnapshot;e=e.value;try{var n=t();return!o(e,n)}catch(e){return!0}}"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());var n=r.default,o="function"==typeof Object.is?Object.is:function(e,t){return e===t&&(0!==e||1/e==1/t)||e!=e&&t!=t},u=n.useState,i=n.useEffect,a=n.useLayoutEffect,c=n.useDebugValue,s=!1,f=!1,l="undefined"==typeof window||void 0===window.document||void 0===window.document.createElement?function(e,t){return t()}:function(t,r){s||void 0===n.startTransition||(s=!0,console.error("You are using an outdated, pre-release alpha of React 18 that does not support useSyncExternalStore. The use-sync-external-store shim will not work correctly. Upgrade to a newer pre-release."));var l=r();if(!f){var d=r();o(l,d)||(console.error("The result of getSnapshot should be cached to avoid an infinite loop"),f=!0)}var p=(d=u({inst:{value:l,getSnapshot:r}}))[0].inst,v=d[1];return a(function(){p.value=l,p.getSnapshot=r,e(p)&&v({inst:p})},[t,l,r]),i(function(){return e(p)&&v({inst:p}),t(function(){e(p)&&v({inst:p})})},[t]),c(l),l};t.useSyncExternalStore=void 0!==n.useSyncExternalStore?n.useSyncExternalStore:l,"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error())}()}),g=s(function(e){"production"===process.env.NODE_ENV?e.exports=S:e.exports=_});e.IntentProvider=function(e){var t=e.engine,n=e.children;return r.default.createElement(a.Provider,{value:t},n)},e.createIntentEngine=function(e){var t,n,r,a,c,s,f=(r=e.initialState,a=structuredClone(r),c=new Set,{getState:function(){return a},setState:function(e){a=e(a),c.forEach(function(e){return e()})},subscribe:function(e){return c.add(e),function(){return c.delete(e)}}}),l=null!==(t=e.effects)&&void 0!==t?t:{},d=new Map,p=new Map,v=new Map,y=(s=null!==(n=e.middleware)&&void 0!==n?n:[],function(e,t){var n=-1;return function r(i){return o(this,void 0,void 0,function(){var o;return u(this,function(u){if(i<=n)throw new Error("next() called twice");return n=i,(o=s[i])?[2,o(e,function(){return r(i+1)})]:[2,t()]})})}(0)});return{emit:function e(t){var n;return o(this,void 0,void 0,function(){var r,i=this;return u(this,function(a){switch(a.label){case 0:return r=function(e,t,n){return{get:function(t){return t.split(".").reduce(function(e,t){return null==e?void 0:e[t]},e.getState())},set:function(t,n){e.setState(function(e){for(var r,o=structuredClone(e),u=t.split("."),i=o,a=0;a<u.length-1;a++)i=i[r=u[a]]||(i[r]={});return i[u.at(-1)]=n,o})},emit:t,effects:n}}(f,e,l),(null===(n=p.get(t.type))||void 0===n?void 0:n.some(function(e){return!e(r)}))?[2]:(v.set(t.type,"pending"),[4,y(t,function(){return o(i,void 0,void 0,function(){var e,n,o;return u(this,function(u){switch(u.label){case 0:e=0,n=null!==(o=d.get(t.type))&&void 0!==o?o:[],u.label=1;case 1:return e<n.length?[4,(0,n[e])(t,r)]:[3,4];case 2:u.sent(),u.label=3;case 3:return e++,[3,1];case 4:return[2]}})})})]);case 1:return a.sent(),v.set(t.type,"success"),[2]}})})},on:function(e,t){var n;d.set(e,i(i([],null!==(n=d.get(e))&&void 0!==n?n:[],!0),[t],!1))},guard:function(e,t){var n;p.set(e,i(i([],null!==(n=p.get(e))&&void 0!==n?n:[],!0),[t],!1))},store:f,getStatus:function(e){var t;return null!==(t=v.get(e))&&void 0!==t?t:"idle"}}},e.useEngine=c,e.useIntent=function(){var e=c();return function(t){return e.emit(t)}},e.useIntentState=function(e){var t=c();return g.useSyncExternalStore(function(n){return t.store.subscribe(e,n)},function(){return t.store.getState(e)})},e.useIntentStatus=function(e){return c().getStatus(e)},Object.defineProperty(e,"__esModule",{value:!0})});
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import React, { ReactNode } from "react";
|
|
2
|
+
type EngineContextType = any;
|
|
3
|
+
export declare function IntentProvider({ engine, children, }: {
|
|
4
|
+
engine: EngineContextType;
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
}): React.JSX.Element;
|
|
3
7
|
export declare function useEngine(): any;
|
|
8
|
+
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function useIntentState<T>(
|
|
1
|
+
export declare function useIntentState<T = any>(intentType: string): T | undefined;
|