Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syncCallbacks property for WorkerPromise #238

Merged
merged 1 commit into from
Jul 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions modules/ringo/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function Worker(moduleId) {
var invokeCallback = function(callback, arg) {
if (syncCallbacks) callback(arg);
else currentWorker.submit(self, callback, arg);
}
};
var source = {
postMessage: function(data) {
invokeCallback(onmessage, {data: data, source: self});
Expand Down Expand Up @@ -136,17 +136,20 @@ function Worker(moduleId) {
*
* @param {String} moduleId the id of the module to load in the worker.
* @param {Object} message the message to post to the worker.
* @param {Boolean} [syncCallbacks] flag that indicates whether
* callbacks from the worker should be called synchronously in the worker's
* own thread rather than in our own local event loop thread.
* @constructor
* @see ringo/promise#Promise
*/
function WorkerPromise(moduleId, message) {
function WorkerPromise(moduleId, message, syncCallbacks) {
var deferred = new Deferred();
var worker = new Worker(moduleId);
var resolved = false;

worker.onmessage = function(e) {
resolve(e.data, false);
}
};

worker.onerror = function(e) {
resolve(e.data, true);
Expand All @@ -160,7 +163,7 @@ function WorkerPromise(moduleId, message) {
}
}

worker.postMessage(message);
worker.postMessage(message, syncCallbacks);
return deferred.promise;

/**
Expand Down
17 changes: 17 additions & 0 deletions test/ringo/promise_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var assert = require("assert");
var {Deferred, PromiseList} = require("ringo/promise");
var {WorkerPromise} = require("ringo/worker");
var system = require("system");

exports.testPromise = function() {
Expand Down Expand Up @@ -42,6 +43,22 @@ exports.testPromiseList = function() {
assert.deepEqual(result, [{value: "ok"}, {value: 1}, {error: "error"}]);
};

exports.testPromiseListWait = function() {
var w1 = new WorkerPromise( module.resolve('./promise_worker'), { delay: 100 }, true );
var w2 = new WorkerPromise( module.resolve('./promise_worker'), { delay: 100 }, true );
var l = PromiseList(w1, w2);

var then = new Date().getTime();
var result = l.wait(1000);
var elapsed = new Date().getTime() - then;

// The workers should finish in 100ms, therefore the PromiseList should end at
// about the same time. If a second has passed, the PromiseList is not properly
// being notified of the Workers who are ending.
assert.isTrue(elapsed < 500);
assert.deepEqual(result, [{value: {success: true}}, {value: {success: true}}]);
};

exports.testPromiseListAsArray = function() {
var d1 = Deferred(), d2 = Deferred(), d3 = Deferred(), done = Deferred();
var l = PromiseList([d1.promise, d2.promise, d3]); // PromiseList should convert d3 to promise
Expand Down
17 changes: 17 additions & 0 deletions test/ringo/promise_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var log = require( 'ringo/logging' ).getLogger( module.id );
/**
* The worker module needed by promise_test
*/
function onmessage(e) {

var source = e.source;
var result = e.data.result || { success: true };
var delay = e.data.delay || 2000;

var success = function() {
log.info( 'Posting the result: ', JSON.stringify( result ) );
source.postMessage( result );
};

setTimeout( success, delay );
}