@ray-js/robot-map 0.0.5-beta.6 → 0.0.5-beta.7

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.
@@ -5,15 +5,14 @@ import "core-js/modules/esnext.iterator.map.js";
5
5
  import "core-js/modules/web.dom-collections.iterator.js";
6
6
  /* eslint-disable func-names */
7
7
  import Render from './index.rjs';
8
- import { MAP_API_METHODS, MAP_CALLBACK_METHODS } from '@ray-js/robot-map-sdk';
8
+ import { MAP_CALLBACK_METHODS } from '@ray-js/robot-map-sdk';
9
9
  import { isEqual } from 'lodash-es';
10
10
  const componentOptions = {
11
11
  options: {
12
12
  pureDataPattern: /^init$/
13
13
  },
14
14
  data: {
15
- initialized: false,
16
- mapApi: null
15
+ initialized: false
17
16
  },
18
17
  properties: {
19
18
  containerId: {
@@ -129,7 +128,6 @@ const componentOptions = {
129
128
  lifetimes: {
130
129
  created() {
131
130
  this.render = new Render(this);
132
- this.mapApi = null;
133
131
  },
134
132
  async ready() {
135
133
  const {
@@ -189,56 +187,26 @@ const componentOptions = {
189
187
  },
190
188
  pageLifetimes: {},
191
189
  methods: {
192
- // render 对象动态添加 API 代理方法
193
- setupApiProxyMethods() {
194
- const {
195
- render
196
- } = this;
197
- if (!render) {
198
- return;
199
- }
200
-
201
- // 如果已经设置过,不重复设置(使用标记避免重复执行)
202
- if (render.__apiProxyMethodsSetup) {
203
- return;
190
+ // 调用 RJS 层的 mapInstance 方法(异步,通过回调返回结果)
191
+ invokeMapApi(requestId, methodName, args) {
192
+ if (this.render) {
193
+ this.render.callMapInstanceMethodAsync(requestId, methodName, ...args);
204
194
  }
205
- MAP_API_METHODS.forEach(methodName => {
206
- // 为 this.render 添加代理方法,通过 callMapInstanceMethod 来间接调用
207
- // 因为 render.mapInstance 在外部无法访问(封装的私有属性)
208
- render[methodName] = function () {
209
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
210
- args[_key] = arguments[_key];
211
- }
212
- return render.callMapInstanceMethod(methodName, ...args);
213
- };
214
- });
215
-
216
- // 标记已设置,避免重复执行
217
- render.__apiProxyMethodsSetup = true;
218
195
  },
219
- // 创建MapApi对象,将rjs方法映射为API
220
- createMapApi() {
221
- if (!this.render) {
222
- return null;
223
- }
224
- const mapApi = {};
225
- MAP_API_METHODS.forEach(methodName => {
226
- // 将方法绑定到 this.render,这样调用时会通过 render 代理到 mapInstance
227
- if (typeof this.render[methodName] === 'function') {
228
- mapApi[methodName] = this.render[methodName].bind(this.render);
229
- }
196
+ // 接收 RJS 层的 API 调用结果回调
197
+ onApiResponse(requestId, result, error) {
198
+ this.triggerEvent('onApiResponse', {
199
+ requestId,
200
+ result,
201
+ error
230
202
  });
231
- return mapApi;
232
203
  },
233
204
  // 处理rjs回调事件
234
205
  onInitialized() {
235
- // 确保代理方法已设置(防止时序问题)
236
- this.setupApiProxyMethods();
237
- this.mapApi = this.createMapApi();
238
- this.setData({
239
- mapApi: this.mapApi
206
+ // 传递组件实例,让 TS 层可以通过 instance.invokeMapApi() 调用方法
207
+ this.triggerEvent('onInitialized', {
208
+ instance: this
240
209
  });
241
- this.triggerEvent('onInitialized', this.mapApi);
242
210
  }
243
211
  }
244
212
  };
@@ -246,8 +214,8 @@ const componentOptions = {
246
214
  // 动态添加所有 MAP_CALLBACK_METHODS 中定义的回调方法
247
215
  MAP_CALLBACK_METHODS.forEach(methodName => {
248
216
  componentOptions.methods[methodName] = function () {
249
- for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
250
- args[_key2] = arguments[_key2];
217
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
218
+ args[_key] = arguments[_key];
251
219
  }
252
220
  this.triggerEvent(methodName, ...args);
253
221
  };
@@ -1,4 +1,4 @@
1
- import { MapApplication, MAP_API_METHODS, MAP_CALLBACK_METHODS } from "@ray-js/robot-map-sdk/rjs";
1
+ import { MapApplication, MAP_CALLBACK_METHODS } from "@ray-js/robot-map-sdk/rjs";
2
2
  import { decodeMap, decodeMapStructured, decodePath } from '@ray-js/robot-protocol'
3
3
 
4
4
  export default Render({
@@ -140,11 +140,22 @@ export default Render({
140
140
 
141
141
  // 通用的 API 调用方法,用于代理所有 mapInstance 上的方法
142
142
  // 因为外部无法直接访问 this.mapInstance,所以提供这个方法来间接调用
143
- callMapInstanceMethod(methodName, ...args) {
143
+ // 注意:RJS 层无法直接 return 值给 JS 层,需要通过 callMethod 回调
144
+ callMapInstanceMethodAsync(requestId, methodName, ...args) {
145
+ let result = null
146
+ let error = null
147
+
144
148
  if (this.mapInstance && typeof this.mapInstance[methodName] === 'function') {
145
- return this.mapInstance[methodName](...args)
149
+ try {
150
+ result = this.mapInstance[methodName](...args)
151
+ } catch (e) {
152
+ error = e?.message || String(e)
153
+ }
154
+ } else {
155
+ error = `Method ${methodName} not available on mapInstance`
146
156
  }
147
- console.warn(`Method ${methodName} not available on mapInstance`)
148
- return null
157
+
158
+ // 通过 callMethod 将结果回调给 JS 层
159
+ this.callMethod('onApiResponse', requestId, result, error)
149
160
  }
150
161
  })
@@ -1,6 +1,5 @@
1
1
  import _extends from "@babel/runtime/helpers/esm/extends";
2
2
  import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
3
- import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
4
3
  const _excluded = ["map", "path", "roomProperties", "forbiddenSweepZones", "forbiddenMopZones", "cleanZones", "virtualWalls", "spots", "wayPoints", "detectedObjects", "customElements", "config", "runtime", "onMapReady"];
5
4
  import "core-js/modules/esnext.iterator.constructor.js";
6
5
  import "core-js/modules/esnext.iterator.for-each.js";
@@ -8,18 +7,13 @@ import "core-js/modules/esnext.iterator.map.js";
8
7
  import "core-js/modules/esnext.iterator.reduce.js";
9
8
  import "core-js/modules/web.dom-collections.iterator.js";
10
9
  import React, { useCallback, useEffect, useRef, useMemo } from 'react';
11
- import { MAP_CALLBACK_METHODS } from '@ray-js/robot-map-sdk';
10
+ import { MAP_CALLBACK_METHODS, MAP_API_METHODS } from '@ray-js/robot-map-sdk';
12
11
  import { nanoid } from 'nanoid/non-secure';
13
12
  import RjsComponent from './RjsComponent';
14
13
  import { robotMapDefaultProps } from './props';
15
14
 
16
- // 回调名到事件名的映射
17
- const CALLBACK_TO_EVENT_MAP = _objectSpread({
18
- onInitialized: 'onInitialized'
19
- }, MAP_CALLBACK_METHODS.reduce((acc, methodName) => {
20
- acc[methodName] = methodName;
21
- return acc;
22
- }, {}));
15
+ // 存储待处理的请求:requestId -> { resolve, reject }
16
+
23
17
  const RjsRobotMap = _ref => {
24
18
  let {
25
19
  map,
@@ -40,6 +34,10 @@ const RjsRobotMap = _ref => {
40
34
  callbacks = _objectWithoutProperties(_ref, _excluded);
41
35
  const componentRef = useRef(null);
42
36
  const callbacksRef = useRef(callbacks);
37
+ // 存储小程序组件实例的引用
38
+ const instanceRef = useRef(null);
39
+ // 存储待处理的 API 请求
40
+ const pendingRequestsRef = useRef(new Map());
43
41
  // 为每个组件实例生成唯一的 containerId
44
42
  const containerId = useRef("robot-map-container-".concat(nanoid())).current;
45
43
 
@@ -48,29 +46,86 @@ const RjsRobotMap = _ref => {
48
46
  callbacksRef.current = callbacks;
49
47
  });
50
48
 
51
- // 事件分发函数 - 这个函数是稳定的,不会随渲染变化
49
+ // 事件分发函数
52
50
  const eventDispatcher = useCallback(function (eventName) {
53
- const propName = CALLBACK_TO_EVENT_MAP[eventName];
54
- if (propName && callbacksRef.current[propName]) {
55
- for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
56
- args[_key - 1] = arguments[_key];
51
+ var _callbacksRef$current, _callbacksRef$current2;
52
+ for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
53
+ args[_key - 1] = arguments[_key];
54
+ }
55
+ (_callbacksRef$current = (_callbacksRef$current2 = callbacksRef.current)[eventName]) === null || _callbacksRef$current === void 0 || _callbacksRef$current.call(_callbacksRef$current2, ...args);
56
+ }, []);
57
+
58
+ // 在 TS 层创建 mapApi,每个方法返回 Promise
59
+ const createMapApi = useCallback(() => {
60
+ const mapApi = MAP_API_METHODS.reduce((acc, methodName) => {
61
+ acc[methodName] = function () {
62
+ for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
63
+ args[_key2] = arguments[_key2];
64
+ }
65
+ return new Promise((resolve, reject) => {
66
+ const instance = instanceRef.current;
67
+ if (!instance) {
68
+ reject(new Error('Map instance not available'));
69
+ return;
70
+ }
71
+
72
+ // 生成唯一的请求 ID
73
+ const requestId = nanoid();
74
+
75
+ // 存储 resolve/reject,等待回调
76
+ pendingRequestsRef.current.set(requestId, {
77
+ resolve,
78
+ reject
79
+ });
80
+
81
+ // 调用组件实例的方法,通知 RJS 层执行
82
+ instance.invokeMapApi(requestId, methodName, args);
83
+ });
84
+ };
85
+ return acc;
86
+ }, {});
87
+ return mapApi;
88
+ }, []);
89
+
90
+ // 处理 API 响应事件
91
+ const handleApiResponse = useCallback(event => {
92
+ const {
93
+ requestId,
94
+ result,
95
+ error
96
+ } = event.detail || {};
97
+ const pending = pendingRequestsRef.current.get(requestId);
98
+ if (pending) {
99
+ pendingRequestsRef.current.delete(requestId);
100
+ if (error) {
101
+ pending.reject(new Error(error));
102
+ } else {
103
+ pending.resolve(result);
57
104
  }
58
- // 调用最新的prop回调
59
- callbacksRef.current[propName](...args);
60
105
  }
61
106
  }, []);
62
107
 
63
108
  // 处理初始化完成事件
64
109
  const handleInitialized = useCallback(event => {
65
- const api = event.detail;
66
- onMapReady === null || onMapReady === void 0 || onMapReady(api);
67
- eventDispatcher('onInitialized', api);
68
- }, [onMapReady, eventDispatcher]);
110
+ const {
111
+ instance
112
+ } = event.detail || {};
113
+ if (instance) {
114
+ // 保存组件实例引用
115
+ instanceRef.current = instance;
116
+
117
+ // 创建 mapApi
118
+ const mapApi = createMapApi();
119
+ onMapReady === null || onMapReady === void 0 || onMapReady(mapApi);
120
+ eventDispatcher('onInitialized', mapApi);
121
+ }
122
+ }, [onMapReady, eventDispatcher, createMapApi]);
69
123
 
70
124
  // 动态生成所有回调处理函数的绑定属性
71
125
  const eventBindings = useMemo(() => {
72
126
  const bindings = {
73
- bindonInitialized: handleInitialized
127
+ bindonInitialized: handleInitialized,
128
+ bindonApiResponse: handleApiResponse
74
129
  };
75
130
 
76
131
  // 为 MAP_CALLBACK_METHODS 中的每个方法创建绑定
@@ -80,7 +135,7 @@ const RjsRobotMap = _ref => {
80
135
  };
81
136
  });
82
137
  return bindings;
83
- }, [handleInitialized, eventDispatcher]);
138
+ }, [handleInitialized, handleApiResponse, eventDispatcher]);
84
139
  return /*#__PURE__*/React.createElement(RjsComponent, _extends({
85
140
  ref: componentRef,
86
141
  map: map,
package/lib/RobotMap.js CHANGED
@@ -13,7 +13,6 @@ import { createWebviewContext, WebView } from '@ray-js/ray';
13
13
  import { MAP_API_METHODS, MAP_CALLBACK_METHODS } from '@ray-js/robot-map-sdk';
14
14
  import { nanoid } from 'nanoid/non-secure';
15
15
  import { robotMapDefaultProps } from './props';
16
-
17
16
  // 回调名到事件名的映射
18
17
  const CALLBACK_TO_EVENT_MAP = _objectSpread({
19
18
  onInitialized: 'onInitialized'
package/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { RobotMap } from './RobotMap';
2
2
  export { RjsRobotMap } from './RjsRobotMap';
3
+ export type { MapApi } from './types';
3
4
  export * from '@ray-js/robot-map-sdk';
package/lib/index.js CHANGED
@@ -1,3 +1,7 @@
1
1
  export { RobotMap } from './RobotMap';
2
2
  export { RjsRobotMap } from './RjsRobotMap';
3
+
4
+ // 先导出我们的类型定义(覆盖 MapApi 为 Promise 版本)
5
+
6
+ // 然后导出 SDK 的其他内容(MapApi 会被我们的定义覆盖)
3
7
  export * from '@ray-js/robot-map-sdk';
package/lib/props.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { DeepPartialAppConfig, DeepPartialRuntimeConfig, MapApi, RoomProperty, ZoneParam, VirtualWallParam, DetectedObjectParam, CustomElementParam, MapState, PathState, Point, RoomData, SpotParam, WayPointParam } from '@ray-js/robot-map-sdk';
1
+ import { DeepPartialAppConfig, DeepPartialRuntimeConfig, RoomProperty, ZoneParam, VirtualWallParam, DetectedObjectParam, CustomElementParam, MapState, PathState, Point, RoomData, SpotParam, WayPointParam } from '@ray-js/robot-map-sdk';
2
+ import { MapApi } from './types';
2
3
  /**
3
4
  * 机器人地图组件属性接口
4
5
  *
@@ -102,7 +103,7 @@ export interface RobotMapProps {
102
103
  * @description.en Callback when the map is initialized and the API is ready
103
104
  * @description.zh 当地图组件初始化完成并且所有API功能可用时触发的回调函数。
104
105
  * @description.en Callback function triggered when the map component is initialized and all API functions are available.
105
- * @param mapApi - 可供使用的地图 API 函数对象 / Available map API functions object
106
+ * @param mapApi - 可供使用的地图 API 函数对象,所有方法返回 Promise / Available map API functions object, all methods return Promise
106
107
  */
107
108
  onMapReady?: (mapApi: MapApi) => void;
108
109
  /**
package/lib/types.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { MapApi as OriginalMapApi } from '@ray-js/robot-map-sdk';
2
+ /**
3
+ * MapApi 类型定义(所有方法返回 Promise)
4
+ * MapApi type definition (all methods return Promise)
5
+ */
6
+ export type MapApi = {
7
+ [K in keyof OriginalMapApi]: OriginalMapApi[K] extends (...args: infer Args) => infer Return ? (...args: Args) => Promise<Return> : OriginalMapApi[K];
8
+ };
package/lib/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/robot-map",
3
- "version": "0.0.5-beta.6",
3
+ "version": "0.0.5-beta.7",
4
4
  "description": "机器人地图组件",
5
5
  "main": "lib/index",
6
6
  "files": [
@@ -33,7 +33,7 @@
33
33
  "@ray-js/ray": "^1.7.39"
34
34
  },
35
35
  "dependencies": {
36
- "@ray-js/robot-map-sdk": "0.0.4-beta.4",
36
+ "@ray-js/robot-map-sdk": "0.0.4-beta.5",
37
37
  "clsx": "^1.2.1",
38
38
  "nanoid": "^5.1.6"
39
39
  },