@ray-core/runtime
Version:
Ray 是一个全新的基于 React 的小程序开发框架
228 lines (227 loc) • 10.1 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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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.");
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
require("./polyfills/Function");
var React = __importStar(require("react"));
var react_is_1 = require("react-is");
var framework_shared_1 = require("@ray-core/framework-shared");
var get_1 = __importDefault(require("lodash-es/get"));
var set_1 = __importDefault(require("lodash-es/set"));
var render_1 = __importDefault(require("./render"));
var AppContainer_1 = __importDefault(require("./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));
function createAppConfig(App) {
var WrappedApp = framework_shared_1.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 = (0, get_1.default)(_data, parentPath);
if (!(0, get_1.default)(_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;
}
(0, set_1.default)(_data, p, v);
}
},
_instance: React.createRef(),
onLaunch: function (options) {
_container = new AppContainer_1.default(this);
this._render();
return this.callLifecycle(framework_shared_1.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 = framework_shared_1.AppInstanceContext.lifecycleCallback[lifecycle] || [];
var result;
callbacks.forEach(function (callback) {
result = callback.apply(void 0, __spreadArray([], __read(args), false));
});
if (result) {
return result;
}
var callback = (0, framework_shared_1.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 ((0, framework_shared_1.isClassComponent)(AppComponent) || AppComponent.$$typeof === react_is_1.ForwardRef) {
props.ref = this._instance;
}
return (0, render_1.default)(React.createElement(AppComponent, props, React.createElement('page-slot'), this._pages.map(function (p) { return p.element; })), _container);
},
onShow: function (options) {
return this.callLifecycle(framework_shared_1.AppLifecycle.show, options);
},
onHide: function () {
return this.callLifecycle(framework_shared_1.AppLifecycle.hide);
},
onError: function (error) {
return this.callLifecycle(framework_shared_1.AppLifecycle.error, error);
},
// 微信
onPageNotFound: function (options) {
return this.callLifecycle(framework_shared_1.AppLifecycle.pageNotFound, options);
},
// 微信
onUnhandledRejection: function (options) {
return this.callLifecycle(framework_shared_1.AppLifecycle.unhandledRejection, options);
},
// 微信
onThemeChange: function (options) {
return this.callLifecycle(framework_shared_1.AppLifecycle.themeChange, options);
},
};
var lifecycleEvent = {
// 阿里
onShareAppMessage: function (options) {
return this.callLifecycle(framework_shared_1.AppLifecycle.shareAppMessage, options);
},
};
try {
for (var _b = __values(framework_shared_1.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 framework_shared_1.RuntimeOptions.get('pluginDriver').onAppConfig(config);
};
return createConfig(WrappedApp);
}
exports.default = createAppConfig;