UNPKG

@intuitionrobotics/thunderstorm

Version:
110 lines 4.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseComponent = void 0; const React = require("react"); const ts_common_1 = require("@intuitionrobotics/ts-common"); const StorageModule_1 = require("../modules/StorageModule"); const ResourcesModule_1 = require("../modules/ResourcesModule"); const HistoryModule_1 = require("../modules/HistoryModule"); const Thunder_1 = require("./Thunder"); class BaseComponent extends React.Component { constructor(props) { super(props); this.timeoutMap = {}; this.logger = new ts_common_1.Logger(this.constructor.name); this._componentDidMount = this.componentDidMount; this.componentDidMount = () => { // @ts-ignore Thunder_1.Thunder.getInstance().addUIListener(this); if (this._componentDidMount) this._componentDidMount(); }; this._componentWillUnmount = this.componentWillUnmount; this.componentWillUnmount = () => { if (this._componentWillUnmount) this._componentWillUnmount(); // @ts-ignore Thunder_1.Thunder.getInstance().removeUIListener(this); }; } debounce(handler, key, ms = 0) { const k = "debounce" + key; (0, ts_common_1._clearTimeout)(this.timeoutMap[k]); this.timeoutMap[k] = (0, ts_common_1._setTimeout)(handler, ms); } throttle(handler, key, ms = 0) { const k = "throttle" + key; if (this.timeoutMap[k]) return; this.timeoutMap[k] = (0, ts_common_1._setTimeout)(() => { handler(); delete this.timeoutMap[k]; }, ms); } throttleV2(handler, key, ms, force = false) { const k = "throttle_v2" + key; const now = (0, ts_common_1.currentTimeMillies)(); const timeoutMapElement = this.timeoutMap[k]; if (timeoutMapElement && now - timeoutMapElement <= ms && !force) return; handler(); this.timeoutMap[k] = (0, ts_common_1.currentTimeMillies)(); } setStateKeysToUpdate(stateKeysToUpdate) { this.stateKeysToUpdate = stateKeysToUpdate; } shouldComponentUpdate(nextProps, nextState, nextContext) { if (!this.stateKeysToUpdate) return true; return this.stateKeysToUpdate.find(key => this.state[key] !== nextState[key]) !== undefined; } logVerbose(...toLog) { this.logImpl(ts_common_1.LogLevel.Verbose, false, toLog); } logDebug(...toLog) { this.logImpl(ts_common_1.LogLevel.Debug, false, toLog); } logInfo(...toLog) { this.logImpl(ts_common_1.LogLevel.Info, false, toLog); } logWarning(...toLog) { this.logImpl(ts_common_1.LogLevel.Warning, false, toLog); } logError(...toLog) { this.logImpl(ts_common_1.LogLevel.Error, false, toLog); } log(level, bold, ...toLog) { this.logImpl(level, bold, toLog); } logImpl(level, bold, toLog) { this.logger.log(level, bold, toLog); } static store(key, value) { StorageModule_1.StorageModule.set(key, value); } static load(key, defaultValue) { return StorageModule_1.StorageModule.get(key, defaultValue); } static getElementId(e) { return e.currentTarget.id; } static getImageUrl(_relativePath) { let relativePath = _relativePath; if (!relativePath) return ""; if (relativePath.indexOf(".") === -1) relativePath += ".png"; return ResourcesModule_1.ResourcesModule.getImageUrl(relativePath); } static getQueryParameter(name) { return HistoryModule_1.BrowserHistoryModule.getQueryParams()[name]; } static getUrl() { return HistoryModule_1.BrowserHistoryModule.getCurrent().pathname; } toString() { return this.constructor.name; } } exports.BaseComponent = BaseComponent; //# sourceMappingURL=BaseComponent.js.map