Use latest JSDeferred (based on Official)

This commit is contained in:
YUKI Hiroshi 2014-10-21 21:44:13 +09:00
parent b0b2305bd7
commit 4f43006773

View File

@ -3,24 +3,28 @@
// See http://github.com/cho45/jsdeferred // See http://github.com/cho45/jsdeferred
var EXPORTED_SYMBOLS = ["Deferred"]; var EXPORTED_SYMBOLS = ["Deferred"];
function D () { const Cc = Components.classes;
const Ci = Components.interfaces;
var timers = []; var timers = [];
function setTimeout (f, i) { if (typeof setTimeout == 'undefined') {
let timer = Components.classes["@mozilla.org/timer;1"] function setTimeout (f, i) {
.createInstance(Components.interfaces.nsITimer); let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
timer.initWithCallback(f, i, timer.TYPE_ONE_SHOT); timer.initWithCallback(f, i, timer.TYPE_ONE_SHOT);
timers.push(timer); timers.push(timer);
return timer; return timer;
} }
function clearTimeout (timer) { function clearTimeout (timer) {
timers.splice(timers.indexOf(timer), 1); timers.splice(timers.indexOf(timer), 1);
timer.cancel(); timer.cancel();
}
} }
// JSDeferred 0.4.0 Copyright (c) 2007 cho45 ( www.lowreal.net )
// See http://github.com/cho45/jsdeferred
function Deferred () { return (this instanceof Deferred) ? this.init() : new Deferred() } function Deferred () { return (this instanceof Deferred) ? this.init() : new Deferred() }
Deferred.ok = function (x) { return x }; Deferred.ok = function (x) { return x };
Deferred.ng = function (x) { throw x }; Deferred.ng = function (x) { throw x };
@ -80,7 +84,7 @@ Deferred.prototype = {
} }
}; };
Deferred.isDeferred = function (obj) { Deferred.isDeferred = function (obj) {
return !!(obj && obj._id == Deferred.prototype._id); return !!(obj && obj._id === Deferred.prototype._id);
}; };
Deferred.next_default = function (fun) { Deferred.next_default = function (fun) {
@ -92,7 +96,7 @@ Deferred.next_default = function (fun) {
}; };
Deferred.next_faster_way_readystatechange = ((typeof window === 'object') && (location.protocol == "http:") && !window.opera && /\bMSIE\b/.test(navigator.userAgent)) && function (fun) { Deferred.next_faster_way_readystatechange = ((typeof window === 'object') && (location.protocol == "http:") && !window.opera && /\bMSIE\b/.test(navigator.userAgent)) && function (fun) {
var d = new Deferred(); var d = new Deferred();
var t = Date.now(); var t = new Date().getTime();
if (t - arguments.callee._prev_timeout_called < 150) { if (t - arguments.callee._prev_timeout_called < 150) {
var cancel = false; var cancel = false;
var script = document.createElement("script"); var script = document.createElement("script");
@ -143,10 +147,11 @@ Deferred.next_tick = (typeof process === 'object' && typeof process.nextTick ===
if (fun) d.callback.ok = fun; if (fun) d.callback.ok = fun;
return d; return d;
}; };
Deferred.next = Deferred.next_faster_way_readystatechange || Deferred.next =
Deferred.next_faster_way_Image || Deferred.next_faster_way_readystatechange ||
Deferred.next_tick || Deferred.next_faster_way_Image ||
Deferred.next_default; Deferred.next_tick ||
Deferred.next_default;
Deferred.chain = function () { Deferred.chain = function () {
var chain = Deferred.next(); var chain = Deferred.next();
@ -174,9 +179,9 @@ Deferred.chain = function () {
}; };
Deferred.wait = function (n) { Deferred.wait = function (n) {
var d = new Deferred(), t = Date.now(); var d = new Deferred(), t = new Date();
var id = setTimeout(function () { var id = setTimeout(function () {
d.call(Date.now() - t); d.call((new Date()).getTime() - t.getTime());
}, n * 1000); }, n * 1000);
d.canceller = function () { clearTimeout(id) }; d.canceller = function () { clearTimeout(id) };
return d; return d;
@ -190,14 +195,20 @@ Deferred.call = function (fun) {
}; };
Deferred.parallel = function (dl) { Deferred.parallel = function (dl) {
if (arguments.length > 1) dl = Array.prototype.slice.call(arguments); var isArray = false;
if (arguments.length > 1) {
dl = Array.prototype.slice.call(arguments);
isArray = true;
} else if (Array.isArray && Array.isArray(dl) || typeof dl.length == "number") {
isArray = true;
}
var ret = new Deferred(), values = {}, num = 0; var ret = new Deferred(), values = {}, num = 0;
for (var i in dl) if (dl.hasOwnProperty(i)) (function (d, i) { for (var i in dl) if (dl.hasOwnProperty(i)) (function (d, i) {
if (typeof d == "function") d = Deferred.next(d); if (typeof d == "function") dl[i] = d = Deferred.next(d);
d.next(function (v) { d.next(function (v) {
values[i] = v; values[i] = v;
if (--num <= 0) { if (--num <= 0) {
if (dl instanceof Array) { if (isArray) {
values.length = dl.length; values.length = dl.length;
values = Array.prototype.slice.call(values, 0); values = Array.prototype.slice.call(values, 0);
} }
@ -219,17 +230,23 @@ Deferred.parallel = function (dl) {
}; };
Deferred.earlier = function (dl) { Deferred.earlier = function (dl) {
if (arguments.length > 1) dl = Array.prototype.slice.call(arguments); var isArray = false;
if (arguments.length > 1) {
dl = Array.prototype.slice.call(arguments);
isArray = true;
} else if (Array.isArray && Array.isArray(dl) || typeof dl.length == "number") {
isArray = true;
}
var ret = new Deferred(), values = {}, num = 0; var ret = new Deferred(), values = {}, num = 0;
for (var i in dl) if (dl.hasOwnProperty(i)) (function (d, i) { for (var i in dl) if (dl.hasOwnProperty(i)) (function (d, i) {
d.next(function (v) { d.next(function (v) {
values[i] = v; values[i] = v;
if (dl instanceof Array) { if (isArray) {
values.length = dl.length; values.length = dl.length;
values = Array.prototype.slice.call(values, 0); values = Array.prototype.slice.call(values, 0);
} }
ret.canceller();
ret.call(values); ret.call(values);
ret.canceller();
}).error(function (e) { }).error(function (e) {
ret.fail(e); ret.fail(e);
}); });
@ -284,15 +301,12 @@ Deferred.loop = function (n, fun) {
Deferred.repeat = function (n, fun) { Deferred.repeat = function (n, fun) {
var i = 0, end = {}, ret = null; var i = 0, end = {}, ret = null;
return Deferred.next(function () { return Deferred.next(function () {
var t = Date.now(); var t = (new Date()).getTime();
divide: { do {
do { if (i >= n) return null;
if (i >= n) break divide; ret = fun(i++);
ret = fun(i++); } while ((new Date()).getTime() - t < 20);
} while (Date.now() - t < 20); return Deferred.call(arguments.callee);
return Deferred.call(arguments.callee);
}
return null;
}); });
}; };
@ -344,7 +358,7 @@ Deferred.connect = function (funo, options) {
} }
Deferred.next(function () { func.apply(target, args) }); Deferred.next(function () { func.apply(target, args) });
return d; return d;
} };
}; };
Deferred.Arguments = function (args) { this.args = Array.prototype.slice.call(args, 0) }; Deferred.Arguments = function (args) { this.args = Array.prototype.slice.call(args, 0) };
@ -383,118 +397,3 @@ Deferred.define = function (obj, list) {
}; };
this.Deferred = Deferred; this.Deferred = Deferred;
Deferred.postie_for_message_manager = function (manager) {
var ret = {
__proto__ : manager,
__noSuchMethod__ : function (name, args) {
return manager[name].apply(manager, args);
}
};
var id = 0;
var cb = {};
var mm = [];
var postieId = Date.now() + ':' + parseInt(Math.random() * 65000);
var messageListener = function (message) {
message = message.json;
if (message.init) {
for (let i = 0, it; it = mm[i]; i++) {
manager.sendAsyncMessage(postieId+':request', it);
}
mm = null;
} else {
let c = cb[message.id];
if (c) c(message.value, message.error);
}
};
manager.addMessageListener(postieId+':response', messageListener);
manager.loadFrameScript('data:application/javascript,'+encodeURIComponent(
('(function(_global) {' +
' var [Deferred, timers] = %JSDEFERRED%();' +
' var _onMessage = function (message) {' +
' switch (message.name) {' +
' case "%ID%:request":' +
' var data = { id : message.json.id }' +
' Deferred' +
' .next(function () {' +
' return eval(message.json.code);' +
' })' +
' .next(function (value) {' +
' data.value = value;' +
' sendAsyncMessage("%ID%:response", data);' +
' })' +
' .error(function (error) {' +
' data.error = error;' +
' sendAsyncMessage("%ID%:response", data);' +
' });' +
' break;' +
' case "%ID%:destroy":' +
' removeMessageListener("%ID%:request", onMessage);' +
' removeMessageListener("%ID%:destroy", onMessage);' +
' timers.forEach(function(aTimer) {' +
' aTimer.cancel();' +
' });' +
' timers = undefined;' +
' _onMessage = undefined;' +
' _global = undefined;' +
' Deferred = undefined;' +
' break;' +
' }' +
' };' +
' addMessageListener("%ID%:request", _onMessage);' +
' addMessageListener("%ID%:destroy", _onMessage);' +
' sendAsyncMessage("%ID%:response", { id : -1, init : true });' +
'})(this);')
.replace(/%ID%/g, postieId)
.replace(/%JSDEFERRED%/, D.toSource())
), false);
ret.post = function (args, code) {
var deferred = new Deferred();
args = Array.prototype.slice.call(arguments, 0);
code = args.pop();
code = (typeof code == 'function') ? code.toSource() : 'function () {' + code + '}';
var mes = {
id : id++,
code : '(' + code + ').apply(_global, ' + JSON.stringify(args) + ')'
};
cb[mes.id] = function (v, e) { e ? deferred.fail(e) : deferred.call(v) };
if (mm) {
mm.push(mes);
} else {
manager.sendAsyncMessage(postieId+':request', mes);
}
return deferred;
};
ret.destroy = function () {
manager.sendAsyncMessage(postieId+':destroy');
manager.removeMessageListener(postieId+':response', messageListener);
};
return ret;
};
Deferred.postie = function (target) {
if (target instanceof Components.interfaces.nsIFrameMessageManager)
return Deferred.postie_for_message_manager(target);
else
throw new Error('unknown type object was given to Deferred.postie().\n'+target);
};
Deferred.Deferred = Deferred;
return [Deferred, timers];
}
var [Deferred, timers] = D();