@ray-js/runtime 1.2.0-beta.0 → 1.2.0-beta.1

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,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
- var UniqueId = 0;
15
- export var generateUniqueId = function () {
16
- return UniqueId += 1;
6
+ let UniqueId = 0;
7
+ export const generateUniqueId = () => {
8
+ return (UniqueId += 1);
17
9
  };
18
- export var generatePageKey = function (uniqueId) {
19
- return 'Page-' + uniqueId;
10
+ export const generatePageKey = (uniqueId) => {
11
+ return 'Page-' + uniqueId;
20
12
  };
21
- export var PageInstance = /*#__PURE__*/function () {
22
- function PageInstance(uniqueId) {
23
- _classCallCheck(this, PageInstance);
24
-
25
- this.uniqueId = uniqueId !== null && uniqueId !== void 0 ? uniqueId : generateUniqueId();
26
- this.lifecycleCallback = {};
27
- }
28
- /**
29
- * 注册生命周期事件, 页面运行底层实例
30
- * @param lifecycleName - 事件名
31
- * @param context - fn 上下文
32
- * @param fn - 回调函数
33
- */
34
-
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
- };
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
- 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
- }
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
- var PageInstanceContext = /*#__PURE__*/React.createContext(null);
2
+ const PageInstanceContext = React.createContext(null);
3
3
  PageInstanceContext.displayName = 'PageInstanceContext';
4
- export { PageInstanceContext };
4
+ export { PageInstanceContext };
package/lib/current.js CHANGED
@@ -1,8 +1,8 @@
1
- export var current = {
2
- app: null,
3
- page: null,
4
- renderOptions: {}
1
+ export const current = {
2
+ app: null,
3
+ page: null,
4
+ renderOptions: {},
5
5
  };
6
6
  export function getCurrent() {
7
- return current;
8
- }
7
+ return current;
8
+ }
package/lib/index.js CHANGED
@@ -1,21 +1,15 @@
1
1
  export { lifecycle } from './lifecycle';
2
2
  export { PageInstance } from './PageInstance';
3
3
  export { PageInstanceContext } from './PageInstanceContext';
4
- export { useAppEvent } from './useAppEvent'; // 页面事件
5
-
4
+ export { useAppEvent } from './useAppEvent';
5
+ // 页面事件
6
6
  export { usePageEvent } from './usePageEvent';
7
7
  export { withPageLifecycle } from './withPageLifecycle';
8
8
  export { getCurrent } from './current';
9
- export var useNativeEffect = function () {
10
- return console.warn("// TODO \u6682\u672A\u5B9E\u73B0\u94A9\u5B50 'useNativeEffect'");
9
+ export const useNativeEffect = () => console.warn(`// TODO 暂未实现钩子 'useNativeEffect'`);
10
+ export const useQuery = () => {
11
+ console.warn(`// TODO 暂未实现钩子 'useQuery'`);
12
+ return null;
11
13
  };
12
- export var useQuery = function () {
13
- console.warn("// TODO \u6682\u672A\u5B9E\u73B0\u94A9\u5B50 'useQuery'");
14
- return null;
15
- };
16
- export var usePageInstance = function () {
17
- return console.warn("// TODO \u6682\u672A\u5B9E\u73B0\u94A9\u5B50 'usePageInstance'");
18
- };
19
- export var useComponentInstance = function () {
20
- return console.warn("// TODO \u6682\u672A\u5B9E\u73B0\u94A9\u5B50 'useComponentInstance'");
21
- };
14
+ export const usePageInstance = () => console.warn(`// TODO 暂未实现钩子 'usePageInstance'`);
15
+ export const useComponentInstance = () => console.warn(`// TODO 暂未实现钩子 'useComponentInstance'`);
package/lib/index.mini.js CHANGED
@@ -2,4 +2,4 @@ export * from '@ray-core/runtime';
2
2
  export { withPageLifecycle } from './withPageLifecycle';
3
3
  export { lifecycle } from './lifecycle';
4
4
  export { PageInstance } from './PageInstance';
5
- export { PageInstanceContext } from './PageInstanceContext';
5
+ export { PageInstanceContext } from './PageInstanceContext';
package/lib/instance.js CHANGED
@@ -1 +1 @@
1
- export {};
1
+ export {};
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
- var Lifecycle = /*#__PURE__*/function () {
11
- function Lifecycle() {
12
- _classCallCheck(this, Lifecycle);
13
-
14
- this.instanceMap = new Map();
15
- }
16
- /**
17
- * 增加一个 PageWrapper 实例
18
- *
19
- * @param uniqueId - 页面的 ID
20
- * @param ins - 示例对象
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
- key: "removeInstance",
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
- 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
- }
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,2 +1,3 @@
1
- export function useAppEvent() {// TODO: App 事件
2
- }
1
+ export function useAppEvent(event, fn) {
2
+ // TODO: App 事件
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;
5
- $instance.registerLifecycle(event, null, fn);
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": "1.2.0-beta.0",
3
+ "version": "1.2.0-beta.1",
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.3.0-beta.1"
24
+ "@ray-core/runtime": "^0.3.0-beta.2"
25
25
  },
26
26
  "devDependencies": {
27
- "@ray-js/cli": "^1.2.0-beta.0",
28
- "@ray-js/types": "^1.2.0-beta.0"
27
+ "@ray-js/cli": "^1.2.0-beta.1",
28
+ "@ray-js/types": "^1.2.0-beta.1"
29
29
  },
30
30
  "maintainers": [
31
31
  {
@@ -33,6 +33,6 @@
33
33
  "email": "tuyafe@tuya.com"
34
34
  }
35
35
  ],
36
- "gitHead": "7e74c61f91edf7d331396664808b5223f8a73950",
36
+ "gitHead": "2b34058549219b57afaa6b6f80c1b896e1602d53",
37
37
  "repository": {}
38
38
  }