@ray-js/runtime 1.4.0-alpha.0 → 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.
@@ -1,78 +1,60 @@
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
-
1
+ import "core-js/modules/web.dom-collections.iterator.js";
9
2
  /**
10
3
  * 管理 web 页面生命周期的调用
11
4
  * 小程序端
12
5
  * TODO: 支持 RN
13
6
  */
14
- var UniqueId = 0;
15
- export var generateUniqueId = function () {
7
+
8
+ let UniqueId = 0;
9
+ export const generateUniqueId = () => {
16
10
  return UniqueId += 1;
17
11
  };
18
- export var generatePageKey = function (uniqueId) {
12
+ export const generatePageKey = uniqueId => {
19
13
  return 'Page-' + uniqueId;
20
14
  };
21
- export var PageInstance = /*#__PURE__*/function () {
22
- function PageInstance(uniqueId) {
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
- _createClass(PageInstance, [{
37
- key: "registerLifecycle",
38
- value: function registerLifecycle(lifecycleName, context, fn) {
39
- var _this = this;
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
- };
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
+ }
@@ -1,4 +1,4 @@
1
1
  import React from 'react';
2
- var PageInstanceContext = /*#__PURE__*/React.createContext(null);
2
+ const PageInstanceContext = /*#__PURE__*/React.createContext(null);
3
3
  PageInstanceContext.displayName = 'PageInstanceContext';
4
4
  export { PageInstanceContext };
package/lib/current.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AppInstance } from './instance';
2
- declare type Current = {
2
+ type Current = {
3
3
  app: null | AppInstance;
4
4
  page: null;
5
5
  renderOptions: {};
package/lib/current.js CHANGED
@@ -1,4 +1,4 @@
1
- export var current = {
1
+ export const current = {
2
2
  app: null,
3
3
  page: null,
4
4
  renderOptions: {}
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 var useNativeEffect = function () {
11
- return console.warn("// TODO \u6682\u672A\u5B9E\u73B0\u94A9\u5B50 'useNativeEffect'");
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 var usePageInstance = function () {
18
- return console.warn("// TODO \u6682\u672A\u5B9E\u73B0\u94A9\u5B50 'usePageInstance'");
19
- };
20
- export var useComponentInstance = function () {
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
- var Lifecycle = /*#__PURE__*/function () {
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
- _createClass(Lifecycle, [{
25
- key: "addInstance",
26
- value: function addInstance(ins) {
27
- var key = generatePageKey(ins.uniqueId);
28
- this.instanceMap.set(key, ins);
29
- }
30
- /**
31
- * 删除一个 PageWrapper 实例
32
- *
33
- * @param uniqueId - 页面的 ID
34
- * @param ins - 示例对象
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
- return Lifecycle;
56
- }();
57
-
58
- export var lifecycle = new Lifecycle();
35
+ }
36
+ }
37
+ export const lifecycle = new Lifecycle();
@@ -1,2 +1,3 @@
1
- export function useAppEvent() {// TODO: App 事件
1
+ export function useAppEvent() {
2
+ // TODO: App 事件
2
3
  }
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
2
  import { PageInstanceContext } from '../PageInstanceContext';
3
3
  export function usePageEvent(event, fn) {
4
- var $instance = React.useContext(PageInstanceContext).$instance;
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
- var _excluded = ["forwardedRef"];
11
- import "core-js/modules/es.array.concat.js";
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
- var displayName = "WithPageLifecycle(".concat(Component.displayName || Component.name, ")");
20
-
21
- var WithPageLifeCycle = /*#__PURE__*/function (_React$Component) {
22
- _inherits(WithPageLifeCycle, _React$Component);
23
-
24
- var _super = _createSuper(WithPageLifeCycle);
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
- _this.additionToLifecycle(ref);
17
+ this.additionToLifecycle(ref);
42
18
  }
43
-
44
19
  if (forwardedRef) {
45
20
  if (typeof forwardedRef === 'function') {
46
21
  forwardedRef(ref);
47
- } else if (_typeof(forwardedRef) === 'object') {
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
- _createClass(WithPageLifeCycle, [{
57
- key: "render",
58
- value: function render() {
59
- var _this2 = this;
60
-
61
- var _this$props = this.props,
62
- forwardedRef = _this$props.forwardedRef,
63
- rest = _objectWithoutProperties(_this$props, _excluded);
64
-
65
- return /*#__PURE__*/React.createElement(PageInstanceContext.Consumer, null, function (context) {
66
- var $instance = context.$instance; // 挂载页面事件
67
-
68
- _this2.additionToLifecycle = function (ins) {
69
- Object.keys(pageLifecycles).forEach(function (event) {
70
- if (typeof ins[event] === 'function') {
71
- $instance.registerLifecycle(event, ins, ins[event]);
72
- }
73
- });
74
- };
75
-
76
- return /*#__PURE__*/React.createElement(Component // fixed: 坑货,不能直接一个匿名函数,因为每次渲染匿名函数都是个新的,导致每次渲染都会调用一次
77
- // 进而导致生命周期函数重复添加
78
- , _extends({
79
- ref: _this2.refHandle
80
- }, rest));
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.0",
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.5"
27
+ "@ray-core/runtime": "^0.3.6"
25
28
  },
26
29
  "devDependencies": {
27
- "@ray-js/cli": "^1.4.0-alpha.0",
28
- "@ray-js/types": "^1.4.0-alpha.0"
30
+ "@ray-js/cli": "^1.4.0-alpha.10",
31
+ "@ray-js/types": "^1.4.0-alpha.10"
29
32
  },
30
- "maintainers": [
31
- {
32
- "name": "tuyafe",
33
- "email": "tuyafe@tuya.com"
34
- }
35
- ],
36
- "gitHead": "92a9834964bb74c3a3b0553438b0f0c7a77ad8d0",
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.