UNPKG

@ray-core/runtime

Version:

Ray 是一个全新的基于 React 的小程序开发框架

214 lines (213 loc) 8.86 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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)); }; import VNode from './VNode'; import { unstable_batchedUpdates } from './index'; import { SLOT_PLACEHOLDER } from './constants'; import { gStore } from './gStore'; import { createCallbackProxy } from './SyntheticEvent/createCallbackProxy'; var AppContainer = /** @class */ (function () { function AppContainer(context, rootKey) { if (rootKey === void 0) { rootKey = 'root'; } this.updateQueue = []; this.cbIds = []; this._slot = { idsCache: [], node: undefined }; this._pageSlot = null; this.context = context; this.root = new VNode({ type: 'root', container: this, }); this.root.mounted = true; this.rootKey = rootKey; } AppContainer.prototype.requestUpdate = function (update) { this.updateQueue.push(update); }; AppContainer.prototype.normalizeUpdatePath = function (paths) { return __spreadArray([this.rootKey], __read(paths), false).join('.'); }; AppContainer.prototype.getPageSlotParentInfo = function () { var slot = this._pageSlot; if (!slot) { return; } var parent = slot.parent; var i = parent.children.findIndex(function (item) { return slot.id === item.id; }); return { slotIndexAtParentChildren: i, parentPath: parent.path, parentChildren: parent.children.map(function (c) { return c.id; }), }; }; AppContainer.prototype.applyUpdate = function () { var _this = this; var payload = this.handlePayload(); // console.log('>>>>> app container applyUpdate', payload); if (payload) { this.context.setData(payload); } this.context._pages.forEach(function (page) { _this.cbIds.forEach(function (id) { page[id] = gStore.getCallback(id); }); page.container.applyUpdate(_this); page.modalContainer.applyUpdate(); }); // 页面渲染完成后 if (this.context._pages.length) { this.updateQueue = []; } }; AppContainer.prototype.handlePayload = function () { var _this = this; if (this.stopUpdate || this.updateQueue.length === 0) { return; } var updatePayload = this.updateQueue.reduce(function (acc, update) { if (update.node.isDeleted()) { return acc; } if (update.type === 'splice') { acc[_this.normalizeUpdatePath(__spreadArray(__spreadArray([], __read(update.path), false), ['nodes', update.id.toString()], false))] = update.raw || null; if (update.children) { acc[_this.normalizeUpdatePath(__spreadArray(__spreadArray([], __read(update.path), false), ['children'], false))] = (update.children || []).map(function (c) { return c.id; }); } } else { acc[_this.normalizeUpdatePath(__spreadArray(__spreadArray([], __read(update.path), false), [update.name], false))] = update.value; } return acc; }, {}); return updatePayload; }; AppContainer.prototype.clearUpdate = function () { this.stopUpdate = true; }; AppContainer.prototype.createCallback = function (id, propKey, node, fn) { var proxyHandle = createCallbackProxy(propKey, node, fn); function cbOrEventHandler() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } return unstable_batchedUpdates(function (args) { return proxyHandle.apply(void 0, __spreadArray([], __read(args), false)); }, args); } cbOrEventHandler.__original = fn; // 微信端:除input事件需要挂在实例上外,其他事件都可以挂在原型上 if (/^(onInput|bindinput)$/.test(propKey)) { this.cbIds.push(id); } gStore.setCallback(id, cbOrEventHandler); }; AppContainer.prototype.removeCallback = function (name) { gStore.unsetCallback(name); // delete this.context[name]; this.cbIds = this.cbIds.filter(function (n) { return n !== name; }); }; AppContainer.prototype.appendChild = function (child) { this.root.appendChild(child); }; AppContainer.prototype.removeChild = function (child) { this.root.removeChild(child); }; AppContainer.prototype.insertBefore = function (child, beforeChild) { this.root.insertBefore(child, beforeChild); }; AppContainer.prototype.replaceSlot = function (parent, child, index) { var _a, _b; if ((_a = child.text) === null || _a === void 0 ? void 0 : _a.startsWith(SLOT_PLACEHOLDER)) { var slotChildren = ((_b = this._slot.node) === null || _b === void 0 ? void 0 : _b.children) || []; // 页面中Ray构建的原生自定义组件根节点 // 子节点 slotChildren = slotChildren.map(function (item) { return ((item.parent = parent), item); }); var curChildren = parent.children; if (typeof index === 'number') { // 首次插入中间位置 curChildren.splice.apply(curChildren, __spreadArray([index, 0], __read(slotChildren), false)); } else { var idsCache = this._slot.idsCache; if (idsCache.length === 0) { curChildren.push.apply(curChildren, __spreadArray([], __read(slotChildren), false)); // 首次追加到尾部 } else { // 更新 var start = -1; for (var i = 0; i < curChildren.length; i++) { var item = curChildren[i]; if (~idsCache.indexOf(item.id)) { curChildren.splice(i, 1); if (start < 0) start = i; i--; } } curChildren.splice.apply(curChildren, __spreadArray([start, 0], __read(slotChildren), false)); } } this._slot.idsCache = slotChildren.map(function (item) { return item.id; }); parent.children = curChildren; return slotChildren; } }; AppContainer.prototype.removeSlot = function (parent, child) { var _a; if (!((_a = child.text) === null || _a === void 0 ? void 0 : _a.startsWith(SLOT_PLACEHOLDER))) { return; } if (this._slot.node) { this._slot.node.children.forEach(function (slot) { parent.children = parent.children.filter(function (child) { return child.id !== slot.id; }); }); this._slot.idsCache = []; this._slot.node = undefined; } }; AppContainer.prototype.copyVNode = function (node, option) { var _this = this; var fn = function (node, opts) { var o = Object.create(Object.getPrototypeOf(node), Object.getOwnPropertyDescriptors(node)); o.children = o.children.map(function (c) { return fn(c, { parent: o, container: _this }); }); return Object.assign(o, opts); }; return fn(node, __assign(__assign({}, option), { container: this })); }; return AppContainer; }()); export default AppContainer;