diff --git a/lib/xcore.js b/lib/xcore.js index 17eb9ed..b403ca3 100644 --- a/lib/xcore.js +++ b/lib/xcore.js @@ -114,6 +114,7 @@ XClient.prototype.init = function(stream) this.event_consumers = {}; // maps window id to eventemitter TODO: bad name this.eventParsers = {}; this.errorParsers = {}; + this._extensions = {}; this.importRequestsFromTemplates(this, coreRequests); @@ -553,9 +554,24 @@ XClient.prototype.startHandshake = function() { XClient.prototype.require = function(extName, callback) { - var ext = require('./ext/' + extName); - ext.requireExt(this.display, callback); -} + var self = this; + var ext = this._extensions[extName]; + if (ext) { + return process.nextTick(function() { + callback(null, ext); + }); + } + + ext = require('./ext/' + extName); + ext.requireExt(this.display, function(err, _ext) { + if (err) { + return callback(err); + } + + self._extensions[extName] = _ext; + callback(null, _ext); + }); +}; module.exports.createClient = function(options, initCb) { diff --git a/test/cache-extensions.js b/test/cache-extensions.js new file mode 100644 index 0000000..decc429 --- /dev/null +++ b/test/cache-extensions.js @@ -0,0 +1,34 @@ +var x11 = require('../lib'); +var should = require('should'); + +describe('requiring an X11 extension on same connection', function() { + before(function(done) { + var self = this; + var client = x11.createClient(function(err, dpy) { + should.not.exist(err); + self.X = dpy.client; + done(); + }); + + client.on('error', function (err) { + console.error('Error : ', err); + }); + }); + + it('should be cached', function(done) { + var self = this; + this.X.require('xtest', function(err, randr) { + should.not.exist(err); + self.X.require('xtest', function(err, randr1) { + should.not.exist(err); + randr.should.equal(randr1); + done(); + }); + }); + }); + + after(function(done) { + this.X.terminate(); + this.X.on('end', done); + }); +});