UNPKG

haunted

Version:
29 lines (28 loc) 722 B
import { hook, Hook } from "./hook"; /** * @function * @template T * @param {() => T} fn function to memoize * @param {unknown[]} values dependencies to the memoized computation * @return {T} The next computed value */ const useMemo = hook(class extends Hook { value; values; constructor(id, state, fn, values) { super(id, state); this.value = fn(); this.values = values; } update(fn, values) { if (this.hasChanged(values)) { this.values = values; this.value = fn(); } return this.value; } hasChanged(values = []) { return values.some((value, i) => this.values[i] !== value); } }); export { useMemo };