@bytedance/mona-client-web
Version:
web for mona
129 lines • 5.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 { isClassComponent } from '@bytedance/mona-shared/dist/reactNode';
import { parseSearch } from '@bytedance/mona-shared/dist/search';
import { LifecycleContext, PageLifecycleGlobalContext, PageLifecycle } from '@bytedance/mona';
export function createPageLifecycle(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);
}
};
var handleResize = function () {
var rest = [];
for (var _i = 0; _i < arguments.length; _i++) {
rest[_i] = arguments[_i];
}
callLifecycle.apply(void 0, __spreadArray([PageLifecycle.resize], rest, false));
};
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));
};
var handleVisibilityChange = function () {
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=createWebPage.js.map