Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(498)

Side by Side Diff: pkg/polymer/lib/job.dart

Issue 41293002: Fixes from polymer.dart API review (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Made Job a part of polymer Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 // Remove this type in favor of Timer?
32 class Job {
33 Function _callback;
34 Timer _timer;
35
36 void go(void callback(), Duration wait) {
37 this._callback = callback;
38 _timer = new Timer(wait, complete);
39 }
40
41 void stop() {
42 if (_timer != null) {
43 _timer.cancel();
44 _timer = null;
45 }
46 }
47
48 void complete() {
49 if (_timer != null) {
50 stop();
51 _callback();
52 }
53 }
54 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698