From a1c6f1bbe99a1070657764dafd4c0dc1dcf0989b Mon Sep 17 00:00:00 2001 From: vsavkin Date: Thu, 6 Nov 2014 10:55:34 -0800 Subject: [PATCH] refactor(Key): extract KeyRegistry to remove the need in `Key.clear` --- modules/di/src/key.js | 45 +++++++++++++++++++++------------- modules/di/test/di/key_spec.js | 18 ++++++++------ 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/modules/di/src/key.js b/modules/di/src/key.js index 057c7be54d..9e08abfdba 100644 --- a/modules/di/src/key.js +++ b/modules/di/src/key.js @@ -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(); diff --git a/modules/di/test/di/key_spec.js b/modules/di/test/di/key_spec.js index 34eed487f4..1b9c3ccffd 100644 --- a/modules/di/test/di/key_spec.js +++ b/modules/di/test/di/key_spec.js @@ -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");