/* eslint max-classes-per-file: off */ import { Component, Ref } from './preact.js' import { ViewContextType, ViewContext } from './ViewContext.js' import { compareObjs, EqualityFuncs, getUnequalProps } from './util/object.js' import { Dictionary } from './options.js' export abstract class PureComponent extends Component { static addPropsEquality = addPropsEquality static addStateEquality = addStateEquality static contextType: any = ViewContextType context: ViewContext propEquality: EqualityFuncs stateEquality: EqualityFuncs debug: boolean shouldComponentUpdate(nextProps: Props, nextState: State) { if (this.debug) { // eslint-disable-next-line no-console console.log(getUnequalProps(nextProps, this.props), getUnequalProps(nextState, this.state)) } return !compareObjs(this.props, nextProps, this.propEquality) || !compareObjs(this.state, nextState, this.stateEquality) } // HACK for freakin' React StrictMode safeSetState(newState: Partial): void { if (!compareObjs(this.state, { ...this.state, ...newState }, this.stateEquality)) { this.setState(newState) } } } PureComponent.prototype.propEquality = {} PureComponent.prototype.stateEquality = {} export abstract class BaseComponent extends PureComponent { static contextType: any = ViewContextType context: ViewContext } function addPropsEquality(this: { prototype: { propEquality: any } }, propEquality) { let hash = Object.create(this.prototype.propEquality) Object.assign(hash, propEquality) this.prototype.propEquality = hash } function addStateEquality(this: { prototype: { stateEquality: any } }, stateEquality) { let hash = Object.create(this.prototype.stateEquality) Object.assign(hash, stateEquality) this.prototype.stateEquality = hash } // use other one export function setRef(ref: Ref | void, current: RefType) { if (typeof ref === 'function') { ref(current) } else if (ref) { // see https://github.com/facebook/react/issues/13029 (ref as any).current = current } }