| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library polymer.job; | 5 part of polymer; |
| 6 | |
| 7 import 'dart:async' show Timer; | |
| 8 | 6 |
| 9 /** | 7 /** |
| 10 * Invoke [callback] in [wait], unless the job is re-registered, | 8 * Invoke [callback] in [wait], unless the job is re-registered, |
| 11 * which resets the timer. For example: | 9 * which resets the timer. For example: |
| 12 * | 10 * |
| 13 * _myJob = runJob(_myJob, callback, const Duration(milliseconds: 100)); | 11 * _myJob = runJob(_myJob, callback, const Duration(milliseconds: 100)); |
| 14 * | 12 * |
| 15 * Returns a job handle which can be used to re-register a job. | 13 * Returns a job handle which can be used to re-register a job. |
| 16 */ | 14 */ |
| 17 // Dart note: renamed to runJob to avoid conflict with instance member "job". | 15 // Dart note: renamed to runJob to avoid conflict with instance member "job". |
| 18 Job runJob(Job job, void callback(), Duration wait) { | 16 _Job _runJob(_Job job, void callback(), Duration wait) { |
| 19 if (job != null) { | 17 if (job != null) { |
| 20 job.stop(); | 18 job.stop(); |
| 21 } else { | 19 } else { |
| 22 job = new Job(); | 20 job = new _Job(); |
| 23 } | 21 } |
| 24 job.go(callback, wait); | 22 job.go(callback, wait); |
| 25 return job; | 23 return job; |
| 26 } | 24 } |
| 27 | 25 |
| 28 // TODO(jmesserly): it isn't clear to me what is supposed to be public API here. | 26 // Public in Polymer.js but private as not sure it's the correct API for Dart. |
| 29 // Or what name we should use. "Job" is awfully generic. | 27 // Switch to Timer when 14414 is addressed. |
| 30 // (The type itself is not exported in Polymer.) | 28 class _Job { |
| 31 // Remove this type in favor of Timer? | |
| 32 class Job { | |
| 33 Function _callback; | 29 Function _callback; |
| 34 Timer _timer; | 30 Timer _timer; |
| 35 | 31 |
| 36 void go(void callback(), Duration wait) { | 32 void go(void callback(), Duration wait) { |
| 37 this._callback = callback; | 33 this._callback = callback; |
| 38 _timer = new Timer(wait, complete); | 34 _timer = new Timer(wait, complete); |
| 39 } | 35 } |
| 40 | 36 |
| 41 void stop() { | 37 void stop() { |
| 42 if (_timer != null) { | 38 if (_timer != null) { |
| 43 _timer.cancel(); | 39 _timer.cancel(); |
| 44 _timer = null; | 40 _timer = null; |
| 45 } | 41 } |
| 46 } | 42 } |
| 47 | 43 |
| 48 void complete() { | 44 void complete() { |
| 49 if (_timer != null) { | 45 if (_timer != null) { |
| 50 stop(); | 46 stop(); |
| 51 _callback(); | 47 _callback(); |
| 52 } | 48 } |
| 53 } | 49 } |
| 54 } | 50 } |
| OLD | NEW |