javascript 异步编程2

javascript 异步编程2

好像有这么一句名言——”每一个优雅的接口,背后都有一个龌龊的实现”。最明显的例子,jQuery。之所以弄得这么复杂,因为它本来就是那复杂。虽然有些实现相对简明些,那是它们的兼容程度去不了那个地步。当然,世上总有例外,比如mootools,但暴露到我们眼前的接口,又不知到底是那个父类的东西,结构清晰但不明撩。我之所以说这样的话,因为异步列队真的很复杂,但我会尽可能让API简单易用。无new实例化,不区分实例与类方法,链式,等时髦的东西都用上。下面先奉上源码:

;(function(){
 var dom = this.dom = this.dom || {
 mix : function(target, source ,override) {
 var i, ride = (override === void 0) || override;
 for (i in source) {
 if (ride || !(i in target)) {
 target[i] = source[i];
 }
 }
 return target;
 }
 }
 //
 //=======================异步列队模块===================================
 var Deferred = dom.Deferred = function (fn) {
 return this instanceof Deferred ? this.init(fn) : new Deferred(fn)
 }
 var A_slice = Array.prototype.slice;
 dom.mix(Deferred, {
 get:function(obj){//确保this为Deferred实例
 return obj instanceof Deferred ? obj : new Deferred
 },
 ok : function (r) {//传递器
 return r
 },
 ng : function (e) {//传递器
 throw e
 }
 });
 Deferred.prototype = {
 init:function(fn){//初始化,建立两个列队
 this._firing = [];
 this._fired = [];
 if(typeof fn === "function")
 return this.then(fn)
 return this;
 },
 _add:function(okng,fn){
 var obj = {
 ok:Deferred.ok,
 ng:Deferred.ng,
 arr:[]
 }
 if(typeof fn === "function")
 obj[okng] = fn;
 this._firing.push(obj);
 return this;
 },
 then:function(fn){//_add的包装方法1,用于添加正向回调
 return Deferred.get(this)._add("ok",fn)
 },
 once:function(fn){//_add的包装方法2,用于添加负向回调
 return Deferred.get(this)._add("ng",fn)
 },
 wait:function(timeout){
 var self = Deferred.get(this);
 self._firing.push(~~timeout)
 return self
 },
 _fire:function(okng,args,result){
 var type = "ok",
 obj = this._firing.shift();
 if(obj){
 this._fired.push(obj);//把执行过的回调函数包,从一个列队倒入另一个列队
 var self = this;
 if(typeof obj === "number"){//如果是延时操作
 var timeoutID = setTimeout(function(){
 self._fire(okng,self.before(args,result))
 },obj)
 this.onabort = function(){
 clearTimeout(timeoutID );
 }
 }else if(obj.arr.length){//如果是并行操作
 var i = 0, d;
 while(d = obj.arr[i++]){
 d.fire(args)
 }
 }else{//如果是串行操作
 try{//
 result = obj[okng].apply(this,args);
 }catch(e){
 type = "ng";
 result = e;
 }
 this._fire(type,this.before(args,result))
 }
 }else{//队列执行完毕,还原
 (this.after || Deferred.ok)(result);
 this._firing = this._fired;
 this._fired = [];
 }
 return this;
 },
 fire:function(){//执行正向列队
 return this._fire("ok",this.before(arguments));
 },
 error:function(){//执行负向列队
 return this._fire("ng",this.before(arguments));
 },
 abort:function(){//中止列队
 (this.onabort || Deferred.ok)();
 return this;
 },
 //每次执行用户回调函数前都执行此函数,返回一个数组
 before:function(args,result){
 return result ? result instanceof Array ? result : [result] : A_slice.call(args)
 },
 //并行操作,并把所有的子线程的结果作为主线程的下一个操作的参数
 paiallel : function (fns) {
 var self = Deferred.get(this),
 obj = {
 ok:Deferred.ok,
 ng:Deferred.ng,
 arr:[]
 },
 count = 0,
 values = {}
 for(var key in fns){//参数可以是一个对象或数组
 if(fns.hasOwnProperty(key)){
 (function(key,fn){
 if (typeof fn == "function")
 fn = Deferred(fn);
 fn.then(function(value){
 values[key] = value;
 if(--count <= 0){
 if(fns instanceof Array){//如果是数组强制转换为数组
 values.length = fns.length;
 values = A_slice.call(values)
 }
 self._fire("ok",[values])
 }
 }).once(function(e){
 self._fire("ng",[e])
 });
 obj.arr.push(fn);
 count++
 })(key,fns[key])
 }
 }
 self.onabort = function(){
 var i = 0, d;
 while(d = obj.arr[i++]){
 d.abort();
 }
 }
 self._firing.push(obj);
 return self
 },
 //处理相近的迭代操作
 loop : function (obj, fn, result) {
 obj = {
 begin : obj.begin || 0,
 end : (typeof obj.end == "number") ? obj.end : obj - 1,
 step : obj.step || 1,
 last : false,
 prev : null
 }
 var step = obj.step,
 _loop = function(i,obj){
 if (i <= obj.end) {
 if ((i + step) > obj.end) {
 obj.last = true;
 obj.step = obj.end - i + 1;
 }
 obj.prev = result;
 result = fn.call(obj,i);
 Deferred.get(result).then(_loop).fire(i+step,obj);
 }else{
 return result;
 }
 }
 return (obj.begin <= obj.end) ? Deferred.get(this).then(_loop).fire(obj.begin,obj) : null;
 }
 }
 //将原型方法转换为类方法
 "loop wait then once paiallel".replace(/\w+/g, function(method){
 Deferred[method] = Deferred.prototype[method]
 });
})();

Deferred提供的接口其实不算多,then once loop wait paialle就这五个,我们可以new一个实例出来,用它的实例方法,可以直接用类名加方法名,其实里面还是new了一个实例。另外,还有两个专门用于复写的方法,before与after。before执行于每个回调函数之前,after执行于所有回调之后,相当于complete了。既然是列队,就有入队操作与出队操作,我不可能使用queue与dequeue这样土的命名。queue换成两个时间状语,then与once,相当于jQuery的done、fail,或dojo的addCallback、addErrback。dequeue则用fire与error取替,jQuery1.5的beta版也曾经用过fire,不知为何换成resolve这样难记的单词。好了,我们先不管其他API,现在就试试身手吧。

 var log = function (s) {
 window.console && window.console.log(s);
 }
 dom.Deferred(function () {
 log(1);//1
 })
 .then(function () {
 log(2);//2
 })
 .then(function () {
 log(3);//3
 })
 .fire();
//如果不使用异步列队,实现这种效果,就需要用套嵌函数
/*
 var fun = function(fn){
 fn()
 };
 fun(function(){
 log(1);
 fun(function(){
 log(2);
 fun(function(){
 log(3);
 })
 });
 });
*/

当然,现在是同步操作。注意,第一个回调函数是作为构造器的参数传入,可以节约了一个then^_^。

默认如果回调函数存在返回值,它会把它作为下一个回调函数的参数传入,如:

 dom.Deferred(function (a) {
 a += 10
 log(a);//11
 return a
 })
 .then(function (b) {
 b += 12
 log(b);//23
 return b
 })
 .then(function (c) {
 c += 130
 log(c);//153
 })
 .fire(1);

我们可以重载before函数,让它的结果不影响下一个回调函数。在多投事件中,我们也可以在before中定义,如果返回false,就中断队列了。

我们再来看它如何处理异常。dom.Deferred的负向列队与jQuery的是完全不同的,jQuery的只是正向列队的一个克隆,而在dom.Deferred中,负向列队只是用于突发情况,是配角。

 dom.Deferred(function () {
 log(1111111111)//11111111111
 }).
 then(function () {
 throw "error!";//发生错误
 }).
 then(function (e) {//这个回调函数不执行
 log(e+"222222")
 return 2222222
 }).
 once(function(e){//直到 遇上我们自定义的负向回调
 log(e+'333333')//error!333333
 return 333333333
 }).
 then(function (c) {//回到正向列队中
 log(c)//33333333
 }).
 fire()

<!doctype html> <html> <head> <title>异步列队 by 司徒正美 </title> <script src=”http://files.cnblogs.com/rubylouvre/dom.Deferred.js”> </script> <script> var log = function(a){ var div = document.createElement(“div”); div.innerHTML = a; document.body.appendChild(div); } window.onload = function(){ dom.Deferred(function () { log(1111111111)//11111111111 }). then(function () { throw “error!”;//发生错误 }). then(function (e) {//这个回调函数不执行 log(e+”222222″) return 2222222 }). once(function(e){//直到 遇上我们自定义的负向回调 log(e+’333333′)//error!333333 return 333333333 }). then(function (c) {//回到正向列队中 log(c)//33333333 }). fire() } </script> </head> <body> </body> </html>

运行代码

上面几个例子严格来说是同步执行,想实现异步就要用到setTimeout。当然除了setTimeout,我们还有许多方案,img.onerror script.onreadystatechange script.onload xhr.onreadystatechange self.postMessage……但它们 都有一个缺点,就是不能指定回调函数的执行时间。更何况setTimeout是没有什么兼容问题,如img.onerrot就不能用于IE6-8,postMessage虽然很快,但只支持非常新的浏览器版本。我说过,异步就是延时,延时就是等待,因此这方法叫做wait。

 dom.Deferred(function(){
 log(1)
 }).wait(1000).then(function(){
 log(2)
 }).wait(1000).then(function(){
 log(3)
 }).wait(1000).then(function(){
 log(4)
 }).fire()

<!doctype html> <html> <head> <title>异步列队 by 司徒正美 </title> <script src=”http://files.cnblogs.com/rubylouvre/dom.Deferred.js”> </script> <script> var log = function(a){ var div = document.createElement(“div”); div.innerHTML = a; document.body.appendChild(div); } window.onload = function(){ dom.Deferred(function(){ log(1) }).wait(1000).then(function(){ log(2) }).wait(1000).then(function(){ log(3) }).wait(1000).then(function(){ log(4) }).fire() } </script> </head> <body> </body> </html>

运行代码

好了,我们看异步列队中最难的部分,并行操作。这相当于模拟线程了,两个不相干的东西各自做自己的事,互不干扰。当然在时间我们还是能看出先后顺序来的。担当这职责的方法为paiallel。

 dom.Deferred.paiallel([function(){
 log("司徒正美")
 return 11
 },function(i){
 log("上官莉绮")
 return 12
 },function(i){
 log("朝沐金风")
 return 13
 }]).then(function(d){
 log(d)
 }).fire(10)

<!doctype html> <html> <head> <title>异步列队 by 司徒正美 </title> <script src=”http://files.cnblogs.com/rubylouvre/dom.Deferred.js”> </script> <script> var log = function(a){ var div = document.createElement(“div”); div.innerHTML = a; document.body.appendChild(div); } window.onload = function(){ dom.Deferred.paiallel([function(){ log(“司徒正美”) return 11 },function(i){ log(“上官莉绮”) return 12 },function(i){ log(“朝沐金风”) return 13 }]).then(function(d){ log(d) }).fire(10) } </script> </head> <body> </body> </html>

运行代码

不过,上面并没有用到异步,都是同步,这时,paiallel就相当于一个map操作。

 var d = dom.Deferred
 d.paiallel([
 d.wait(2000).then(function(a){
 log("第1个子列队");
 return 123
 }),
 d.wait(1500).then(function(a){
 log("第2个子列队");
 return 456
 }),
 d.then(function(a){
 log("第3个子列队")
 return 789
 })]).then(function(a){
 log(a)
 }).fire(3000);

<!doctype html> <html> <head> <title>异步列队 by 司徒正美 </title> <script src=”http://files.cnblogs.com/rubylouvre/dom.Deferred.js?1111111″> </script> <script> var log = function(a){ var div = document.createElement(“div”); div.innerHTML = a; document.body.appendChild(div); } window.onload = function(){ var d = dom.Deferred d.paiallel([ d.wait(2000).then(function(a){ log(“第1个子列队”); return 123 }), d.wait(1500).then(function(a){ log(“第2个子列队”); return 456 }), d.then(function(a){ log(“第3个子列队”) return 789 })]).then(function(a){ log(a) }).fire(3000); } </script> </head> <body> </body> </html>

运行代码

最后要介绍的是loop方法,它只要改变一下就能当作animate函数使用。

 d.loop(10, function(i){
 log(i);
 return d.wait(500)
 });

<!doctype html> <html> <head> <title>异步列队 by 司徒正美 </title> <script src=”http://files.cnblogs.com/rubylouvre/dom.Deferred.js?1111111″> </script> <script> var log = function(a){ var div = document.createElement(“div”); div.innerHTML = a; document.body.appendChild(div); } window.onload = function(){ var d = dom.Deferred d.loop(10, function(i){ log(i); return d.wait(500) }); } </script> </head> <body> </body> </html>

运行代码

添加多个列队,让它们交错进行,模拟“多线程”效果。

 d.loop(10, function(i){
 log("第一个列队的第"+i+"个操作");
 return d.wait(100)
 });
 d.loop(10, function(i){
 log("第二个列队的第"+i+"个操作");
 return d.wait(100)
 });
 d.loop(10, function(i){
 log("第三个列队的第"+i+"个操作");
 return d.wait(100)
 });

<!doctype html> <html> <head> <title>异步列队 by 司徒正美 </title> <script src=”http://files.cnblogs.com/rubylouvre/dom.Deferred.js?1111111″> </script> <script> var log = function(a){ var div = document.createElement(“div”); div.innerHTML = a; document.body.appendChild(div); } window.onload = function(){ var d = dom.Deferred; d.loop(10, function(i){ log(“第一个列队的第”+i+”个操作”); return d.wait(100) }); d.loop(10, function(i){ log(“第二个列队的第”+i+”个操作”); return d.wait(100) }); d.loop(10, function(i){ log(“第三个列队的第”+i+”个操作”); return d.wait(100) }); } </script> </head> <body> </body> </html>

运行代码

 

 

转载自:http://www.cnblogs.com/rubylouvre/archive/2011/03/18/1984336.html 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/110644.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Macbook pro/air 2013 late -2014 使用转接卡更换NVME SSD休眠不醒问题的解决办法

    Macbook pro/air 2013 late -2014 使用转接卡更换NVME SSD休眠不醒问题的解决办法、、1.手上512GMBP2013late差不多满了,因为穷,所以在淘宝上买了一个NVME转Macbookpcie,然后再买一个NVME2T的硬盘2.NVME因为需要最新的FirmwareRom支持,所以必须使用原装的硬盘(必须原装)安装Mac14以上,我安装了14.5.要不然识别不出来新安装的NVME硬盘3.买之前就知道是会有休眠问题的,问了卖家推荐了一些型号说不…

    2022年6月23日
    42
  • vscode连接力扣题库

    vscode连接力扣题库vscode连接力扣题库下载力扣插件回到力扣,点击题库,打开开发者工具(f12),点击网络,再次点击题库,点击标头,下滑找到cookie,复制cookie的值回到vscode,点击力扣图标,点击登录图标(如图),选择cookie登录,将刚刚复制的cookie值粘贴在这里即可…

    2022年10月23日
    0
  • Ural 1025-Democrary in Danger

    Ural 1025-Democrary in Danger

    2021年9月14日
    42
  • 多项式曲线拟合之最小二乘法推导[通俗易懂]

    多项式曲线拟合之最小二乘法推导[通俗易懂]1、多项式曲线拟合之最小二乘法1.1问题来源1801年,意大利天文学家朱赛普·皮亚齐发现了第一颗小行星谷神星。经过40天的跟踪观测后,由于谷神星运行至太阳背后,使得皮亚齐失去了谷神星的位置。随后全世界的科学家利用皮亚齐的已有观测数据开始寻找谷神星,但是根据大多数人计算的结果来寻找谷神星都没有结果。只有时年24岁的高斯所计算的谷神星的轨道,被奥地利天文学家海因里希·奥尔伯斯的观测所证实,使天文界从此可以预测到谷神星的精确位置。同样的方法也产生了哈雷彗星等很多天文学成果。高斯使用的方法就是最小二乘法,

    2022年5月17日
    53
  • CTK的编译「建议收藏」

    CTK的编译「建议收藏」CTK的编译CTK主要是一个基于QT的GUI开发框架,用于医学成像、外科导航等相关的项目。典型的应用就是3DSclier。在医学图像的软件开发中,ctk通常和vtk、ITK等开源库一起使用。一些编译的问题在CTK中编译QT的5.10等较高版本时可能遇到的问题:(1)VTK编译错误CTK默认下载的VTK版本比较旧,编译过程可能会遇到C++语法方面的错误,这时可以更改Git中下载的VT…

    2022年6月5日
    25

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号