export type NextFactory = (current: T, delta: U) => T; export class NextCache { private readonly cache = new WeakMap>(); private factory: NextFactory; constructor(factory: NextFactory) { this.factory = factory; } next(current: T, delta: U): T { let slot = this.cache.get(current); if (!slot) this.cache.set(current, (slot = new WeakMap())); let next = slot.get(delta); if (!next) slot.set(delta, (next = this.factory(current, delta))); return next; } }