refactor(Key): extract KeyRegistry to remove the need in `Key.clear`

This commit is contained in:
vsavkin 2014-11-06 10:55:34 -08:00
parent 9448d78aa8
commit a1c6f1bbe9
2 changed files with 38 additions and 25 deletions

View File

@ -2,8 +2,6 @@ import {KeyMetadataError} from './exceptions';
import {MapWrapper, Map} from 'facade/collection';
import {FIELD, int, isPresent} from 'facade/lang';
var _allKeys = MapWrapper.create();
export class Key {
@FIELD('final token')
@FIELD('final id:int')
@ -14,18 +12,6 @@ export class Key {
this.metadata = null;
}
static get(token):Key {
if (token instanceof Key) return token;
if (MapWrapper.contains(_allKeys, token)) {
return MapWrapper.get(_allKeys, token);
}
var newKey = new Key(token, Key.numberOfKeys);
MapWrapper.set(_allKeys, token, newKey);
return newKey;
}
static setMetadata(key:Key, metadata):Key {
if (isPresent(key.metadata) && key.metadata !== metadata) {
throw new KeyMetadataError();
@ -34,11 +20,36 @@ export class Key {
return key;
}
static clear() {
_allKeys = MapWrapper.create();
static get(token):Key {
return _globalKeyRegistry.get(token);
}
static get numberOfKeys():int {
return MapWrapper.size(_allKeys);
return _globalKeyRegistry.numberOfKeys;
}
}
export class KeyRegistry {
@FIELD('final _allKeys:Map')
constructor() {
this._allKeys = MapWrapper.create();
}
get(token):Key {
if (token instanceof Key) return token;
if (MapWrapper.contains(this._allKeys, token)) {
return MapWrapper.get(this._allKeys, token);
}
var newKey = new Key(token, Key.numberOfKeys);
MapWrapper.set(this._allKeys, token, newKey);
return newKey;
}
get numberOfKeys():int {
return MapWrapper.size(this._allKeys);
}
}
var _globalKeyRegistry = new KeyRegistry();

View File

@ -1,27 +1,29 @@
import {describe, it, expect, beforeEach} from 'test_lib/test_lib';
import {Key} from 'di/di';
import {Key, KeyRegistry} from 'di/di';
export function main() {
describe("key", function () {
var registry;
beforeEach(function () {
Key.clear();
registry = new KeyRegistry();
});
it('should be equal to another key if type is the same', function () {
expect(Key.get('car')).toBe(Key.get('car'));
expect(registry.get('car')).toBe(registry.get('car'));
});
it('should not be equal to another key if types are different', function () {
expect(Key.get('car')).not.toBe(Key.get('porsche'));
expect(registry.get('car')).not.toBe(registry.get('porsche'));
});
it('should return the passed in key', function () {
expect(Key.get(Key.get('car'))).toBe(Key.get('car'));
expect(registry.get(registry.get('car'))).toBe(registry.get('car'));
});
describe("metadata", function () {
it("should assign metadata to a key", function () {
var key = Key.get('car');
var key = registry.get('car');
Key.setMetadata(key, "meta");
@ -29,7 +31,7 @@ export function main() {
});
it("should allow assigning the same metadata twice", function () {
var key = Key.get('car');
var key = registry.get('car');
Key.setMetadata(key, "meta");
Key.setMetadata(key, "meta");
@ -38,7 +40,7 @@ export function main() {
});
it("should throw when assigning different metadata", function () {
var key = Key.get('car');
var key = registry.get('car');
Key.setMetadata(key, "meta1");