@ray-js/runtime 1.4.0-alpha.1 → 1.4.0-alpha.10
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 +40 -58
- package/lib/PageInstanceContext.js +1 -1
- package/lib/current.d.ts +1 -1
- package/lib/current.js +1 -1
- package/lib/index.js +8 -14
- package/lib/lifecycle.js +25 -46
- package/lib/useAppEvent/index.js +2 -1
- package/lib/usePageEvent/index.js +1 -1
- package/lib/withPageLifecycle/index.js +38 -72
- package/package.json +17 -17
- package/LICENSE.md +0 -9
package/lib/PageInstance.js
CHANGED
|
@@ -1,78 +1,60 @@
|
|
|
1
|
-
import
|
|
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
|
-
|
|
1
|
+
import "core-js/modules/web.dom-collections.iterator.js";
|
|
9
2
|
/**
|
|
10
3
|
* 管理 web 页面生命周期的调用
|
|
11
4
|
* 小程序端
|
|
12
5
|
* TODO: 支持 RN
|
|
13
6
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
7
|
+
|
|
8
|
+
let UniqueId = 0;
|
|
9
|
+
export const generateUniqueId = () => {
|
|
16
10
|
return UniqueId += 1;
|
|
17
11
|
};
|
|
18
|
-
export
|
|
12
|
+
export const generatePageKey = uniqueId => {
|
|
19
13
|
return 'Page-' + uniqueId;
|
|
20
14
|
};
|
|
21
|
-
export
|
|
22
|
-
|
|
23
|
-
_classCallCheck(this, PageInstance);
|
|
24
|
-
|
|
15
|
+
export class PageInstance {
|
|
16
|
+
constructor(uniqueId) {
|
|
25
17
|
this.uniqueId = uniqueId !== null && uniqueId !== void 0 ? uniqueId : generateUniqueId();
|
|
26
18
|
this.lifecycleCallback = {};
|
|
27
19
|
}
|
|
20
|
+
|
|
28
21
|
/**
|
|
29
22
|
* 注册生命周期事件, 页面运行底层实例
|
|
30
23
|
* @param lifecycleName - 事件名
|
|
31
24
|
* @param context - fn 上下文
|
|
32
25
|
* @param fn - 回调函数
|
|
33
26
|
*/
|
|
27
|
+
registerLifecycle(lifecycleName, context, fn) {
|
|
28
|
+
this.lifecycleCallback[lifecycleName] = this.lifecycleCallback[lifecycleName] || [];
|
|
29
|
+
const fnObj = {
|
|
30
|
+
context,
|
|
31
|
+
fn
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// 先注册的函数先调用
|
|
35
|
+
this.lifecycleCallback[lifecycleName].push(fnObj);
|
|
36
|
+
return () => {
|
|
37
|
+
this.lifecycleCallback[lifecycleName].splice(this.lifecycleCallback[lifecycleName].indexOf(fnObj), 1);
|
|
38
|
+
};
|
|
39
|
+
}
|
|
34
40
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* 触发生命周期函数
|
|
54
|
-
* @param lifecycleName
|
|
55
|
-
* @param args
|
|
56
|
-
*/
|
|
57
|
-
|
|
58
|
-
}, {
|
|
59
|
-
key: "callLifecycle",
|
|
60
|
-
value: function callLifecycle(params) {
|
|
61
|
-
var callbacks = this.lifecycleCallback[params.name] || [];
|
|
62
|
-
var result;
|
|
63
|
-
|
|
64
|
-
_toConsumableArray(callbacks).forEach(function (_ref) {
|
|
65
|
-
var _ref$context = _ref.context,
|
|
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
|
-
}
|
|
41
|
+
/**
|
|
42
|
+
* 触发生命周期函数
|
|
43
|
+
* @param lifecycleName
|
|
44
|
+
* @param args
|
|
45
|
+
*/
|
|
46
|
+
callLifecycle(params) {
|
|
47
|
+
const callbacks = this.lifecycleCallback[params.name] || [];
|
|
48
|
+
let result;
|
|
49
|
+
[...callbacks].forEach(_ref => {
|
|
50
|
+
let {
|
|
51
|
+
context = null,
|
|
52
|
+
fn
|
|
53
|
+
} = _ref;
|
|
54
|
+
result = fn.apply(context, Array.isArray(params.args) ? params.args : [params.args]);
|
|
55
|
+
});
|
|
56
|
+
if (result) {
|
|
57
|
+
return result;
|
|
74
58
|
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return PageInstance;
|
|
78
|
-
}();
|
|
59
|
+
}
|
|
60
|
+
}
|
package/lib/current.d.ts
CHANGED
package/lib/current.js
CHANGED
package/lib/index.js
CHANGED
|
@@ -2,26 +2,20 @@ 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 { useAppEvent } from './useAppEvent';
|
|
6
|
-
|
|
5
|
+
export { useAppEvent } from './useAppEvent';
|
|
6
|
+
// 页面事件
|
|
7
7
|
export { usePageEvent } from './usePageEvent';
|
|
8
8
|
export { withPageLifecycle } from './withPageLifecycle';
|
|
9
9
|
export { getCurrent } from './current';
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
export var useQuery = function () {
|
|
10
|
+
export const useNativeEffect = () => console.warn("// TODO \u6682\u672A\u5B9E\u73B0\u94A9\u5B50 'useNativeEffect'");
|
|
11
|
+
export const useQuery = () => {
|
|
14
12
|
console.warn("// TODO \u6682\u672A\u5B9E\u73B0\u94A9\u5B50 'useQuery'");
|
|
15
13
|
return null;
|
|
16
14
|
};
|
|
17
|
-
export
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
export
|
|
21
|
-
return console.warn("// TODO \u6682\u672A\u5B9E\u73B0\u94A9\u5B50 'useComponentInstance'");
|
|
22
|
-
};
|
|
23
|
-
export var createPortal = runtime.createPortal;
|
|
24
|
-
export var useModal = function () {
|
|
15
|
+
export const usePageInstance = () => console.warn("// TODO \u6682\u672A\u5B9E\u73B0\u94A9\u5B50 'usePageInstance'");
|
|
16
|
+
export const useComponentInstance = () => console.warn("// TODO \u6682\u672A\u5B9E\u73B0\u94A9\u5B50 'useComponentInstance'");
|
|
17
|
+
export const createPortal = runtime.createPortal;
|
|
18
|
+
export const useModal = () => {
|
|
25
19
|
console.warn("// TODO \u6682\u672A\u5B9E\u73B0\u94A9\u5B50 'useModal'");
|
|
26
20
|
return undefined;
|
|
27
21
|
};
|
package/lib/lifecycle.js
CHANGED
|
@@ -1,58 +1,37 @@
|
|
|
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
1
|
import "core-js/modules/web.dom-collections.iterator.js";
|
|
8
2
|
import { generatePageKey } from './PageInstance';
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
function Lifecycle() {
|
|
12
|
-
_classCallCheck(this, Lifecycle);
|
|
13
|
-
|
|
3
|
+
class Lifecycle {
|
|
4
|
+
constructor() {
|
|
14
5
|
this.instanceMap = new Map();
|
|
15
6
|
}
|
|
7
|
+
|
|
16
8
|
/**
|
|
17
9
|
* 增加一个 PageWrapper 实例
|
|
18
10
|
*
|
|
19
11
|
* @param uniqueId - 页面的 ID
|
|
20
12
|
* @param ins - 示例对象
|
|
21
13
|
*/
|
|
14
|
+
addInstance(ins) {
|
|
15
|
+
const key = generatePageKey(ins.uniqueId);
|
|
16
|
+
this.instanceMap.set(key, ins);
|
|
17
|
+
}
|
|
22
18
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
key: "removeInstance",
|
|
39
|
-
value: function removeInstance(ins) {
|
|
40
|
-
var key = generatePageKey(ins.uniqueId);
|
|
41
|
-
this.instanceMap.delete(key);
|
|
42
|
-
}
|
|
43
|
-
}, {
|
|
44
|
-
key: "emit",
|
|
45
|
-
value: function emit(params) {
|
|
46
|
-
var key = generatePageKey(params.uniqueId);
|
|
47
|
-
var $instance = this.instanceMap.get(key);
|
|
48
|
-
|
|
49
|
-
if ($instance) {
|
|
50
|
-
$instance.callLifecycle(params);
|
|
51
|
-
}
|
|
19
|
+
/**
|
|
20
|
+
* 删除一个 PageWrapper 实例
|
|
21
|
+
*
|
|
22
|
+
* @param uniqueId - 页面的 ID
|
|
23
|
+
* @param ins - 示例对象
|
|
24
|
+
*/
|
|
25
|
+
removeInstance(ins) {
|
|
26
|
+
const key = generatePageKey(ins.uniqueId);
|
|
27
|
+
this.instanceMap.delete(key);
|
|
28
|
+
}
|
|
29
|
+
emit(params) {
|
|
30
|
+
const key = generatePageKey(params.uniqueId);
|
|
31
|
+
const $instance = this.instanceMap.get(key);
|
|
32
|
+
if ($instance) {
|
|
33
|
+
$instance.callLifecycle(params);
|
|
52
34
|
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}();
|
|
57
|
-
|
|
58
|
-
export var lifecycle = new Lifecycle();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export const lifecycle = new Lifecycle();
|
package/lib/useAppEvent/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { PageInstanceContext } from '../PageInstanceContext';
|
|
3
3
|
export function usePageEvent(event, fn) {
|
|
4
|
-
|
|
4
|
+
const $instance = React.useContext(PageInstanceContext).$instance;
|
|
5
5
|
$instance.registerLifecycle(event, null, fn);
|
|
6
6
|
}
|
|
@@ -1,93 +1,59 @@
|
|
|
1
1
|
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
2
|
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
3
|
-
import _typeof from "@babel/runtime/helpers/esm/typeof";
|
|
4
|
-
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
5
|
-
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
6
|
-
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
|
|
7
|
-
import _inherits from "@babel/runtime/helpers/esm/inherits";
|
|
8
|
-
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
|
|
9
3
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
10
|
-
|
|
11
|
-
import "core-js/modules/
|
|
12
|
-
import "core-js/modules/es.object.to-string.js";
|
|
13
|
-
import "core-js/modules/web.dom-collections.for-each.js";
|
|
14
|
-
import "core-js/modules/es.object.keys.js";
|
|
4
|
+
const _excluded = ["forwardedRef"];
|
|
5
|
+
import "core-js/modules/web.dom-collections.iterator.js";
|
|
15
6
|
import React from 'react';
|
|
16
7
|
import { pageLifecycles } from '@ray-js/types';
|
|
17
8
|
import { PageInstanceContext } from '../PageInstanceContext';
|
|
18
9
|
export function withPageLifecycle(Component) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
function WithPageLifeCycle() {
|
|
27
|
-
var _this;
|
|
28
|
-
|
|
29
|
-
_classCallCheck(this, WithPageLifeCycle);
|
|
30
|
-
|
|
31
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
32
|
-
args[_key] = arguments[_key];
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
_this = _super.call.apply(_super, [this].concat(args));
|
|
36
|
-
|
|
37
|
-
_defineProperty(_assertThisInitialized(_this), "refHandle", function (ref) {
|
|
38
|
-
var forwardedRef = _this.props.forwardedRef;
|
|
39
|
-
|
|
10
|
+
const displayName = "WithPageLifecycle(".concat(Component.displayName || Component.name, ")");
|
|
11
|
+
class WithPageLifeCycle extends React.Component {
|
|
12
|
+
constructor() {
|
|
13
|
+
super(...arguments);
|
|
14
|
+
_defineProperty(this, "refHandle", ref => {
|
|
15
|
+
const forwardedRef = this.props.forwardedRef;
|
|
40
16
|
if (ref) {
|
|
41
|
-
|
|
17
|
+
this.additionToLifecycle(ref);
|
|
42
18
|
}
|
|
43
|
-
|
|
44
19
|
if (forwardedRef) {
|
|
45
20
|
if (typeof forwardedRef === 'function') {
|
|
46
21
|
forwardedRef(ref);
|
|
47
|
-
} else if (
|
|
22
|
+
} else if (typeof forwardedRef === 'object') {
|
|
48
23
|
forwardedRef.current = ref;
|
|
49
24
|
}
|
|
50
25
|
}
|
|
51
26
|
});
|
|
52
|
-
|
|
53
|
-
return _this;
|
|
54
27
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
}]);
|
|
84
|
-
|
|
85
|
-
return WithPageLifeCycle;
|
|
86
|
-
}(React.Component);
|
|
87
|
-
|
|
28
|
+
render() {
|
|
29
|
+
const _this$props = this.props,
|
|
30
|
+
{
|
|
31
|
+
forwardedRef
|
|
32
|
+
} = _this$props,
|
|
33
|
+
rest = _objectWithoutProperties(_this$props, _excluded);
|
|
34
|
+
return /*#__PURE__*/React.createElement(PageInstanceContext.Consumer, null, context => {
|
|
35
|
+
const {
|
|
36
|
+
$instance
|
|
37
|
+
} = context;
|
|
38
|
+
// 挂载页面事件
|
|
39
|
+
this.additionToLifecycle = function (ins) {
|
|
40
|
+
Object.keys(pageLifecycles).forEach(event => {
|
|
41
|
+
if (typeof ins[event] === 'function') {
|
|
42
|
+
$instance.registerLifecycle(event, ins, ins[event]);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
return /*#__PURE__*/React.createElement(Component
|
|
47
|
+
// fixed: 坑货,不能直接一个匿名函数,因为每次渲染匿名函数都是个新的,导致每次渲染都会调用一次
|
|
48
|
+
// 进而导致生命周期函数重复添加
|
|
49
|
+
, _extends({
|
|
50
|
+
ref: this.refHandle
|
|
51
|
+
}, rest));
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
88
55
|
_defineProperty(WithPageLifeCycle, "displayName", displayName);
|
|
89
|
-
|
|
90
|
-
var forwardRef = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
56
|
+
const forwardRef = /*#__PURE__*/React.forwardRef((props, ref) => {
|
|
91
57
|
return /*#__PURE__*/React.createElement(WithPageLifeCycle, _extends({}, props, {
|
|
92
58
|
forwardedRef: ref
|
|
93
59
|
}));
|
package/package.json
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ray-js/runtime",
|
|
3
|
-
"version": "1.4.0-alpha.
|
|
3
|
+
"version": "1.4.0-alpha.10",
|
|
4
4
|
"description": "Ray cross runtime",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ray"
|
|
7
7
|
],
|
|
8
|
+
"repository": {},
|
|
8
9
|
"license": "MIT",
|
|
10
|
+
"maintainers": [
|
|
11
|
+
{
|
|
12
|
+
"name": "tuyafe",
|
|
13
|
+
"email": "tuyafe@tuya.com"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
9
16
|
"main": "main.js",
|
|
10
17
|
"files": [
|
|
11
18
|
"lib",
|
|
12
19
|
"main.js"
|
|
13
20
|
],
|
|
14
|
-
"publishConfig": {
|
|
15
|
-
"access": "public",
|
|
16
|
-
"registry": "https://registry.npmjs.org"
|
|
17
|
-
},
|
|
18
21
|
"scripts": {
|
|
19
|
-
"clean": "rm -rf lib ",
|
|
20
22
|
"build": "ray build --type=component --transform-mode=pure",
|
|
23
|
+
"clean": "rm -rf lib ",
|
|
21
24
|
"watch": "tsc -p ./tsconfig.build.json --module esnext --outDir lib --watch"
|
|
22
25
|
},
|
|
23
26
|
"dependencies": {
|
|
24
|
-
"@ray-core/runtime": "^0.3.
|
|
27
|
+
"@ray-core/runtime": "^0.3.6"
|
|
25
28
|
},
|
|
26
29
|
"devDependencies": {
|
|
27
|
-
"@ray-js/cli": "^1.4.0-alpha.
|
|
28
|
-
"@ray-js/types": "^1.4.0-alpha.
|
|
30
|
+
"@ray-js/cli": "^1.4.0-alpha.10",
|
|
31
|
+
"@ray-js/types": "^1.4.0-alpha.10"
|
|
29
32
|
},
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
],
|
|
36
|
-
"gitHead": "4340240aafe16c03853241e2356134610e65443e",
|
|
37
|
-
"repository": {}
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public",
|
|
35
|
+
"registry": "https://registry.npmjs.org"
|
|
36
|
+
},
|
|
37
|
+
"gitHead": "14cb935df10f020e714b44bb41d4899646db751c"
|
|
38
38
|
}
|
package/LICENSE.md
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
Copyright © 2014-2022 Tuya.inc
|
|
2
|
-
|
|
3
|
-
MIT License
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
-
|
|
7
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
-
|
|
9
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|