reimplemented queue with async.js

This commit is contained in:
sebv
2014-03-28 22:53:47 +08:00
parent b04e858dee
commit 767a6bd992
2 changed files with 54 additions and 59 deletions

View File

@@ -68,8 +68,7 @@ IOS.prototype.init = function () {
this.iosSimProcess = null;
this.logs = {};
this.instruments = null;
this.queue = [];
this.progress = 0;
this.initQueue();
this.onInstrumentsDie = function () {};
this.stopping = false;
this.cbForCurrentCmd = null;
@@ -1120,8 +1119,6 @@ IOS.prototype.stop = function (cb) {
};
IOS.prototype.shutdown = function (code, traceDir, cb) {
this.queue = [];
this.progress = 0;
this.instruments = null;
var removeTraceDir = function (cb) {
@@ -1151,62 +1148,60 @@ IOS.prototype.proxy = deviceCommon.proxy;
IOS.prototype.proxyWithMinTime = deviceCommon.proxyWithMinTime;
IOS.prototype.respond = deviceCommon.respond;
IOS.prototype.push = function (elem) {
this.queue.push(elem);
IOS.prototype.initQueue = function () {
var next = function () {
if (this.queue.length <= 0 || this.progress > 0) {
return;
}
var target = this.queue.shift()
, command = target[0]
, cb = target[1];
if (this.selectingNewPage && this.curContext) {
logger.info("We're in the middle of selecting a new page, " +
"waiting to run next command until done");
setTimeout(next, 500);
this.queue.unshift(target);
return;
} else if (this.curContext && this.processingRemoteCmd) {
var matches = ["au.alertIsPresent", "au.getAlertText", "au.acceptAlert",
"au.dismissAlert", "au.setAlertText",
"au.waitForAlertToClose"];
var matched = false;
_.each(matches, function (match) {
if (command.indexOf(match) === 0) {
matched = true;
}
});
if (!matched) {
logger.info("We're in the middle of processing a remote debugger " +
"command, waiting to run next command until done");
setTimeout(next, 500);
this.queue.unshift(target);
return;
this.queue = async.queue(function (command, cb) {
if (!this.instruments) return cb();
async.series([
function (cb) {
async.whilst(
function () { return this.selectingNewPage && this.curContext; }.bind(this),
function (cb) {
logger.info("We're in the middle of selecting a new page, " +
"waiting to run next command until done");
setTimeout(cb, 100);
},
cb
);
}.bind(this),
function (cb) {
var matched = false;
var matches = ["au.alertIsPresent", "au.getAlertText", "au.acceptAlert",
"au.dismissAlert", "au.setAlertText",
"au.waitForAlertToClose"];
_.each(matches, function (match) {
if (command.indexOf(match) === 0) {
matched = true;
}
});
async.whilst(
function () { return !matched && this.curContext && this.processingRemoteCmd; }.bind(this),
function (cb) {
logger.info("We're in the middle of processing a remote debugger " +
"command, waiting to run next command until done");
setTimeout(cb, 100);
},
cb
);
}.bind(this)
], function (err) {
if (err) return cb(err);
this.cbForCurrentCmd = cb;
if (this.instruments) {
logger.debug("Sending command to instruments: " + command);
this.instruments.sendCommand(command, function (response) {
this.cbForCurrentCmd = null;
if (typeof cb === 'function') {
this.respond(response, cb);
}
}.bind(this));
}
}
}.bind(this));
}.bind(this), 1);
};
this.cbForCurrentCmd = cb;
this.progress++;
if (this.instruments) {
logger.debug("Sending command to instruments: " + command);
this.instruments.sendCommand(command, function (response) {
this.cbForCurrentCmd = null;
if (typeof cb === 'function') {
this.respond(response, cb);
}
// maybe there's moar work to do
this.progress--;
next();
}.bind(this));
}
}.bind(this);
next();
IOS.prototype.push = function (elem) {
this.queue.push(elem[0], elem[1]);
};
IOS.prototype.isAppInstalled = function (bundleId, cb) {

View File

@@ -32,14 +32,14 @@ describe('IOS', function () {
intercept.push(result);
if (intercept.length >= iterations) {
for (var x = 0; x < iterations; x++) {
intercept[x][0].should.equal(x);
intercept[x][0].should.equal('' + x);
}
done();
}
};
for (var i = 0; i < iterations; i++) {
inst.proxy(i, check);
inst.proxy("" + i, check);
}
});
});