2011-02-27 04:15:48 -05:00
|
|
|
// Usage:: Components.utils.import('..../jsdeferred.jscodemodule.js');
|
|
|
|
// JSDeferred 0.3.4 Copyright (c) 2007 cho45 ( www.lowreal.net )
|
|
|
|
// See http://github.com/cho45/jsdeferred
|
2011-02-28 09:09:15 -05:00
|
|
|
var EXPORTED_SYMBOLS = ["Deferred"];
|
2010-11-10 11:14:04 -05:00
|
|
|
|
2011-02-28 09:09:15 -05:00
|
|
|
function setTimeout (f, i) {
|
|
|
|
let timer = Components.classes["@mozilla.org/timer;1"]
|
2011-02-27 04:15:48 -05:00
|
|
|
.createInstance(Components.interfaces.nsITimer);
|
2011-02-28 09:09:15 -05:00
|
|
|
timer.initWithCallback(f, i, timer.TYPE_ONE_SHOT);
|
2011-02-27 04:15:48 -05:00
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
2011-02-28 09:09:15 -05:00
|
|
|
function clearTimeout (timer) {
|
|
|
|
timer.cancel();
|
2011-02-27 04:15:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-10 11:14:04 -05:00
|
|
|
function Deferred () { return (this instanceof Deferred) ? this.init() : new Deferred() }
|
|
|
|
Deferred.ok = function (x) { return x };
|
|
|
|
Deferred.ng = function (x) { throw x };
|
|
|
|
Deferred.prototype = {
|
2011-02-27 04:15:48 -05:00
|
|
|
|
2010-11-10 11:14:04 -05:00
|
|
|
init : function () {
|
|
|
|
this._next = null;
|
|
|
|
this.callback = {
|
|
|
|
ok: Deferred.ok,
|
|
|
|
ng: Deferred.ng
|
|
|
|
};
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2011-02-27 04:15:48 -05:00
|
|
|
|
2010-11-10 11:14:04 -05:00
|
|
|
next : function (fun) { return this._post("ok", fun) },
|
2011-02-27 04:15:48 -05:00
|
|
|
|
|
|
|
|
2010-11-10 11:14:04 -05:00
|
|
|
error : function (fun) { return this._post("ng", fun) },
|
2011-02-27 04:15:48 -05:00
|
|
|
|
|
|
|
|
2010-11-10 11:14:04 -05:00
|
|
|
call : function (val) { return this._fire("ok", val) },
|
2011-02-27 04:15:48 -05:00
|
|
|
|
|
|
|
|
2010-11-10 11:14:04 -05:00
|
|
|
fail : function (err) { return this._fire("ng", err) },
|
|
|
|
|
2011-02-27 04:15:48 -05:00
|
|
|
|
2010-11-10 11:14:04 -05:00
|
|
|
cancel : function () {
|
|
|
|
(this.canceller || function () {})();
|
|
|
|
return this.init();
|
|
|
|
},
|
|
|
|
|
|
|
|
_post : function (okng, fun) {
|
|
|
|
this._next = new Deferred();
|
|
|
|
this._next.callback[okng] = fun;
|
|
|
|
return this._next;
|
|
|
|
},
|
|
|
|
|
|
|
|
_fire : function (okng, value) {
|
|
|
|
var next = "ok";
|
|
|
|
try {
|
|
|
|
value = this.callback[okng].call(this, value);
|
|
|
|
} catch (e) {
|
|
|
|
next = "ng";
|
|
|
|
value = e;
|
|
|
|
if (Deferred.onerror) Deferred.onerror(e);
|
|
|
|
}
|
|
|
|
if (value instanceof Deferred) {
|
|
|
|
value._next = this._next;
|
|
|
|
} else {
|
|
|
|
if (this._next) this._next._fire(next, value);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Deferred.next_default = function (fun) {
|
|
|
|
var d = new Deferred();
|
|
|
|
var id = setTimeout(function () { d.call() }, 0);
|
|
|
|
d.canceller = function () { clearTimeout(id) };
|
|
|
|
if (fun) d.callback.ok = fun;
|
|
|
|
return d;
|
|
|
|
};
|
2011-02-27 04:15:48 -05:00
|
|
|
Deferred.next_faster_way_readystatechange = ((typeof window === 'object') && (location.protocol == "http:") && !window.opera && /\bMSIE\b/.test(navigator.userAgent)) && function (fun) {
|
2010-11-10 11:14:04 -05:00
|
|
|
var d = new Deferred();
|
|
|
|
var t = new Date().getTime();
|
|
|
|
if (t - arguments.callee._prev_timeout_called < 150) {
|
|
|
|
var cancel = false;
|
|
|
|
var script = document.createElement("script");
|
|
|
|
script.type = "text/javascript";
|
2011-02-27 04:15:48 -05:00
|
|
|
script.src = "data:text/javascript,";
|
2010-11-10 11:14:04 -05:00
|
|
|
script.onreadystatechange = function () {
|
|
|
|
if (!cancel) {
|
|
|
|
d.canceller();
|
|
|
|
d.call();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
d.canceller = function () {
|
|
|
|
if (!cancel) {
|
|
|
|
cancel = true;
|
|
|
|
script.onreadystatechange = null;
|
|
|
|
document.body.removeChild(script);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
document.body.appendChild(script);
|
|
|
|
} else {
|
|
|
|
arguments.callee._prev_timeout_called = t;
|
|
|
|
var id = setTimeout(function () { d.call() }, 0);
|
|
|
|
d.canceller = function () { clearTimeout(id) };
|
|
|
|
}
|
|
|
|
if (fun) d.callback.ok = fun;
|
|
|
|
return d;
|
|
|
|
};
|
2011-02-27 04:15:48 -05:00
|
|
|
Deferred.next_faster_way_Image = ((typeof window === 'object') && (typeof(Image) != "undefined") && !window.opera && document.addEventListener) && function (fun) {
|
2010-11-10 11:14:04 -05:00
|
|
|
var d = new Deferred();
|
|
|
|
var img = new Image();
|
|
|
|
var handler = function () {
|
|
|
|
d.canceller();
|
|
|
|
d.call();
|
|
|
|
};
|
|
|
|
img.addEventListener("load", handler, false);
|
|
|
|
img.addEventListener("error", handler, false);
|
|
|
|
d.canceller = function () {
|
|
|
|
img.removeEventListener("load", handler, false);
|
|
|
|
img.removeEventListener("error", handler, false);
|
|
|
|
};
|
2011-02-27 04:15:48 -05:00
|
|
|
img.src = "data:image/png," + Math.random();
|
|
|
|
if (fun) d.callback.ok = fun;
|
|
|
|
return d;
|
|
|
|
};
|
|
|
|
Deferred.next_tick = (typeof process === 'object' && typeof process.nextTick === 'function') && function (fun) {
|
|
|
|
var d = new Deferred();
|
|
|
|
process.nextTick(function() { d.call() });
|
2010-11-10 11:14:04 -05:00
|
|
|
if (fun) d.callback.ok = fun;
|
|
|
|
return d;
|
|
|
|
};
|
|
|
|
Deferred.next = Deferred.next_faster_way_readystatechange ||
|
|
|
|
Deferred.next_faster_way_Image ||
|
2011-02-27 04:15:48 -05:00
|
|
|
Deferred.next_tick ||
|
2010-11-10 11:14:04 -05:00
|
|
|
Deferred.next_default;
|
|
|
|
|
2011-02-27 04:15:48 -05:00
|
|
|
Deferred.chain = function () {
|
|
|
|
var chain = Deferred.next();
|
|
|
|
for (var i = 0, len = arguments.length; i < len; i++) (function (obj) {
|
|
|
|
switch (typeof obj) {
|
|
|
|
case "function":
|
|
|
|
var name = null;
|
|
|
|
try {
|
|
|
|
name = obj.toString().match(/^\s*function\s+([^\s()]+)/)[1];
|
|
|
|
} catch (e) { }
|
|
|
|
if (name != "error") {
|
|
|
|
chain = chain.next(obj);
|
|
|
|
} else {
|
|
|
|
chain = chain.error(obj);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "object":
|
|
|
|
chain = chain.next(function() { return Deferred.parallel(obj) });
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw "unknown type in process chains";
|
|
|
|
}
|
|
|
|
})(arguments[i]);
|
|
|
|
return chain;
|
|
|
|
};
|
|
|
|
|
2010-11-10 11:14:04 -05:00
|
|
|
Deferred.wait = function (n) {
|
|
|
|
var d = new Deferred(), t = new Date();
|
|
|
|
var id = setTimeout(function () {
|
|
|
|
d.call((new Date).getTime() - t.getTime());
|
|
|
|
}, n * 1000);
|
|
|
|
d.canceller = function () { clearTimeout(id) };
|
|
|
|
return d;
|
|
|
|
};
|
|
|
|
|
2011-02-27 04:15:48 -05:00
|
|
|
Deferred.call = function (fun) {
|
2010-11-10 11:14:04 -05:00
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
|
|
|
return Deferred.next(function () {
|
2011-02-27 04:15:48 -05:00
|
|
|
return fun.apply(this, args);
|
2010-11-10 11:14:04 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Deferred.parallel = function (dl) {
|
|
|
|
if (arguments.length > 1) dl = Array.prototype.slice.call(arguments);
|
|
|
|
var ret = new Deferred(), values = {}, num = 0;
|
|
|
|
for (var i in dl) if (dl.hasOwnProperty(i)) (function (d, i) {
|
2011-02-27 04:15:48 -05:00
|
|
|
if (typeof d == "function") d = Deferred.next(d);
|
2010-11-10 11:14:04 -05:00
|
|
|
d.next(function (v) {
|
|
|
|
values[i] = v;
|
|
|
|
if (--num <= 0) {
|
|
|
|
if (dl instanceof Array) {
|
|
|
|
values.length = dl.length;
|
|
|
|
values = Array.prototype.slice.call(values, 0);
|
|
|
|
}
|
|
|
|
ret.call(values);
|
|
|
|
}
|
|
|
|
}).error(function (e) {
|
|
|
|
ret.fail(e);
|
|
|
|
});
|
|
|
|
num++;
|
|
|
|
})(dl[i], i);
|
|
|
|
|
|
|
|
if (!num) Deferred.next(function () { ret.call() });
|
|
|
|
ret.canceller = function () {
|
|
|
|
for (var i in dl) if (dl.hasOwnProperty(i)) {
|
|
|
|
dl[i].cancel();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
Deferred.earlier = function (dl) {
|
2011-02-27 04:15:48 -05:00
|
|
|
if (arguments.length > 1) dl = Array.prototype.slice.call(arguments);
|
2010-11-10 11:14:04 -05:00
|
|
|
var ret = new Deferred(), values = {}, num = 0;
|
|
|
|
for (var i in dl) if (dl.hasOwnProperty(i)) (function (d, i) {
|
|
|
|
d.next(function (v) {
|
|
|
|
values[i] = v;
|
|
|
|
if (dl instanceof Array) {
|
|
|
|
values.length = dl.length;
|
|
|
|
values = Array.prototype.slice.call(values, 0);
|
|
|
|
}
|
|
|
|
ret.canceller();
|
|
|
|
ret.call(values);
|
|
|
|
}).error(function (e) {
|
|
|
|
ret.fail(e);
|
|
|
|
});
|
|
|
|
num++;
|
|
|
|
})(dl[i], i);
|
|
|
|
|
|
|
|
if (!num) Deferred.next(function () { ret.call() });
|
|
|
|
ret.canceller = function () {
|
|
|
|
for (var i in dl) if (dl.hasOwnProperty(i)) {
|
|
|
|
dl[i].cancel();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Deferred.loop = function (n, fun) {
|
|
|
|
var o = {
|
|
|
|
begin : n.begin || 0,
|
|
|
|
end : (typeof n.end == "number") ? n.end : n - 1,
|
|
|
|
step : n.step || 1,
|
|
|
|
last : false,
|
|
|
|
prev : null
|
|
|
|
};
|
|
|
|
var ret, step = o.step;
|
|
|
|
return Deferred.next(function () {
|
|
|
|
function _loop (i) {
|
|
|
|
if (i <= o.end) {
|
|
|
|
if ((i + step) > o.end) {
|
|
|
|
o.last = true;
|
|
|
|
o.step = o.end - i + 1;
|
|
|
|
}
|
|
|
|
o.prev = ret;
|
|
|
|
ret = fun.call(this, i, o);
|
|
|
|
if (ret instanceof Deferred) {
|
|
|
|
return ret.next(function (r) {
|
|
|
|
ret = r;
|
|
|
|
return Deferred.call(_loop, i + step);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return Deferred.call(_loop, i + step);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (o.begin <= o.end) ? Deferred.call(_loop, o.begin) : null;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-02-27 04:15:48 -05:00
|
|
|
Deferred.repeat = function (n, fun) {
|
2010-11-10 11:14:04 -05:00
|
|
|
var i = 0, end = {}, ret = null;
|
|
|
|
return Deferred.next(function () {
|
|
|
|
var t = (new Date()).getTime();
|
|
|
|
divide: {
|
|
|
|
do {
|
|
|
|
if (i >= n) break divide;
|
2011-02-27 04:15:48 -05:00
|
|
|
ret = fun(i++);
|
2010-11-10 11:14:04 -05:00
|
|
|
} while ((new Date()).getTime() - t < 20);
|
|
|
|
return Deferred.call(arguments.callee);
|
|
|
|
}
|
2011-02-27 04:15:48 -05:00
|
|
|
return null;
|
2010-11-10 11:14:04 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Deferred.register = function (name, fun) {
|
|
|
|
this.prototype[name] = function () {
|
|
|
|
var a = arguments;
|
|
|
|
return this.next(function () {
|
|
|
|
return fun.apply(this, a);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
Deferred.register("loop", Deferred.loop);
|
|
|
|
Deferred.register("wait", Deferred.wait);
|
|
|
|
|
2011-02-27 04:15:48 -05:00
|
|
|
Deferred.connect = function (funo, options) {
|
|
|
|
var target, func, obj;
|
|
|
|
if (typeof arguments[1] == "string") {
|
|
|
|
target = arguments[0];
|
|
|
|
func = target[arguments[1]];
|
|
|
|
obj = arguments[2] || {};
|
|
|
|
} else {
|
|
|
|
func = arguments[0];
|
|
|
|
obj = arguments[1] || {};
|
|
|
|
target = obj.target;
|
|
|
|
}
|
|
|
|
|
|
|
|
var partialArgs = obj.args ? Array.prototype.slice.call(obj.args, 0) : [];
|
|
|
|
var callbackArgIndex = isFinite(obj.ok) ? obj.ok : obj.args ? obj.args.length : undefined;
|
2010-11-10 11:14:04 -05:00
|
|
|
var errorbackArgIndex = obj.ng;
|
|
|
|
|
|
|
|
return function () {
|
2011-02-27 04:15:48 -05:00
|
|
|
var d = new Deferred().next(function (args) {
|
|
|
|
var next = this._next.callback.ok;
|
|
|
|
this._next.callback.ok = function () {
|
|
|
|
return next.apply(this, args.args);
|
|
|
|
};
|
|
|
|
});
|
2010-11-10 11:14:04 -05:00
|
|
|
|
2011-02-27 04:15:48 -05:00
|
|
|
var args = partialArgs.concat(Array.prototype.slice.call(arguments, 0));
|
2010-11-10 11:14:04 -05:00
|
|
|
if (!(isFinite(callbackArgIndex) && callbackArgIndex !== null)) {
|
|
|
|
callbackArgIndex = args.length;
|
|
|
|
}
|
|
|
|
var callback = function () { d.call(new Deferred.Arguments(arguments)) };
|
|
|
|
args.splice(callbackArgIndex, 0, callback);
|
|
|
|
if (isFinite(errorbackArgIndex) && errorbackArgIndex !== null) {
|
|
|
|
var errorback = function () { d.fail(arguments) };
|
|
|
|
args.splice(errorbackArgIndex, 0, errorback);
|
|
|
|
}
|
|
|
|
Deferred.next(function () { func.apply(target, args) });
|
|
|
|
return d;
|
|
|
|
}
|
2011-02-27 04:15:48 -05:00
|
|
|
};
|
|
|
|
Deferred.Arguments = function (args) { this.args = Array.prototype.slice.call(args, 0) };
|
2010-11-10 11:14:04 -05:00
|
|
|
|
2011-02-27 04:15:48 -05:00
|
|
|
Deferred.retry = function (retryCount, funcDeferred, options) {
|
2010-11-10 11:14:04 -05:00
|
|
|
if (!options) options = {};
|
|
|
|
|
|
|
|
var wait = options.wait || 0;
|
|
|
|
var d = new Deferred();
|
|
|
|
var retry = function () {
|
|
|
|
var m = funcDeferred(retryCount);
|
|
|
|
m.
|
|
|
|
next(function (mes) {
|
|
|
|
d.call(mes);
|
|
|
|
}).
|
|
|
|
error(function (e) {
|
|
|
|
if (--retryCount <= 0) {
|
|
|
|
d.fail(['retry failed', e]);
|
|
|
|
} else {
|
|
|
|
setTimeout(retry, wait * 1000);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
setTimeout(retry, 0);
|
|
|
|
return d;
|
2011-02-27 04:15:48 -05:00
|
|
|
};
|
2010-11-10 11:14:04 -05:00
|
|
|
|
2011-02-27 04:15:48 -05:00
|
|
|
Deferred.methods = ["parallel", "wait", "next", "call", "loop", "repeat", "chain"];
|
2010-11-10 11:14:04 -05:00
|
|
|
Deferred.define = function (obj, list) {
|
2011-02-27 04:15:48 -05:00
|
|
|
if (!list) list = Deferred.methods;
|
2010-11-10 11:14:04 -05:00
|
|
|
if (!obj) obj = (function getGlobal () { return this })();
|
|
|
|
for (var i = 0; i < list.length; i++) {
|
|
|
|
var n = list[i];
|
|
|
|
obj[n] = Deferred[n];
|
|
|
|
}
|
|
|
|
return Deferred;
|
|
|
|
};
|
|
|
|
|
2011-02-28 09:09:15 -05:00
|
|
|
this.Deferred = Deferred;
|