Skip to content

Commit

Permalink
Merge pull request #20
Browse files Browse the repository at this point in the history
Bridges progress events
  • Loading branch information
kriskowal committed Jul 3, 2013
2 parents 5e53f7b + ea5a1e3 commit 1746583
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
41 changes: 34 additions & 7 deletions q-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ function Connection(connection, local, options) {
var receivers = {
"resolve": function (message) {
if (locals.has(message.to)) {
resolveLocal(message.to, decode(message.resolution));
resolveLocal(message.to, 'resolve', decode(message.resolution));
}
},
"notify": function (message) {
if (locals.has(message.to)) {
resolveLocal(message.to, 'notify', decode(message.resolution));
}
},
// a "send" message forwards messages from a remote
Expand All @@ -65,6 +70,7 @@ function Connection(connection, local, options) {
// which will return a response promise
var local = locals.get(message.to).promise;
var response = Q.dispatch(local, message.op, decode(message.args));
var envelope;

// connect the local response promise with the
// remote response promise:
Expand All @@ -81,7 +87,7 @@ function Connection(connection, local, options) {
resolution = {"!": null};
}
}
var envelope = JSON.stringify({
envelope = JSON.stringify({
"type": "resolve",
"to": message.from,
"resolution": resolution
Expand All @@ -103,6 +109,27 @@ function Connection(connection, local, options) {
"resolution": {"!": reason}
})
connection.put(envelope);
}, function (progress) {
try {
progress = encode(progress);
envelope = JSON.stringify({
"type": "notify",
"to": message.from,
"resolution": progress
});
} catch (exception) {
try {
progress = {"!": encode(exception)};
} catch (exception) {
progress = {"!": null};
}
envelope = JSON.stringify({
"type": "resolve",
"to": message.from,
"resolution": progress
});
}
connection.put(envelope);
})
.done();

Expand All @@ -123,9 +150,9 @@ function Connection(connection, local, options) {

// a utility for resolving the local promise
// for a given identifier.
function resolveLocal(id, value) {
_debug('resolve:', "L" + JSON.stringify(id), JSON.stringify(value), typeof value);
locals.get(id).resolve(value);
function resolveLocal(id, op, value) {
_debug(op + ':', "L" + JSON.stringify(id), JSON.stringify(value), typeof value);
locals.get(id)[op](value);
}

// makes a promise that will send all of its events to a
Expand Down Expand Up @@ -161,7 +188,7 @@ function Connection(connection, local, options) {
} if (Q.isPromise(object) || typeof object === "function") {
var id = makeId();
makeLocal(id);
resolveLocal(id, object);
resolveLocal(id, 'resolve', object);
return {"@": id, "type": typeof object};
} else if (object instanceof Error) {
return {
Expand Down Expand Up @@ -231,7 +258,7 @@ function Connection(connection, local, options) {
// the root object is an empty-string by convention.
// All other identifiers are numbers.
makeLocal(rootId);
resolveLocal(rootId, local);
resolveLocal(rootId, 'resolve', local);
return makeRemote(rootId);

}
Expand Down
25 changes: 25 additions & 0 deletions spec/q-connection-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,31 @@ describe("remote promises that fulfill to functions", function () {
});
});

describe("remote promises that notify progress", function () {
it("should trigger local progress handler", function () {
var peers = makePeers({
resolveAfterNotify: function (times) {
var deferred = Q.defer();
var count = 0;
setTimeout(function () {
while (count++ < times) {
deferred.notify('Notify' + count + ' time');
}
deferred.resolve('Resolving');
}, 0);
return deferred.promise;
}
});

var notifyCount = 0;
return peers.remote.invoke('resolveAfterNotify', 3).progress(function(p) {
notifyCount++;
}).then(function (message) {
expect(notifyCount).toBe(3);
});
});
});

describe("rejection", function () {
it("should become local functions", function () {
var peers = makePeers({
Expand Down

0 comments on commit 1746583

Please sign in to comment.