Skip to content

Commit

Permalink
Modify requestqueue to use es6-promise
Browse files Browse the repository at this point in the history
  • Loading branch information
Shuwen Qian authored and Dan Lasky committed Mar 1, 2016
1 parent 943d0e0 commit c88e754
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
21 changes: 13 additions & 8 deletions src/shared/js/requestqueue.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<link rel="import" href="zousan.html"/>
<link rel="import" href="es6-promise.html"/>
<!-- <link rel="import" href="zousan.html"/> -->
<script>
/**
* @license
Expand All @@ -17,7 +18,11 @@
this.flight = [];
this.concurrency = concurrency;
this.processHook = hook;
this.promise = new Zousan();
this.defer = {};
this.promise = new Promise(function(resolve, reject) {
this.defer.resolve = resolve;
this.defer.reject = reject;
}.bind(this));

}

Expand All @@ -34,14 +39,14 @@
},this);

if (chunk && chunk.length > 0) {
var all = Zousan.all(chunk);
var all = Promise.all(chunk);
all.then(this.next.bind(this), this.fail.bind(this));
} else {
this.promise.resolve(this.results);
this.defer.resolve(this.results);
}
},
fail: function(val) {
this.promise.reject(val);
this.defer.reject(val);
this.failures.push(val);
},
exec: function() {
Expand All @@ -52,11 +57,11 @@
this.flight.forEach(function(r) {
r.abort();
});
this.promise.reject(new Error("Aborted"));
this.defer.reject(new Error("Aborted"));
}
};

scope.RequestQueue = RequestQueue;

})(window.StrandLib = window.StrandLib || {});
</script>
})(window.StrandLib = window.StrandLib || {});
</script>
19 changes: 11 additions & 8 deletions test/lib_requestqueue.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,41 @@
<body>
<script>
function Req() {
this.promise = new Zousan();
this.promise = new Promise(function(resolve) {
resolve({response:"yes"});
});
}
Req.prototype = {
exec: function() {
this.promise.resolve({response:"yes"});
return this.promise;
}
};

function FReq() {
this.promise = new Zousan();
this.promise = new Promise(function(resolve, reject) {
reject({response:"no"});
});
}
FReq.prototype = {
exec: function() {
this.promise.then(function(){},function(){});
this.promise.reject({response:"no"});
return this.promise;
}
};
function AReq() {
this.promise = new Zousan();
this.promise = new Promise(function(resolve) {
resolve({response:"yes"});
});
}
AReq.prototype = {
exec: function() {
this.promise.resolve({response:"yes"});
return this.promise;
},
abort: sinon.spy()
};

describe("RequestQueue", function() {

it("should exist", function() {
StrandLib.RequestQueue.should.be.a.function;
});
Expand Down Expand Up @@ -137,4 +140,4 @@
});
</script>
</body>
</html>
</html>

0 comments on commit c88e754

Please sign in to comment.