@ray-js/robot-data-stream 0.0.10-beta-6 → 0.0.10-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.
Files changed (2) hide show
  1. package/lib/index.js +37 -45
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  /* eslint-disable consistent-return */
2
- import { useEffect, useRef } from 'react';
2
+ import { useEffect, useRef, useCallback } from 'react';
3
3
  import mitt from 'mitt';
4
4
  import log4js from '@ray-js/log4js';
5
5
  import { SweeperP2pInstance } from './api';
6
6
  import sweeperP2pInstance from './api/sweeperP2p';
7
- import { getSystemInfoSync, usePageEvent } from '@ray-js/ray';
7
+ import { getSystemInfoSync } from '@ray-js/ray';
8
8
  const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
9
9
  const {
10
10
  logTag,
@@ -16,6 +16,33 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
16
16
  const isAppOnBackground = useRef(false);
17
17
  const timer = useRef(null);
18
18
  const isIDE = getSystemInfoSync().brand === 'devtools';
19
+ const handleAppHide = useCallback(() => {
20
+ log4js.info('hooks onAppHide');
21
+ isAppOnBackground.current = true;
22
+ if (isInit.current) {
23
+ // 判断进入后台之后,维持定时器,如果进入后台超过2分钟, 则断开P2P
24
+ timer.current = setTimeout(() => {
25
+ log4js.info('background timer has been exe');
26
+ if (isAppOnBackground.current) {
27
+ unmount();
28
+ }
29
+ clearTimeout(timer.current);
30
+ timer.current = null;
31
+ }, 10 * 1000);
32
+ }
33
+ }, []);
34
+ const handleAppShow = useCallback(() => {
35
+ log4js.info('hooks onAppShow');
36
+ timer.current && clearTimeout(timer.current);
37
+ setTimeout(() => {
38
+ var _ty$p2p$isP2PActiveSy, _ty$p2p;
39
+ if (!isInit.current || !((_ty$p2p$isP2PActiveSy = (_ty$p2p = ty.p2p).isP2PActiveSync) !== null && _ty$p2p$isP2PActiveSy !== void 0 && _ty$p2p$isP2PActiveSy.call(_ty$p2p, {
40
+ deviceId: devId
41
+ }))) {
42
+ connectP2p();
43
+ }
44
+ }, 500);
45
+ }, []);
19
46
  useEffect(() => {
20
47
  if (logTag) {
21
48
  log4js.setTag(logTag);
@@ -36,13 +63,17 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
36
63
  SweeperP2pInstance.initP2pSdk(devId).then(() => {
37
64
  connectP2p();
38
65
  });
39
- onEnterBackground();
40
- onEnterForeground();
66
+ ty.onAppHide(handleAppHide);
67
+ ty.onAppShow(handleAppShow);
41
68
  }, []);
42
- usePageEvent('onUnload', () => {
69
+ ty.onAppClose(() => {
70
+ // 退出被拦截了,可以在此处理一些逻辑,如果允许退出则调用
71
+ // ty.exitMiniProgram 进行,否则当前小程序无法退出
43
72
  unmount();
44
73
  SweeperP2pInstance.deInitP2PSDK();
45
- timer && clearInterval(timer);
74
+ ty.offAppHide(handleAppHide);
75
+ ty.offAppShow(handleAppShow);
76
+ ty.exitMiniProgram();
46
77
  });
47
78
 
48
79
  /**
@@ -65,27 +96,6 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
65
96
  });
66
97
  };
67
98
 
68
- /**
69
- * 进入后台时断开P2P连接
70
- */
71
- const onEnterBackground = () => {
72
- ty.onAppHide(() => {
73
- log4js.info('hooks onAppHide');
74
- isAppOnBackground.current = true;
75
- if (isInit.current) {
76
- // 判断进入后台之后,维持定时器,如果进入后台超过2分钟, 则断开P2P
77
- timer.current = setTimeout(() => {
78
- log4js.info('background timer has been exe');
79
- if (isAppOnBackground.current) {
80
- unmount();
81
- }
82
- clearTimeout(timer.current);
83
- timer.current = null;
84
- }, 10 * 1000);
85
- }
86
- });
87
- };
88
-
89
99
  /**
90
100
  * p2p断开
91
101
  */
@@ -98,24 +108,6 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
98
108
  }
99
109
  SweeperP2pInstance.stopObserverSweeperDataByP2P();
100
110
  };
101
-
102
- /**
103
- * 进入前台时开启P2P连接
104
- */
105
- const onEnterForeground = () => {
106
- ty.onAppShow(() => {
107
- log4js.info('hooks onAppShow');
108
- timer.current && clearTimeout(timer.current);
109
- setTimeout(() => {
110
- var _ty$p2p$isP2PActiveSy, _ty$p2p;
111
- if (!isInit.current || !((_ty$p2p$isP2PActiveSy = (_ty$p2p = ty.p2p).isP2PActiveSync) !== null && _ty$p2p$isP2PActiveSy !== void 0 && _ty$p2p$isP2PActiveSy.call(_ty$p2p, {
112
- deviceId: devId
113
- }))) {
114
- connectP2p();
115
- }
116
- }, 500);
117
- });
118
- };
119
111
  return {
120
112
  appendDownloadStreamDuringTask: sweeperP2pInstance.appendDownloadStreamDuringTask
121
113
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/robot-data-stream",
3
- "version": "0.0.10-beta-6",
3
+ "version": "0.0.10-beta-7",
4
4
  "description": "扫地机P2P数据流标准化组件",
5
5
  "main": "lib/index",
6
6
  "files": [