Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Adds cancelAsync to cancel a call to the async method (e.g. this.canc…
Browse files Browse the repository at this point in the history
…elAsync(this.async(<method>))
  • Loading branch information
sorvell committed Feb 11, 2014
1 parent 797274e commit 74c3d71
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/instance/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@
(this[method] || method).apply(this, args);
}.bind(this);
// execute `fn` sooner or later
return timeout ? setTimeout(fn, timeout) : requestAnimationFrame(fn);
var handle = timeout ? setTimeout(fn, timeout) :
requestAnimationFrame(fn);
// NOTE: switch on inverting handle to determine which time is used.
return timeout ? handle : 1 / handle;
},
cancelAsync: function(handle) {
if (handle < 1) {
cancelAnimationFrame(Math.round(1 / handle));
} else {
clearTimeout(handle);
}
},
/**
* Fire an event.
Expand Down
41 changes: 41 additions & 0 deletions test/html/async.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!doctype html>
<html>
<head>
<title>async test</title>
<script src="../../../platform/platform.js"></script>
<link rel="import" href="../../polymer.html">
<script src="../../../tools/test/htmltest.js"></script>
<script src="../../../tools/test/chai/chai.js"></script>
</head>
<body>

<x-foo>I am x-foo.</x-foo>

<polymer-element name="x-foo">
<script>
Polymer('x-foo', {
asyncCount: 0,
totalAsyncs: 5000,
ready: function() {
var c = 0, d, e;
for (var i=0; i < this.totalAsyncs; i++) {
this.cancelAsync(this.async('asyncHandler'));
this.cancelAsync(this.async('asyncHandler', null, 1));
}
this.async('done');
},
asyncHandler: function() {
this.asyncCount++;
},
done: function() {
this.async('test', null, 50);
},
test: function() {
chai.assert.equal(this.asyncCount, 0);
done();
}
});
</script>
</polymer-element>
</body>
</html>
9 changes: 9 additions & 0 deletions test/js/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright 2013 The Polymer Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
htmlSuite('utils', function() {
htmlTest('html/async.html');
});

1 change: 1 addition & 0 deletions test/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<script src="js/styling.js"></script>
<script src="js/mdv-syntax.js"></script>
<script src="js/paths.js"></script>
<script src="js/utils.js"></script>
<!-- -->
<script>
document.addEventListener('polymer-ready', function() {
Expand Down

0 comments on commit 74c3d71

Please sign in to comment.