Bug 636402 - Simplify Sync.js to avoid creating new objects. r=rnewman a=blocking-fennec

This commit is contained in:
Philipp von Weitershausen 2011-02-25 14:43:18 -08:00
Родитель e7ef2a70d4
Коммит 1a4b88fb4e
9 изменённых файлов: 100 добавлений и 233 удалений

Просмотреть файл

@ -49,7 +49,6 @@ const Cu = Components.utils;
Cu.import("resource://services-sync/record.js");
Cu.import("resource://services-sync/constants.js");
Cu.import("resource://services-sync/ext/Observers.js");
Cu.import("resource://services-sync/ext/Sync.js");
Cu.import("resource://services-sync/identity.js");
Cu.import("resource://services-sync/log4moz.js");
Cu.import("resource://services-sync/resource.js");
@ -484,6 +483,10 @@ Engine.prototype = {
function SyncEngine(name) {
Engine.call(this, name || "SyncEngine");
this.loadToFetch();
Utils.lazy2(this, "_timer", function() {
return Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
});
}
SyncEngine.prototype = {
__proto__: Engine.prototype,
@ -575,6 +578,13 @@ SyncEngine.prototype = {
return record;
},
_sleep: function _sleep(delay) {
let cb = Utils.makeSyncCallback();
this._timer.initWithCallback({notify: cb}, delay,
Ci.nsITimer.TYPE_ONE_SHOT);
Utils.waitForSyncCallback(cb);
},
// Any setup that needs to happen at the beginning of each sync.
_syncStartup: function SyncEngine__syncStartup() {
@ -741,7 +751,7 @@ SyncEngine.prototype = {
if (applyBatch.length == this.applyIncomingBatchSize) {
doApplyBatch.call(this);
}
Sync.sleep(0);
this._sleep(0);
});
// Only bother getting data from the server if there's new things
@ -974,7 +984,7 @@ SyncEngine.prototype = {
if ((++count % MAX_UPLOAD_RECORDS) == 0)
doUpload((count - MAX_UPLOAD_RECORDS) + " - " + count + " out");
Sync.sleep(0);
this._sleep(0);
}
// Final upload

Просмотреть файл

@ -54,7 +54,6 @@ Cu.import("resource://services-sync/engines.js");
Cu.import("resource://services-sync/record.js");
Cu.import("resource://services-sync/util.js");
Cu.import("resource://services-sync/log4moz.js");
Cu.import("resource://services-sync/ext/Sync.js");
function HistoryRec(collection, id) {
CryptoWrapper.call(this, collection, id);
@ -459,8 +458,7 @@ HistoryStore.prototype = {
return failed;
}
let [updatePlaces, cb] = Sync.withCb(this._asyncHistory.updatePlaces,
this._asyncHistory);
let cb = Utils.makeSyncCallback();
let onPlace = function onPlace(result, placeInfo) {
if (!Components.isSuccessCode(result)) {
failed.push(placeInfo.guid);
@ -471,7 +469,8 @@ HistoryStore.prototype = {
cb();
};
Svc.Obs.add(TOPIC_UPDATEPLACES_COMPLETE, onComplete);
updatePlaces(placeInfos, onPlace);
this._asyncHistory.updatePlaces(placeInfos, onPlace);
Utils.waitForSyncCallback(cb);
return failed;
},

Просмотреть файл

@ -1,215 +0,0 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Weave.
*
* The Initial Developer of the Original Code is Mozilla.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Edward Lee <edilee@mozilla.com>
* Dan Mills <thunder@mozilla.com>
* Myk Melez <myk@mozilla.org>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
let EXPORTED_SYMBOLS = ["Sync"];
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;
// Define some constants to specify various sync. callback states
const CB_READY = {};
const CB_COMPLETE = {};
const CB_FAIL = {};
// Share a secret only for functions in this file to prevent outside access
const SECRET = {};
/**
* Check if the app is ready (not quitting)
*/
function checkAppReady() {
// Watch for app-quit notification to stop any sync. calls
let os = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
os.addObserver({
observe: function observe() {
// Now that the app is quitting, make checkAppReady throw
checkAppReady = function() {
throw Components.Exception("App. Quitting", Cr.NS_ERROR_ABORT);
};
os.removeObserver(this, "quit-application");
}
}, "quit-application", false);
// In the common case, checkAppReady just returns true
return (checkAppReady = function() true)();
};
/**
* Create a callback that remembers state like whether it's been called
*/
function makeCallback() {
// Initialize private callback data to prepare to be called
let _ = {
state: CB_READY,
value: null
};
// The main callback remembers the value it's passed and that it got data
let onComplete = function makeCallback_onComplete(data) {
_.state = CB_COMPLETE;
_.value = data;
};
// Only allow access to the private data if the secret matches
onComplete._ = function onComplete__(secret) secret == SECRET ? _ : {};
// Allow an alternate callback to trigger an exception to be thrown
onComplete.throw = function onComplete_throw(data) {
_.state = CB_FAIL;
_.value = data;
// Cause the caller to get an exception and stop execution
throw data;
};
return onComplete;
}
/**
* Make a synchronous version of the function object that will be called with
* the provided thisArg.
*
* @param func {Function}
* The asynchronous function to make a synchronous function
* @param thisArg {Object} [optional]
* The object that the function accesses with "this"
* @param callback {Function} [optional] [internal]
* The callback that will trigger the end of the async. call
* @usage let ret = Sync(asyncFunc, obj)(arg1, arg2);
* @usage let ret = Sync(ignoreThisFunc)(arg1, arg2);
* @usage let sync = Sync(async); let ret = sync(arg1, arg2);
*/
function Sync(func, thisArg, callback) {
return function syncFunc(/* arg1, arg2, ... */) {
// Grab the current thread so we can make it give up priority
let thread = Cc["@mozilla.org/thread-manager;1"].getService().currentThread;
// Save the original arguments into an array
let args = Array.slice(arguments);
let instanceCallback = callback;
// We need to create a callback and insert it if we weren't given one
if (instanceCallback == null) {
// Create a new callback for this invocation instance and pass it in
instanceCallback = makeCallback();
args.unshift(instanceCallback);
}
// Call the async function bound to thisArg with the passed args
func.apply(thisArg, args);
// Keep waiting until our callback is triggered unless the app is quitting
let callbackData = instanceCallback._(SECRET);
while (checkAppReady() && callbackData.state == CB_READY)
thread.processNextEvent(true);
// Reset the state of the callback to prepare for another call
let state = callbackData.state;
callbackData.state = CB_READY;
// Throw the value the callback decided to fail with
if (state == CB_FAIL)
throw callbackData.value;
// Return the value passed to the callback
return callbackData.value;
};
}
/**
* Make a synchronous version of an async. function and the callback to trigger
* the end of the async. call.
*
* @param func {Function}
* The asynchronous function to make a synchronous function
* @param thisArg {Object} [optional]
* The object that the function accesses with "this"
* @usage let [sync, cb] = Sync.withCb(async); let ret = sync(arg1, arg2, cb);
*/
Sync.withCb = function Sync_withCb(func, thisArg) {
let cb = makeCallback();
return [Sync(func, thisArg, cb), cb];
};
/**
* Set a timer, simulating the API for the window.setTimeout call.
* This only simulates the API for the version of the call that accepts
* a function as its first argument and no additional parameters,
* and it doesn't return the timeout ID.
*
* @param func {Function}
* the function to call after the delay
* @param delay {Number}
* the number of milliseconds to wait
*/
function setTimeout(func, delay) {
let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
let callback = {
notify: function notify() {
// This line actually just keeps a reference to timer (prevent GC)
timer = null;
// Call the function so that "this" is global
func();
}
}
timer.initWithCallback(callback, delay, Ci.nsITimer.TYPE_ONE_SHOT);
}
function sleep(callback, milliseconds) {
setTimeout(callback, milliseconds);
}
/**
* Sleep the specified number of milliseconds, pausing execution of the caller
* without halting the current thread.
* For example, the following code pauses 1000ms between dumps:
*
* dump("Wait for it...\n");
* Sync.sleep(1000);
* dump("Wait for it...\n");
* Sync.sleep(1000);
* dump("What are you waiting for?!\n");
*
* @param milliseconds {Number}
* The number of milliseconds to sleep
*/
Sync.sleep = Sync(sleep);

Просмотреть файл

@ -42,7 +42,6 @@ const Cr = Components.results;
const Cu = Components.utils;
Cu.import("resource://services-sync/constants.js");
Cu.import("resource://services-sync/ext/Sync.js");
Cu.import("resource://services-sync/log4moz.js");
Cu.import("resource://services-sync/util.js");

Просмотреть файл

@ -48,7 +48,6 @@ const Cu = Components.utils;
Cu.import("resource://services-sync/constants.js");
Cu.import("resource://services-sync/ext/Observers.js");
Cu.import("resource://services-sync/ext/Preferences.js");
Cu.import("resource://services-sync/ext/Sync.js");
Cu.import("resource://services-sync/log4moz.js");
Cu.import("resource://services-sync/util.js");
@ -403,7 +402,7 @@ Resource.prototype = {
// is never called directly, but is used by the high-level
// {{{get}}}, {{{put}}}, {{{post}}} and {{delete}} methods.
_request: function Res__request(action, data) {
let [doRequest, cb] = Sync.withCb(this._doRequest, this);
let cb = Utils.makeSyncCallback();
function callback(error, ret) {
if (error)
cb.throw(error);
@ -412,7 +411,8 @@ Resource.prototype = {
// The channel listener might get a failure code
try {
return doRequest(action, data, callback);
this._doRequest(action, data, callback);
return Utils.waitForSyncCallback(cb);
} catch(ex) {
// Combine the channel stack with this request stack. Need to create
// a new error object for that.

Просмотреть файл

@ -61,7 +61,6 @@ Cu.import("resource://services-sync/record.js");
Cu.import("resource://services-sync/constants.js");
Cu.import("resource://services-sync/engines.js");
Cu.import("resource://services-sync/engines/clients.js");
Cu.import("resource://services-sync/ext/Sync.js");
Cu.import("resource://services-sync/ext/Preferences.js");
Cu.import("resource://services-sync/identity.js");
Cu.import("resource://services-sync/log4moz.js");

Просмотреть файл

@ -46,7 +46,6 @@ Cu.import("resource://services-sync/constants.js");
Cu.import("resource://services-sync/ext/Observers.js");
Cu.import("resource://services-sync/ext/Preferences.js");
Cu.import("resource://services-sync/ext/StringBundle.js");
Cu.import("resource://services-sync/ext/Sync.js");
Cu.import("resource://services-sync/log4moz.js");
let NetUtil;
@ -58,6 +57,12 @@ try {
// Firefox 3.5 :(
}
// Constants for makeSyncCallback, waitForSyncCallback
const CB_READY = {};
const CB_COMPLETE = {};
const CB_FAIL = {};
/*
* Utility functions
*/
@ -230,8 +235,8 @@ let Utils = {
names = names == null ? [] : [names];
// Synchronously asyncExecute fetching all results by name
let [exec, execCb] = Sync.withCb(query.executeAsync, query);
return exec({
let execCb = Utils.makeSyncCallback();
query.executeAsync({
items: [],
handleResult: function handleResult(results) {
let row;
@ -249,6 +254,7 @@ let Utils = {
execCb(this.items);
}
});
return Utils.waitForSyncCallback(execCb);
},
byteArrayToString: function byteArrayToString(bytes) {
@ -1503,6 +1509,77 @@ let Utils = {
this.__prefs.QueryInterface(Ci.nsIPrefBranch2);
}
return this.__prefs;
},
/**
* Helpers for making asynchronous calls within a synchronous API possible.
*
* If you value your sanity, do not look closely at the following functions.
*/
/**
* Check if the app is ready (not quitting)
*/
checkAppReady: function checkAppReady() {
// Watch for app-quit notification to stop any sync calls
Svc.Obs.add("quit-application", function() {
Utils.checkAppReady = function() {
throw Components.Exception("App. Quitting", Cr.NS_ERROR_ABORT);
};
});
// In the common case, checkAppReady just returns true
return (Utils.checkAppReady = function() true)();
},
/**
* Create a sync callback that remembers state like whether it's been called
*/
makeSyncCallback: function makeSyncCallback() {
// The main callback remembers the value it's passed and that it got data
let onComplete = function onComplete(data) {
onComplete.state = CB_COMPLETE;
onComplete.value = data;
};
// Initialize private callback data to prepare to be called
onComplete.state = CB_READY;
onComplete.value = null;
// Allow an alternate callback to trigger an exception to be thrown
onComplete.throw = function onComplete_throw(data) {
onComplete.state = CB_FAIL;
onComplete.value = data;
// Cause the caller to get an exception and stop execution
throw data;
};
return onComplete;
},
/**
* Wait for a sync callback to finish
*/
waitForSyncCallback: function waitForSyncCallback(callback) {
// Grab the current thread so we can make it give up priority
let thread = Cc["@mozilla.org/thread-manager;1"].getService().currentThread;
// Keep waiting until our callback is triggered unless the app is quitting
while (Utils.checkAppReady() && callback.state == CB_READY) {
thread.processNextEvent(true);
}
// Reset the state of the callback to prepare for another call
let state = callback.state;
callback.state = CB_READY;
// Throw the value the callback decided to fail with
if (state == CB_FAIL) {
throw callback.value;
}
// Return the value passed to the callback
return callback.value;
}
};

Просмотреть файл

@ -3,7 +3,6 @@ Cu.import("resource://services-sync/identity.js");
Cu.import("resource://services-sync/log4moz.js");
Cu.import("resource://services-sync/resource.js");
Cu.import("resource://services-sync/util.js");
Cu.import("resource://services-sync/ext/Sync.js");
let logger;

Просмотреть файл

@ -1,6 +1,5 @@
Cu.import("resource://services-sync/engines.js");
Cu.import("resource://services-sync/util.js");
Cu.import("resource://services-sync/ext/Sync.js");
function makeSteamEngine() {
return new SyncEngine('Steam');
@ -83,7 +82,7 @@ function test_toFetch() {
engine.toFetch = toFetch;
do_check_eq(engine.toFetch, toFetch);
// toFetch is written asynchronously
Sync.sleep(0);
engine._sleep(0);
let fakefile = syncTesting.fakeFilesystem.fakeContents[filename];
do_check_eq(fakefile, JSON.stringify(toFetch));