Permalink
Browse files

improved module using module pattern

 * improved npm package
  • Loading branch information...
1 parent 37ee650 commit d60a2ca6ca4df44acf81218e09e4510fdcba22bd Tim Schindler committed Feb 25, 2012
Showing with 228 additions and 124 deletions.
  1. +1 −0 .gitignore
  2. +2 −3 Readme.md
  3. +0 −59 callback.js
  4. +86 −0 index.js
  5. +0 −59 lib/index.js
  6. +8 −3 {lib → }/package.json
  7. +131 −0 test/callback_queue_test.js
View
1 .gitignore
@@ -0,0 +1 @@
+node_modules/
View
5 Readme.md
@@ -11,14 +11,13 @@
-
## Usage
- var callback = require('./callback.js')
+ var CallbackQueue = require('callbackQueue')
, moduleOne = require('./moduleOne.js')
, moduleTwo = require('./moduleTwo.js');
- var callbackQueue = callback.init(['moduleOne', 'moduleTwo'], function() {
+ var callbackQueue = CallbackQueue(['moduleOne', 'moduleTwo'], function() {
console.log(this.moduleOneData);
console.log(this.moduleTwoData);
});
View
59 callback.js
@@ -1,59 +0,0 @@
-/*
- *
- * Server site callback module.
- *
- */
-
-/*
- * Dependencies.
- */
-
-var callback = {
- /*
- * Register a dependencie of a initialized dependencie list.
- *
- * @param dependencieName, the name of the dependencie to register.
- */
- register: function(dependencieName, data) {
- this[dependencieName] = data;
-
- var dependencies = this.dependencies
- , i = 0
- , ii = dependencies.length;
-
- for(i; i < ii; i++) {
- if(dependencies[i] === dependencieName) {
- dependencies.splice(i, 1);
- }
- }
-
- // Execute the callback, if all necessary dependencies are registered.
- if(dependencies.length === 0) {
- this.callback();
- }
- },
-
- /*
- * Initialize the callback helper module. Register a list of
- * dependencies that have to be registered, to execute a callback
- * after the registration of all necessary dependencies.
- *
- * @param dependencies, array of dependencie names.
- * @param cb, function to execute if all necessary dependencies are registered.
- *
- * @return object, of that module.
- */
- init: function(dependencies, cb) {
- var instance = new function() {};
-
- instance.dependencies = dependencies;
- instance.callback = cb;
-
- instance.register = callback.register;
-
- return instance;
- },
-};
-
-module.exports = callback;
-
View
86 index.js
@@ -0,0 +1,86 @@
+/*
+ *
+ * Server site callback queue.
+ *
+ */
+
+/*
+ * Class function definition.
+ */
+var CallbackQueue = function(dependencies, cb) {
+ /*
+ *
+ * public
+ *
+ */
+
+ var self = {};
+
+ self.dependencies = dependencies;
+ self.callback = cb;
+ self.registered = 0;
+
+ /*
+ * Register a dependencie of a initialized dependencie list.
+ *
+ * @param dependencieName, the name of the dependencie to register.
+ */
+ self.register = function(dependencieName, data) {
+ if(inArray(dependencieName, self.dependencies)) {
+ self[dependencieName] = data;
+ self.registered++;
+ }
+
+ // Execute the callback, if all necessary dependencies are registered.
+ if(self.registered === self.dependencies.length) {
+ this.callback();
+ }
+ };
+
+ /*
+ *
+ * private
+ *
+ */
+
+ /*
+ * Check if array contains item.
+ *
+ * @param needle, the term to search for.
+ * @param array haystack, the array to search trough.
+ *
+ * @return boolean.
+ */
+ function inArray(needle, haystack) {
+ if(haystack.indexOf(needle) === -1) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ /*
+ * Iterate trough array.
+ *
+ * @param function cb.
+ */
+ function each(array, cb) {
+ var i = 0
+ , ii = array.length;
+
+ for(i; i < ii; i++) {
+ var returnValue = cb(i, array[i]);
+
+ if(returnValue === false) {
+ break;
+ } else {
+ continue;
+ }
+ }
+ }
+
+ return self;
+};
+
+module.exports = CallbackQueue;
+
View
59 lib/index.js
@@ -1,59 +0,0 @@
-/*
- *
- * Server site callback module.
- *
- */
-
-/*
- * Dependencies.
- */
-
-var callback = {
- /*
- * Register a dependencie of a initialized dependencie list.
- *
- * @param dependencieName, the name of the dependencie to register.
- */
- register: function(dependencieName, data) {
- this[dependencieName] = data;
-
- var dependencies = this.dependencies
- , i = 0
- , ii = dependencies.length;
-
- for(i; i < ii; i++) {
- if(dependencies[i] === dependencieName) {
- dependencies.splice(i, 1);
- }
- }
-
- // Execute the callback, if all necessary dependencies are registered.
- if(dependencies.length === 0) {
- this.callback();
- }
- },
-
- /*
- * Initialize the callback helper module. Register a list of
- * dependencies that have to be registered, to execute a callback
- * after the registration of all necessary dependencies.
- *
- * @param dependencies, array of dependencie names.
- * @param cb, function to execute if all necessary dependencies are registered.
- *
- * @return object, of that module.
- */
- init: function(dependencies, cb) {
- var instance = new function() {};
-
- instance.dependencies = dependencies;
- instance.callback = cb;
-
- instance.register = callback.register;
-
- return instance;
- },
-};
-
-module.exports = callback;
-
View
11 lib/package.json → package.json
@@ -2,13 +2,18 @@
"author": "Tim Schindler <tim.schindler@adcloud.com>",
"name": "callbackQueue",
"description": "Dependencie registration module to control your workflow.",
- "version": "0.0.1",
+ "version": "0.0.2",
"repository": {
"type": "git",
"url": "git://github.com/zyndiecate/callbackQueue.git"
},
"engines": {
- "node": ">=v0.4.9"
+ "node": ">=v0.4.8"
},
- "devDependencies": {}
+ "scripts": {
+ "test": "./node_modules/jasmine-node/bin/jasmine-node --matchall test/"
+ },
+ "devDependencies": {
+ "jasmine-node": ">=1.0.3"
+ }
}
View
131 test/callback_queue_test.js
@@ -0,0 +1,131 @@
+/*
+ *
+ * CallbackQueue Tests.
+ *
+ */
+
+/*
+ *
+ */
+var CallbackQueue = require('../index.js');
+
+beforeEach(function() {
+ this.addMatchers({
+ toBeFunction: function() {
+ return typeof this.actual === 'function';
+ },
+ toBeNumber: function() {
+ return typeof this.actual === 'number';
+ },
+ toBeObject: function() {
+ return typeof this.actual === 'object' && !Array.isArray(this.actual);
+ },
+ toBeEmptyObject: function() {
+ return !Object.keys(this.actual).length;
+ }
+ });
+});
+
+describe('callbackQueue', function() {
+ describe('module', function() {
+ it('should be a function', function() {
+ expect(CallbackQueue).toBeFunction();
+ });
+ });
+
+ describe('instance', function() {
+ it('should be a object', function() {
+ expect(CallbackQueue()).toBeObject();
+ });
+ });
+
+ describe('instance property "registered"', function() {
+ it('should be 0 by default', function() {
+ expect(CallbackQueue().registered).toBe(0);
+ });
+
+ it('should be incremented by dependency registrations', function() {
+ var dependencies = ['first', 'second', 'third'];
+
+ var cb = function() {
+ expect(this.registered).toBe(3);
+ };
+
+ var callbackQueue = CallbackQueue(dependencies, cb);
+
+ callbackQueue.register('first');
+ callbackQueue.register('second');
+ callbackQueue.register('third');
+ });
+ });
+
+ describe('instance property "register"', function() {
+ it('should be a function', function() {
+ expect(CallbackQueue().register).toBeFunction();
+ });
+ });
+
+ describe('instance property "dependencies"', function() {
+ it('should equal the given dependencies', function() {
+ var dependencies = ['first', 'second', 'third'];
+ expect(CallbackQueue(dependencies).dependencies).toBe(dependencies);
+ });
+
+ it('should extend instance with dependencies', function() {
+ var dependencies = ['first', 'second', 'third']
+ , cb = function() {}
+ , callbackQueue = CallbackQueue(dependencies, cb);
+
+ callbackQueue.register('first', 'first data set');
+
+ expect(callbackQueue.first).toBe('first data set');
+ });
+ });
+
+ describe('instance property "cb"', function() {
+ it('should equal the given cb', function() {
+ var Callback = function Callback() {};
+ expect(CallbackQueue(undefined, Callback).callback).toBe(Callback);
+ });
+
+ it('should`nt be fired if not all dependencies are registered', function() {
+ var dependencies = ['first', 'second', 'third']
+ , cb = jasmine.createSpy()
+ , callbackQueue = CallbackQueue(dependencies, cb);
+
+ callbackQueue.register('first');
+ callbackQueue.register('second');
+
+ expect(cb).not.toHaveBeenCalled();
+ });
+
+ it('should be fired if all dependencies are registered', function() {
+ var dependencies = ['first', 'second', 'third']
+ , cb = jasmine.createSpy()
+ , callbackQueue = CallbackQueue(dependencies, cb);
+
+ callbackQueue.register('first');
+ callbackQueue.register('second');
+ callbackQueue.register('third');
+
+ expect(cb).toHaveBeenCalled();
+ });
+
+ it('should be fired in the scope of the instance', function() {
+ var dependencies = ['first', 'second', 'third'];
+
+ var cb = function() {
+ expect(this.first).toBe('first data set');
+ expect(this.second).toBe('second data set');
+ expect(this.third).toBe('third data set');
+ };
+
+ var callbackQueue = CallbackQueue(dependencies, cb);
+
+ callbackQueue.register('first', 'first data set');
+ callbackQueue.register('second', 'second data set');
+ callbackQueue.register('third', 'third data set');
+ });
+ });
+});
+

0 comments on commit d60a2ca

Please sign in to comment.