-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Weakmap – Weak from Map -> Obj, not Obj -> Map
- Loading branch information
1 parent
43d6745
commit 6a1ea35
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { GUID_KEY } from 'ember-metal/utils'; | ||
import { meta } from 'ember-metal/meta'; | ||
|
||
var id = 0; | ||
|
||
/* | ||
* @private | ||
* @class Ember.WeakMap | ||
* | ||
* Weak relationship from Map -> Key, but not Key to Map. | ||
* | ||
* Key must be a non null object | ||
*/ | ||
export default function WeakMap() { | ||
this._id = GUID_KEY + (id++); | ||
} | ||
|
||
/* | ||
* @method get | ||
* @param key {Object} | ||
* @return {*} stored value | ||
*/ | ||
WeakMap.prototype.get = function(obj) { | ||
var map = meta(obj).readableWeak(); | ||
if (map) { | ||
return map[this._id]; | ||
} | ||
}; | ||
|
||
/* | ||
* @method set | ||
* @param key {Object} | ||
* @param value {Any} | ||
* @return {Any} stored value | ||
*/ | ||
WeakMap.prototype.set = function(obj, value) { | ||
meta(obj).writableWeak()[this._id] = value; | ||
return this; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import WeakMap from 'ember-metal/weak_map'; | ||
|
||
QUnit.module('Ember.WeakMap'); | ||
|
||
QUnit.test('has weakMap like qualities', function(assert) { | ||
var map = new WeakMap(); | ||
var map2 = new WeakMap(); | ||
|
||
var a = {}; | ||
var b = {}; | ||
var c = {}; | ||
|
||
equal(map.get(a), undefined); | ||
equal(map.get(b), undefined); | ||
equal(map.get(c), undefined); | ||
|
||
equal(map2.get(a), undefined); | ||
equal(map2.get(b), undefined); | ||
equal(map2.get(c), undefined); | ||
|
||
equal(map.set(a, 1), map, 'map.set should return itself'); | ||
equal(map.get(a), 1); | ||
equal(map.set(b, undefined), map); | ||
equal(map.set(a, 2), map); | ||
equal(map.get(a), 2); | ||
equal(map.set(b, undefined), map); | ||
|
||
equal(map2.get(a), undefined); | ||
equal(map2.get(b), undefined); | ||
equal(map2.get(c), undefined); | ||
|
||
equal(map.set(c, 1), map); | ||
equal(map.get(c), 1); | ||
equal(map.get(a), 2); | ||
equal(map.get(b), undefined); | ||
|
||
equal(map2.set(a, 3), map2); | ||
equal(map2.set(b, 4), map2); | ||
equal(map2.set(c, 5), map2); | ||
|
||
equal(map2.get(a), 3); | ||
equal(map2.get(b), 4); | ||
equal(map2.get(c), 5); | ||
|
||
equal(map.get(c), 1); | ||
equal(map.get(a), 2); | ||
equal(map.get(b), undefined); | ||
}); |