UNPKG

@rimbu/proximity

Version:

Immutable ProximityMap implementation for TypeScript

95 lines 3.4 kB
import {} from '@rimbu/hashed/map'; import { wrapHashMap } from './wrapping.mjs'; export class ProximityMapBuilder { constructor(context, source) { this.context = context; this.source = source; /** * Applying `get()` to the Builder does NOT apply the proximity algorithm - which would * be pointless at this construction stage; the internal, hash-based builder * is queried instead * */ this.get = (key, otherwise) => { if (undefined !== this.source) return this.source.get(key, otherwise); return this.internalBuilder.get(key, otherwise); }; // prettier-ignore this.hasKey = (key) => { return this.internalBuilder.hasKey(key); }; this.forEach = (f, options = {}) => { this.internalBuilder.forEach(f, options); }; this.addEntry = (entry) => { const hasChanged = this.internalBuilder.addEntry(entry); if (hasChanged) { this.source = undefined; } return hasChanged; }; this.addEntries = (entries) => { const hasChanged = this.internalBuilder.addEntries(entries); if (hasChanged) { this.source = undefined; } return hasChanged; }; this.set = (key, value) => { const hasChanged = this.internalBuilder.set(key, value); if (hasChanged) { this.source = undefined; } return hasChanged; }; this.removeKey = (key, otherwise) => { if (this.internalBuilder.hasKey(key)) { this.source = undefined; } return this.internalBuilder.removeKey(key, otherwise); }; // prettier-ignore this.removeKeys = (keys) => { const hasChanged = this.internalBuilder.removeKeys(keys); if (hasChanged) { this.source = undefined; } return hasChanged; }; this.modifyAt = (key, options) => { const hasChanged = this.internalBuilder.modifyAt(key, options); if (hasChanged) { this.source = undefined; } return hasChanged; }; // prettier-ignore this.updateAt = (key, update, otherwise) => { const previousValue = this.internalBuilder.updateAt(key, update, otherwise); const newValue = this.internalBuilder.get(key); if (!Object.is(previousValue, newValue)) { this.source = undefined; } return previousValue; }; this.build = () => { return this.source !== undefined ? this.source : wrapHashMap(this.context, this.internalBuilder.build()); }; // prettier-ignore this.buildMapValues = (mapFun) => { return wrapHashMap(this.context, this.internalBuilder.buildMapValues(mapFun)); }; this.internalBuilder = context.hashMapContext.builder(); this.internalBuilder.addEntries(source); } get size() { return this.internalBuilder.size; } get isEmpty() { return this.internalBuilder.isEmpty; } } //# sourceMappingURL=builder.mjs.map