Files
WebODF/webodf/lib/core/ScheduledTask.js
Philip Peitsch 5b7cd7c543 Re-implement up/down movements caching the last caret X position
When navigating vi the up/down arrow keys, the user often wants to move
multiple lines in sequence (e.g., to visually place the caret in one of
the following lines). To stop the caret suddenly jumping to the left
side of the page when crossing an empty paragraph, the caret's X position
will be stored and re-used by subsequent up/down movement for up to 2s
after navigating by line.

Any other operations performed by the local user (e.g., cursor movement
to the left or right), or edit operations by collaborative users will
immediately clear the stored X position.
2014-07-23 02:17:20 +10:00

120 lines
3.5 KiB
JavaScript

/**
* Copyright (C) 2012 KO GmbH <copyright@kogmbh.com>
*
* @licstart
* This file is part of WebODF.
*
* WebODF is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License (GNU AGPL)
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* WebODF is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with WebODF. If not, see <http://www.gnu.org/licenses/>.
* @licend
*
* @source: http://www.webodf.org/
* @source: https://github.com/kogmbh/WebODF/
*/
/*global Node, core, runtime*/
/**
* @class
* A scheduled task allows multiple requests for the same function to
* be aggregated into a single call at a future time. This is useful for
* batching up draw requests or other lower-priority or high-volume calls
*
* @constructor
* @implements {core.Destroyable}
* @param {!Function} fn The function to execute for this task
* @param {!function(!function():undefined):!number} scheduleTask Schedule the task to execute
* @param {!function(!number):undefined} cancelTask Cancel a scheduled task
*/
core.ScheduledTask = function ScheduledTask(fn, scheduleTask, cancelTask) {
"use strict";
var timeoutId,
scheduled = false,
args = [],
destroyed = false;
function cancel() {
if (scheduled) {
cancelTask(timeoutId);
scheduled = false;
}
}
function execute() {
cancel();
fn.apply(undefined, args);
args = null;
}
/**
* Schedule this task to execute. If one has already been requested,
* this call will have no impact
*/
this.trigger = function () {
runtime.assert(destroyed === false, "Can't trigger destroyed ScheduledTask instance");
args = Array.prototype.slice.call(arguments);
if (!scheduled) {
scheduled = true;
timeoutId = scheduleTask(execute);
}
};
/**
* Immediately trigger this task and clear any pending requests.
*/
this.triggerImmediate = function () {
runtime.assert(destroyed === false, "Can't trigger destroyed ScheduledTask instance");
args = Array.prototype.slice.call(arguments);
execute();
};
/**
* Execute any pending requests, but do not start any new ones.
* If there are no pending requests, this call will do nothing.
*/
this.processRequests = function () {
if (scheduled) {
execute();
}
};
/**
* Cancel any current pending requests
* @type {Function}
*/
this.cancel = cancel;
/**
* Cancel any scheduled callbacks and immediately reschedule a new call with
* any existing arguments.
* @return {undefined}
*/
this.restart = function () {
runtime.assert(destroyed === false, "Can't trigger destroyed ScheduledTask instance");
cancel();
scheduled = true;
timeoutId = scheduleTask(execute);
};
/**
* Cancel any pending requests
* @param {!function(!Error=)} callback
*/
this.destroy = function (callback) {
cancel();
destroyed = true;
callback();
};
};