UNPKG

@obsidize/rx-map

Version:

ES6 Map with rxjs extensions for change detection

55 lines (54 loc) 1.71 kB
import { isFunction, cloneObject } from '../common/utility'; import { ProxyIterableIterator } from './proxy-iterable-iterator'; /** * ES6 Map implementation that prevents entry mutation outside of explicit set() calls. * This is the default map implementation used by RxMap, so as to prevent iterative / query methods * from passing back raw object refs (which would allow the caller to bypass change detection by direct editing). */ export class ImmutableMap { constructor(source) { this.source = source; } static standard() { return new ImmutableMap(new Map()); } get size() { return this.source.size; } get [Symbol.toStringTag]() { return 'ImmutableMap'; } [Symbol.iterator]() { return this.entries(); } clear() { this.source.clear(); } get(key) { return cloneObject(this.source.get(key)); } has(key) { return this.source.has(key); } delete(key) { return this.source.delete(key); } set(key, value) { this.source.set(cloneObject(key), cloneObject(value)); return this; } entries() { return new ProxyIterableIterator(this.source.entries(), v => cloneObject(v)); } keys() { return new ProxyIterableIterator(this.source.keys(), v => cloneObject(v)); } values() { return new ProxyIterableIterator(this.source.values(), v => cloneObject(v)); } forEach(callbackfn) { if (!isFunction(callbackfn)) return; this.source.forEach((value, key) => callbackfn(cloneObject(value), cloneObject(key), this)); } }