Commits

Daniel Roberts committed 921bfd1 Draft

Added data function, tests for map, filter and sort, and rudimentary chaining functionality currently without tests.

Comments (0)

Files changed (2)

                     this.rejectObservers.push(reject);
                 }
             } else if (this.deferred.state === "resolved" && typeof resolve !== "undefined") {
-                resolve(this);
-            } else if (this.deferred.state === "rejected" && typeof reject !== "undefined") {
-                reject(this.deferred.error);
+                var result = resolve(this);
+                if (typeof result !== "undefined") {
+                    if (typeof result.then !== "undefined") {
+                        return result;
+                    } else {
+                        return {then: function(f, e) {f(result);}};
+                    }
+                } else {
+                    return {then: function(f, e) {f();}};
+                }
+            } else if (this.deferred.state === "rejected") {
+                if (typeof reject !== "undefined") {
+                    reject(this.deferred.error);
+                } else {
+                    return {then: function(f, e) {e(this.deferred.error);}};
+                }
             }
         }
+        PromisedCollection.prototype.data = function data(f) {
+            f(this.deferred.data); // FIXME: probably should provide copy
+        }
         function DeferredCollection(data) {
             if (typeof data !== "undefined") {
                 this.data = data;
         
         PromisedCollection.prototype.map = function map(f) {
             var deferred = new DeferredCollection();
-            var promise = deferredCollection.promise;
+            var promise = deferred.promise;
 
             this.forEach(function(element, index) {
                 deferred.emit(f(element, index));
 
         PromisedCollection.prototype.filter = function filter(f) {
             var deferred = new DeferredCollection();
-            var promise = deferredCollection.promise;
+            var promise = deferred.promise;
 
             this.forEach(function(element, index) {
                 if (f(element, index)) {
 
         PromisedCollection.prototype.sort = function sort(f) {
             var deferred = new DeferredCollection();
-            var promise = deferredCollection.promise;
+            var promise = deferred.promise;
 
             var collection = this;
             this.then(function (result) {

tests/test_dcollection.js

     });
     deferred.resolve();
 });
+
+asyncTest("map()", function () {
+    var deferred, promise;
+    
+    deferred = new DeferredCollection();
+    promise = deferred.promise;
+    
+    var mapped = promise.map(function (element, index) {
+        return {element: element, index: index};
+    });
+
+    mapped.forEach(function (element, index) {
+        ok(element.element == index && element.index == index, "Mapped forEach works.");
+    });
+
+    deferred.emit(0);
+    deferred.emit(1);
+    deferred.emit(2);
+
+    var previous = true;
+
+    mapped.forEach(function (element, index) {
+        if (previous) {
+            ok(index < 3, "Mapped forEach called for previous elements.");
+        } else {
+            ok(element.element == index && element.index == index, "Mapped forEach works for future elements.");
+        }
+    });
+
+    mapped.then(function (resolveData) {
+        ok(mapped == resolveData, "Mapped collection resolved when source is resolved."); 
+        start();
+    });
+    deferred.resolve();
+});
+
+asyncTest("filter()", function () {
+    var deferred, promise;
+    
+    deferred = new DeferredCollection();
+    promise = deferred.promise;
+    
+    var evens = promise.filter(function (element, index) {
+        return element % 2 == 0;
+    });
+
+    evens.forEach(function (element, index) {
+        ok(element % 2 == 0, "Elements filtered correctly.");
+    });
+
+    deferred.emit(0);
+    deferred.emit(1);
+    deferred.emit(2);
+    deferred.emit(3);
+    deferred.emit(4);
+    deferred.emit(5);
+
+    evens.then(function (resolveData) {
+        ok(evens == resolveData, "Filtered collection resolved when source is resolved."); 
+        start();
+    });
+    deferred.resolve();
+});
+
+asyncTest("sort()", function () {
+    var deferred, promise;
+    
+    deferred = new DeferredCollection();
+    promise = deferred.promise;
+    
+    var sorted = promise.sort();
+
+    sorted.forEach(function (element, index) {
+        ok(element == index, "Elements filtered correctly.");
+    });
+
+    deferred.emit(2);
+    deferred.emit(0);
+    deferred.emit(3);
+    deferred.emit(1);
+    deferred.emit(5);
+    deferred.emit(4);
+
+    sorted.then(function (resolveData) {
+        ok(sorted == resolveData, "Sorted collection resolved when source is resolved."); 
+        start();
+    });
+    deferred.resolve();
+});