Skip to content

Instantly share code, notes, and snippets.

@tschneidereit
Created November 3, 2013 21:15
Show Gist options
  • Save tschneidereit/7294906 to your computer and use it in GitHub Desktop.
Save tschneidereit/7294906 to your computer and use it in GitHub Desktop.
Iterable WeakMap implemented using Weak References
function IterableWeakKeyMap() {
const keyMap = new WeakMap();
const refValueMap = new Map();
return Object.freeze({
get: function(key) {
const entry = keyMap.get(key);
return entry && entry.value;
},
set: function(key, value) {
const ref = makeWeakRef(key);
keyMap.set(key, {value: value, ref: ref});
refValueMap.set(ref, value);
ref.register(function() {
refValueMap.delete(ref);
});
},
delete: function(key) {
const entry = keyMap.get(key);
if (!entry) {
return;
}
keyMap.delete(key);
refValueMap.delete(entry.ref);
},
iterator: function() {
for (const item of refValueMap) {
const key = item[0].get();
if (key) {
yield [key, item[1]];
}
}
},
items: function() {
return this.iterator();
},
keys: function() {
for (const ref of refValueMap.keys()) {
const key = ref.get();
if (key) {
yield key;
}
}
},
values: function() {
// Not sure if we also need to filter these
return refValueMap.values();
}
});
}
@gsathya
Copy link

gsathya commented Apr 1, 2019

Updated using the new WeakRef API (+ESNext sauce):

class IterableWeakMap {
  #weakMap = new WeakMap();
  #refMap = new Map();
  #finalizationGroup = new FinalizationGroup(IterableWeakMap.#cleanup);

  static #cleanup(iterator) {
    for (const { map, ref } of iterator) {
      map.delete(ref);
    }
  }

  set(key, value) {
    const ref = new WeakRef(key);

    this.#weakMap.set(key, { value, ref });
    this.#refMap.set(ref, value);
    finalizationGroup.register(key, { map: this.#weakRefMap, ref }, ref);
  }

  get(key) {
    const entry = this.#weakMap.get(key);
    return entry && entry.value;
  }

  delete(key) {
    const entry = this.#weakMap.get(key);
    if (!entry) {
      return;
    }

    this.#weakMap.delete(key);
    this.#refMap.delete(entry.ref);
    this.#finalizationGroup.unregister(entry.ref);
  }

  *iterator() {
    for (const [ref, value] of this.#refMap) {
      const key = ref.get();
      if (key) yield [key, value];
    }
  }

  items() {
    return this.iterator();
  }

  *keys() {
    for (const [ref] of this.#refMap) {
      const key = ref.get();
      if (key) yield key;
    }
  }

  values() {
    return this.#refMap.values();
  }
}

@petternordholm
Copy link

A warning if using the code above. There is a bug in the set method if the same key is used multiple times. This will cause multiple weak references for the same key in the #refMap. A simple solution is to call this.delete(ref) after creating the WeakRef .

map = new IterableWeakMap():
const key1 = Symbol('1');

map.set(key1, 'TEST_1');
map.set(key1, 'TEST_1_1');

map.values() // Returns ['TEST_1', 'TEST_1_1']

@Mortimer333
Copy link

Seems pretty complex for something that should be as simple as possible. Here is my version in Typescript, please tag me if you find any issues (known issue would be desynchronization of array maps, but this should be clearly visible in this small flow):

export interface IIterableWeakMap<T extends object, P> {
  get: (key: T) => P|undefined;
  set: (key: T, value: P) => IIterableWeakMap<T, P>;
  delete: (key: T) => boolean;
  has: (key: T) => boolean;
  keys: () => T[];
  values: () => P[];
  [Symbol.toStringTag]: string;
}
export default function IterableWeakMap<T extends object, P>(): IIterableWeakMap<T, P> {
  const weakMap = new WeakMap(),
    arrKeys: T[] = [],
    arrValues: P[] = [],
    objectToIndex = new WeakMap<T, number>(),
    _ = {
      get [Symbol.toStringTag]() {
        return 'IterableWeakMap';
      },
      get: (key: T): P|undefined => weakMap.get(key) as P|undefined,
      set: (key: T, value: P): IIterableWeakMap<T, P> => {
        weakMap.set(key, value);
        objectToIndex.set(key, arrKeys.length);
        arrKeys.push(key);
        arrValues.push(value);

        return _;
      },
      delete: (key: T): boolean => {
        if (!weakMap.get(key) || !objectToIndex.has(key)) {
          return false;
        }
        weakMap.delete(key);
        arrKeys.splice(objectToIndex.get(key)!, 1);
        arrValues.splice(objectToIndex.get(key)!, 1);
        objectToIndex.delete(key);

        return true;
      },
      has: (key: T): boolean => weakMap.has(key),
      keys: (): T[] => arrKeys,
      values: (): P[] => arrValues,
    }
  ;
  return Object.freeze(_);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment