Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

asyncevents

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

asyncevents

Extension to EventEmitter to facilitate asynchronous firing of events, and asynchronous handling of those events.

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
Maintainers
0
Weekly downloads
 
Created
Source

Asynchronous Event Emitter

What?

An augmented version of the standard EventEmitter that exposes an additional function: emitAsync.

emitAsync will fire each handler in a non-parallel manner (each handler has to finish before the next will be fired).

Why?!

... Because I can!

Seriously though, use this if you want to handle events, but don't want execution to continue until all handlers have completed their (possibly) asynchronous handler logic.

How?

Observe, a different approach to http middleware.

** An event producer: httpServer.js ** var RequestHandler = function(req, res) { // ... a request has come in. // We want to let all interested parties know the a request has come in, and let one of them register a method to respond to the event. this.emitAsync("request", function(err) { if(!err) req.responder(res); else response500(err); }, req, res); }

util.inherits(RequestHandler, AsyncEventEmitter);

var requestModule = new RequestHandler();
var httpServer = require("http").createServer();
httpServer.listen(80);

** An async event consumer: staticFile.js ** requestModule.on("request", function(req) { // Gonna need to see if the file exists locally. var callback = this.deferHandler();

	path.exists(someReferenceToDocumentRoot + url.parse(req.url).pathname, function(exists) {
		// Invoking callback() with no arguments (or false, null, undefined) will transfer control to next handler.
		if(!exists) return callback();

		fs.readFile(someReferenceToDocumentRoot + url.parse(req.url).pathname, function(err, data) {
			// Invoking the callback with an error will immediately stop any further handler execution, and call the emit callback with the exception.
			if(err) return callback(err);
			
			req.responder = function(res) {
				res.writeHead(200 /* http headers here*/);
				res.end(data);
			};

			// Invoking the callback with true ensures no further handlers are executed.
			callback(true);
		});
	});
});

** A synchronous event consumer: helloworld.js ** requestModule.on("request", function(req) { var handled = false; if(/.hello$/.test(req.url)) { req.responder = function(res) { res.writeHead(200 /* http headers here*/); res.end("Hello, world!"); }; handled = true; }

	// Returning true will ensure no further handlers in the chain are executed.
	return handled;
});

Promises? Futures?

Could achieve the same result in a way, yes. However asynchronous event handling is more specific to .... handling event emitting in an asynchronous manner. Also, using this method gives you a formal way to terminate an event in much the same way as you would in the W3C event model. Finally, this model supports handlers being both asynchronous and synchronous.

Issues

Probably a few. If you find anything please let me know, or fix it yourself and push the changes to me.

Maybe TODO

One issue is if handler is deferred, and the resulting callback is not invoked, then the emit() callback will never be called. This could potentially leave HTTP requests/whatever in an eternal wait. Perhaps a timeout should be worked in somewhere.

Keywords

FAQs

Package last updated on 07 Feb 2011

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc