JSDeferredãèªã
åããamachangããèªãã¨ãããã£ã¦è¨ã£ã¦ããJSDeferredã®ã³ã¼ããèªããdefferedãããªãã¦deferredãªã®ã§æ³¨æã
Deferred.define = function (obj, list) { if (!list) list = ["parallel", "wait", "next", "call", "loop"]; if (!obj) obj = (function () { return this })(); for (var i = 0; i < list.length; i++) { var n = list[i]; obj[n] = Deferred[n]; } return Deferred; };
ããã¯ã°ãã¼ãã«ã¹ã³ã¼ãã«æ¸ãåºãã³ã¼ãã(function () { return this })()ã§windowãªãã¸ã§ã¯ããã¨ãã¦ããã¿ããã
class Deferred(object): def define(self, obj=None, list=None): if not list: list = ["parallel", "wait", "next", "call", "loop"] if not obj: obj = globals() for n in list: obj[n] = Deferred.__dict__[n] return Deferred
-
-
-
- -
-
-
/* function next (fun) //=> Deferred * * `next` is shorthand for creating new deferred which * is called after current queue. */ Deferred.next = function (fun) { var d = new Deferred(); var id = setTimeout(function () { clearTimeout(id); d.call() }, 0); if (fun) d.callback.ok = fun; d.canceller = function () { try { clearTimeout(id) } catch (e) {} }; return d; };
setTimeout(function(){alert(1)}, 1000)ã¨ããã¨1ç§å¾ã«alertãèµ·ããããã ãsetTimeoutã¯æ°å¤ãè¿ãã¦ãã¦ãclearTimeoutã§ã¹ã±ã¸ã¥ã¼ã«ãããé¢æ°ã®å®è¡ããã£ã³ã»ã«ã§ããã
d.callã¨ããèªã¾ãªãã¨ããã ãããããããªããªã
Deferred.prototype = { init : function () { this._next = null; this.callback = { ok: function (x) { return x }, ng: function (x) { throw x } }; return this; }, next : function (fun) { return this._post("ok", fun) }, error : function (fun) { return this._post("ng", fun) }, call : function (val) { return this._fire("ok", val) }, fail : function (err) { return this._fire("ng", err) }, 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 self = this, next = "ok"; try { value = self.callback[okng].call(self, value); } catch (e) { next = "ng"; value = e; } if (value instanceof Deferred) { value._next = self._next; } else { if (self._next) self._next._fire(next, value); } return this; } };
_fireã®ä¸ã§self = thisãã¦ããæå³ãããããããªãâ¦ã
class Deferred(object): def define(self, obj=None, list=None): if not list: list = ["parallel", "wait", "next", "call", "loop"] if not obj: obj = globals() for n in list: obj[n] = Deferred.__dict__[n] return Deferred def init(self): self._next = None def ok(x): return x def ng(x): raise x sefl.callback = dict(ok=ok, ng=ng) return self def next(self, fun): return self._post("ok", fun) def error(self, fun): return self._post("ng", fun) def call(self, val): return self._fire("ok", val) def fail(self, err): return self._fire("ng", err) def _post(self, okng, fun): n = Deferred() n.callback[okng] = fun self._next = n return n def _fire(self, okng, value): self_ = self next = "ok" try: value = self.callback[okng].call(self_, value) except Exception, e: next = "ng" value = e if isinstance(value, Deferred): value._next = self_._next else: if self_._next: self_._next._fire(next, value) def _post(self, okng, fun): self._next = Deferred() self._next.callback[okng] = fun return self._next
-
-
-
- -
-
-
Deferred.prototype.nextã¨Deferred.nextã®ä¸¡æ¹ããããã¨ã«æ··ä¹±ãã¦amachangã«èããããããã¯å
¨ç¶ç¡é¢ä¿ã ãããªãJSã®å ´åãé¢æ°ã平置ããã¦ãã¾ãã¨ã°ãã¼ãã«ç©ºéãæ±æãã¦ãã¾ãã®ã§é©å½ãªããã·ã¥ã«ã¤ã£ããã§ããã ããDeferred.nextã¯Foo.nextã§ããªãã§ãããã
ã¢ã¸ã¥ã¼ã«åä½ã§ã¹ã³ã¼ããåããããããªãJSã§ã®ã³ã¼ãã£ã³ã°ã¹ã¿ã¤ã«ã
Pythonã§ã¯ãããã¬ãã«ã§å®ç¾©ããé¢æ°ãã¢ã¸ã¥ã¼ã«åä½ã®ã¹ã³ã¼ãã«ãªãã®ã§ãããã¬ãã«ã§æ¸ãã®ãé©åã
-
-
-
- -
-
-
ããã
JSDeferredã¯æ³åãã¦ãããã®ã¨ã ãã¶éã£ãã
next(f1).next(f2).next(f3)ã§å®è¡ãã¹ãã¿ã¹ã¯ãçµã¿ç«ã¦ãå¾ã§ã¾ã¨ãã¦å®è¡ããã®ãã¨æã£ã¦ãããã©ãnext(f1)ã®æç¹ã§f1ã¯å®è¡ããããã ãªã
-
-
-
- -
-
-
> next(f1).next(f2).next(f3)ã§å®è¡ãã¹ãã¿ã¹ã¯ãçµã¿ç«ã¦ãå¾ã§ã¾ã¨ãã¦å®è¡ããã®ãã¨æã£ã¦ãããã©ãnext(f1)ã®æç¹ã§f1ã¯å®è¡ããããã ãªã
æåã® next ã¯èªãã§ããã ããéã setTimeout ãã¦ã§ã¤ã 0 ã§èªãã§ãã¾ããJS ã§ã¯ãã®ã¨ãã¯ã¾ã ãå®è¡ãã¥ã¼ã«ãã®é¢æ°ãå ¥ãã ãã§ãå®è¡ããã¾ããããªã®ã§ã.next(f2).next(f3) ã¨ããã«ç¹ããå ´åããããã®ãã§ã¤ã³ãæ§ç¯ããããã¨ã« f1 ã¯å®è¡ãããããã«ãªãã¾ãã(èªåã§ã¿ã¹ã¯ããã¿ãã¦ã¦èªåã§ãã¨ããå¼ã¶ãã¨ãã§ãã¾ãããæ®éã¯ç¾å¨å®è¡ä¸ã®å¦çã®ãã¨ã«èªåã§å®è¡ãã¦ãããã»ãã楽ãªã®ã§ãããªã£ã¦ãã¾ã)
ãããåéããã¦ã¾ããã
ä¸ã®ãããªã³ã¼ããæ¸ãã¦ãå³åº§ã«è¡¨ç¤ºãè¡ãããã®ã§ããããå³åº§ã ãªãã¨æã£ããã©ãã
function foo(){ console.log("foo!"); } var id = setTimeout(function () { clearTimeout(id); foo() }, 0);
ä¸ã®ãããªã³ã¼ãã試ãã¦ã¿ãã¹ãã ã£ãããã§ãããbarãå ã«è¡¨ç¤ºãããã
function foo(){ console.log("foo!"); } var id = setTimeout(function () { clearTimeout(id); foo() }, 0); console.log("bar!");
ãªãã»ã©ãªãã»ã©ã
ãã£ã¦ã«next(f1).next(f2).next(f3).start()çãªä½¿ãæ¹ãããããã ã¨æãããã§ã¾ããã