Skip to content
This repository has been archived by the owner on Jan 10, 2022. It is now read-only.

Commit

Permalink
Fixed bug in custom events, added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cliftonc committed Apr 15, 2012
1 parent d8712e4 commit 9695137
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 361 deletions.
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -9,6 +9,7 @@ test-cov: lib-cov
@CALIPSO_COV=1 $(MAKE) test REPORTER=html-cov > docs/coverage.html

lib-cov:
rm -rf lib-cov
@jscoverage lib lib-cov

site:
Expand Down
2 changes: 1 addition & 1 deletion docs/coverage.html

Large diffs are not rendered by default.

73 changes: 37 additions & 36 deletions lib/core/Event.js
Expand Up @@ -62,7 +62,7 @@ function CalipsoEventEmitter(options) {

if (self.events[event].custom) {
for (var key in self.events[event].custom) {
self.emitter.removeAllListeners(key + "_" + event);
self.emitter.removeAllListeners(event + "_" + key);
}
}

Expand Down Expand Up @@ -93,7 +93,7 @@ function CalipsoEventEmitter(options) {

// Pre and post event prefixes
var pre_prefix = 'PRE_',
post_prefix = 'POST_';
post_prefix = 'POST_';

// Register a pre listener
this.pre = function(event, listener, fn) {
Expand All @@ -119,7 +119,7 @@ function CalipsoEventEmitter(options) {
// Register a custom event listener
this.custom = function(event, key, listener, fn) {

self.emitter.on(event, fn);
self.emitter.on(event + '_' + key, fn);

// Register under key
this.events[event].custom[key] = this.events[event].custom[key] || {
Expand Down Expand Up @@ -187,7 +187,7 @@ function CalipsoEventEmitter(options) {
cb = function() {};
}

self.emitter.emit(event, key, data, cb);
self.emitter.emit(event + '_' + key, event + '_' + key, data, cb);

} else {
next(data);
Expand All @@ -199,11 +199,11 @@ function CalipsoEventEmitter(options) {

function createCallback(total, data, callback) {

this.count = 0;
this.total = total;
this.outputStack = [];
var count = 0,
total = total,
outputStack = [];

if (data) this.outputStack.push(data);
if (data) outputStack.push(data);

// No listeners, so callback immediately
if (total === 0) {
Expand All @@ -213,13 +213,13 @@ function CalipsoEventEmitter(options) {

return function(data) {

this.count += 1;
count += 1;

if (data) this.outputStack.push(data);
if (data) outputStack.push(data);

// Merge the outputs from the stack
if (this.count === this.total) {
callback(mergeArray(this.outputStack));
if (count === total) {
callback(mergeArray(outputStack));
}

};
Expand Down Expand Up @@ -256,6 +256,30 @@ function ModuleInitEventEmitter(moduleName) {

}


/**
* Event listener linked to the module itself
* This is for server events (e.g. init, reload)
* No events here can sit within the request context as
* they will apply to all requests
*/

function addModuleEventListener(module) {

var moduleEventEmitter = module.event = new ModuleInitEventEmitter(module.name);

// Link events
moduleEventEmitter.once(exports.INIT_START, function(moduleName, options) {
// Do nothing
});

moduleEventEmitter.once(exports.INIT_FINISH, function(moduleName, options) {
// Check for dependent modules, init them
calipso.module.notifyDependenciesOfInit(moduleName, options);
});

}

/**
* Module event emitter, object that enables modules to emit events.
* This contains both server and request scope event emitters, though clearly
Expand Down Expand Up @@ -285,30 +309,6 @@ function ModuleRequestEventEmitter(moduleName) {

}


/**
* Event listener linked to the module itself
* This is for server events (e.g. init, reload)
* No events here can sit within the request context as
* they will apply to all requests
*/

function addModuleEventListener(module) {

var moduleEventEmitter = module.event = new ModuleInitEventEmitter(module.name);

// Link events
moduleEventEmitter.once(exports.INIT_START, function(moduleName, options) {
// Do nothing
});

moduleEventEmitter.once(exports.INIT_FINISH, function(moduleName, options) {
// Check for dependent modules, init them
calipso.module.notifyDependenciesOfInit(moduleName, options);
});

}

/**
* Event listener linked to the request object
* This is the object that will listen to each module event emitter
Expand All @@ -332,6 +332,7 @@ function RequestEventListener() {
// Configure event listener
self.modules[moduleName].routed = false; // Is it done
self.modules[moduleName].check = {}; // Hash of dependent modules to check if initialised

// self.req = req; // Request
// self.res = res; // Response
var notifyDependencies = function(moduleName) {
Expand Down
98 changes: 0 additions & 98 deletions test/event.test.js-disabled

This file was deleted.

0 comments on commit 9695137

Please sign in to comment.