@ray-js/runtime 0.9.6 → 0.10.0-beta.0
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/PageInstance.js +34 -64
- package/lib/PageInstanceContext.js +2 -2
- package/lib/lifecycle.js +24 -48
- package/lib/usePageEvent/index.js +3 -3
- package/lib/withPageLifecycle/index.jsx +55 -0
- package/package.json +5 -5
package/lib/PageInstance.js
CHANGED
|
@@ -1,78 +1,48 @@
|
|
|
1
|
-
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
2
|
-
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
3
|
-
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
4
|
-
import "core-js/modules/es.array.splice.js";
|
|
5
|
-
import "core-js/modules/es.array.index-of.js";
|
|
6
|
-
import "core-js/modules/es.object.to-string.js";
|
|
7
|
-
import "core-js/modules/web.dom-collections.for-each.js";
|
|
8
|
-
|
|
9
1
|
/**
|
|
10
2
|
* 管理 web 页面生命周期的调用
|
|
11
3
|
* 小程序端
|
|
12
4
|
* TODO: 支持 RN
|
|
13
5
|
*/
|
|
14
|
-
|
|
15
|
-
export
|
|
16
|
-
|
|
6
|
+
let UniqueId = 0;
|
|
7
|
+
export const generateUniqueId = () => {
|
|
8
|
+
return (UniqueId += 1);
|
|
17
9
|
};
|
|
18
|
-
export
|
|
19
|
-
|
|
10
|
+
export const generatePageKey = (uniqueId) => {
|
|
11
|
+
return 'Page-' + uniqueId;
|
|
20
12
|
};
|
|
21
|
-
export
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
this.lifecycleCallback[lifecycleName] = this.lifecycleCallback[lifecycleName] || [];
|
|
42
|
-
var fnObj = {
|
|
43
|
-
context: context,
|
|
44
|
-
fn: fn
|
|
45
|
-
}; // 先注册的函数先调用
|
|
46
|
-
|
|
47
|
-
this.lifecycleCallback[lifecycleName].push(fnObj);
|
|
48
|
-
return function () {
|
|
49
|
-
_this.lifecycleCallback[lifecycleName].splice(_this.lifecycleCallback[lifecycleName].indexOf(fnObj), 1);
|
|
50
|
-
};
|
|
13
|
+
export class PageInstance {
|
|
14
|
+
constructor(uniqueId) {
|
|
15
|
+
this.uniqueId = uniqueId !== null && uniqueId !== void 0 ? uniqueId : generateUniqueId();
|
|
16
|
+
this.lifecycleCallback = {};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* 注册生命周期事件, 页面运行底层实例
|
|
20
|
+
* @param lifecycleName - 事件名
|
|
21
|
+
* @param context - fn 上下文
|
|
22
|
+
* @param fn - 回调函数
|
|
23
|
+
*/
|
|
24
|
+
registerLifecycle(lifecycleName, context, fn) {
|
|
25
|
+
this.lifecycleCallback[lifecycleName] = this.lifecycleCallback[lifecycleName] || [];
|
|
26
|
+
const fnObj = { context, fn };
|
|
27
|
+
// 先注册的函数先调用
|
|
28
|
+
this.lifecycleCallback[lifecycleName].push(fnObj);
|
|
29
|
+
return () => {
|
|
30
|
+
this.lifecycleCallback[lifecycleName].splice(this.lifecycleCallback[lifecycleName].indexOf(fnObj), 1);
|
|
31
|
+
};
|
|
51
32
|
}
|
|
52
33
|
/**
|
|
53
34
|
* 触发生命周期函数
|
|
54
35
|
* @param lifecycleName
|
|
55
36
|
* @param args
|
|
56
37
|
*/
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
context = _ref$context === void 0 ? null : _ref$context,
|
|
67
|
-
fn = _ref.fn;
|
|
68
|
-
result = fn.apply(context, Array.isArray(params.args) ? params.args : [params.args]);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
if (result) {
|
|
72
|
-
return result;
|
|
73
|
-
}
|
|
38
|
+
callLifecycle(params) {
|
|
39
|
+
const callbacks = this.lifecycleCallback[params.name] || [];
|
|
40
|
+
let result;
|
|
41
|
+
[...callbacks].forEach(({ context = null, fn }) => {
|
|
42
|
+
result = fn.apply(context, Array.isArray(params.args) ? params.args : [params.args]);
|
|
43
|
+
});
|
|
44
|
+
if (result) {
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
74
47
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return PageInstance;
|
|
78
|
-
}();
|
|
48
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
|
|
2
|
+
const PageInstanceContext = React.createContext(null);
|
|
3
3
|
PageInstanceContext.displayName = 'PageInstanceContext';
|
|
4
|
-
export { PageInstanceContext };
|
|
4
|
+
export { PageInstanceContext };
|
package/lib/lifecycle.js
CHANGED
|
@@ -1,31 +1,17 @@
|
|
|
1
|
-
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
2
|
-
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
3
|
-
import "core-js/modules/es.array.iterator.js";
|
|
4
|
-
import "core-js/modules/es.map.js";
|
|
5
|
-
import "core-js/modules/es.object.to-string.js";
|
|
6
|
-
import "core-js/modules/es.string.iterator.js";
|
|
7
|
-
import "core-js/modules/web.dom-collections.iterator.js";
|
|
8
1
|
import { generatePageKey } from './PageInstance';
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
_createClass(Lifecycle, [{
|
|
25
|
-
key: "addInstance",
|
|
26
|
-
value: function addInstance(ins) {
|
|
27
|
-
var key = generatePageKey(ins.uniqueId);
|
|
28
|
-
this.instanceMap.set(key, ins);
|
|
2
|
+
class Lifecycle {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.instanceMap = new Map();
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* 增加一个 PageWrapper 实例
|
|
8
|
+
*
|
|
9
|
+
* @param uniqueId - 页面的 ID
|
|
10
|
+
* @param ins - 示例对象
|
|
11
|
+
*/
|
|
12
|
+
addInstance(ins) {
|
|
13
|
+
const key = generatePageKey(ins.uniqueId);
|
|
14
|
+
this.instanceMap.set(key, ins);
|
|
29
15
|
}
|
|
30
16
|
/**
|
|
31
17
|
* 删除一个 PageWrapper 实例
|
|
@@ -33,26 +19,16 @@ var Lifecycle = /*#__PURE__*/function () {
|
|
|
33
19
|
* @param uniqueId - 页面的 ID
|
|
34
20
|
* @param ins - 示例对象
|
|
35
21
|
*/
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
value: function removeInstance(ins) {
|
|
40
|
-
var key = generatePageKey(ins.uniqueId);
|
|
41
|
-
this.instanceMap.delete(key);
|
|
22
|
+
removeInstance(ins) {
|
|
23
|
+
const key = generatePageKey(ins.uniqueId);
|
|
24
|
+
this.instanceMap.delete(key);
|
|
42
25
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if ($instance) {
|
|
50
|
-
$instance.callLifecycle(params);
|
|
51
|
-
}
|
|
26
|
+
emit(params) {
|
|
27
|
+
const key = generatePageKey(params.uniqueId);
|
|
28
|
+
const $instance = this.instanceMap.get(key);
|
|
29
|
+
if ($instance) {
|
|
30
|
+
$instance.callLifecycle(params);
|
|
31
|
+
}
|
|
52
32
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return Lifecycle;
|
|
56
|
-
}();
|
|
57
|
-
|
|
58
|
-
export var lifecycle = new Lifecycle();
|
|
33
|
+
}
|
|
34
|
+
export const lifecycle = new Lifecycle();
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { PageInstanceContext } from '../PageInstanceContext';
|
|
3
3
|
export function usePageEvent(event, fn) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
4
|
+
const $instance = React.useContext(PageInstanceContext).$instance;
|
|
5
|
+
$instance.registerLifecycle(event, null, fn);
|
|
6
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import React from 'react';
|
|
13
|
+
import { pageLifecycles } from '@ray-js/types';
|
|
14
|
+
import { PageInstanceContext } from '../PageInstanceContext';
|
|
15
|
+
export function withPageLifecycle(Component) {
|
|
16
|
+
const displayName = `WithPageLifecycle(${Component.displayName || Component.name})`;
|
|
17
|
+
class WithPageLifeCycle extends React.Component {
|
|
18
|
+
render() {
|
|
19
|
+
const _a = this.props, { forwardedRef } = _a, rest = __rest(_a, ["forwardedRef"]);
|
|
20
|
+
return (<PageInstanceContext.Consumer>
|
|
21
|
+
{(context) => {
|
|
22
|
+
const { $instance } = context;
|
|
23
|
+
// 挂载页面事件
|
|
24
|
+
function additionToLifecycle(ins) {
|
|
25
|
+
Object.keys(pageLifecycles).forEach((event) => {
|
|
26
|
+
if (typeof ins[event] === 'function') {
|
|
27
|
+
;
|
|
28
|
+
$instance.registerLifecycle(event, ins, ins[event]);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return (<Component ref={(ref) => {
|
|
33
|
+
if (ref) {
|
|
34
|
+
additionToLifecycle(ref);
|
|
35
|
+
}
|
|
36
|
+
if (forwardedRef) {
|
|
37
|
+
if (typeof forwardedRef === 'function') {
|
|
38
|
+
forwardedRef(ref);
|
|
39
|
+
}
|
|
40
|
+
else if (typeof forwardedRef === 'object') {
|
|
41
|
+
forwardedRef.current = ref;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}} {...rest}/>);
|
|
45
|
+
}}
|
|
46
|
+
</PageInstanceContext.Consumer>);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
WithPageLifeCycle.displayName = displayName;
|
|
50
|
+
const forwardRef = React.forwardRef((props, ref) => {
|
|
51
|
+
return <WithPageLifeCycle {...props} forwardedRef={ref}/>;
|
|
52
|
+
});
|
|
53
|
+
forwardRef.displayName = displayName;
|
|
54
|
+
return forwardRef;
|
|
55
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ray-js/runtime",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0-beta.0",
|
|
4
4
|
"description": "Ray cross runtime",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ray"
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
"watch": "tsc -p ./tsconfig.build.json --module esnext --outDir lib --watch"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@ray-core/runtime": "^0.
|
|
24
|
+
"@ray-core/runtime": "^0.2.0-beta.2"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@ray-js/cli": "^0.
|
|
28
|
-
"@ray-js/types": "^0.
|
|
27
|
+
"@ray-js/cli": "^0.10.0-beta.0",
|
|
28
|
+
"@ray-js/types": "^0.10.0-beta.0"
|
|
29
29
|
},
|
|
30
30
|
"maintainers": [
|
|
31
31
|
{
|
|
@@ -33,6 +33,6 @@
|
|
|
33
33
|
"email": "tuyafe@tuya.com"
|
|
34
34
|
}
|
|
35
35
|
],
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "84f7689f7ab2bb23abf6a7dd48ac0cce03127cb2",
|
|
37
37
|
"repository": {}
|
|
38
38
|
}
|