@ray-core/runtime
Version:
Ray 是一个全新的基于 React 的小程序开发框架
205 lines (204 loc) • 8.68 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import './polyfills/Function';
import * as React from 'react';
import { ForwardRef } from 'react-is';
import { AppInstanceContext, RuntimeOptions, AppLifecycle, callbackName, isClassComponent, } from '@ray-core/framework-shared';
import get from 'lodash-es/get';
import set from 'lodash-es/set';
import render from './render';
import AppContainer from './AppContainer';
var DefaultAppComponent = /** @class */ (function (_super) {
__extends(DefaultAppComponent, _super);
function DefaultAppComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
DefaultAppComponent.prototype.render = function () {
return React.createElement(React.Fragment, null, this.props.children);
};
return DefaultAppComponent;
}(React.Component));
export default function createAppConfig(App) {
var WrappedApp = RuntimeOptions.get('pluginDriver').onAppComponent(App);
var createConfig = function (AppComponent) {
var e_1, _a;
if (AppComponent === void 0) { AppComponent = DefaultAppComponent; }
var _data = {};
var _container = null;
var config = {
_pages: [],
getData: function () {
return JSON.parse(JSON.stringify(_data));
},
setData: function (d) {
var props = Object.keys(d);
for (var i = 0; i < props.length; i++) {
var p = props[i];
var v = d[p];
var pSlices = p.split('.');
var parentPath = '';
for (var j = 0; j < pSlices.length; j++) {
var piece = pSlices[j];
var path = !parentPath ? piece : "".concat(parentPath, ".").concat(piece);
var parentV = get(_data, parentPath);
if (!get(_data, path)) {
if (path === 'root' || path === 'modalRoot') {
_data[path] = {};
}
else if (/\.nodes$/.test(path)) {
parentV.nodes = {};
}
else if (/\.nodes\.\d+$/.test(path)) {
parentV[piece] = {};
}
else if (/\.children$/.test(path)) {
parentV.children = [];
}
}
parentPath = path;
}
set(_data, p, v);
}
},
_instance: React.createRef(),
onLaunch: function (options) {
_container = new AppContainer(this);
this._render();
return this.callLifecycle(AppLifecycle.launch, options);
},
callLifecycle: function (lifecycle) {
var _a;
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var callbacks = AppInstanceContext.lifecycleCallback[lifecycle] || [];
var result;
callbacks.forEach(function (callback) {
result = callback.apply(void 0, __spreadArray([], __read(args), false));
});
if (result) {
return result;
}
var callback = callbackName(lifecycle);
if (this._instance.current && this._instance.current[callback]) {
return (_a = this._instance.current)[callback].apply(_a, __spreadArray([], __read(args), false));
}
},
_mount: function (pageInstance) {
this._pages.push(pageInstance);
this._render();
},
_unmount: function (pageInstance) {
this._pages.splice(this._pages.indexOf(pageInstance), 1);
this._render();
},
_render: function () {
var props = { asSubPackageRuntimeOptions: this.asSubPackageRuntimeOptions };
if (isClassComponent(AppComponent) || AppComponent.$$typeof === ForwardRef) {
props.ref = this._instance;
}
return render(React.createElement(AppComponent, props, React.createElement('page-slot'), this._pages.map(function (p) { return p.element; })), _container);
},
onShow: function (options) {
return this.callLifecycle(AppLifecycle.show, options);
},
onHide: function () {
return this.callLifecycle(AppLifecycle.hide);
},
onPause: function () {
return this.callLifecycle(AppLifecycle.pause);
},
onResume: function () {
return this.callLifecycle(AppLifecycle.resume);
},
onError: function (error) {
return this.callLifecycle(AppLifecycle.error, error);
},
// 微信
onPageNotFound: function (options) {
return this.callLifecycle(AppLifecycle.pageNotFound, options);
},
// 微信
onUnhandledRejection: function (options) {
return this.callLifecycle(AppLifecycle.unhandledRejection, options);
},
// 微信
onThemeChange: function (options) {
return this.callLifecycle(AppLifecycle.themeChange, options);
},
};
var lifecycleEvent = {
// 阿里
onShareAppMessage: function (options) {
return this.callLifecycle(AppLifecycle.shareAppMessage, options);
},
};
try {
for (var _b = __values(RuntimeOptions.get('appEvents') || []), _c = _b.next(); !_c.done; _c = _b.next()) {
var eventName = _c.value;
if (lifecycleEvent[eventName]) {
config[eventName] = lifecycleEvent[eventName];
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
return RuntimeOptions.get('pluginDriver').onAppConfig(config);
};
return createConfig(WrappedApp);
}