UNPKG

fullts

Version:

Full stack framework in TypeScript, based on TSRPC.

142 lines (141 loc) 5.25 kB
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 (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); } return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import * as React from "react"; import FulltsApp from './FulltsApp'; import TsrpcClient from "tsrpc-browser"; import * as PropTypes from 'prop-types'; var FulltsComponent = /** @class */ (function (_super) { __extends(FulltsComponent, _super); function FulltsComponent(props, context) { var _this = _super.call(this, props, context) || this; _this._apiRequests = {}; _this._setTimeoutTimers = []; _this._setIntervalTimers = []; _this._globalEventListeners = []; //componentWillUnmount cancel all api requests if (_this.componentWillUnmount) { var originalFunc_1 = _this.componentWillUnmount; _this.componentWillUnmount = function () { _this._componentWillUnmount(); originalFunc_1(); }; } else { _this.componentWillUnmount = _this._componentWillUnmount; } return _this; } FulltsComponent.prototype.callApi = function (ptl, req) { var _this = this; //ADD TO CONN POOL AND POP WHEN DISPOSE var conn = this.app.callApi(ptl, req); var sn = TsrpcClient.getLastReqSn(); this._apiRequests[sn] = conn; //pop when complete conn.always(function () { delete _this._apiRequests[sn]; }).onCancel(function () { delete _this._apiRequests[sn]; }); return conn; }; Object.defineProperty(FulltsComponent.prototype, "app", { /** * Current FullTSApp Instance */ get: function () { return this.context.fullTsApp; }, enumerable: true, configurable: true }); FulltsComponent.prototype._componentWillUnmount = function () { this._cancelAllApiRequest(); //clearTimeout for (var _i = 0, _a = this._setTimeoutTimers; _i < _a.length; _i++) { var timer = _a[_i]; clearTimeout(timer); this._setTimeoutTimers = undefined; } //clearInterval for (var _b = 0, _c = this._setIntervalTimers; _b < _c.length; _b++) { var timer = _c[_b]; clearInterval(timer); this._setIntervalTimers = undefined; } //clear global event listeners for (var _d = 0, _e = this._globalEventListeners; _d < _e.length; _d++) { var item = _e[_d]; item.target.removeEventListener(item.type, item.listener, item.options); this._globalEventListeners = undefined; } }; FulltsComponent.prototype._cancelAllApiRequest = function () { for (var sn in this._apiRequests) { if (this._apiRequests.hasOwnProperty(sn)) { this._apiRequests[sn].cancel(); } } }; /** * 同原生setTimeout 但uncomponent时会自动清除 * @param handler * @param ms */ FulltsComponent.prototype.setTimeout = function (handler, ms) { var _this = this; var timer = setTimeout(function () { handler(); var index = _this._setTimeoutTimers.binarySearch(timer); if (index > -1) { _this._setTimeoutTimers.splice(index, 1); } }, ms); this._setTimeoutTimers.binaryInsert(timer); return timer; }; /** * 同原生setInterval 但uncomponent时会自动清除 * @param handler * @param ms */ FulltsComponent.prototype.setInterval = function (handler, ms) { var _this = this; var timer = setInterval(function () { handler(); var index = _this._setIntervalTimers.binarySearch(timer); if (index > -1) { _this._setIntervalTimers.splice(index, 1); } }, ms); this._setIntervalTimers.binaryInsert(timer); return timer; }; /** * 同xxx.addEventListener unmount时会自动removeEventListener * @param target * @param type * @param listener * @param options */ FulltsComponent.prototype.addEventListener = function (target, type, listener, options) { target.addEventListener(type, listener, options); this._globalEventListeners.push({ target: target, type: type, listener: listener, options: options }); }; FulltsComponent.contextTypes = { fullTsApp: PropTypes.instanceOf(FulltsApp) }; return FulltsComponent; }(React.Component)); export default FulltsComponent;