jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/352565 )

Change subject: Add service container
......................................................................


Add service container

Change-Id: I554fa160e1848a0398e32c796578138e4cc506ec
---
A src/container.js
A tests/node-qunit/container.test.js
2 files changed, 148 insertions(+), 0 deletions(-)

Approvals:
  Jhernandez: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/src/container.js b/src/container.js
new file mode 100644
index 0000000..50ae86b
--- /dev/null
+++ b/src/container.js
@@ -0,0 +1,95 @@
+/**
+ * @module container
+ */
+
+/**
+ * Creates an empty service container.
+ *
+ * @return {Container}
+ */
+module.exports = function createContainer() {
+       var factories = {},
+               cache = {};
+
+       /**
+        * @interface Container
+        *
+        * The interface implemented by all service containers.
+        *
+        * @global
+        */
+       return {
+
+               /**
+                * Defines a service.
+                *
+                * @function
+                * @name Container#set
+                * @param {String} name
+                * @param {*} factory
+                */
+               set: function ( name, factory ) {
+                       factories[ name ] = factory;
+               },
+
+               /**
+                * Gets whether a service has been defined.
+                *
+                * @function
+                * @name Container#has
+                * @param {String} name
+                * @return {Boolean} `true` if the service has been defined; 
otherwise,
+                *  `false`
+                */
+               has: function ( name ) {
+                       return factories.hasOwnProperty( name );
+               },
+
+               /**
+                * Gets a service.
+                *
+                * If the service was defined with a factory function, then the 
factory
+                * function is called and the result is returned. For 
performance reasons,
+                * the result of calling the factory function is cached.
+                *
+                * If the service was defined with a value, then the value is 
returned.
+                *
+                * @example
+                * var container = createContainer();
+                *
+                * container.set( 'foo', true );
+                * container.set( 'baz', function ( c ) {
+                *   if ( c.get( 'foo' ) ) {
+                *     return 'qux';
+                *   }
+                *
+                *   return 'quux';
+                * } );
+                *
+                * @function
+                * @name Container#get
+                * @param {String} name
+                * @return {*}
+                * @throws Error If the service hasn't been defined
+                */
+               get: function ( name ) {
+                       var factory;
+
+                       if ( !this.has( name ) ) {
+                               throw new Error( 'The service "' + name + '" 
hasn\'t been defined.' );
+                       }
+
+                       factory = factories[ name ];
+
+                       if ( typeof factory !== 'function' ) {
+                               return factory;
+                       }
+
+                       if ( !cache.hasOwnProperty( name ) ) {
+                               cache[ name ] = factories[ name ]( this );
+                       }
+
+                       return cache[ name ];
+               }
+       };
+};
diff --git a/tests/node-qunit/container.test.js 
b/tests/node-qunit/container.test.js
new file mode 100644
index 0000000..529b04a
--- /dev/null
+++ b/tests/node-qunit/container.test.js
@@ -0,0 +1,53 @@
+var createContainer = require( '../../src/container' );
+
+QUnit.module( 'container', {
+       beforeEach: function () {
+               this.container = createContainer();
+               this.factory = this.sandbox.stub();
+       }
+} );
+
+QUnit.test( '#has', function ( assert ) {
+       this.container.set( 'foo', this.factory );
+
+       assert.ok( this.container.has( 'foo' ) );
+} );
+
+QUnit.test( '#get', function ( assert ) {
+       var service = {},
+               that = this;
+
+       this.factory.returns( service );
+
+       this.container.set( 'foo', this.factory );
+
+       assert.strictEqual( service, this.container.get( 'foo' ) );
+       assert.strictEqual(
+               this.container,
+               this.factory.getCall( 0 ).args[ 0 ]
+       );
+
+       // ---
+
+       this.container.get( 'foo' );
+
+       assert.ok(
+               this.factory.calledOnce,
+               'It should memoize the result of the factory.'
+       );
+
+       // ---
+
+       assert.throws(
+               function () {
+                       that.container.get( 'bar' );
+               },
+               /The service "bar" hasn't been defined./
+       );
+} );
+
+QUnit.test( '#get should handle values, not just functions', function ( assert 
) {
+       this.container.set( 'foo', 'bar' );
+
+       assert.strictEqual( 'bar', this.container.get( 'foo' ) );
+} );

-- 
To view, visit https://gerrit.wikimedia.org/r/352565
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I554fa160e1848a0398e32c796578138e4cc506ec
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Popups
Gerrit-Branch: master
Gerrit-Owner: Phuedx <[email protected]>
Gerrit-Reviewer: Jhernandez <[email protected]>
Gerrit-Reviewer: Phuedx <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to