Permalink
Browse files

Merge branch 'shiftspace'

  • Loading branch information...
2 parents 2ce3046 + 5a36ee9 commit 09485c62bd266ace792d47e2418b757c9ad8b7d1 @swannodette swannodette committed Dec 25, 2009
Showing with 70 additions and 49 deletions.
  1. +32 −14 Promises.js
  2. +1 −0 examples/data/nested.json
  3. +20 −26 examples/exampleA.html
  4. +16 −8 examples/exampleA.js
  5. +1 −1 externals/functools
View
@@ -80,9 +80,10 @@ var Promise = new Class({
}.bind(this));
} else if(Promise.isPromise(value)) {
// if handed a promise, watch it
+ this.__promise = value;
value.addEvent('realized', function() {
this.setValue(value.value());
- }.bind(this))
+ }.bind(this));
} else if(typeof value != 'undefined') {
// if handed a regular value, set the value immediately but don't
// trigger a realized event.
@@ -168,6 +169,15 @@ var Promise = new Class({
this.__req.send();
} else if(this.__plain) {
this.setValue(this.value());
+ } else if(this.__promise) {
+ if(this.__promise.isRealized())
+ {
+ this.setValue(this.__promise.value());
+ }
+ else
+ {
+ this.__promise.realize();
+ }
}
return this;
},
@@ -225,12 +235,12 @@ var Promise = new Class({
setValue: function(value, notify) {
if(value && value.xhr) {
this.initReq(value);
+ } else if(!this.__realized && notify !== false) {
+ this.__realized = true;
+ this.__value = this.applyOps(value);
+ this.fireEvent('realized', this.__value);
} else {
this.__value = value;
- if(!this.__realized && notify !== false) {
- this.__realized = true;
- this.fireEvent('realized', this.applyOps(this.__value));
- }
}
},
@@ -305,7 +315,15 @@ var Promise = new Class({
*/
get: function() {
var args = $A(arguments);
- if(!this.isRealized()) return (new Promise(this, {lazy:this.options.lazy})).op(function(v) { return Function.get.apply(null, [v].extend(args)); });
+ if(!this.isRealized())
+ {
+ return (new Promise(this, {lazy:this.options.lazy})).op(
+ function(v) {
+ var result = $get.apply(null, [v].extend(args));
+ return result;
+ }
+ );
+ }
return Function.get.apply(null, [this.value()].extend(args));
},
@@ -361,7 +379,7 @@ Promise.debug = false;
*/
Promise.isPromise = function(obj) {
return (obj && obj.name == "Promise");
-}
+};
/*
Function: Promise.getValue
@@ -371,7 +389,7 @@ Promise.isPromise = function(obj) {
Promise.getValue = function(v) {
while (Promise.isPromise(v) && v.isRealized()) v = v.value();
return v;
-}
+};
/*
Function: Promise.toValues
@@ -387,7 +405,7 @@ Promise.getValue = function(v) {
Promise.toValues = function(ary) {
while(ary.some(Promise.isPromise)) ary = ary.map(Promise.getValue);
return ary;
-}
+};
/*
Function: Promise.promiseOrValue
@@ -406,7 +424,7 @@ Promise.promiseOrValue = function(v) {
if(v && v.xhr) return new Promise(v);
if(Promise.isPromise(v) && v.isRealized()) return v.value();
return v;
-}
+};
/*
Function: Promise.watch
@@ -446,9 +464,9 @@ Promise.watch = function(args, cb, errCb) {
// don't attempt to realize lazy values. They are realized then setValue is called on them.
unrealized.filter(Function.msg('isNotLazy')).each(Function.msg('realize'));
} else {
- cb(Promise.toValues(args))
+ cb(Promise.toValues(args));
}
-}
+};
/*
Function: Promise.allRealized
@@ -461,7 +479,7 @@ Promise.watch = function(args, cb, errCb) {
Returns:
A boolean.
*/
-Promise.allRealized = function(vs) { return vs.filter(Promise.isPromise).every(Function.msg("isRealized")); }
+Promise.allRealized = function(vs) { return vs.filter(Promise.isPromise).every(Function.msg("isRealized")); };
/*
Decorator: promise
@@ -515,7 +533,7 @@ function promise(fn) {
var porv = Promise.promiseOrValue(fn.apply(this, args.map(Promise.getValue)));
return porv;
}
- }
+ };
}
/*
@@ -0,0 +1 @@
+{data: {foo: {bar: "baz"}}}
View
@@ -1,31 +1,25 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-<head>
+ <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <script src="../externals/mootools-1.2.3-core.js" type="text/javascript" charset="utf-8"></script>
- <script src="../externals/mootools-1.2.3.1-more.js" type="text/javascript" charset="utf-8"></script>
- <script src="../externals/functools/FuncTools.js" type="text/javascript" charset="utf-8"></script>
- <script src="../Promises.js" type="text/javascript" charset="utf-8"></script>
- <script src="exampleA.js" type="text/javascript" charset="utf-8"></script>
+ <script src="../externals/mootools-1.2.3-core.js" type="text/javascript" charset="utf-8"></script>
+ <script src="../externals/mootools-1.2.3.1-more.js" type="text/javascript" charset="utf-8"></script>
+ <script src="../externals/functools/FuncTools.js" type="text/javascript" charset="utf-8"></script>
+ <script src="../Promises.js" type="text/javascript" charset="utf-8"></script>
+ <script src="exampleA.js" type="text/javascript" charset="utf-8"></script>
<title>Example A</title>
-
-</head>
-
-<body>
-
-<label>Example 1:</label>
-<input id="ex1"></input>
-
-<label>Example 2:</label>
-<input id="ex2"></input>
-
-<label>Example 3:</label>
-<input id="ex3"></input>
-
-<label>Example 4:</label>
-<input id="ex4"></input>
-
-</body>
+ </head>
+ <body>
+ <label>Example 1:</label>
+ <input id="ex1"></input>
+ <label>Example 2:</label>
+ <input id="ex2"></input>
+ <label>Example 3:</label>
+ <input id="ex3"></input>
+ <label>Example 4:</label>
+ <input id="ex4"></input>
+ <label>Example 11:</label>
+ <input id="ex11"></input>
+ </body>
</html>
View
@@ -10,15 +10,15 @@ var get = function(rsrc)
return new Request({
url: 'data/'+rsrc+'.json'
});
-}.decorate(promise)
+}.decorate(promise);
/*
An adding function that can recieve promises.
*/
var add = function(a, b)
{
return a + b;
-}.decorate(promise)
+}.decorate(promise);
/*
A logging function that can recieve promises.
@@ -27,13 +27,13 @@ var show = function show(value, target)
{
if(target)
{
- $(target).setProperty('value', value)
+ $(target).setProperty('value', value);
}
else
{
- console.log("show: " + value);
+ console.log("show:", value);
}
-}.decorate(promise)
+}.decorate(promise);
function example1()
@@ -63,7 +63,7 @@ function example3()
while(resource = resources.shift())
{
var aPromise = get(resource);
- if(resources.length > 0) aPromise.op(function(value) { return value += ", " });
+ if(resources.length > 0) aPromise.op(function(value) { return value += ", "; });
result = add(result, aPromise);
}
show(result, 'ex3'); // -> "a, b, c, d, e, f"
@@ -114,12 +114,12 @@ var oddEven = [odd, even];
var add2 = function(a, b)
{
return a + b;
-}.decorate(promise, Function.pre(evenOdd, true))
+}.decorate(promise, Function.pre(evenOdd, true));
var add3 = function(a, b)
{
return a + b;
-}.decorate(promise, Function.pre(evenOdd, console.log))
+}.decorate(promise, Function.pre(evenOdd, console.log));
function example7()
{
@@ -158,6 +158,13 @@ function example10()
p2.realize();
}
+function example11()
+{
+ var nested = get("nested"),
+ bar = nested.get("foo", "bar");
+ show(bar, 'ex11');
+}
+
// Run Examples
// ============
function runExamples()
@@ -178,6 +185,7 @@ function runExamples()
example8();
example9();
example10();
+ example11();
console.log(">>>>>>>>>>>>>>>>>>>> EXIT runExamples");
}
Submodule functools updated 2 files
+88 −24 FuncTools.js
+2 −1 readme.textile

0 comments on commit 09485c6

Please sign in to comment.