@ray-js/runtime 1.7.56-beta.7 → 1.7.56-beta.8

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/lib/index.d.ts CHANGED
@@ -2,6 +2,7 @@ import * as runtime from '@ray-core/runtime';
2
2
  export { lifecycle } from './lifecycle';
3
3
  export { PageInstance } from './PageInstance';
4
4
  export { PageInstanceContext } from './PageInstanceContext';
5
+ export { useAsyncState } from './useAsyncState';
5
6
  export { useAppEvent } from './useAppEvent';
6
7
  export { usePageEvent } from './usePageEvent';
7
8
  export { withPageLifecycle } from './withPageLifecycle';
package/lib/index.js CHANGED
@@ -2,6 +2,7 @@ import * as runtime from '@ray-core/runtime';
2
2
  export { lifecycle } from './lifecycle';
3
3
  export { PageInstance } from './PageInstance';
4
4
  export { PageInstanceContext } from './PageInstanceContext';
5
+ export { useAsyncState } from './useAsyncState';
5
6
  export { useAppEvent } from './useAppEvent';
6
7
  // 页面事件
7
8
  export { usePageEvent } from './usePageEvent';
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 异步 State Hook
3
+ * 利用对象引用变化强制触发更新,即使 value 相同
4
+ */
5
+ export declare function useAsyncState<T>(initialValue: T): [T, (value: T | ((prev: T) => T)) => Promise<T>];
@@ -0,0 +1,35 @@
1
+ import "core-js/modules/web.dom-collections.iterator.js";
2
+ import { useState, useRef } from 'react';
3
+ import { useNativeEffect } from '@ray-core/runtime';
4
+
5
+ /**
6
+ * 异步 State Hook
7
+ * 利用对象引用变化强制触发更新,即使 value 相同
8
+ */
9
+ export function useAsyncState(initialValue) {
10
+ // 包装在对象中,通过创建新对象强制触发更新
11
+ const [state, setState] = useState(() => ({
12
+ value: initialValue
13
+ }));
14
+ const pendingResolvers = useRef([]);
15
+ // 此时 state.value 已经是最新的,可以正确 resolve Promise
16
+ useNativeEffect(() => {
17
+ if (pendingResolvers.current.length > 0) {
18
+ const resolvers = [...pendingResolvers.current];
19
+ pendingResolvers.current = [];
20
+ resolvers.forEach(res => res(state.value));
21
+ }
22
+ }, [state]);
23
+ return [state.value, value => {
24
+ return new Promise(resolve => {
25
+ pendingResolvers.current.push(resolve);
26
+ setState(prev => {
27
+ const nextValue = typeof value === 'function' ? value(prev.value) : value;
28
+ // 总是创建新对象,即使 value 相同
29
+ return {
30
+ value: nextValue
31
+ };
32
+ });
33
+ });
34
+ }];
35
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/runtime",
3
- "version": "1.7.56-beta.7",
3
+ "version": "1.7.56-beta.8",
4
4
  "description": "Ray cross runtime",
5
5
  "keywords": [
6
6
  "ray"
@@ -27,13 +27,13 @@
27
27
  "@ray-core/runtime": "^0.4.9"
28
28
  },
29
29
  "devDependencies": {
30
- "@ray-js/cli": "1.7.56-beta.7",
31
- "@ray-js/types": "1.7.56-beta.7",
30
+ "@ray-js/cli": "1.7.56-beta.8",
31
+ "@ray-js/types": "1.7.56-beta.8",
32
32
  "typescript": "^5.8.3"
33
33
  },
34
34
  "publishConfig": {
35
35
  "access": "public",
36
36
  "registry": "https://registry.npmjs.org"
37
37
  },
38
- "gitHead": "4af5e87574c24597a6da1112bc3c40bf27df713c"
38
+ "gitHead": "8f42d569ceece3840cdbf9aa4a223abcd06b0915"
39
39
  }