Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: youngjay/rxc
base: 03cdb73157
...
head fork: youngjay/rxc
compare: e4746ded51
  • 4 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jun 01, 2013
@youngjay d 63127e3
Commits on Jun 02, 2013
@youngjay cmd add ) 19a426c
@youngjay d d30b55e
Commits on Jun 06, 2013
@youngjay d e4746de
View
18 lib/index.js
@@ -25,17 +25,7 @@ var enhanceEvent = function(Klass) {
var EnhancedEvent = enhanceEvent(Event);
var EnhancedPreservedEvent = enhanceEvent(PreservedEvent);
-var EventEvent = EnhancedPreservedEvent.extend({
- resolveEvent: function() {
- return this.then(function(evt, callback) {
- return evt.subscribe(callback);
- });
- },
- replaceEvent: function(evt) {
- return this.notify(evt);
- }
-});
module.exports = {
createEvent: function() {
@@ -46,13 +36,7 @@ module.exports = {
return new EnhancedPreservedEvent();
},
- createEventEvent: function() {
- return new EventEvent();
- },
-
- then: function(fn) {
- return new Promise(fn);
- },
+ then: promise.then,
when: promise.when,
View
45 lib/promise.js
@@ -5,6 +5,10 @@ var Subscribable = require('./subscribable');
var noop = function() {};
+var returnValueNeedToBeEvaluate = function(o) {
+ return o !== undefined && !_.isFunction(o);
+};
+
var PromiseInterFace = Subscribable.extend({
subscribe: function(subscriber) {
throw new Error('to be implemented')
@@ -25,7 +29,13 @@ var PromiseInterFace = Subscribable.extend({
var disposeCurrentSubscriber = self.subscribe(function() {
disposePreviousSubscriber();
- subscriberDisposeHandler = transformer.call(null, slice.call(arguments), subscriber);
+ subscriberDisposeHandler = null;
+ var ret = transformer(slice.call(arguments), subscriber);
+ if (returnValueNeedToBeEvaluate(ret)) {
+ subscriber(ret);
+ } else {
+ subscriberDisposeHandler = ret;
+ }
});
return function() {
@@ -66,15 +76,34 @@ var Promise = PromiseInterFace.extend(
this.subscribe = function(subscriber) {
var isDisposed = false;
- var dispose = handle(function() {
- if (!isDisposed) {
- subscriber.apply(null, arguments);
+ var prevArgsDisposeHandler;
+
+ var disposePrevArgs = function() {
+ if (prevArgsDisposeHandler) {
+ prevArgsDisposeHandler();
}
- });
+ };
+
+ var evaluateArgs = function() {
+ if (!isDisposed) {
+ disposePrevArgs();
+ prevArgsDisposeHandler = whenAllSubscribablesAreNotified(arguments).subscribe(function(args) {
+ subscriber.apply(null, args);
+ });
+ }
+ };
+
+ var dispose = handle(evaluateArgs);
+ // if return something that was not a dispose
+
+ if (returnValueNeedToBeEvaluate(dispose)) {
+ evaluateArgs(dispose);
+ }
return function() {
if (!isDisposed) {
- isDisposed = true;
+ isDisposed = true;
+ disposePrevArgs();
if (_.isFunction(dispose)) {
dispose();
}
@@ -191,6 +220,10 @@ var checkEachSubscribables = function(subscribables, check) {
var never = new Promise(noop);
var aggregate = {
+ then: function(fn) {
+ return new Promise(fn);
+ },
+
when: function(singleSubscribable) {
// shortcut for single subscribable
if (arguments.length === 1 && !_.isArray(singleSubscribable)) {
View
55 node/fs.js
@@ -43,7 +43,7 @@ var readdirr = function(dir, options) {
return rfs.stat(p.join(curDir, path));
}), paths);
})
- .then(function(fileStats, paths) {
+ .then(function(fileStats, paths, callback) {
var dirs = [], files = [];
fileStats.forEach(function(stats, i) {
var path = p.join(curDir, paths[i]);
@@ -65,12 +65,12 @@ var readdirr = function(dir, options) {
dirs.push(files);
- return dirs;
+ callback(dirs);
})
- .then(function(filesInDirs) {
- return filesInDirs.reduce(function(all, filesInDir) {
+ .then(function(filesInDirs, callback) {
+ callback(filesInDirs.reduce(function(all, filesInDir) {
return all.concat(filesInDir)
- }, []);
+ }, []));
});
};
@@ -80,20 +80,20 @@ var readdirr = function(dir, options) {
var rmr = function(path) {
var process = function(curPath) {
- return rfs.stat(curPath).then(function(stats) {
- return stats.isDirectory() ? rmdir(curPath) : rfs.unlink(curPath)
+ return rfs.stat(curPath).then(function(stats, callback) {
+ callback(stats.isDirectory() ? rmdir(curPath) : rfs.unlink(curPath))
});
};
var rmdir = function(curPath) {
return rfs.readdir(curPath)
- .then(function(subPaths) {
- return subPaths.map(function(subPath) {
+ .then(function(subPaths, callback) {
+ callback(subPaths.map(function(subPath) {
return process(p.join(curPath, subPath))
- })
+ }))
})
- .then(function() {
- return rfs.rmdir(curPath)
+ .then(function(results, callback) {
+ callback(rfs.rmdir(curPath));
})
};
@@ -108,14 +108,14 @@ var rmr = function(path) {
var cpr = function(src, dist) {
return rmr(dist)
- .then(function() {
- return rfs.stat(src)
+ .then(function(callback) {
+ callback(rfs.stat(src))
})
- .then(function(stat) {
- return rfs.mkdir(dist, stat.mode);
+ .then(function(stat, callback) {
+ callback(rfs.mkdir(dist, stat.mode));
})
- .then(function() {
- return rfs.readdir(src)
+ .then(function(callback) {
+ callback(rfs.readdir(src))
})
.then(function(paths, callback) {
callback(
@@ -125,8 +125,8 @@ var cpr = function(src, dist) {
paths
)
})
- .then(function(stats, paths) {
- return stats.map(function(stat, i) {
+ .then(function(stats, paths, callback) {
+ callback(stats.map(function(stat, i) {
var srcPath = p.join(src, paths[i]);
var distPath = p.join(dist, paths[i]);
@@ -135,15 +135,18 @@ var cpr = function(src, dist) {
}
if (stat.isSymbolicLink()) {
- return rfs.readlink(srcPath).then(function(link){
- return rfs.symlink(link, distPath);
+ return rfs.readlink(srcPath).then(function(link, callback){
+ callback(rfs.symlink(link, distPath));
});
}
- return rfs.readFile(srcPath).then(function(content) {
- return rfs.writeFile(distPath, content);
- })
- });
+ return rfs.readFile(srcPath).then(function(content, callback) {
+ callback(rfs.writeFile(distPath, content));
+ });
+ }));
+ })
+ .then(function(results, callback) {
+ callback();
})
};
View
154 test/fs.js
@@ -14,92 +14,92 @@ var TXT2_CONTENT = '1000'
-// describe('fs', function() {
- // describe('#readFile', function() {
- // var file1 = rfs.readFile(TXT1, 'utf8'), file2 = rfs.readFile(TXT2, 'utf8');
+describe('fs', function() {
+ describe('#readFile', function() {
+ var file1 = rfs.readFile(TXT1, 'utf8'), file2 = rfs.readFile(TXT2, 'utf8');
- // it('should readFile', function(done) {
- // file1.subscribe(function() {
- // assert.deepEqual(slice.call(arguments), [TXT1_CONTENT]);
- // done();
- // })
- // })
+ it('should readFile', function(done) {
+ file1.subscribe(function() {
+ assert.deepEqual(slice.call(arguments), [TXT1_CONTENT]);
+ done();
+ })
+ })
- // it('rx merge file', function(done) {
- // rx.when(file1, file2).subscribe(function() {
- // assert.deepEqual(slice.call(arguments), [TXT1_CONTENT, TXT2_CONTENT]);
- // done();
- // })
- // })
- // })
+ it('rx merge file', function(done) {
+ rx.when(file1, file2).subscribe(function() {
+ assert.deepEqual(slice.call(arguments), [TXT1_CONTENT, TXT2_CONTENT]);
+ done();
+ })
+ })
+ })
- // describe('#readdirr', function() {
+ describe('#readdirr', function() {
- // it('should read all files right', function(done) {
- // rfs.readdirr(RECURSIVE_DIR).subscribe(function(files) {
- // assert.equal(files.length, 3);
- // done();
- // })
- // })
+ it('should read all files right1', function(done) {
+ rfs.readdirr(RECURSIVE_DIR).subscribe(function(files) {
+ assert.equal(files.length, 3);
+ done();
+ })
+ })
- // it('should read all files right include hidden file', function(done) {
- // rfs.readdirr(RECURSIVE_DIR, {
- // hidden: true
- // }).subscribe(function(files) {
- // assert.equal(files.length, 5);
- // done();
- // })
- // })
+ it('should read all files right include hidden file', function(done) {
+ rfs.readdirr(RECURSIVE_DIR, {
+ hidden: true
+ }).subscribe(function(files) {
+ assert.equal(files.length, 5);
+ done();
+ })
+ })
- // it('should read all files right include directory', function(done) {
- // rfs.readdirr(RECURSIVE_DIR, {
- // directory: true
- // }).subscribe(function(files) {
- // assert.equal(files.length, 5);
- // done();
- // })
- // })
+ it('should read all files right include directory', function(done) {
+ rfs.readdirr(RECURSIVE_DIR, {
+ directory: true
+ }).subscribe(function(files) {
+ assert.equal(files.length, 5);
+ done();
+ })
+ })
- // it('should read all files right include hidden file and directory', function(done) {
- // rfs.readdirr(RECURSIVE_DIR, {
- // hidden: true,
- // directory: true
- // }).subscribe(function(files) {
- // assert.equal(files.length, 8);
- // done();
- // })
- // })
- // })
+ it('should read all files right include hidden file and directory', function(done) {
+ rfs.readdirr(RECURSIVE_DIR, {
+ hidden: true,
+ directory: true
+ }).subscribe(function(files) {
+ assert.equal(files.length, 8);
+ done();
+ })
+ })
+ })
- // describe('#cpr', function() {
- // it('should cp recursive', function(done) {
- // rfs.cpr(RECURSIVE_DIR, RECURSIVE_DIR_TEMP).then(function() {
- // return rfs.readdirr(RECURSIVE_DIR_TEMP, {
- // hidden: true
- // })
- // }).subscribe(function(files) {
- // assert.equal(files.length, 5);
- // done();
- // })
- // })
- // })
+ describe('#cpr', function() {
+ it('should cp recursive', function(done) {
+ rfs.cpr(RECURSIVE_DIR, RECURSIVE_DIR_TEMP).then(function(callback) {
+ callback(rfs.readdirr(RECURSIVE_DIR_TEMP, {
+ hidden: true
+ }))
+ }).subscribe(function(files) {
+ assert.equal(files.length, 5);
+ done();
+ })
+ })
+ })
- // describe('#rmr', function() {
+ describe('#rmr', function() {
- // it('should rm recursive', function(done) {
- // rfs.rmr(RECURSIVE_DIR_TEMP).then(function(callback) {
- // callback(rfs.exists(RECURSIVE_DIR_TEMP))
- // }).subscribe(function(exists) {
- // assert.equal(exists, false);
- // done();
- // });
- // })
+ it('should rm recursive', function(done) {
+ rfs.rmr(RECURSIVE_DIR_TEMP).then(function(callback) {
+ callback(rfs.exists(RECURSIVE_DIR_TEMP))
+ }).subscribe(function(exists) {
+ assert.equal(exists, false);
+ done();
+ });
+ })
- // it('should rm recursive accept not exists dir', function(done) {
- // rfs.rmr('some_fake_dir').subscribe(function() {
- // done();
- // })
- // })
- // })
-// });
+ it('should rm recursive accept not exists dir', function(done) {
+ rfs.rmr('some_fake_dir').subscribe(function() {
+ done();
+ })
+ })
+ })
+});
View
125 test/promise.js
@@ -37,19 +37,30 @@ describe('promise', function() {
})
- // describe('::new', function() {
- // it('evaluate promise', function() {
- // var a = new Promise(function(callback) {
- // callback(new Promise(function(callback2) {
- // callback2(2)
- // }))
- // });
+ describe('::new', function() {
+ it('evaluate promise', function() {
+ var a = new Promise(function(callback) {
+ callback(new Promise(function(callback2) {
+ callback2(2)
+ }))
+ });
+
+ a.subscribe(spy);
+
+ assert(spy.calledWith(2))
+ });
+
+
+ it('return promise', function() {
+ var a = new Promise(function(callback) {
+ return 2;
+ });
- // a.subscribe(spy);
+ a.subscribe(spy);
- // assert(spy.calledWith(2))
- // });
- // });
+ assert(spy.calledWith(2))
+ })
+ });
describe('#then', function() {
it('should transform data', function() {
@@ -60,17 +71,25 @@ describe('promise', function() {
});
});
- // it('should transform promise data', function() {
- // r1.then(function(a, next) {
- // next(new Promise(function(callback) {
- // callback(a + 1)
- // }));
- // }).subscribe(spy);
+ it('should transform promise data', function() {
+ r1.then(function(a, next) {
+ next(new Promise(function(callback) {
+ callback(a + 1)
+ }));
+ }).subscribe(spy);
- // assert(spy.calledWith(2))
- // });
+ assert(spy.calledWith(2))
+ });
+ it('should evaluate return promise data', function() {
+ r1.then(function(a, next) {
+ return new Promise(function(callback) {
+ return a + 1
+ });
+ }).subscribe(spy);
+ assert(spy.calledWith(2))
+ });
it('should call previous return function when called', function() {
@@ -105,43 +124,43 @@ describe('promise', function() {
})
- // it('should dispose previous when next is attached', function() {
- // var e1 = new Event();
- // var e2 = new Event();
- // var host = new Event();
+ it('should dispose previous when next is attached', function() {
+ var e1 = new Event();
+ var e2 = new Event();
+ var host = new Event();
- // var p = new Promise(function(callback) {
- // return host.subscribe(callback);
- // });
+ var p = new Promise(function(callback) {
+ return host.subscribe(callback);
+ });
- // // p.then(function(evt) {
- // // return evt.subscribe(spy);
- // // }).done();
+ // p.then(function(evt) {
+ // return evt.subscribe(spy);
+ // }).done();
- // p.then(spy).done()
+ p.then(spy).done()
- // host.notify(e1);
- // e1.notify('e1');
- // expect(spy).calledWith('e1')
+ host.notify(e1);
+ e1.notify('e1');
+ expect(spy).calledWith('e1')
- // spy.reset();
- // host.notify(e2);
- // e2.notify('e2');
- // expect(spy).calledWith('e2')
+ spy.reset();
+ host.notify(e2);
+ e2.notify('e2');
+ expect(spy).calledWith('e2')
- // spy.reset();
- // e1.notify('e1');
- // expect(spy).not.to.be.called;
+ spy.reset();
+ e1.notify('e1');
+ expect(spy).not.to.be.called;
- // spy.reset();
- // e2.notify('e2');
- // expect(spy).calledWith('e2')
+ spy.reset();
+ e2.notify('e2');
+ expect(spy).calledWith('e2')
- // })
+ })
it('should dispose for return dipose', function() {
r1.then(function(n, callback) {
@@ -149,20 +168,9 @@ describe('promise', function() {
})
})
-
- // it('transform return promise like callback', function() {
- // r1.then(function(a, next) {
- // return [new Promise(function(callback) {
- // callback(a + 1)
- // })];
- // }).subscribe(spy);
-
- // assert(spy.calledWith([2]))
- // });
-
})
- describe('when', function() {
+ describe('::when', function() {
it('call without args should notify', function(done) {
when().subscribe(function() {
assert.equal(arguments.length, 0)
@@ -275,7 +283,7 @@ describe('promise', function() {
})
- describe('pick', function() {
+ describe('#pick', function() {
it('skip empty', function() {
var p = new Promise(function(callback) {
@@ -304,7 +312,7 @@ describe('promise', function() {
})
})
- describe('pluck', function() {
+ describe('#pluck', function() {
it('skip empty', function() {
var p = new Promise(function(callback) {
@@ -333,5 +341,4 @@ describe('promise', function() {
})
})
-
})
View
16 test/rx.js
@@ -29,18 +29,16 @@ describe('rx', function() {
function() {
var self = this;
- this.loadEventSource = rx.createEventEvent();
- this.idChangeEventSource = rx.createEventEvent();
-
- this.idChangeEvent = this.idChangeEventSource.resolveEvent();
+ this.loadEventSource = rx.createEvent();
+ this.idChangeEventSource = rx.createEvent();
this.loadEvent = rx.any(
- this.idChangeEvent.then(function(id, callback) {
+ this.idChangeEventSource.then(function(id, callback) {
callback({
id: id
})
}),
- this.loadEventSource.resolveEvent()
+ this.loadEventSource
);
this.remoteData = rx.preserve(this.loadEvent.then(function(query, callback) {
@@ -60,17 +58,17 @@ describe('rx', function() {
})
this.id = rx.any(
- this.idChangeEvent,
+ this.idChangeEventSource,
this.data.pluck('id')
)
},
{
replaceLoadEvent: function(evt) {
- this.loadEventSource.replaceEvent(evt);
+ this.loadEventSource.notify(evt);
},
replaceIdEvent: function(evt) {
- this.idChangeEventSource.replaceEvent(evt);
+ this.idChangeEventSource.notify(evt);
},
request: function(query, callback) {
View
22 tool/build-lib-cmd.js
@@ -1,22 +1,22 @@
var p = require('path');
-var rfs = require('../fs');
+var rfs = require('../node/fs');
var src = p.join(__dirname, '../lib');
var dist = p.join(__dirname, '../lib-cmd');
var wrapCMD = function(str) {
- return 'define(function(require, exports, module) {\n\n ' + str.replace(/(\n)/g, '$1 ') + '\n}';
+ return 'define(function(require, exports, module) {\n\n ' + str.replace(/(\n)/g, '$1 ') + '\n})';
};
rfs.rmr(dist)
- .then(function() {
- return rfs.mkdir(dist);
+ .then(function(callback) {
+ callback(rfs.mkdir(dist));
})
- .then(function() {
- return rfs.readdirr(src);
+ .then(function(callback) {
+ callback(rfs.readdirr(src));
})
- .then(function(files, next) {
- next(
+ .then(function(files, callback) {
+ callback(
files.map(function(file) {
return file.substring(file.lastIndexOf(p.sep) + 1);
}),
@@ -25,10 +25,10 @@ rfs.rmr(dist)
})
)
})
- .then(function(fileNames, contents) {
- return contents.map(function(content, i) {
+ .then(function(fileNames, contents, callback) {
+ callback(contents.map(function(content, i) {
return rfs.writeFile(p.join(dist, fileNames[i]), wrapCMD(content), 'utf8');
- });
+ }));
})
.subscribe(function() {
console.log('files build in:' + dist)

No commit comments for this range

Something went wrong with that request. Please try again.