Jscex沒有xxxAsync().stop()怎么辦?
2011-11-17 22:09 【當(dāng)耐特】 閱讀(2648) 評論(5) 收藏 舉報今天一同事問我Jscex怎么沒有stop啊?
異步任務(wù)就像斷線的風(fēng)箏,我們沒法讓它說停就停,但是我們在放飛它之前,可以裝個定時炸彈。
通常我們可以這樣退出一個異步任務(wù):
var xxxAsync = eval(Jscex.compile("async", function () {
while (condition) {
....
dosomething
....
$await(Jscex.Async.sleep(1000));
}
}))
通過condition判斷是否退出異步任務(wù)。或者等同于下面這種方式:
var xxxAsync = eval(Jscex.compile("async", function () {
while (true) {
if(condition)break;
....
dosomething
....
$await(Jscex.Async.sleep(1000));
}
}))
這兩種方式的效果是一模一樣的。
下面這種方式有個好處就是可以做一些初始化設(shè)置什么的:
var xxxAsync = eval(Jscex.compile("async", function () {
while (true) {
if (condition) {
//dosomething
break;
}
//dosomething
$await(Jscex.Async.sleep(1000));
}
}))
后來,同事又提供了一個要跳出循環(huán)的場景
var xxxAsync = eval(Jscex.compile("async", function () {
while (true) {
for (i in XXX) {
if (condition) {
//要在這里跳出最外層的循環(huán)
}
}
//dosomething
$await(Jscex.Async.sleep(1000));
}
}))
所以就只能這樣:
var xxxAsync = eval(Jscex.compile("async", function () {
while (true) {
for (i in XXX) {
if (condition) {
//要在這里跳出最外層的循環(huán)
breakTag = true;
}
}
if (breakTag) break;
//dosomething
$await(Jscex.Async.sleep(1000));
}
}))
后來同事又提供了一個BT的場景:
var countAsync1 = eval(Jscex.compile("async", function () {
while (true) {
for (i in XXX) {
if (condition) {
//要在這里跳出最外層的循環(huán)
}
}
$await(Jscex.Async.sleep(1000));
}
}))
var countAsync2 = eval(Jscex.compile("async", function () {
while (true) {
for (i in XXX) {
if (condition) {
//要在這里跳出最外層的循環(huán)
}
}
$await(Jscex.Async.sleep(1000));
}
}))
var executeAsyncQueue = eval(Jscex.compile("async", function () {
while (true) {
$await(countAsync1())
$await(countAsync2())
$await(Jscex.Async.sleep(1000));
}
}))
executeAsyncQueue().start();
相當(dāng)于要在異步隊列中跳出最最外層的循環(huán)。
我給出的方案還是breakTag
var executeAsyncQueue = eval(Jscex.compile("async", function () {
while (true) {
$await(countAsync1())
$await(countAsync2())
if (breakTag) break;
$await(Jscex.Async.sleep(1000));
}
}))
同理這個場景也用tag:
var xxAsync = eval(Jscex.compile("async", function () {
while (true) {
//dosomething
$await(xxxAsync())
if (breakTag) break;
$await(Jscex.Async.sleep("1000"));
}
}))
var xxxAsync = eval(Jscex.compile("async", function () {
if (condition) {
breakTag = true;
}
$await(Jscex.Async.sleep("1000"));
}))
所以:Jscex沒有必要提供stop,你說呢?
Jscex.Async.Task = function (delegate) {
this._delegate = delegate;
this._handlers = [];
this.status = "ready";
}
Jscex.Async.Task.prototype = {
start: function () {
if (this.status != "ready") {
throw 'Task can only be started in "ready" status.';
}
var _this = this;
this.status = "running";
this._delegate["onStart"](function (type, value) {
if (_this.status != "running") {
throw ('Callback can only be used in "running" status.');
}
if (type == "success") {
_this.result = value;
_this.status = "succeeded";
} else if (type == "failure") {
_this.error = value;
_this.status = "failed";
} else if (type == "cancel") {
_this.status = "canceled";
} else {
throw ("Unsupported type: " + type);
}
_this._notify();
});
},
cancel: function () {
if (this.status != "running") {
throw 'Task can only be canceled in "running" status';
}
var onCancel = this._delegate["onCancel"];
if (onCancel) onCancel();
this._notify();
},
_notify: function () {
var handlers = this._handlers;
delete this._handlers;
for (var i = 0; i < handlers.length; i++) {
handlers[i](this);
}
},
addListener: function (handler) {
if (!this._handlers) {
throw ('Listeners can only be added in "ready" or "running" status.');
}
this._handlers.push(handler);
}
};
浙公網(wǎng)安備 33010602011771號