@bytedance/mona-client-plugin
Version:
plugin for mona
197 lines • 8.45 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 __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 __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 React from 'react';
import { LifecycleContext, AppLifecycleGlobalContext, AppLifecycle, PageLifecycle, PageLifecycleGlobalContext, } from '@bytedance/mona';
import { isClassComponent, GLOBAL_LIFECYCLE_STORE, parseSearch } from '@bytedance/mona-shared';
import { lightAppLifeCycleParamsKey } from './constants';
export function createPluginLifeCycle(Component) {
var appLifecycleContext = new LifecycleContext();
var appEntryRef = React.createRef();
var callLifecycle = function (callbackName) {
var _a;
var _b;
var params = [];
for (var _i = 1; _i < arguments.length; _i++) {
params[_i - 1] = arguments[_i];
}
var cbs = appLifecycleContext.lifecycle[callbackName] || new Set([]);
Array.from(cbs).forEach(function (cb) { return cb.apply(void 0, params); });
if ((_b = appEntryRef.current) === null || _b === void 0 ? void 0 : _b[callbackName]) {
return (_a = appEntryRef.current)[callbackName].apply(_a, params);
}
};
var handleShow = function () {
var rest = [];
for (var _i = 0; _i < arguments.length; _i++) {
rest[_i] = arguments[_i];
}
callLifecycle.apply(void 0, __spreadArray([AppLifecycle.show], rest, false));
};
var handleHide = function () {
var rest = [];
for (var _i = 0; _i < arguments.length; _i++) {
rest[_i] = arguments[_i];
}
callLifecycle.apply(void 0, __spreadArray([AppLifecycle.hide], rest, false));
};
var handleLaunch = function () {
var rest = [];
for (var _i = 0; _i < arguments.length; _i++) {
rest[_i] = arguments[_i];
}
callLifecycle.apply(void 0, __spreadArray([AppLifecycle.launch], rest, false));
};
var handlePageNotFound = function () {
var rest = [];
for (var _i = 0; _i < arguments.length; _i++) {
rest[_i] = arguments[_i];
}
callLifecycle.apply(void 0, __spreadArray([AppLifecycle.pageNotFound], rest, false));
};
// 这里名称不要随便改,微应用会在容器层调用部分函数
//@ts-ignore
window[GLOBAL_LIFECYCLE_STORE] = {
handleLaunch: handleLaunch,
handlePageNotFound: handlePageNotFound,
handleShow: handleShow,
handleHide: handleHide,
};
var AppConfig = /** @class */ (function (_super) {
__extends(AppConfig, _super);
function AppConfig() {
return _super !== null && _super.apply(this, arguments) || this;
}
AppConfig.prototype.componentDidMount = function () {
handleShow(window[lightAppLifeCycleParamsKey.show] || {});
handleLaunch(window[lightAppLifeCycleParamsKey.launch] || {});
};
// componentWillUnmount() {}
AppConfig.prototype.render = function () {
if (isClassComponent(Component)) {
return React.createElement(Component, __assign({}, this.props, { ref: appEntryRef }));
}
return React.createElement(Component, __assign({}, this.props));
};
return AppConfig;
}(React.Component));
return function (props) { return (React.createElement(AppLifecycleGlobalContext.Provider, { value: appLifecycleContext },
React.createElement(AppConfig, __assign({}, props)))); };
}
export function createPluginPageLifecycle(Component) {
var PageLifecycleContext = new LifecycleContext();
var pageEntryRef = React.createRef();
var callLifecycle = function (callbackName) {
var _a;
var _b;
var params = [];
for (var _i = 1; _i < arguments.length; _i++) {
params[_i - 1] = arguments[_i];
}
var cbs = PageLifecycleContext.lifecycle[callbackName] || new Set([]);
Array.from(cbs).forEach(function (cb) { return cb.apply(void 0, params); });
if ((_b = pageEntryRef.current) === null || _b === void 0 ? void 0 : _b[callbackName]) {
return (_a = pageEntryRef.current)[callbackName].apply(_a, params);
}
};
// const handleResize = (...rest: any[]) => {
// callLifecycle(PageLifecycle.resize, ...rest);
// };
var handleLoad = function () {
var rest = [];
for (var _i = 0; _i < arguments.length; _i++) {
rest[_i] = arguments[_i];
}
callLifecycle.apply(void 0, __spreadArray([PageLifecycle.load], rest, false));
};
var handleUnload = function () {
var rest = [];
for (var _i = 0; _i < arguments.length; _i++) {
rest[_i] = arguments[_i];
}
callLifecycle.apply(void 0, __spreadArray([PageLifecycle.unload], rest, false));
};
var handleReady = function () {
var rest = [];
for (var _i = 0; _i < arguments.length; _i++) {
rest[_i] = arguments[_i];
}
callLifecycle.apply(void 0, __spreadArray([PageLifecycle.ready], rest, false));
};
// const handleVisibilityChange = () => {
// if (document.visibilityState === 'visible') {
// callLifecycle(PageLifecycle.show);
// } else {
// callLifecycle(PageLifecycle.hide);
// }
// };
// document.addEventListener('visibilitychange', handleVisibilityChange);
// onResize
// window.addEventListener('resize', handleResize);
var PageWrapper = /** @class */ (function (_super) {
__extends(PageWrapper, _super);
function PageWrapper() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.isReachBottom = false;
return _this;
}
PageWrapper.prototype.componentDidMount = function () {
// onLoad
handleLoad(parseSearch(location.search));
// onShow & onHide
// if (document.visibilityState === 'visible') {
// callLifecycle(PageLifecycle.show);
// }
// onReady
handleReady();
};
PageWrapper.prototype.componentWillUnmount = function () {
// onUnload
handleUnload();
// window.removeEventListener('resize', handleResize);
// document.removeEventListener('visibilitychange', handleVisibilityChange);
};
PageWrapper.prototype.render = function () {
if (isClassComponent(Component)) {
return React.createElement(Component, __assign({}, this.props, { ref: pageEntryRef }));
}
return React.createElement(Component, __assign({}, this.props));
};
return PageWrapper;
}(React.Component));
return function (props) { return (React.createElement(PageLifecycleGlobalContext.Provider, { value: PageLifecycleContext },
React.createElement(PageWrapper, __assign({}, props)))); };
}
//# sourceMappingURL=createPluginLifeCycle.js.map