@ray-js/robot-data-stream 0.0.10-beta-6 → 0.0.10-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.js +33 -43
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
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';
|
|
@@ -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,15 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
|
|
|
36
63
|
SweeperP2pInstance.initP2pSdk(devId).then(() => {
|
|
37
64
|
connectP2p();
|
|
38
65
|
});
|
|
39
|
-
|
|
40
|
-
|
|
66
|
+
ty.onAppHide(handleAppHide);
|
|
67
|
+
ty.onAppShow(handleAppShow);
|
|
41
68
|
}, []);
|
|
42
69
|
usePageEvent('onUnload', () => {
|
|
43
70
|
unmount();
|
|
44
71
|
SweeperP2pInstance.deInitP2PSDK();
|
|
45
|
-
|
|
72
|
+
ty.offAppHide(handleAppHide);
|
|
73
|
+
ty.offAppShow(handleAppShow);
|
|
74
|
+
ty.exitMiniProgram();
|
|
46
75
|
});
|
|
47
76
|
|
|
48
77
|
/**
|
|
@@ -65,27 +94,6 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
|
|
|
65
94
|
});
|
|
66
95
|
};
|
|
67
96
|
|
|
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
97
|
/**
|
|
90
98
|
* p2p断开
|
|
91
99
|
*/
|
|
@@ -98,24 +106,6 @@ const useP2PDataStream = (devId, onReceiveMapData, onReceivePathData, opt) => {
|
|
|
98
106
|
}
|
|
99
107
|
SweeperP2pInstance.stopObserverSweeperDataByP2P();
|
|
100
108
|
};
|
|
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
109
|
return {
|
|
120
110
|
appendDownloadStreamDuringTask: sweeperP2pInstance.appendDownloadStreamDuringTask
|
|
121
111
|
};
|