UNPKG

@intuitionrobotics/thunderstorm

Version:
110 lines 3.83 kB
import * as React from "react"; import { _clearTimeout, _setTimeout, Logger, LogLevel, currentTimeMillies } from "@intuitionrobotics/ts-common"; import { StorageModule } from "../modules/StorageModule.js"; import { ResourcesModule } from "../modules/ResourcesModule.js"; import { BrowserHistoryModule } from "../modules/HistoryModule.js"; import { Thunder } from "./Thunder.js"; export class BaseComponent extends React.Component { stateKeysToUpdate; logger; _componentDidMount; _componentWillUnmount; timeoutMap = {}; constructor(props) { super(props); this.logger = new Logger(this.constructor.name); this._componentDidMount = this.componentDidMount; this.componentDidMount = () => { // @ts-expect-error TS struggles with this dynamic typing Thunder.getInstance().addUIListener(this); if (this._componentDidMount) this._componentDidMount(); }; this._componentWillUnmount = this.componentWillUnmount; this.componentWillUnmount = () => { if (this._componentWillUnmount) this._componentWillUnmount(); // @ts-expect-error TS struggles with this dynamic typing Thunder.getInstance().removeUIListener(this); }; } debounce(handler, key, ms = 0) { const k = "debounce" + key; _clearTimeout(this.timeoutMap[k]); this.timeoutMap[k] = _setTimeout(handler, ms); } throttle(handler, key, ms = 0) { const k = "throttle" + key; if (this.timeoutMap[k]) return; this.timeoutMap[k] = _setTimeout(() => { handler(); delete this.timeoutMap[k]; }, ms); } throttleV2(handler, key, ms, force = false) { const k = "throttle_v2" + key; const now = currentTimeMillies(); const timeoutMapElement = this.timeoutMap[k]; if (timeoutMapElement && now - timeoutMapElement <= ms && !force) return; handler(); this.timeoutMap[k] = 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(LogLevel.Verbose, false, toLog); } logDebug(...toLog) { this.logImpl(LogLevel.Debug, false, toLog); } logInfo(...toLog) { this.logImpl(LogLevel.Info, false, toLog); } logWarning(...toLog) { this.logImpl(LogLevel.Warning, false, toLog); } logError(...toLog) { this.logImpl(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.set(key, value); } static load(key, defaultValue) { return 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.getImageUrl(relativePath); } static getQueryParameter(name) { return BrowserHistoryModule.getQueryParams()[name]; } static getUrl() { return BrowserHistoryModule.getCurrent().pathname; } toString() { return this.constructor.name; } } //# sourceMappingURL=BaseComponent.js.map