This commit is contained in:
jonathandicarlo@jonathan-dicarlos-macbook-pro.local 2008-06-09 11:49:04 -07:00
commit 8573f956e3
4 changed files with 80 additions and 16 deletions

View File

@ -48,6 +48,15 @@ Cu.import("resource://weave/util.js");
* Asynchronous generator helpers * Asynchronous generator helpers
*/ */
// Returns a timer that is scheduled to call the given callback as
// soon as possible.
function makeTimer(cb) {
let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
timer.initWithCallback(new Utils.EventListener(cb),
0, timer.TYPE_ONE_SHOT);
return timer;
}
function AsyncException(initFrame, message) { function AsyncException(initFrame, message) {
this.message = message; this.message = message;
this._trace = initFrame; this._trace = initFrame;
@ -199,9 +208,7 @@ Generator.prototype = {
return; return;
let self = this; let self = this;
let cb = function() { self._done(retval); }; let cb = function() { self._done(retval); };
this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); this._timer = makeTimer(cb);
this._timer.initWithCallback(new Utils.EventListener(cb),
0, this._timer.TYPE_ONE_SHOT);
}, },
_done: function AsyncGen__done(retval) { _done: function AsyncGen__done(retval) {

View File

@ -26,3 +26,16 @@ let provider = {
ds.QueryInterface(Ci.nsIDirectoryService).registerProvider(provider); ds.QueryInterface(Ci.nsIDirectoryService).registerProvider(provider);
do_bind_resource(do_get_file("modules"), "weave"); do_bind_resource(do_get_file("modules"), "weave");
function loadInSandbox(aUri) {
var sandbox = Components.utils.Sandbox(this);
var request = Components.
classes["@mozilla.org/xmlextras/xmlhttprequest;1"].
createInstance();
request.open("GET", aUri, false);
request.send(null);
Components.utils.evalInSandbox(request.responseText, sandbox);
return sandbox;
}

View File

@ -0,0 +1,57 @@
function run_test() {
var async = loadInSandbox("resource://weave/async.js");
var callbackQueue = [];
Function.prototype.async = async.Async.sugar;
async.makeTimer = function fake_makeTimer(cb) {
// Just add the callback to our queue and we'll call it later, so
// as to simulate a real nsITimer.
callbackQueue.push(cb);
return "fake nsITimer";
};
var onCompleteCalled = false;
function onComplete() {
onCompleteCalled = true;
}
let timesYielded = 0;
function testAsyncFunc(x) {
let self = yield;
timesYielded++;
// Ensure that argument was passed in properly.
do_check_eq(x, 5);
// Ensure that 'this' is set properly.
do_check_eq(this.sampleProperty, true);
// Simulate the calling of an asynchronous function that will call
// our callback.
callbackQueue.push(self.cb);
yield;
timesYielded++;
self.done();
}
var thisArg = {sampleProperty: true};
testAsyncFunc.async(thisArg, onComplete, 5);
do_check_eq(timesYielded, 1);
let func = callbackQueue.pop();
do_check_eq(typeof func, "function");
func();
do_check_eq(timesYielded, 2);
func = callbackQueue.pop();
do_check_eq(typeof func, "function");
func();
do_check_eq(callbackQueue.length, 0);
}

View File

@ -1,16 +1,3 @@
function loadInSandbox(aUri) {
var sandbox = Components.utils.Sandbox(this);
var request = Components.
classes["@mozilla.org/xmlextras/xmlhttprequest;1"].
createInstance();
request.open("GET", aUri, false);
request.send(null);
Components.utils.evalInSandbox(request.responseText, sandbox);
return sandbox;
}
function run_test() { function run_test() {
// The JS module we're testing, with all members exposed. // The JS module we're testing, with all members exposed.
var passwords = loadInSandbox("resource://weave/engines/passwords.js"); var passwords = loadInSandbox("resource://weave/engines/passwords.js");