|
| 1 | +- Start Date: 2015-09-11 |
| 2 | +- RFC PR: https://github.com/emberjs/ember.js/pull/12224 |
| 3 | +- Ember Issue: (leave this empty) |
| 4 | + |
| 5 | +# Summary |
| 6 | + |
| 7 | +Introduce `Ember.WeakMap` (`@ember/weakmap`), an ES6 enspired WeakMap. A |
| 8 | +WeakMap provides a mechanism for storing and retriving private state. The |
| 9 | +WeakMap itself does not retain a reference to the state, allowing the state to |
| 10 | +be reclaimed when the key is reclaimed. |
| 11 | + |
| 12 | +A traditional WeakMap (and the one that will be part of the language) allows |
| 13 | +for weakness from key -> map, and also from map -> key. This allows either the |
| 14 | +Map, or the key being reclaimed to also release the state. |
| 15 | + |
| 16 | +Unforunately, this bi-directional weakness is problemative to polyfil. Luckily, |
| 17 | +uni-directional weakness, in either direction, "just works". A polyfil must |
| 18 | +just choose a direction. |
| 19 | + |
| 20 | +*Note: Just like ES2015 WeakMap, only non null Objects can be used as keys* |
| 21 | +*Note: `Ember.WeakMap` can be used interchangibly with the ES2015 WeakMap. This |
| 22 | +will allow us to eventually cut over entirely to the Native WeakMap.* |
| 23 | + |
| 24 | +# Motivation |
| 25 | + |
| 26 | +It is a common pattern to want to store private state about a specific object. |
| 27 | +When one stores this private state off-object, it can be tricky to understand |
| 28 | +when to release the state. When one stores this state on-object, it will be |
| 29 | +released when the object is released. Unfortunately, storing the state |
| 30 | +on-object without poluting the object itself is non-obvious. |
| 31 | + |
| 32 | +As it turns out, Ember's Meta already solves this problem for |
| 33 | +listeners/caches/chains/descriptors etc. Unfortunately today, there is no |
| 34 | +public API for apps or addons to utilize this. `Ember.WeakMap` aims to be |
| 35 | +exactly that API. |
| 36 | + |
| 37 | +Some examples: |
| 38 | + |
| 39 | +* https://github.com/offirgolan/ember-cp-validations/blob/master/addon/utils/cycle-breaker.js |
| 40 | +* https://github.com/stefanpenner/ember-state-services/ (will soon utilize the user-land polyfil of this) to prevent common leaks. |
| 41 | + |
| 42 | +# Detailed design |
| 43 | + |
| 44 | +## Public API |
| 45 | + |
| 46 | +```js |
| 47 | +import WeakMap from '@ember/weak-map' |
| 48 | + |
| 49 | +var private = new WeakMap(); |
| 50 | +var object = {}; |
| 51 | +var otherObject = {}; |
| 52 | + |
| 53 | +private.set(object, { |
| 54 | + id: 1, |
| 55 | + name: 'My File', |
| 56 | + progress: 0 |
| 57 | +}) === private; |
| 58 | + |
| 59 | +private.get(object) === { |
| 60 | + id: 1, |
| 61 | + name: 'My File', |
| 62 | + progress: 0 |
| 63 | +}); |
| 64 | + |
| 65 | + |
| 66 | +private.has(object) === true; |
| 67 | +private.has(otherObject) === false; |
| 68 | + |
| 69 | +private.delete(object) === private; |
| 70 | +private.has(object) === false; |
| 71 | +``` |
| 72 | + |
| 73 | +## Implementation Details |
| 74 | + |
| 75 | +The backing store for `Ember.WeakMap` will reside in a lazy `ownMap` named |
| 76 | +`weak` on the key objects `__meta__` object. |
| 77 | + |
| 78 | +Each `WeakMap` has its own internal GUID, which will be the name of its slot, |
| 79 | +in the key objects meta weak bucket. This will allow one object to belong in |
| 80 | +multiple weakMaps without chance of collision. |
| 81 | + |
| 82 | +Concrete Implementation: https://github.com/emberjs/ember.js/pull/12224 |
| 83 | +Polyfill: https://www.npmjs.com/package/ember-weakmap |
| 84 | + |
| 85 | +# Drawbacks |
| 86 | + |
| 87 | +* implementing bi-direction Weakness in userland is problematic. |
| 88 | +* Using WeakMap will insert a non-enumerable `meta` onto the key Object. |
| 89 | + |
| 90 | +# Alternatives |
| 91 | + |
| 92 | +* Weakness could be implemented in the other direction, but this has questionable utility. |
| 93 | + |
| 94 | +# Unresolved questions |
| 95 | + |
| 96 | +N/A |
0 commit comments