ember-weakmap

3.3.2 • Public • Published

Ember WeakMap

This addon is a polyfill for the Ember.WeakMap RFC.

A WeakMap provides a mechanism for storing and retrieving private state. The WeakMap itself does not retain a reference to the state, allowing the state to be reclaimed when the key is reclaimed.

A traditional WeakMap (and the one that will be part of the ES2015 language) allows for weakness from key -> map, and also from map -> key. This allows either the Map, or the key being reclaimed to also release the state.

Unfortunately, this bi-directional weakness is problematic to polyfill. Luckily, uni-directional weakness, in either direction, "just works". A polyfill must just choose a direction.

Note: Just like ES2015 WeakMap, only non null Objects can be used as keys

Installation

ember install ember-weakmap

Usage

import Ember from 'ember';
 
const myWeakMap = new Ember.WeakMap();

Set

/*
 * @method set
 * @param key {Object}
 * @param value {Any}
 * @return {Any} stored value
 */
 
const emailObj = { id: 1, subject: 'Hello World' };
 
myWeakMap.set(emailObj, {read: true});

The key must be an object.

Get

/*
 * @method get
 * @param key {Object}
 * @return {*} stored value
*/
 
myWeakMap.get(emailObj); // => { read: true }
myWeakMap.get(someObjThatWasNotSet); // => undefined

Has

/*
 * @method has
 * @param key {Object}
 * @return {Boolean} if the key exists
*/
 
myWeakMap.has(emailObj); // => true
myWeakMap.has(someObjThatWasNotSet); // => false

Delete

/*
 * @method delete
 * @param key {Object}
 */
 
myWeakMap.delete(emailObj);
myWeakMap.get(emailObj); // => undefined
myWeakMap.has(emailObj); // false

Package Sidebar

Install

npm i ember-weakmap

Weekly Downloads

20,672

Version

3.3.2

License

MIT

Unpacked Size

9.8 kB

Total Files

8

Last publish

Collaborators

  • thoov