Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 9 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
var fs = require('fs');
var path = require('path');
var spawn = require('child_process').spawn;
fs.exists = fs.exists || path.exists;
fs.existsSync = fs.existsSync || path.existsSync;

var usleep;
try {
usleep = require('sleep').usleep;
} catch (e) {
usleep = function(microsecs) {
// Fall back to busy loop.
var deadline = Date.now() + microsecs / 1000;
while (Date.now() <= deadline);
};
}

function usleep(n) {
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, n / 1000);
}

function Xvfb(options) {
options = options || {};
Expand All @@ -32,7 +22,8 @@ Xvfb.prototype = {

this._setDisplayEnvVariable();

fs.exists(lockFile, function(exists) {
fs.access(lockFile, fs.constants.F_OK, function(e) {
var exists = !e;
var didSpawnFail = false;
try {
this._spawnProcess(exists, function(e) {
Expand All @@ -45,7 +36,8 @@ Xvfb.prototype = {

var totalTime = 0;
(function checkIfStarted() {
fs.exists(lockFile, function(exists) {
fs.access(lockFile, fs.constants.F_OK, function(e) {
var exists = !e;
if (didSpawnFail) {
// When spawn fails, the callback will immediately be called.
// So we don't have to check whether the lock file exists.
Expand Down Expand Up @@ -99,7 +91,8 @@ Xvfb.prototype = {
var lockFile = this._lockFile();
var totalTime = 0;
(function checkIfStopped() {
fs.exists(lockFile, function(exists) {
fs.access(lockFile, fs.constants.F_OK, function(e) {
var exists = !e;
if (!exists) {
return cb && cb(null, this._process);
} else {
Expand Down Expand Up @@ -188,34 +181,3 @@ Xvfb.prototype = {
}

module.exports = Xvfb;


if (require.main === module) {
var assert = require('assert');
var xvfb = new Xvfb({ displayNum: 88 });
xvfb.startSync();
console.error('started sync');
xvfb.stopSync();
console.error('stopped sync');
xvfb.start(function(err) {
assert.equal(err, null);
console.error('started async');
xvfb.stop(function(err) {
assert.equal(err, null);
console.error('stopped async');
xvfb.start(function(err) {
assert.equal(err, null);
console.error('started async');
xvfb.stopSync();
console.error('stopped sync');
xvfb.startSync();
console.error('started sync');
xvfb.stop(function(err) {
assert.equal(err, null);
console.error('stopped async');
});
});
});
});
}

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"repository": {
"url": "https://github.com/Rob--W/node-xvfb.git"
},
"optionalDependencies": {
"sleep": "6.1.0"
"scripts": {
"test": "node test.js"
},
"license": "MIT"
}
29 changes: 29 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var assert = require('assert');
var Xvfb = require('./index.js');

var xvfb = new Xvfb({ displayNum: 88 });

xvfb.startSync();
console.error('started sync');
xvfb.stopSync();
console.error('stopped sync');
xvfb.start(function(err) {
assert.equal(err, null);
console.error('started async');
xvfb.stop(function(err) {
assert.equal(err, null);
console.error('stopped async');
xvfb.start(function(err) {
assert.equal(err, null);
console.error('started async');
xvfb.stopSync();
console.error('stopped sync');
xvfb.startSync();
console.error('started sync');
xvfb.stop(function(err) {
assert.equal(err, null);
console.error('stopped async');
});
});
});
});