OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library polymer.job; | |
6 | |
7 import 'dart:async' show Timer; | |
8 | |
9 /** | |
10 * Invoke [callback] in [wait], unless the job is re-registered, | |
11 * which resets the timer. For example: | |
12 * | |
13 * _myJob = runJob(_myJob, callback, const Duration(milliseconds: 100)); | |
14 * | |
15 * Returns a job handle which can be used to re-register a job. | |
16 */ | |
17 // Dart note: renamed to runJob to avoid conflict with instance member "job". | |
18 Job runJob(Job job, void callback(), Duration wait) { | |
19 if (job != null) { | |
20 job.stop(); | |
21 } else { | |
22 job = new Job(); | |
23 } | |
24 job.go(callback, wait); | |
25 return job; | |
26 } | |
27 | |
28 // TODO(jmesserly): it isn't clear to me what is supposed to be public API here. | |
29 // Or what name we should use. "Job" is awfully generic. | |
30 // (The type itself is not exported in Polymer.) | |
31 class Job { | |
32 Function _callback; | |
33 Timer _timer; | |
34 | |
35 void go(void callback(), Duration wait) { | |
36 this._callback = callback; | |
37 _timer = new Timer(wait, complete); | |
38 } | |
39 | |
40 void stop() { | |
41 if (_timer != null) { | |
42 _timer.cancel(); | |
43 _timer = null; | |
44 } | |
45 } | |
46 | |
47 void complete() { | |
blois
2013/09/27 21:40:52
Any reason complete is public?
If not, can the _t
Jennifer Messerly
2013/09/30 17:44:37
See TODO, I had the same question :). But yeah, I
| |
48 if (_timer != null) { | |
49 stop(); | |
50 _callback(); | |
51 } | |
52 } | |
53 } | |
OLD | NEW |