@bytedance/mona-client-web
Version:
web for mona
143 lines • 6.05 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 } from '@bytedance/mona';
import { isClassComponent } from '@bytedance/mona-shared/dist/reactNode';
import { GLOBAL_LIFECYCLE_STORE } from '@bytedance/mona-shared/dist/constants';
var lightAppLifeCycleParamsKey = {
launch: '__MONA_LIGHT_APP_LIFE_CYCLE_LANUCH_QUERY',
show: '__MONA_LIGHT_APP_LIFE_CYCLE_SHOW_QUERY',
};
export function createAppLifeCycle(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 handleError = function () {
var rest = [];
for (var _i = 0; _i < arguments.length; _i++) {
rest[_i] = arguments[_i];
}
callLifecycle.apply(void 0, __spreadArray([AppLifecycle.error], 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));
};
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 handleVisibilityChange = function () {
if (document.visibilityState === 'visible') {
handleShow(window[lightAppLifeCycleParamsKey.show] || {});
}
else {
handleHide();
}
};
//@ts-ignore
window[GLOBAL_LIFECYCLE_STORE] = {
handleError: handleError,
handleLaunch: handleLaunch,
handleVisibilityChange: handleVisibilityChange,
handlePageNotFound: handlePageNotFound,
handleShow: handleShow,
handleHide: handleHide,
};
// onError
window.addEventListener('error', handleError);
window.addEventListener('unhandledrejection', handleError);
// onShow & onHide
document.addEventListener('visibilitychange', handleVisibilityChange);
var AppConfig = /** @class */ (function (_super) {
__extends(AppConfig, _super);
function AppConfig() {
return _super !== null && _super.apply(this, arguments) || this;
}
// componentDidCatch
AppConfig.prototype.componentDidMount = function () {
handleVisibilityChange();
handleLaunch(window[lightAppLifeCycleParamsKey.launch] || {});
};
AppConfig.prototype.componentWillUnmount = function () {
window.removeEventListener('error', handleError);
window.removeEventListener('unhandledrejection', handleError);
document.removeEventListener('visibilitychange', handleVisibilityChange);
};
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)))); };
}
//# sourceMappingURL=createWebAppEvent.js.map