1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2013, 2014, 2016 Synacor, Inc.
  5  *
  6  * The contents of this file are subject to the Common Public Attribution License Version 1.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at: https://www.zimbra.com/license
  9  * The License is based on the Mozilla Public License Version 1.1 but Sections 14 and 15
 10  * have been added to cover use of software over a computer network and provide for limited attribution
 11  * for the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B.
 12  *
 13  * Software distributed under the License is distributed on an "AS IS" basis,
 14  * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 15  * See the License for the specific language governing rights and limitations under the License.
 16  * The Original Code is Zimbra Open Source Web Client.
 17  * The Initial Developer of the Original Code is Zimbra, Inc.  All rights to the Original Code were
 18  * transferred by Zimbra, Inc. to Synacor, Inc. on September 14, 2015.
 19  *
 20  * All portions of the code are Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * 
 26  * @private
 27  */
 28 AjxTimedAction = function(obj, func, args) {
 29 	AjxCallback.call(this, obj, func, args);
 30 	this._tid = -1;
 31 	this._id = -1;
 32     this._runResult = null;
 33 }
 34 AjxTimedAction.prototype = new AjxCallback();
 35 AjxTimedAction.prototype.constructor = AjxTimedAction;
 36 
 37 // Setting a timeout of 25 days or more appears to revert it
 38 // to 0 in FF3 and Safari3. There's really no reason to set
 39 // it to anything above a few days, so set a max of 20 days.
 40 AjxTimedAction.MAX_TIMEOUT = 20 * 24 * 60 * 60 * 1000;
 41 
 42 AjxTimedAction.prototype.toString = 
 43 function() {
 44 	return "AjxTimedAction";
 45 };
 46 
 47 AjxTimedAction.prototype.getRunResult =
 48 function() {
 49     return this._runResult;
 50 };
 51 
 52 AjxTimedAction._pendingActions = {};
 53 AjxTimedAction._nextActionId = 1;
 54 
 55 AjxTimedAction.scheduleAction =
 56 function(action, timeout){
 57 	if (!action) { return; }
 58 	// if tid already exists, cancel previous timeout before setting a new one
 59 	if (action._tid && action._tid != -1) {
 60 		AjxTimedAction.cancelAction(action._id);
 61 	}
 62 
 63 	timeout = timeout || 0; // make sure timeout is numeric
 64 	if (timeout > AjxTimedAction.MAX_TIMEOUT) {
 65 		if (window.DBG) {
 66 			DBG.println(AjxDebug.DBG1, "timeout value above maximum: " + timeout);
 67 		}
 68 		timeout = AjxTimedAction.MAX_TIMEOUT;
 69 	}
 70 	var id = action._id = AjxTimedAction._nextActionId++;
 71 	AjxTimedAction._pendingActions[id] = action;
 72 	var actionStr = "AjxTimedAction._exec(" + id + ")";
 73 	action._tid = window.setTimeout(actionStr, timeout);
 74 	return action._id;
 75 };
 76 
 77 AjxTimedAction.cancelAction =
 78 function(actionId) {
 79 	var action = AjxTimedAction._pendingActions[actionId];
 80 	if (action) {
 81 		window.clearTimeout(action._tid);
 82 		delete AjxTimedAction._pendingActions[actionId];
 83 		delete action._tid;
 84 	}
 85 };
 86 
 87 AjxTimedAction._exec =
 88 function(actionId) {
 89 
 90 	try {
 91 
 92 	var action = AjxTimedAction._pendingActions[actionId];
 93 	if (action) {
 94 		delete AjxTimedAction._pendingActions[actionId];
 95 		delete action._tid;
 96 	    action._runResult = action.run();
 97 	}
 98 
 99 	} catch (ex) {
100 		AjxException.reportScriptError(ex);
101 	}
102 };
103 
104