Cache X11 extensions

This commit is contained in:
Santiago Gimeno 2016-10-06 11:37:25 +02:00
parent f040f8bbba
commit 4f241ba2b3
2 changed files with 53 additions and 3 deletions

View file

@ -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)
{

34
test/cache-extensions.js Normal file
View file

@ -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);
});
});