Browse files

Added def, variadic send.

  • Loading branch information...
1 parent 8d6c90e commit a87ad6f2decf578c5affa7fd2d8dcf970b8924f5 @kriskowal kriskowal committed Dec 22, 2010
Showing with 49 additions and 7 deletions.
  1. +8 −3 CHANGES
  2. +10 −0 README
  3. +30 −3 lib/q.js
  4. +1 −1 package.json
View
11 CHANGES
@@ -1,8 +1,13 @@
-Next
+0.2.0 - BACKWARD INCOMPATIBLE
- Changed post(ref, name, args) to variadic
- post(ref, name, ...args)
- - Added a send(value, op, args) method to the public API, for
+ post(ref, name, ...args). BACKWARD INCOMPATIBLE
+ - Added a def(value) method to annotate an object as being
+ necessarily a local value that cannot be serialized, such
+ that inter-process/worker/vat promise communication
+ libraries will send messages to it, but never send it
+ back.
+ - Added a send(value, op, ...args) method to the public API, for
forwarding messages to a value or promise in a future turn.
0.1.9
View
10 README
@@ -164,6 +164,16 @@ ref(value)
already been resolved with the given value.
+def(value)
+
+ Annotates a value, wrapping it in a promise, such that
+ that it is a local promise object which cannot be
+ serialized and sent to resolve a remote promise. A
+ def'ed value will respond to the `isDef` message without
+ a rejection so remote promise communication libraries
+ can distinguish it from non-def values.
+
+
reject(reason)
Returns a promise that has already been rejected
View
33 lib/q.js
@@ -288,6 +288,32 @@ function ref(object) {
}
/**
+ * Annotates an object such that it will never be
+ * transferred away from this process over any promise
+ * communication channel.
+ * @param object
+ * @returns promise a wrapping of that object that
+ * additionally responds to the 'isDef' message
+ * without a rejection.
+ */
+exports.def = function (object) {
+ return Promise({
+ "isDef": function () {}
+ }, function fallback(op, resolve) {
+ var args = Array.prototype.slice.call(arguments, 2);
+ var result = send.apply(undefined, [object, op].concat(args));
+ // TODO? return resolve ? resolve(result) : result;
+ return result;
+ }, function valueOf() {
+ return {
+ 'toString': function () {
+ return '[object Promise def]'
+ }
+ };
+ });
+}
+
+/**
* Registers an observer on a promise.
*
* Guarantees:
@@ -359,20 +385,21 @@ exports.Method = Method;
function Method (op) {
return function (object) {
var args = Array.prototype.slice.call(arguments, 1);
- return send(object, op, args);
+ return send.apply(undefined, [object, op].concat(args));
};
}
/**
* sends a message to a value in a future turn
* @param object* the recipient
* @param op the name of the message operation, e.g., "when",
- * @param args {Array} an array of further arguments
+ * @param ...args further arguments to be forwarded to the operation
* @returns result {Promise} a promise for the result of the operation
*/
exports.send = send;
-function send(object, op, args) {
+function send(object, op) {
var deferred = defer();
+ var args = Array.prototype.slice.call(arguments, 2);
forward.apply(undefined, [
ref(object),
op,
View
2 package.json
@@ -1,7 +1,7 @@
{
"name": "q",
"description": "defer/when-style promises (CommonJS/Promises/B)",
- "version": "0.1.9",
+ "version": "0.2.0",
"homepage": "http://github.com/kriskowal/q/",
"author": "Kris Kowal <kris@cixar.com> (http://github.com/kriskowal/)",
"contributors": [

0 comments on commit a87ad6f

Please sign in to comment.