Permalink
Browse files

Added views experiment.

  • Loading branch information...
1 parent 8eec117 commit 393a9f123e94a8ed270b36ea1966e52316e1beda @kriskowal committed Jun 29, 2011
Showing with 121 additions and 1 deletion.
  1. +6 −0 CHANGES.md
  2. +15 −0 examples/view1.js
  3. +17 −0 examples/view2.js
  4. +25 −0 examples/view3.js
  5. +58 −1 q.js
View
@@ -5,6 +5,12 @@ Future Backward Incompatible
- ``def`` will be removed. Use ``master`` instead. The
term ``def`` was too confusing to new users.
+Next minor:
+
+ - Added experimental ``view`` and ``viewInfo`` for creating
+ views of promises either when or before they're
+ fulfilled.
+
0.6.0 - BACKWARD INCOMPATIBILITY
- WARNING: In practice, the implementation of ``spy`` and
View
@@ -0,0 +1,15 @@
+
+// constructing a view of a function
+// view information implicitly provided by fulfillment
+
+var Q = require("q");
+Q.ref(function () {
+ return "called";
+})
+.view()
+.then(function (view) {
+ return Q(view())
+ .when(console.log);
+})
+.end()
+
View
@@ -0,0 +1,17 @@
+
+// constructing a view from an object
+// view information implicitly provided by fulfillment
+
+var Q = require("q");
+Q.ref({
+ "property": function () {
+ return "called";
+ }
+})
+.view()
+.then(function (view) {
+ return Q(view.property())
+ .when(console.log);
+})
+.end()
+
View
@@ -0,0 +1,25 @@
+
+// constructing a view from a promise for a function
+// view information provided explicitly before fulfillment
+
+var Q = require("q");
+
+var callableD = Q.defer();
+setTimeout(function () {
+ callableD.resolve(function () {
+ return "called";
+ });
+}, 1000);
+
+callableD.promise
+.viewInfo({
+ "type": "function"
+})
+.view()
+.when(function (view) {
+ console.log("calling view");
+ Q(view())
+ .when(console.log)
+})
+.end()
+
View
59 q.js
@@ -204,13 +204,14 @@ Promise.prototype.then = function (fulfilled, rejected) {
// Chainable methods
reduce.call(
[
- "when",
+ "when", "send",
"get", "put", "del",
"post", "invoke",
"keys",
"apply", "call",
"all", "wait", "join",
"fail", "fin", "spy", // XXX spy deprecated
+ "view", "viewInfo",
"report", "end"
],
function (prev, name) {
@@ -355,6 +356,21 @@ function ref(object) {
return reject("" + object + " is not a function");
return object.apply(self, args);
},
+ "viewInfo": function () {
+ var on = object;
+ var properties = {};
+ while (on) {
+ Object.getOwnPropertyNames(on).forEach(function (name) {
+ if (!properties[name])
+ properties[name] = typeof on[name];
+ });
+ on = Object.getPrototypeOf(on);
+ }
+ return {
+ "type": typeof object,
+ "properties": properties
+ }
+ },
"keys": function () {
return keys(object);
}
@@ -385,6 +401,47 @@ function def(object) {
});
}
+exports.viewInfo = viewInfo;
+function viewInfo(object, info) {
+ object = ref(object);
+ if (info) {
+ return Promise({
+ "viewInfo": function () {
+ return info;
+ }
+ }, function fallback(op) {
+ var args = Array.prototype.slice.call(arguments);
+ return send.apply(undefined, [object].concat(args));
+ }, function valueOf() {
+ return object.valueOf();
+ });
+ } else {
+ return send(object, "viewInfo")
+ }
+}
+
+exports.view = function (object) {
+ return viewInfo(object).when(function (info) {
+ var view;
+ if (info.type === "function") {
+ view = function () {
+ return apply(object, undefined, arguments);
+ };
+ } else {
+ view = {};
+ }
+ var properties = info.properties || {};
+ Object.keys(properties).forEach(function (name) {
+ if (properties[name] === "function") {
+ view[name] = function () {
+ return post(object, name, arguments);
+ };
+ }
+ });
+ return ref(view);
+ });
+};
+
/**
* Registers an observer on a promise.
*

0 comments on commit 393a9f1

Please sign in to comment.