Add the elementData util library (still not in stable, though).

This commit is contained in:
Thomas Wilburn 2014-07-26 14:49:09 -07:00
parent a1dc93e834
commit b99f616489

39
js/util/elementData.js Normal file
View file

@ -0,0 +1,39 @@
define(function() {
/*
Storing JS data on HTML elements has, in the past, been a likely source of
memory leaks one way or the other. In this module, we create a set of
matching WeakMaps for connecting data to DOM and vice versa. The dom2 module
uses this to emulate jQuery's data() method, and other modules can import this
to ask for an element given a JS object (such as a tab instance).
*/
var elements = new WeakMap();
var objects = new WeakMap();
return {
get: function(key) {
var map = key instanceof HTMLElement ? elements : objects;
return map.get(key);
},
set: function(key, value) {
var isDom = key instanceof HTMLElement;
var forward = isDom ? elements : objects;
var reverse = isDom ? objects : elements;
forward.set(key, value);
reverse.set(value, key);
},
"delete": function(key) {
var isDom = key instanceof HTMLElement;
var forward = isDom ? elements : objects;
var reverse = isDom ? objects : elements;
var data = forward.get(key);
forward.delete(key);
reverse.delete(data);
}
}
});