From 906130fbce48ce01869f7e1a2c37be3cba3742c1 Mon Sep 17 00:00:00 2001 From: Brett Wilcox Date: Wed, 30 Sep 2020 09:24:54 -0500 Subject: [PATCH] v1.2.0 --- .idea/.name | 2 +- .idea/compiler.xml | 22 + .idea/copyright/profiles_settings.xml | 3 + .idea/inspectionProfiles/Project_Default.xml | 6 - .../inspectionProfiles/profiles_settings.xml | 7 - .idea/jsLibraryMappings.xml | 1 - .idea/libraries/psd_simpledi_node_modules.xml | 14 - .idea/misc.xml | 2 +- .idea/modules.xml | 2 +- .idea/{psd-simpledi.iml => scorpion.iml} | 6 +- .idea/vcs.xml | 2 +- .idea/watcherTasks.xml | 4 - .idea/workspace.xml | 711 ++---------------- dist/index.js | 24 +- package.json | 2 +- src/index.js | 16 +- test/unit/index-spec.js | 40 +- 17 files changed, 154 insertions(+), 710 deletions(-) create mode 100644 .idea/compiler.xml create mode 100644 .idea/copyright/profiles_settings.xml delete mode 100644 .idea/inspectionProfiles/Project_Default.xml delete mode 100644 .idea/inspectionProfiles/profiles_settings.xml delete mode 100644 .idea/libraries/psd_simpledi_node_modules.xml rename .idea/{psd-simpledi.iml => scorpion.iml} (54%) delete mode 100644 .idea/watcherTasks.xml diff --git a/.idea/.name b/.idea/.name index 6c39b92..5bee8f2 100644 --- a/.idea/.name +++ b/.idea/.name @@ -1 +1 @@ -psd-simpledi \ No newline at end of file +scorpion \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index c6cc8c8..0000000 --- a/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100644 index 3b31283..0000000 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/jsLibraryMappings.xml b/.idea/jsLibraryMappings.xml index a74c8fa..b8387eb 100644 --- a/.idea/jsLibraryMappings.xml +++ b/.idea/jsLibraryMappings.xml @@ -1,7 +1,6 @@ - \ No newline at end of file diff --git a/.idea/libraries/psd_simpledi_node_modules.xml b/.idea/libraries/psd_simpledi_node_modules.xml deleted file mode 100644 index 407ce98..0000000 --- a/.idea/libraries/psd_simpledi_node_modules.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 19f74da..7e63416 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -10,5 +10,5 @@ - + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index 7024aef..788091a 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/.idea/psd-simpledi.iml b/.idea/scorpion.iml similarity index 54% rename from .idea/psd-simpledi.iml rename to .idea/scorpion.iml index 8206b5e..d6ebd48 100644 --- a/.idea/psd-simpledi.iml +++ b/.idea/scorpion.iml @@ -1,9 +1,9 @@ - - + + + - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/watcherTasks.xml b/.idea/watcherTasks.xml deleted file mode 100644 index 0df7231..0000000 --- a/.idea/watcherTasks.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4105933..846ffc0 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,8 +1,8 @@ - - + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - + + @@ -122,24 +46,6 @@ - - - true @@ -176,64 +82,33 @@ - + + - - - - - - - - - - - - - - - - - - - - - - - @@ -241,562 +116,110 @@ - - - - - - - - - + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - + + - + + - + + - - + + - - - - - - - - - 1441366450290 - - - 1441375025909 - - - 1441383269707 - - - 1441383939017 - - - 1441617557138 - - - 1441618436856 - - - 1441619060813 - - - 1441631202648 - - - 1441702047493 - - - 1441703443202 - - - 1441703981457 - - - 1441716437986 - - - 1441716474578 - - - 1441716942599 - - - 1441726755330 - - - 1441726883202 - - - 1441727153570 - - - 1441727255813 - - - 1441727284902 - - - 1441727699145 - - - 1441727738179 - - - 1441728646375 - - - 1441791210043 - - - + - + + - - - - - - - + + + + + - - + - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + diff --git a/dist/index.js b/dist/index.js index 6bd9ae4..c869729 100644 --- a/dist/index.js +++ b/dist/index.js @@ -25,10 +25,6 @@ var Scorpion = (function () { _createClass(Scorpion, [{ key: 'get', value: function get(name) { - if (!this._registry[name]) { - throw new Error('Module not found: ' + name); - } - return this._resolve(name); } }, { @@ -48,13 +44,29 @@ var Scorpion = (function () { var chain = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1]; var requestedModule = this._registry[name]; + if (!requestedModule) { + return new Promise(function (resolve, reject) { + if (chain.length === 0) { + return reject(new Error('Module not found: ' + name)); + } + return reject(new Error('Dependency not found: ' + name)); + }); + } this._resolvedDependencies[name]++; chain.push(name); return Promise.all(requestedModule.dependencies.map(function (dependencyName) { var clonedChain = [].concat(_toConsumableArray(chain)); if (clonedChain.indexOf(dependencyName) !== -1) { - var stringifiedChain = _this2._stringifyDependencyChain(clonedChain.concat([dependencyName])); - throw new Error('Circular Dependency detected: ' + stringifiedChain); + var _ret = (function () { + var stringifiedChain = _this2._stringifyDependencyChain(clonedChain.concat([dependencyName])); + return { + v: new Promise(function (resolve, reject) { + reject(new Error('Circular Dependency detected: ' + stringifiedChain)); + }) + }; + })(); + + if (typeof _ret === 'object') return _ret.v; } return _this2._resolve(dependencyName, clonedChain); })).then(function (dependencies) { diff --git a/package.json b/package.json index 8915d83..3ed1514 100644 --- a/package.json +++ b/package.json @@ -47,5 +47,5 @@ "prepublish": "npm run bundle", "test": "mocha --reporter dot --recursive -r setup-referee-sinon/globals test/unit --compilers js:babel/register" }, - "version": "1.1.0" + "version": "1.2.0" } diff --git a/src/index.js b/src/index.js index 4fd5900..38e2721 100644 --- a/src/index.js +++ b/src/index.js @@ -10,10 +10,6 @@ export default class Scorpion { } get(name) { - if (!this._registry[name]) { - throw new Error('Module not found: ' + name); - } - return this._resolve(name); } @@ -25,6 +21,14 @@ export default class Scorpion { _resolve(name, chain = []) { const requestedModule = this._registry[name]; + if (!requestedModule) { + return new Promise((resolve, reject) => { + if (chain.length === 0) { + return reject(new Error('Module not found: ' + name)); + } + return reject(new Error('Dependency not found: ' + name)); + }); + } this._resolvedDependencies[name]++; chain.push(name); return Promise.all(requestedModule.dependencies.map((dependencyName) => { @@ -33,7 +37,9 @@ export default class Scorpion { const stringifiedChain = this._stringifyDependencyChain(clonedChain.concat([ dependencyName ])); - throw new Error('Circular Dependency detected: ' + stringifiedChain); + return new Promise(function(resolve, reject) { + reject(new Error('Circular Dependency detected: ' + stringifiedChain)); + }); } return this._resolve(dependencyName, clonedChain); })).then((dependencies) => { diff --git a/test/unit/index-spec.js b/test/unit/index-spec.js index 6dc8662..a2cfbbb 100644 --- a/test/unit/index-spec.js +++ b/test/unit/index-spec.js @@ -113,11 +113,9 @@ describe('Scorpion', function() { di.register('Foo', ['Bar'], Scorpion.withNew(Foo)); di.register('Bar', ['Foo'], Scorpion.withNew(Bar)); - try { - di.get('Foo'); - } catch(e) { - expect(e.toString()).toEqual('Error: Circular Dependency detected: Foo => Bar => Foo'); - } + return di.get('Foo').catch(function(error) { + expect(error.toString()).toEqual('Error: Circular Dependency detected: Foo => Bar => Foo'); + }); }); it('throws when trying to resolve a cicular dependency', function() { @@ -129,11 +127,9 @@ describe('Scorpion', function() { di.register('Bar', ['Baz'], Scorpion.withNew(Bar)); di.register('Baz', ['Foo'], Scorpion.withNew(Bar)); - try { - di.get('Foo'); - } catch(e) { - expect(e.toString()).toEqual('Error: Circular Dependency detected: Foo => Bar => Baz => Foo'); - } + return di.get('Foo').catch(function(error) { + expect(error.toString()).toEqual('Error: Circular Dependency detected: Foo => Bar => Baz => Foo'); + }); }); it('throws when a circular dependency is detected', function() { @@ -144,11 +140,25 @@ describe('Scorpion', function() { return {}; }); - try { - di.get('Foo'); - } catch(e) { - expect(e.toString()).toEqual('Error: Circular Dependency detected: Foo => Bar => Foo'); - } + return di.get('Foo').catch(function(error) { + expect(error.toString()).toEqual('Error: Circular Dependency detected: Foo => Bar => Foo'); + }); + }); + + it('rejects when the module was not found', function() { + return di.get('foobar').catch(function(error) { + expect(error.toString()).toBe('Error: Module not found: foobar'); + }); + }); + + it('rejects with an explicit error when a dependency does not exist', function() { + di.register('Foo', ['Bar'], () => { + return {}; + }); + + return di.get('Foo').catch(function(error) { + expect(error.toString()).toBe('Error: Dependency not found: Bar'); + }); }); });