Created
November 29, 2012 18:14
-
-
Save tracker1/4170876 to your computer and use it in GitHub Desktop.
Wrapping an external service...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var EventEmitter = require("events").EventEmitter; | |
var util = require("util"); | |
module.exports = CardReader; | |
//the export is essentially a self-healing wrapper around the external interface... | |
function CardReader() { | |
var that = this, someProc = null; | |
loadProc(); | |
function loadProc() { | |
someProc = loadExternalProc(); | |
someProc.on('exit',function(err){ | |
//log error etc... | |
//reload external proc | |
loadProc(); | |
}); | |
//for each instance event, bind a passthrough. | |
someproc.on('something',function(){ | |
that.emit.bind('something').apply(arguments); | |
}); | |
} | |
} | |
util.inherits(CardReader,EventEmitter); | |
function loadExternalProc() { | |
//run your external script that may fail here... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment