Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added def, variadic send.

  • Loading branch information...
commit a87ad6f2decf578c5affa7fd2d8dcf970b8924f5 1 parent 8d6c90e
Kris Kowal kriskowal authored

Showing 4 changed files with 49 additions and 7 deletions. Show diff stats Hide diff stats

  1. +8 3 CHANGES
  2. +10 0 README
  3. +30 3 lib/q.js
  4. +1 1  package.json
11 CHANGES
... ... @@ -1,8 +1,13 @@
1 1
2   -Next
  2 +0.2.0 - BACKWARD INCOMPATIBLE
3 3 - Changed post(ref, name, args) to variadic
4   - post(ref, name, ...args)
5   - - Added a send(value, op, args) method to the public API, for
  4 + post(ref, name, ...args). BACKWARD INCOMPATIBLE
  5 + - Added a def(value) method to annotate an object as being
  6 + necessarily a local value that cannot be serialized, such
  7 + that inter-process/worker/vat promise communication
  8 + libraries will send messages to it, but never send it
  9 + back.
  10 + - Added a send(value, op, ...args) method to the public API, for
6 11 forwarding messages to a value or promise in a future turn.
7 12
8 13 0.1.9
10 README
@@ -164,6 +164,16 @@ ref(value)
164 164 already been resolved with the given value.
165 165
166 166
  167 +def(value)
  168 +
  169 + Annotates a value, wrapping it in a promise, such that
  170 + that it is a local promise object which cannot be
  171 + serialized and sent to resolve a remote promise. A
  172 + def'ed value will respond to the `isDef` message without
  173 + a rejection so remote promise communication libraries
  174 + can distinguish it from non-def values.
  175 +
  176 +
167 177 reject(reason)
168 178
169 179 Returns a promise that has already been rejected
33 lib/q.js
@@ -288,6 +288,32 @@ function ref(object) {
288 288 }
289 289
290 290 /**
  291 + * Annotates an object such that it will never be
  292 + * transferred away from this process over any promise
  293 + * communication channel.
  294 + * @param object
  295 + * @returns promise a wrapping of that object that
  296 + * additionally responds to the 'isDef' message
  297 + * without a rejection.
  298 + */
  299 +exports.def = function (object) {
  300 + return Promise({
  301 + "isDef": function () {}
  302 + }, function fallback(op, resolve) {
  303 + var args = Array.prototype.slice.call(arguments, 2);
  304 + var result = send.apply(undefined, [object, op].concat(args));
  305 + // TODO? return resolve ? resolve(result) : result;
  306 + return result;
  307 + }, function valueOf() {
  308 + return {
  309 + 'toString': function () {
  310 + return '[object Promise def]'
  311 + }
  312 + };
  313 + });
  314 +}
  315 +
  316 +/**
291 317 * Registers an observer on a promise.
292 318 *
293 319 * Guarantees:
@@ -359,7 +385,7 @@ exports.Method = Method;
359 385 function Method (op) {
360 386 return function (object) {
361 387 var args = Array.prototype.slice.call(arguments, 1);
362   - return send(object, op, args);
  388 + return send.apply(undefined, [object, op].concat(args));
363 389 };
364 390 }
365 391
@@ -367,12 +393,13 @@ function Method (op) {
367 393 * sends a message to a value in a future turn
368 394 * @param object* the recipient
369 395 * @param op the name of the message operation, e.g., "when",
370   - * @param args {Array} an array of further arguments
  396 + * @param ...args further arguments to be forwarded to the operation
371 397 * @returns result {Promise} a promise for the result of the operation
372 398 */
373 399 exports.send = send;
374   -function send(object, op, args) {
  400 +function send(object, op) {
375 401 var deferred = defer();
  402 + var args = Array.prototype.slice.call(arguments, 2);
376 403 forward.apply(undefined, [
377 404 ref(object),
378 405 op,
2  package.json
... ... @@ -1,7 +1,7 @@
1 1 {
2 2 "name": "q",
3 3 "description": "defer/when-style promises (CommonJS/Promises/B)",
4   - "version": "0.1.9",
  4 + "version": "0.2.0",
5 5 "homepage": "http://github.com/kriskowal/q/",
6 6 "author": "Kris Kowal <kris@cixar.com> (http://github.com/kriskowal/)",
7 7 "contributors": [

0 comments on commit a87ad6f

Please sign in to comment.
Something went wrong with that request. Please try again.