https://github.com/thoov/ember-weakmap
Ember.WeakMap Polyfill Addon
https://github.com/thoov/ember-weakmap
ember-addon ember-weakmap emberjs polyfill weakmap
Last synced: 11 months ago
JSON representation
Ember.WeakMap Polyfill Addon
- Host: GitHub
- URL: https://github.com/thoov/ember-weakmap
- Owner: thoov
- License: mit
- Created: 2015-09-15T20:21:32.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T18:18:01.000Z (over 3 years ago)
- Last Synced: 2025-04-15T06:14:11.273Z (11 months ago)
- Topics: ember-addon, ember-weakmap, emberjs, polyfill, weakmap
- Language: JavaScript
- Homepage: https://github.com/emberjs/rfcs/blob/master/text/0091-weakmap.md
- Size: 1.07 MB
- Stars: 8
- Watchers: 2
- Forks: 7
- Open Issues: 18
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Ember WeakMap
This addon is a polyfill for the [Ember.WeakMap RFC](https://github.com/emberjs/rfcs/blob/master/text/0091-weakmap.md).
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
```shell
ember install ember-weakmap
```
## Usage
```js
import Ember from 'ember';
const myWeakMap = new Ember.WeakMap();
```
### Set
```js
/*
* @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
```js
/*
* @method get
* @param key {Object}
* @return {*} stored value
*/
myWeakMap.get(emailObj); // => { read: true }
myWeakMap.get(someObjThatWasNotSet); // => undefined
```
### Has
```js
/*
* @method has
* @param key {Object}
* @return {Boolean} if the key exists
*/
myWeakMap.has(emailObj); // => true
myWeakMap.has(someObjThatWasNotSet); // => false
```
### Delete
```js
/*
* @method delete
* @param key {Object}
*/
myWeakMap.delete(emailObj);
myWeakMap.get(emailObj); // => undefined
myWeakMap.has(emailObj); // false
```