mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-08 17:10:06 +08:00
Change HashMap to give $$hashKey also for functions so it will be possible to load multiple module function instances. In order to prevent problem in angular's test suite, added an option to HashMap to maintain its own id counter and added cleanup of $$hashKey from all module functions after each test. Before this CL, functions were added to the HashMap via toString(), which could potentially return the same value for different actual instances of a function. This corrects this behaviour by ensuring that functions are mapped with hashKeys, and ensuring that hashKeys are removed from functions and objects at the end of tests. In addition to these changes, the injector uses its own set of UIDs in order to prevent confusingly breaking tests which expect scopes or ng-repeated items to have specific hash keys. Closes #7255
74 lines
1.7 KiB
JavaScript
74 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
|
|
/**
|
|
* Computes a hash of an 'obj'.
|
|
* Hash of a:
|
|
* string is string
|
|
* number is number as string
|
|
* object is either result of calling $$hashKey function on the object or uniquely generated id,
|
|
* that is also assigned to the $$hashKey property of the object.
|
|
*
|
|
* @param obj
|
|
* @returns {string} hash string such that the same input will have the same hash string.
|
|
* The resulting string key is in 'type:hashKey' format.
|
|
*/
|
|
function hashKey(obj, nextUidFn) {
|
|
var objType = typeof obj,
|
|
key;
|
|
|
|
if (objType == 'function' || (objType == 'object' && obj !== null)) {
|
|
if (typeof (key = obj.$$hashKey) == 'function') {
|
|
// must invoke on object to keep the right this
|
|
key = obj.$$hashKey();
|
|
} else if (key === undefined) {
|
|
key = obj.$$hashKey = (nextUidFn || nextUid)();
|
|
}
|
|
} else {
|
|
key = obj;
|
|
}
|
|
|
|
return objType + ':' + key;
|
|
}
|
|
|
|
/**
|
|
* HashMap which can use objects as keys
|
|
*/
|
|
function HashMap(array, isolatedUid) {
|
|
if (isolatedUid) {
|
|
var uid = 0;
|
|
this.nextUid = function() {
|
|
return ++uid;
|
|
};
|
|
}
|
|
forEach(array, this.put, this);
|
|
}
|
|
HashMap.prototype = {
|
|
/**
|
|
* Store key value pair
|
|
* @param key key to store can be any type
|
|
* @param value value to store can be any type
|
|
*/
|
|
put: function(key, value) {
|
|
this[hashKey(key, this.nextUid)] = value;
|
|
},
|
|
|
|
/**
|
|
* @param key
|
|
* @returns {Object} the value for the key
|
|
*/
|
|
get: function(key) {
|
|
return this[hashKey(key, this.nextUid)];
|
|
},
|
|
|
|
/**
|
|
* Remove the key/value pair
|
|
* @param key
|
|
*/
|
|
remove: function(key) {
|
|
var value = this[key = hashKey(key, this.nextUid)];
|
|
delete this[key];
|
|
return value;
|
|
}
|
|
};
|