API Reference 0.7.1rikulo_view_implRunOnceQueue

RunOnceQueue class

A task queue used to manage deferred run-once tasks. A run-once task is a task that needs to be executed only once. In other words, the result is the same no matter how many times they are executed.

If a run-once task takes long to execute, you can create an instance of RunOnceQueue, and schedule the task by invoking add. Then, the second invocation of add with the same key will replace the previous task if it is not executed yet. In other words, the previous task will be dropped (and not executed).

class RunOnceQueue {
 Map<String, Timer> _tasks;

 /** schedules a run-once task for execution.
  */
 void add(String key, void task(), [int timeout=0]) {
   if (_tasks != null)
     cancel(key);
   else
     _tasks = new HashMap();

   _tasks[key] = new Timer(new Duration(milliseconds: timeout), () {
     _tasks.remove(key);
     task();
   });
 }
 /** Cancels the scheduled task if it is still pending.
  */
 void cancel(String key) {
   final timer = _tasks.remove(key);
   if (timer != null)
     timer.cancel();
 }  
}

Methods

void add(String key, void task(), [int timeout = 0]) #

schedules a run-once task for execution.

void add(String key, void task(), [int timeout=0]) {
 if (_tasks != null)
   cancel(key);
 else
   _tasks = new HashMap();

 _tasks[key] = new Timer(new Duration(milliseconds: timeout), () {
   _tasks.remove(key);
   task();
 });
}

void cancel(String key) #

Cancels the scheduled task if it is still pending.

void cancel(String key) {
 final timer = _tasks.remove(key);
 if (timer != null)
   timer.cancel();
}