1 /**
  2  * Copyright (C) 2012 KO GmbH <copyright@kogmbh.com>
  3  *
  4  * @licstart
  5  * This file is part of WebODF.
  6  *
  7  * WebODF is free software: you can redistribute it and/or modify it
  8  * under the terms of the GNU Affero General Public License (GNU AGPL)
  9  * as published by the Free Software Foundation, either version 3 of
 10  * the License, or (at your option) any later version.
 11  *
 12  * WebODF is distributed in the hope that it will be useful, but
 13  * WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  * GNU Affero General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU Affero General Public License
 18  * along with WebODF.  If not, see <http://www.gnu.org/licenses/>.
 19  * @licend
 20  *
 21  * @source: http://www.webodf.org/
 22  * @source: https://github.com/kogmbh/WebODF/
 23  */
 24 
 25 /*global Node, core, runtime*/
 26 
 27 /**
 28  * @class
 29  * A scheduled task allows multiple requests for the same function to
 30  * be aggregated into a single call at a future time. This is useful for
 31  * batching up draw requests or other lower-priority or high-volume calls
 32  *
 33  * @constructor
 34  * @implements {core.Destroyable}
 35  * @param {!Function} fn The function to execute for this task
 36  * @param {!function(!function():undefined):!number} scheduleTask Schedule the task to execute
 37  * @param {!function(!number):undefined} cancelTask Cancel a scheduled task
 38  */
 39 core.ScheduledTask = function ScheduledTask(fn, scheduleTask, cancelTask) {
 40     "use strict";
 41     var timeoutId,
 42         scheduled = false,
 43         args = [],
 44         destroyed = false;
 45 
 46     function cancel() {
 47         if (scheduled) {
 48             cancelTask(timeoutId);
 49             scheduled = false;
 50         }
 51     }
 52 
 53     function execute() {
 54         cancel();
 55         fn.apply(undefined, args);
 56         args = null;
 57     }
 58 
 59     /**
 60      * Schedule this task to execute. If one has already been requested,
 61      * this call will have no impact
 62      */
 63     this.trigger = function () {
 64         runtime.assert(destroyed === false, "Can't trigger destroyed ScheduledTask instance");
 65         args = Array.prototype.slice.call(arguments);
 66         if (!scheduled) {
 67             scheduled = true;
 68             timeoutId = scheduleTask(execute);
 69         }
 70     };
 71 
 72     /**
 73      * Immediately trigger this task and clear any pending requests.
 74      */
 75     this.triggerImmediate = function () {
 76         runtime.assert(destroyed === false, "Can't trigger destroyed ScheduledTask instance");
 77         args = Array.prototype.slice.call(arguments);
 78         execute();
 79     };
 80 
 81     /**
 82      * Execute any pending requests, but do not start any new ones.
 83      * If there are no pending requests, this call will do nothing.
 84      */
 85     this.processRequests = function () {
 86         if (scheduled) {
 87             execute();
 88         }
 89     };
 90 
 91     /**
 92      * Cancel any current pending requests
 93      * @type {Function}
 94      */
 95     this.cancel = cancel;
 96 
 97     /**
 98      * Cancel any scheduled callbacks and immediately reschedule a new call with
 99      * any existing arguments.
100      * @return {undefined}
101      */
102     this.restart = function () {
103         runtime.assert(destroyed === false, "Can't trigger destroyed ScheduledTask instance");
104         cancel();
105         scheduled = true;
106         timeoutId = scheduleTask(execute);
107     };
108 
109     /**
110      * Cancel any pending requests
111      * @param {!function(!Error=)} callback
112      */
113     this.destroy = function (callback) {
114         cancel();
115         destroyed = true;
116         callback();
117     };
118 
119 };
120