Skip to content
Myr

Refactored Request#trigger()

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by visionmedia · committed by visionmedia · parent 2d84f16 · Browse files · 2 files+15 added−22 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+15 added−22 removed

  • other:+15 added−22 removed

2 files+15 added−22 removed

modified:lib/express/core.js+1 added-1 removed
264 unmodified lines
265 266 route: function(request, response){267 request = new Request(request, response) 268 var self= this;269 request.trigger('request', null, function(error) { 269 request.trigger('request', null, function(error) { 270 if (request.response.finished) return271 try {272 if (typeof (body = (new Router(request)).route()) === 'string')273 request.halt(200, body)
64 unmodified lines
modified:lib/express/request.js+14 added-21 removed
225 unmodified lines
226227 trigger: function(name, data, callback) {228 data = data || {}229 data.merge({ request: this, response: this.response })230 //TODO: we'd want to use the nice collection stuff that was here instead of this crappy approach...231 var pluginsExecuted= 0,232 plugin= undefined,233 totalPlugins= this.plugins.length,234 pluginResult, 235 self= this;236 var callNextPlugin= function(error) {237 if( error || pluginsExecuted >= totalPlugins ) callback(error); // All the plugins have called back or there was an error ..238 else {239 try{240 plugin= self.plugins.at(pluginsExecuted++);241 if( plugin && plugin.trigger ) {242 pluginResult= plugin.trigger(new Event(name, data), callNextPlugin);243 if(pluginResult) callNextPlugin() // If the plugin returned true then it handled the event inline, otherwise we assume it is going to call the callback itself.244 }245 else callNextPlugin();246 } catch(e) {247 callNextPlugin(e);248 }230 var self = this,231 complete = 0,232 total = this.plugins.length233 ;(function next(e) {234 if (e || complete === total)235 callback(e)236 else {237 try {238 if (self.plugins.at(complete++).trigger(new Event(name, data), next) === true)239 next()240 } catch(e) {241 next(e)242 }250 }; 251 callNextPlugin();243 }244 })()245 return this246 },247 248 /**
41 unmodified lines