@ray-js/runtime 0.3.0-beta.1c347991

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.
@@ -0,0 +1,26 @@
1
+ /**
2
+ * 管理 web 页面生命周期的调用
3
+ * 小程序端
4
+ * TODO: 支持 RN
5
+ */
6
+ import { PageLifecycleKeys, PageLifecycleParams } from '@ray-js/types';
7
+ export declare const generateUniqueId: () => number;
8
+ export declare const generatePageKey: (uniqueId: number) => string;
9
+ export declare class PageInstance {
10
+ uniqueId: number;
11
+ private readonly lifecycleCallback;
12
+ constructor(uniqueId?: number);
13
+ /**
14
+ * 注册生命周期事件, 页面运行底层实例
15
+ * @param lifecycleName - 事件名
16
+ * @param context - fn 上下文
17
+ * @param fn - 回调函数
18
+ */
19
+ registerLifecycle(lifecycleName: PageLifecycleKeys, context: any, fn: (...args: any[]) => any): () => void;
20
+ /**
21
+ * 触发生命周期函数
22
+ * @param lifecycleName
23
+ * @param args
24
+ */
25
+ callLifecycle(params: PageLifecycleParams): any;
26
+ }
@@ -0,0 +1,79 @@
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.function.name.js";
7
+ import "core-js/modules/es.object.to-string.js";
8
+ import "core-js/modules/web.dom-collections.for-each.js";
9
+
10
+ /**
11
+ * 管理 web 页面生命周期的调用
12
+ * 小程序端
13
+ * TODO: 支持 RN
14
+ */
15
+ var UniqueId = 0;
16
+ export var generateUniqueId = function () {
17
+ return UniqueId += 1;
18
+ };
19
+ export var generatePageKey = function (uniqueId) {
20
+ return 'Page-' + uniqueId;
21
+ };
22
+ export var PageInstance = /*#__PURE__*/function () {
23
+ function PageInstance(uniqueId) {
24
+ _classCallCheck(this, PageInstance);
25
+
26
+ this.uniqueId = uniqueId !== null && uniqueId !== void 0 ? uniqueId : generateUniqueId();
27
+ this.lifecycleCallback = {};
28
+ }
29
+ /**
30
+ * 注册生命周期事件, 页面运行底层实例
31
+ * @param lifecycleName - 事件名
32
+ * @param context - fn 上下文
33
+ * @param fn - 回调函数
34
+ */
35
+
36
+
37
+ _createClass(PageInstance, [{
38
+ key: "registerLifecycle",
39
+ value: function registerLifecycle(lifecycleName, context, fn) {
40
+ var _this = this;
41
+
42
+ this.lifecycleCallback[lifecycleName] = this.lifecycleCallback[lifecycleName] || [];
43
+ var fnObj = {
44
+ context: context,
45
+ fn: fn
46
+ }; // 先注册的函数先调用
47
+
48
+ this.lifecycleCallback[lifecycleName].push(fnObj);
49
+ return function () {
50
+ _this.lifecycleCallback[lifecycleName].splice(_this.lifecycleCallback[lifecycleName].indexOf(fnObj), 1);
51
+ };
52
+ }
53
+ /**
54
+ * 触发生命周期函数
55
+ * @param lifecycleName
56
+ * @param args
57
+ */
58
+
59
+ }, {
60
+ key: "callLifecycle",
61
+ value: function callLifecycle(params) {
62
+ var callbacks = this.lifecycleCallback[params.name] || [];
63
+ var result;
64
+
65
+ _toConsumableArray(callbacks).forEach(function (_ref) {
66
+ var _ref$context = _ref.context,
67
+ context = _ref$context === void 0 ? null : _ref$context,
68
+ fn = _ref.fn;
69
+ result = fn.apply(context, Array.isArray(params.args) ? params.args : [params.args]);
70
+ });
71
+
72
+ if (result) {
73
+ return result;
74
+ }
75
+ }
76
+ }]);
77
+
78
+ return PageInstance;
79
+ }();
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+ import { PageConfig } from '@ray-js/types';
3
+ import { PageInstance } from './PageInstance';
4
+ declare const PageInstanceContext: React.Context<{
5
+ $instance: PageInstance;
6
+ pageConfig: PageConfig;
7
+ }>;
8
+ export { PageInstanceContext };
@@ -0,0 +1,4 @@
1
+ import React from 'react';
2
+ var PageInstanceContext = /*#__PURE__*/React.createContext(null);
3
+ PageInstanceContext.displayName = 'PageInstanceContext';
4
+ export { PageInstanceContext };
@@ -0,0 +1,9 @@
1
+ import { AppInstance } from './instance';
2
+ declare type Current = {
3
+ app: null | AppInstance;
4
+ page: null;
5
+ renderOptions: {};
6
+ };
7
+ export declare const current: Current;
8
+ export declare function getCurrent(): Current;
9
+ export {};
package/lib/current.js ADDED
@@ -0,0 +1,8 @@
1
+ export var current = {
2
+ app: null,
3
+ page: null,
4
+ renderOptions: {}
5
+ };
6
+ export function getCurrent() {
7
+ return current;
8
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ export { lifecycle } from './lifecycle';
2
+ export { PageInstance } from './PageInstance';
3
+ export { PageInstanceContext } from './PageInstanceContext';
4
+ export { useAppEvent } from './useAppEvent';
5
+ export { usePageEvent } from './usePageEvent';
6
+ export { withPageLifecycle } from './withPageLifecycle';
7
+ export { getCurrent } from './current';
package/lib/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export { lifecycle } from './lifecycle';
2
+ export { PageInstance } from './PageInstance';
3
+ export { PageInstanceContext } from './PageInstanceContext';
4
+ export { useAppEvent } from './useAppEvent'; // 页面事件
5
+
6
+ export { usePageEvent } from './usePageEvent';
7
+ export { withPageLifecycle } from './withPageLifecycle';
8
+ export { getCurrent } from './current';
@@ -0,0 +1,26 @@
1
+ export interface AppInstance {
2
+ onLaunch?: (params: {
3
+ path: string;
4
+ query: object;
5
+ params: object;
6
+ }) => void;
7
+ onShow?: (params: {
8
+ path: string;
9
+ query: object;
10
+ params: object;
11
+ }) => void;
12
+ onHide?: () => void;
13
+ onError?: (error: string) => void;
14
+ onPageNotFound?: (params: {
15
+ path: string;
16
+ query: object;
17
+ isEntryPage: boolean;
18
+ }) => void;
19
+ onUnhandledRejection?: (params: {
20
+ reason: string;
21
+ promise: Promise<any>;
22
+ }) => void;
23
+ onThemeChange?: (params: {
24
+ theme: 'dark' | 'light';
25
+ }) => void;
26
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,23 @@
1
+ import { PageLifecycleParams } from '@ray-js/types';
2
+ import { PageInstance } from './PageInstance';
3
+ declare class Lifecycle {
4
+ private readonly instanceMap;
5
+ constructor();
6
+ /**
7
+ * 增加一个 PageWrapper 实例
8
+ *
9
+ * @param uniqueId - 页面的 ID
10
+ * @param ins - 示例对象
11
+ */
12
+ addInstance(ins: PageInstance): void;
13
+ /**
14
+ * 删除一个 PageWrapper 实例
15
+ *
16
+ * @param uniqueId - 页面的 ID
17
+ * @param ins - 示例对象
18
+ */
19
+ removeInstance(ins: PageInstance): void;
20
+ emit(params: PageLifecycleParams): void;
21
+ }
22
+ export declare const lifecycle: Lifecycle;
23
+ export {};
@@ -0,0 +1,58 @@
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
+ 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);
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
+ }
52
+ }
53
+ }]);
54
+
55
+ return Lifecycle;
56
+ }();
57
+
58
+ export var lifecycle = new Lifecycle();
@@ -0,0 +1 @@
1
+ export declare function useAppEvent(event: string, fn: () => any): void;
@@ -0,0 +1,2 @@
1
+ export function useAppEvent() {// TODO: App 事件
2
+ }
@@ -0,0 +1,2 @@
1
+ import { PageLifecycleKeys } from '@ray-js/types';
2
+ export declare function usePageEvent(event: PageLifecycleKeys, fn: (...args: any[]) => any): void;
@@ -0,0 +1,6 @@
1
+ import React from 'react';
2
+ import { PageInstanceContext } from '../PageInstanceContext';
3
+ export function usePageEvent(event, fn) {
4
+ var $instance = React.useContext(PageInstanceContext).$instance;
5
+ $instance.registerLifecycle(event, null, fn);
6
+ }
@@ -0,0 +1,2 @@
1
+ import React from 'react';
2
+ export declare function withPageLifecycle<P extends object>(Component: React.ComponentType<P>): React.ForwardRefExoticComponent<React.RefAttributes<unknown>>;
@@ -0,0 +1,80 @@
1
+ import _extends from "@babel/runtime/helpers/esm/extends";
2
+ import _typeof from "@babel/runtime/helpers/esm/typeof";
3
+ import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
4
+ import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
5
+ import _createClass from "@babel/runtime/helpers/esm/createClass";
6
+ import _inherits from "@babel/runtime/helpers/esm/inherits";
7
+ import _createSuper from "@babel/runtime/helpers/esm/createSuper";
8
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
9
+ var _excluded = ["forwardedRef"];
10
+ import "core-js/modules/es.function.name.js";
11
+ import "core-js/modules/es.object.to-string.js";
12
+ import "core-js/modules/web.dom-collections.for-each.js";
13
+ import "core-js/modules/es.object.keys.js";
14
+ import React from 'react';
15
+ import { pageLifecycles } from '@ray-js/types';
16
+ import { PageInstanceContext } from '../PageInstanceContext';
17
+ export function withPageLifecycle(Component) {
18
+ var displayName = "WithPageLifecycle(".concat(Component.displayName || Component.name, ")");
19
+
20
+ var WithPageLifeCycle = /*#__PURE__*/function (_React$Component) {
21
+ _inherits(WithPageLifeCycle, _React$Component);
22
+
23
+ var _super = _createSuper(WithPageLifeCycle);
24
+
25
+ function WithPageLifeCycle() {
26
+ _classCallCheck(this, WithPageLifeCycle);
27
+
28
+ return _super.apply(this, arguments);
29
+ }
30
+
31
+ _createClass(WithPageLifeCycle, [{
32
+ key: "render",
33
+ value: function render() {
34
+ var _this$props = this.props,
35
+ forwardedRef = _this$props.forwardedRef,
36
+ rest = _objectWithoutProperties(_this$props, _excluded);
37
+
38
+ return /*#__PURE__*/React.createElement(PageInstanceContext.Consumer, null, function (context) {
39
+ var $instance = context.$instance; // 挂载页面事件
40
+
41
+ function additionToLifecycle(ins) {
42
+ Object.keys(pageLifecycles).forEach(function (event) {
43
+ if (typeof ins[event] === 'function') {
44
+ $instance.registerLifecycle(event, ins, ins[event]);
45
+ }
46
+ });
47
+ }
48
+
49
+ return /*#__PURE__*/React.createElement(Component, _extends({
50
+ ref: function ref(_ref) {
51
+ if (_ref) {
52
+ additionToLifecycle(_ref);
53
+ }
54
+
55
+ if (forwardedRef) {
56
+ if (typeof forwardedRef === 'function') {
57
+ forwardedRef(_ref);
58
+ } else if (_typeof(forwardedRef) === 'object') {
59
+ forwardedRef.current = _ref;
60
+ }
61
+ }
62
+ }
63
+ }, rest));
64
+ });
65
+ }
66
+ }]);
67
+
68
+ return WithPageLifeCycle;
69
+ }(React.Component);
70
+
71
+ _defineProperty(WithPageLifeCycle, "displayName", displayName);
72
+
73
+ var forwardRef = /*#__PURE__*/React.forwardRef(function (props, ref) {
74
+ return /*#__PURE__*/React.createElement(WithPageLifeCycle, _extends({}, props, {
75
+ forwardedRef: ref
76
+ }));
77
+ });
78
+ forwardRef.displayName = displayName;
79
+ return forwardRef;
80
+ }
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@ray-js/runtime",
3
+ "version": "0.3.0-beta.1c347991",
4
+ "description": "Ray cross runtime",
5
+ "keywords": [
6
+ "ray"
7
+ ],
8
+ "author": "子长 <zichang.nong@tuya.com>",
9
+ "license": "MIT",
10
+ "main": "lib/index.js",
11
+ "files": [
12
+ "lib"
13
+ ],
14
+ "publishConfig": {
15
+ "access": "public",
16
+ "registry": "https://registry.npmjs.org"
17
+ },
18
+ "scripts": {
19
+ "clean": "rm -rf lib ",
20
+ "build": "ray build --type=component --transform-mode=pure",
21
+ "watch": "tsc -p ./tsconfig.build.json --module esnext --outDir lib --watch"
22
+ },
23
+ "devDependencies": {
24
+ "@ray-js/cli": "^0.3.0-beta.1c347991",
25
+ "@ray-js/types": "^0.3.0-beta.1c347991"
26
+ },
27
+ "maintainers": [
28
+ {
29
+ "name": "tuyafe",
30
+ "email": "tuyafe@tuya.com"
31
+ }
32
+ ],
33
+ "gitHead": "e0bd013022ddda63380d3c9e20fd8cadb46cd61f",
34
+ "repository": {}
35
+ }