/* 
 * 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.
 */

(function(scope) {
  
  // usage
  
  // invoke cb.call(this) in 100ms, unless the job is re-registered,
  // which resets the timer
  // 
  // this.myJob = this.job(this.myJob, cb, 100)
  //
  // returns a job handle which can be used to re-register a job

  var Job = function(inContext) {
    this.context = inContext;
  };
  Job.prototype = {
    go: function(callback, wait) {
      this.callback = callback;
      this.handle = setTimeout(this.complete.bind(this), wait);
    },
    stop: function() {
      if (this.handle) {
        clearTimeout(this.handle);
        this.handle = null;
      }
    },
    complete: function() {
      if (this.handle) {
        this.stop();
        this.callback.call(this.context);
      }
    }
  };
  
  function job(job, callback, wait) {
    if (job) {
      job.stop();
    } else {
      job = new Job(this);
    }
    job.go(callback, wait);
    return job;
  }
  
  // exports 

  scope.job = job;
  
})(Polymer);

@@ -488,21 +488,71 @@
 ontext;%0A
+    this.boundComplete = this.complete.bind(this)%0A
   %7D;%0A
-
   Job.pr
@@ -636,27 +636,201 @@
 ;%0A      
-this.handle
+var h;%0A      if (!wait) %7B%0A        h = requestAnimationFrame(this.boundComplete);%0A        this.handle = function() %7B%0A          cancelAnimationFrame(h);%0A        %7D%0A      %7D else %7B%0A        h
  = setTi
@@ -844,35 +844,109 @@
 his.
-c
+boundC
 omplete
-.bind(this), wait);
+, wait);%0A        this.handle = function() %7B%0A          clearTimeout(h);%0A        %7D%0A      %7D
 %0A   
@@ -1005,29 +1005,16 @@
         
-clearTimeout(
 this.han
@@ -1016,16 +1016,17 @@
 s.handle
+(
 );%0A     

allow job with no timeout to go at requestAnimationFrame time.