@djvlc/runtime-host-react 1.1.2 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,447 +1 @@
1
- // src/react-runtime.ts
2
- import { useState, useCallback, useRef } from "react";
3
- import {
4
- createRuntime
5
- } from "@djvlc/runtime-core";
6
- function useReactRuntime(options) {
7
- const [runtime, setRuntime] = useState(null);
8
- const [loading, setLoading] = useState(true);
9
- const [phase, setPhase] = useState("idle");
10
- const [page, setPage] = useState(null);
11
- const [error, setError] = useState(null);
12
- const [hostApi, setHostApi] = useState(null);
13
- const runtimeRef = useRef(null);
14
- const init = useCallback(async () => {
15
- const container = options.containerRef?.current;
16
- if (!container) {
17
- throw new Error("Container element not found");
18
- }
19
- const runtimeInstance = createRuntime({
20
- ...options,
21
- container,
22
- onError: (err) => {
23
- setError(err);
24
- options.onError?.(err);
25
- },
26
- onEvent: (event) => {
27
- options.onEvent?.(event);
28
- }
29
- });
30
- runtimeRef.current = runtimeInstance;
31
- setRuntime(runtimeInstance);
32
- runtimeInstance.onStateChange((state) => {
33
- setPhase(state.phase);
34
- setLoading(state.phase !== "ready" && state.phase !== "error");
35
- if (state.page) {
36
- setPage(state.page);
37
- }
38
- if (state.error) {
39
- setError(state.error);
40
- }
41
- });
42
- await runtimeInstance.init();
43
- setHostApi(runtimeInstance.getHostApi());
44
- }, [options]);
45
- const load = useCallback(async () => {
46
- if (!runtimeRef.current) {
47
- throw new Error("Runtime not initialized");
48
- }
49
- const result = await runtimeRef.current.load();
50
- setPage(result);
51
- setHostApi(runtimeRef.current.getHostApi());
52
- return result;
53
- }, []);
54
- const render = useCallback(async () => {
55
- if (!runtimeRef.current) {
56
- throw new Error("Runtime not initialized");
57
- }
58
- await runtimeRef.current.render();
59
- setLoading(false);
60
- }, []);
61
- const destroy = useCallback(() => {
62
- runtimeRef.current?.destroy();
63
- runtimeRef.current = null;
64
- setRuntime(null);
65
- setHostApi(null);
66
- setPage(null);
67
- setError(null);
68
- setPhase("idle");
69
- setLoading(true);
70
- }, []);
71
- const setVariable = useCallback((key, value) => {
72
- runtimeRef.current?.setVariable(key, value);
73
- }, []);
74
- const refreshData = useCallback(async (queryId) => {
75
- await runtimeRef.current?.refreshData(queryId);
76
- }, []);
77
- return {
78
- runtime,
79
- loading,
80
- phase,
81
- page,
82
- error,
83
- hostApi,
84
- init,
85
- load,
86
- render,
87
- destroy,
88
- setVariable,
89
- refreshData
90
- };
91
- }
92
-
93
- // src/context.tsx
94
- import { createContext, useContext } from "react";
95
- import { jsx } from "react/jsx-runtime";
96
- var defaultState = {
97
- phase: "idle",
98
- page: null,
99
- variables: {},
100
- queries: {},
101
- components: /* @__PURE__ */ new Map(),
102
- error: null,
103
- destroyed: false
104
- };
105
- var RuntimeContext = createContext({
106
- runtime: null,
107
- state: defaultState,
108
- hostApi: null
109
- });
110
- function RuntimeProvider({
111
- runtime,
112
- state,
113
- hostApi,
114
- children
115
- }) {
116
- const value = {
117
- runtime,
118
- state,
119
- hostApi
120
- };
121
- return /* @__PURE__ */ jsx(RuntimeContext.Provider, { value, children });
122
- }
123
- function useRuntimeContext() {
124
- const context = useContext(RuntimeContext);
125
- return context;
126
- }
127
-
128
- // src/components/DJVRenderer.tsx
129
- import { useRef as useRef2, useEffect, useState as useState2, useCallback as useCallback2 } from "react";
130
- import { jsx as jsx2, jsxs } from "react/jsx-runtime";
131
- function DJVRenderer({
132
- pageUid,
133
- apiBaseUrl,
134
- cdnBaseUrl,
135
- channel = "prod",
136
- userId,
137
- deviceId,
138
- authToken,
139
- previewToken,
140
- debug = false,
141
- enableSRI = true,
142
- onLoad,
143
- onError,
144
- onReady,
145
- loadingComponent,
146
- errorComponent,
147
- children,
148
- className,
149
- style
150
- }) {
151
- const containerRef = useRef2(null);
152
- const [state, setState] = useState2({
153
- phase: "idle",
154
- page: null,
155
- variables: {},
156
- queries: {},
157
- components: /* @__PURE__ */ new Map(),
158
- error: null,
159
- destroyed: false
160
- });
161
- const options = {
162
- pageUid,
163
- apiBaseUrl,
164
- cdnBaseUrl,
165
- channel,
166
- userId,
167
- deviceId,
168
- authToken,
169
- previewToken,
170
- debug,
171
- enableSRI,
172
- containerRef,
173
- onError: (error2) => {
174
- onError?.(error2);
175
- }
176
- };
177
- const {
178
- runtime,
179
- loading,
180
- phase: _phase,
181
- page: _page,
182
- error,
183
- hostApi,
184
- init,
185
- load,
186
- render,
187
- destroy
188
- } = useReactRuntime(options);
189
- void _phase;
190
- void _page;
191
- const initAndLoad = useCallback2(async () => {
192
- if (!containerRef.current) return;
193
- try {
194
- await init();
195
- const pageData = await load();
196
- onLoad?.(pageData);
197
- await render();
198
- onReady?.();
199
- if (runtime) {
200
- runtime.onStateChange((newState) => {
201
- setState(newState);
202
- });
203
- }
204
- } catch (err) {
205
- }
206
- }, [init, load, render, runtime, onLoad, onReady]);
207
- useEffect(() => {
208
- initAndLoad();
209
- return () => {
210
- destroy();
211
- };
212
- }, [pageUid]);
213
- const renderLoading = () => {
214
- if (!loading) return null;
215
- return loadingComponent || /* @__PURE__ */ jsxs("div", { className: "djvlc-loading", children: [
216
- /* @__PURE__ */ jsx2("div", { className: "djvlc-loading-spinner" }),
217
- /* @__PURE__ */ jsx2("span", { children: "\u52A0\u8F7D\u4E2D..." })
218
- ] });
219
- };
220
- const renderError = () => {
221
- if (!error) return null;
222
- if (typeof errorComponent === "function") {
223
- return errorComponent(error);
224
- }
225
- return errorComponent || /* @__PURE__ */ jsx2("div", { className: "djvlc-error", children: /* @__PURE__ */ jsxs("span", { children: [
226
- "\u52A0\u8F7D\u5931\u8D25\uFF1A",
227
- error.message
228
- ] }) });
229
- };
230
- return /* @__PURE__ */ jsx2(RuntimeProvider, { runtime, state, hostApi, children: /* @__PURE__ */ jsxs(
231
- "div",
232
- {
233
- ref: containerRef,
234
- className: `djvlc-renderer ${className || ""}`,
235
- style,
236
- children: [
237
- renderLoading(),
238
- renderError(),
239
- children
240
- ]
241
- }
242
- ) });
243
- }
244
-
245
- // src/components/DJVProvider.tsx
246
- import { useState as useState3, useEffect as useEffect2 } from "react";
247
- import { jsx as jsx3 } from "react/jsx-runtime";
248
- function DJVProvider({
249
- runtime,
250
- hostApi,
251
- children
252
- }) {
253
- const [state, setState] = useState3(
254
- runtime?.getState() || {
255
- phase: "idle",
256
- page: null,
257
- variables: {},
258
- queries: {},
259
- components: /* @__PURE__ */ new Map(),
260
- error: null,
261
- destroyed: false
262
- }
263
- );
264
- useEffect2(() => {
265
- if (!runtime) return;
266
- const unsubscribe = runtime.onStateChange((newState) => {
267
- setState(newState);
268
- });
269
- return () => {
270
- unsubscribe();
271
- };
272
- }, [runtime]);
273
- return /* @__PURE__ */ jsx3(RuntimeProvider, { runtime, state, hostApi, children: /* @__PURE__ */ jsx3("div", { className: "djvlc-provider", children }) });
274
- }
275
-
276
- // src/hooks/useDJVRuntime.ts
277
- import { useCallback as useCallback3, useState as useState4, useEffect as useEffect3, useMemo } from "react";
278
- function useDJVRuntime() {
279
- const context = useRuntimeContext();
280
- const loading = useMemo(() => {
281
- const phase = context.state.phase;
282
- return phase !== "ready" && phase !== "error";
283
- }, [context.state.phase]);
284
- return {
285
- runtime: context.runtime,
286
- state: context.state,
287
- loading,
288
- phase: context.state.phase,
289
- error: context.state.error,
290
- page: context.state.page
291
- };
292
- }
293
- function useHostApi() {
294
- const context = useRuntimeContext();
295
- if (!context.hostApi) {
296
- throw new Error("HostAPI not available. Make sure runtime is initialized.");
297
- }
298
- return context.hostApi;
299
- }
300
- function useRuntimeState(key) {
301
- const context = useRuntimeContext();
302
- return context.state.variables[key];
303
- }
304
- function useRuntimeStateWritable(key, defaultValue) {
305
- const context = useRuntimeContext();
306
- const value = context.state.variables[key] ?? defaultValue;
307
- const setValue = useCallback3(
308
- (newValue) => {
309
- context.runtime?.setVariable(key, newValue);
310
- },
311
- [context.runtime, key]
312
- );
313
- return [value, setValue];
314
- }
315
- function useQuery(queryId) {
316
- const context = useRuntimeContext();
317
- const [loading, setLoading] = useState4(false);
318
- const [error, setError] = useState4(null);
319
- const data = context.state.queries[queryId];
320
- const refetch = useCallback3(async () => {
321
- setLoading(true);
322
- setError(null);
323
- try {
324
- await context.runtime?.refreshData(queryId);
325
- } catch (e) {
326
- setError(e);
327
- } finally {
328
- setLoading(false);
329
- }
330
- }, [context.runtime, queryId]);
331
- return {
332
- data,
333
- loading,
334
- error,
335
- refetch
336
- };
337
- }
338
- function useAction(actionType) {
339
- const context = useRuntimeContext();
340
- const [loading, setLoading] = useState4(false);
341
- const [result, setResult] = useState4();
342
- const [error, setError] = useState4(null);
343
- const execute = useCallback3(
344
- async (params) => {
345
- const hostApi = context.hostApi;
346
- if (!hostApi) {
347
- throw new Error("HostAPI not available");
348
- }
349
- setLoading(true);
350
- setError(null);
351
- try {
352
- const response = await hostApi.executeAction(
353
- actionType,
354
- params
355
- );
356
- if (response.success) {
357
- setResult(response.data);
358
- return response.data;
359
- } else {
360
- throw new Error(response.message || "Action failed");
361
- }
362
- } catch (e) {
363
- setError(e);
364
- throw e;
365
- } finally {
366
- setLoading(false);
367
- }
368
- },
369
- [context.hostApi, actionType]
370
- );
371
- return {
372
- execute,
373
- loading,
374
- result,
375
- error
376
- };
377
- }
378
- function useData(queryId, params, options) {
379
- const context = useRuntimeContext();
380
- const [data, setData] = useState4();
381
- const [loading, setLoading] = useState4(false);
382
- const [error, setError] = useState4(null);
383
- const refetch = useCallback3(
384
- async (newParams) => {
385
- const hostApi = context.hostApi;
386
- if (!hostApi) {
387
- throw new Error("HostAPI not available");
388
- }
389
- setLoading(true);
390
- setError(null);
391
- try {
392
- const response = await hostApi.requestData(
393
- queryId,
394
- newParams || params
395
- );
396
- if (response.success) {
397
- setData(response.data);
398
- } else {
399
- throw new Error(response.message || "Query failed");
400
- }
401
- } catch (e) {
402
- setError(e);
403
- } finally {
404
- setLoading(false);
405
- }
406
- },
407
- [context.hostApi, queryId, params]
408
- );
409
- useEffect3(() => {
410
- if (options?.immediate !== false && context.hostApi) {
411
- refetch();
412
- }
413
- }, [context.hostApi]);
414
- return {
415
- data,
416
- loading,
417
- error,
418
- refetch
419
- };
420
- }
421
- function useRuntimeEvent(eventType, handler) {
422
- const context = useRuntimeContext();
423
- useEffect3(() => {
424
- const unsubscribe = context.runtime?.on(eventType, (event) => {
425
- handler(event.data);
426
- });
427
- return () => {
428
- unsubscribe?.();
429
- };
430
- }, [context.runtime, eventType, handler]);
431
- }
432
- export {
433
- DJVProvider,
434
- DJVRenderer,
435
- RuntimeContext,
436
- RuntimeProvider,
437
- useAction,
438
- useDJVRuntime,
439
- useData,
440
- useHostApi,
441
- useQuery,
442
- useReactRuntime,
443
- useRuntimeContext,
444
- useRuntimeEvent,
445
- useRuntimeState,
446
- useRuntimeStateWritable
447
- };
1
+ import{createContext as e,useState as r,useRef as n,useMemo as t,useCallback as a,useContext as o,useEffect as i}from"react";import{createRuntime as l}from"@djvlc/runtime-core";import{jsx as s,jsxs as c}from"react/jsx-runtime";var u={initTime:0,loadTime:0,renderTime:0,totalTime:0,initTimestamp:null,readyTimestamp:null};function d(e){const[o,i]=r(null),[s,c]=r(!0),[d,m]=r("idle"),[f,h]=r(null),[p,w]=r(null),[y,v]=r(null),[g,T]=r(u),b=n(null),E=n({startTime:0,initStartTime:0,loadStartTime:0,renderStartTime:0}),A=t(()=>"ready"===d,[d]),I=t(()=>"error"===d||null!==p,[d,p]),N=a(async()=>{const r=e.containerRef?.current;if(!r)throw new Error("Container element not found");E.current.startTime=performance.now(),E.current.initStartTime=E.current.startTime,e.enableMetrics&&T(e=>({...e,initTimestamp:Date.now()}));const n=l({...e,container:r,onError:r=>{w(r),e.onError?.(r)},onEvent:r=>{e.onEvent?.(r)}});b.current=n,i(n),n.onStateChange(e=>{m(e.phase),c("ready"!==e.phase&&"error"!==e.phase),e.page&&h(e.page),e.error&&w(e.error)}),await n.init(),v(n.getHostApi()),e.enableMetrics&&T(e=>({...e,initTime:performance.now()-E.current.initStartTime}))},[e]),j=a(async()=>{if(!b.current)throw new Error("Runtime not initialized");E.current.loadStartTime=performance.now();const r=await b.current.load();return h(r),v(b.current.getHostApi()),e.enableMetrics&&T(e=>({...e,loadTime:performance.now()-E.current.loadStartTime})),r},[e.enableMetrics]),R=a(async()=>{if(!b.current)throw new Error("Runtime not initialized");if(E.current.renderStartTime=performance.now(),await b.current.render(),c(!1),e.enableMetrics){const e=performance.now();T(r=>({...r,renderTime:e-E.current.renderStartTime,totalTime:e-E.current.startTime,readyTimestamp:Date.now()}))}},[e.enableMetrics]),C=a(()=>{b.current?.destroy(),b.current=null,i(null),v(null),h(null),w(null),m("idle"),c(!0),T(u)},[]),P=a(async()=>{if(!b.current)throw new Error("Runtime not initialized");w(null),c(!0),await j(),await R()},[j,R]),x=a((e,r)=>{b.current?.setVariable(e,r)},[]),S=a(e=>{b.current&&Object.entries(e).forEach(([e,r])=>{b.current?.setVariable(e,r)})},[]),U=a(e=>b.current?.getState().variables[e],[]),k=a(async e=>{await(b.current?.refreshData(e))},[]),B=a(async(e,r)=>{const n=y;if(!n)throw new Error("HostAPI not available");const t=await n.executeAction(e,r||{});if(t.success)return t.data;throw new Error(t.errorMessage||"Action failed")},[y]);return{runtime:o,loading:s,phase:d,page:f,error:p,hostApi:y,isReady:A,hasError:I,metrics:g,init:N,load:j,render:R,destroy:C,reload:P,setVariable:x,setVariables:S,getVariable:U,refreshData:k,executeAction:B}}var m=e({runtime:null,state:{phase:"idle",page:null,variables:{},queries:{},components:new Map,error:null,destroyed:!1},hostApi:null});function f({runtime:e,state:r,hostApi:n,children:t}){const a={runtime:e,state:r,hostApi:n};return s(m.Provider,{value:a,children:t})}function h(){return o(m)}function p({pageUid:e,apiBaseUrl:t,cdnBaseUrl:o,channel:l="prod",userId:u,deviceId:m,authToken:h,previewToken:p,debug:w=!1,enableSRI:y=!0,onLoad:v,onError:g,onReady:T,onPhaseChange:b,loadingComponent:E,errorComponent:A,emptyComponent:I,children:N,className:j,style:R,retryCount:C=3,retryDelay:P=1e3,timeout:x=3e4}){const S=n(null),U=n(!0),k=n(0),[B,D]=r({phase:"idle",page:null,variables:{},queries:{},components:new Map,error:null,destroyed:!1}),M={pageUid:e,apiBaseUrl:t,cdnBaseUrl:o,channel:l,userId:u,deviceId:m,authToken:h,previewToken:p,debug:w,enableSRI:y,containerRef:S,onError:e=>{g?.(e)}},{runtime:V,loading:z,phase:L,page:q,error:H,hostApi:O,init:J,load:F,render:$,destroy:G}=d(M);i(()=>{b?.(L)},[L,b]);const K=a(async()=>{if(!S.current||!U.current)return;const e=new Promise((e,r)=>{setTimeout(()=>r(new Error("加载超时")),x)});try{await Promise.race([(async()=>{await J();const e=await F();U.current&&v?.(e),await $(),U.current&&(T?.(),k.current=0,V&&V.onStateChange(e=>{U.current&&D(e)}))})(),e])}catch(e){if(!U.current)return;k.current<C&&(k.current++,setTimeout(()=>{U.current&&K()},P))}},[J,F,$,V,v,T,x,C,P,w]),Q=a(()=>{k.current=0,K()},[K]);return i(()=>(U.current=!0,K(),()=>{U.current=!1,G()}),[e]),s(f,{runtime:V,state:B,hostApi:O,children:c("div",{ref:S,className:`djvlc-renderer ${j||""}`,style:R,"data-phase":L,"data-page-uid":e,children:[z?E||c("div",{className:"djvlc-loading",children:[s("div",{className:"djvlc-loading-spinner"}),s("span",{children:"加载中..."}),k.current>0&&c("span",{className:"djvlc-loading-retry",children:["重试 ",k.current,"/",C]})]}):null,H?"function"==typeof A?A(H,Q):A||c("div",{className:"djvlc-error",children:[s("div",{className:"djvlc-error-icon",children:"⚠️"}),c("span",{className:"djvlc-error-message",children:["加载失败:",H.message]}),s("button",{className:"djvlc-error-retry-btn",onClick:Q,type:"button",children:"重试"})]}):null,z||H||q||"ready"!==L||q?null:I||s("div",{className:"djvlc-empty",children:s("span",{children:"暂无内容"})}),N]})})}var w={phase:"idle",page:null,variables:{},queries:{},components:new Map,error:null,destroyed:!1};function y({runtime:e,hostApi:t,children:a,className:o,debug:l=!1,onStateChange:c,onPhaseChange:u,onError:d}){const[m,h]=r(e?.getState()||w),p=n(m.phase);return i(()=>{if(!e)return void h(w);h(e.getState());const r=e.onStateChange(e=>{h(e),c?.(e),e.phase!==p.current&&(u?.(e.phase),p.current=e.phase),e.error&&d?.(e.error)});return()=>{r()}},[e,c,u,d,l]),i(()=>{},[t]),s(f,{runtime:e,state:m,hostApi:t,children:s("div",{className:["djvlc-provider",o].filter(Boolean).join(" "),"data-phase":m.phase,children:a})})}function v(){const e=h(),r=t(()=>{const r=e.state.phase;return"ready"!==r&&"error"!==r},[e.state.phase]),n="ready"===e.state.phase,o="error"===e.state.phase||null!==e.state.error,i=a(async()=>{if(!e.runtime)throw new Error("Runtime not available");await e.runtime.load(),await e.runtime.render()},[e.runtime]);return{runtime:e.runtime,state:e.state,loading:r,phase:e.state.phase,error:e.state.error,page:e.state.page,isReady:n,hasError:o,reload:i}}function g(){const e=h();if(!e.hostApi)throw new Error("HostAPI not available. Make sure runtime is initialized.");return e.hostApi}function T(e){return h().state.variables[e]}function b(e,r){const n=h();return[n.state.variables[e]??r,a(r=>{n.runtime?.setVariable(e,r)},[n.runtime,e])]}function E(e,t){const o=h(),[l,s]=r(!1),[c,u]=r(null),[d,m]=r(null),f=n(!0),p=o.state.queries[e],w=a(async()=>{if(f.current){s(!0),u(null);try{await(o.runtime?.refreshData(e)),f.current&&m(Date.now())}catch(e){f.current&&u(e)}finally{f.current&&s(!1)}}},[o.runtime,e]);return i(()=>(f.current=!0,t?.refreshOnMount&&o.runtime&&w(),()=>{f.current=!1}),[]),i(()=>{if(!t?.refreshInterval||t.refreshInterval<=0)return;const e=setInterval(()=>{w()},t.refreshInterval);return()=>clearInterval(e)},[t?.refreshInterval,w]),i(()=>{if(!t?.refreshOnFocus)return;const e=()=>{w()};return window.addEventListener("focus",e),()=>window.removeEventListener("focus",e)},[t?.refreshOnFocus,w]),{data:p,loading:l,error:c,refetch:w,lastUpdated:d}}function A(e,t){const o=h(),[l,s]=r(!1),[c,u]=r(),[d,m]=r(null),[f,p]=r(0),w=n(!0);i(()=>(w.current=!0,()=>{w.current=!1}),[]);const y=a(()=>{u(void 0),m(null),p(0)},[]),v=a(async(r,n)=>{const a=o.hostApi;if(!a)throw new Error("HostAPI not available");try{const n=await a.executeAction(e,r);if(n.success)return n.data;throw new Error(n.errorMessage||"Action failed")}catch(e){if(n>0)return await new Promise(e=>setTimeout(e,t?.retryDelay||1e3)),v(r,n-1);throw e}},[o.hostApi,e,t?.retryDelay]);return{execute:a(async e=>{if(w.current){s(!0),m(null),p(e=>e+1);try{const r=await v(e,t?.retryCount||0);return w.current&&(u(r),t?.onSuccess?.(r)),r}catch(e){const r=e;throw w.current&&(m(r),t?.onError?.(r)),e}finally{w.current&&s(!1)}}},[v,t]),loading:l,result:c,error:d,reset:y,executionCount:f}}function I(e,t,o){const l=h(),[s,c]=r(),[u,d]=r(!1),[m,f]=r(null),[p,w]=r(!1),y=n(!0),v=n(t);i(()=>(y.current=!0,()=>{y.current=!1}),[]);const g=a(async r=>{const n=l.hostApi;if(n&&y.current){d(!0),f(null);try{const t=await n.requestData(e,r||v.current);if(!y.current)return;c(t),w(!0),o?.onSuccess?.(t)}catch(e){if(y.current){const r=e;f(r),o?.onError?.(r)}}finally{y.current&&d(!1)}}},[l.hostApi,e,o]);return i(()=>{const e=v.current;v.current=t,!1!==o?.refreshOnParamsChange&&l.hostApi&&p&&JSON.stringify(e)!==JSON.stringify(t)&&g()},[t,l.hostApi,o?.refreshOnParamsChange,p,g]),i(()=>{!1!==o?.immediate&&l.hostApi&&g()},[l.hostApi]),i(()=>{if(!o?.refreshInterval||o.refreshInterval<=0)return;const e=setInterval(()=>{g()},o.refreshInterval);return()=>clearInterval(e)},[o?.refreshInterval,g]),{data:s,loading:u,error:m,refetch:g,isFetched:p}}function N(e,r){const n=h();i(()=>{const t=n.runtime?.on(e,e=>{r(e.data)});return()=>{t?.()}},[n.runtime,e,r])}function j(){const e=h().state.page;return t(()=>({pageUid:e?.pageUid,pageVersionId:e?.pageVersionId,schemaVersion:e?.pageJson?.schemaVersion,title:e?.title,config:e?.config,isLoaded:null!==e}),[e])}function R(e){const r=h().state.components.get(e);return t(()=>({isLoaded:"loaded"===r?.status,isLoading:"loading"===r?.status,hasError:"failed"===r?.status,loadTime:r?.loadTime,info:r}),[r])}function C(e){const t=h(),[a,o]=r(!1),[l,s]=r(!1),c=n(t.state.phase),u=n(!0);return i(()=>(u.current=!0,()=>{u.current=!1}),[]),i(()=>{const r=t.state.phase;r!==c.current&&(e?.onPhaseChange?.(r),c.current=r),a||"idle"===r||(o(!0),Promise.resolve(e?.onMounted?.()).catch(r=>{e?.onError?.(r)})),l||"ready"!==r||(s(!0),Promise.resolve(e?.onReady?.()).catch(r=>{e?.onError?.(r)})),"error"===r&&t.state.error&&e?.onError?.(t.state.error)},[t.state.phase,t.state.error,a,l,e]),{phase:t.state.phase,hasMounted:a,hasReady:l}}function P(e,n,t){const[a,o]=r(!1),{once:l=!0}=t||{};return i(()=>{!e||l&&a||(o(!0),n())},[e,n,l,a]),{executed:a}}function x(e,r=300){const{execute:t,loading:o,result:l,error:s}=A(e),c=n(null),u=n(!0);return i(()=>(u.current=!0,()=>{u.current=!1,c.current&&clearTimeout(c.current)}),[]),{execute:a(e=>{c.current&&clearTimeout(c.current),c.current=setTimeout(()=>{u.current&&t(e).catch(()=>{})},r)},[t,r]),loading:o,result:l,error:s,cancel:a(()=>{c.current&&(clearTimeout(c.current),c.current=null)},[])}}function S(e,t=[],o){const[l,s]=r(),[c,u]=r(!1),[d,m]=r(null),f=n(!0);i(()=>(f.current=!0,()=>{f.current=!1}),[]);const h=a(async()=>{if(f.current){u(!0),m(null);try{const r=await e();return f.current&&(s(r),o?.onSuccess?.(r)),r}catch(e){const r=e;return void(f.current&&(m(r),o?.onError?.(r)))}finally{f.current&&u(!1)}}},[e,...t]);return i(()=>{!1!==o?.immediate&&h()},[]),{data:l,loading:c,error:d,execute:h}}function U(e){const r=n();return i(()=>{r.current=e},[e]),r.current}export{y as DJVProvider,p as DJVRenderer,m as RuntimeContext,f as RuntimeProvider,A as useAction,S as useAsync,R as useComponentState,v as useDJVRuntime,I as useData,x as useDebouncedAction,g as useHostApi,C as useLifecycle,j as usePageInfo,U as usePrevious,E as useQuery,d as useReactRuntime,h as useRuntimeContext,N as useRuntimeEvent,T as useRuntimeState,b as useRuntimeStateWritable,P as useWhen};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@djvlc/runtime-host-react",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "DJV 低代码平台 React 宿主适配器",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -17,8 +17,8 @@
17
17
  "dist"
18
18
  ],
19
19
  "scripts": {
20
- "build": "tsup src/index.ts --format esm,cjs --dts --clean --external react",
21
- "dev": "tsup src/index.ts --format esm,cjs --dts --watch --external react",
20
+ "build": "tsup",
21
+ "dev": "tsup --watch",
22
22
  "test": "vitest run --passWithNoTests",
23
23
  "test:watch": "vitest",
24
24
  "lint": "eslint src --ext .ts,.tsx",
@@ -26,14 +26,14 @@
26
26
  "clean": "rimraf dist"
27
27
  },
28
28
  "dependencies": {
29
- "@djvlc/runtime-core": "1.0.0",
30
- "@djvlc/contracts-types": "^1.4.0"
29
+ "@djvlc/runtime-core": "1.0.2"
31
30
  },
32
31
  "devDependencies": {
33
32
  "@types/node": "^20.10.0",
34
33
  "@types/react": "^18.2.0",
35
34
  "eslint": "^8.55.0",
36
35
  "rimraf": "^5.0.5",
36
+ "terser": "^5.44.1",
37
37
  "tsup": "^8.0.0",
38
38
  "typescript": "^5.3.0",
39
39
  "vitest": "^1.0.0"