Permalink
Browse files

1) Fixed a bug with exceptions not bubbling out

2) Fixed one of the unit tests to make it test exceptions bubbling out
3) Rewrote the cake file to not require modules to be globaly installed
4) Updated package.js to remove unused dependency
  • Loading branch information...
1 parent d96a2b8 commit 820c99793144f8aa4c6d2e67da8254ce80dad154 @freethenation committed Dec 11, 2012
Showing with 98 additions and 69 deletions.
  1. +35 −40 Cakefile
  2. +23 −5 lib/funcflow.js
  3. +1 −2 lib/funcflow.min.js
  4. +1 −2 package.json
  5. +17 −7 src/funcflow.coffee
  6. +7 −5 tests/tests.coffee
  7. +14 −8 tests/tests.js
View
@@ -1,47 +1,42 @@
-{exec} = require 'child_process'
+fs = require 'fs'
task 'build', 'compiles src/funcflow.coffee to lib/funcflow.js', ->
- compile()
-
-task 'build:watch', 'watches src/funcflow.coffee and rebuilds when a change is detected', ->
- watch()
+ readFile('./src/funcflow.coffee', (file)->compile(file, (file)->writeFile('./lib/funcflow.js', file, ()->console.log('Compiled "funcflow.js"!'))))
task 'build:min', 'compiles src/funcflow.coffee to lib/funcflow.js and then runs UglifyJS on it', ->
- compile(()=>compress())
+ invoke('build')
+ setTimeout((()->readFile('./lib/funcflow.js', (file)->compress(file, (file)->writeFile('./lib/funcflow.min.js', file, ()->console.log('Compiled "funcflow.min.js"!'))))), 1500)
task 'build:full', 'compiles src/funcflow.coffee, runs all tests, and minifies', ->
- compile(()->compress(()->compileTest(()->test())))
-
+ invoke('build:min')
+ setTimeout((()->invoke('test')), 3000)
+
task 'test', 'compiles src/funcflow.coffee and then runs tests on it', ->
- compile(()->compileTest(()->test()))
-
-test = (callback) ->
- console.log "Testing funcflow.coffee"
- exec 'nodeunit tests/tests.js', (err, stdout, stderr) ->
- throw err if err
- console.log "Tested funcflow.coffee"
- callback?()
-
-compileTest = (callback) ->
- exec 'coffee -c tests/', (err, stdout, stderr) ->
- throw err if err
- console.log "Compiled tests.coffee"
- callback?()
-
-watch = (callback) ->
- exec 'coffee -w -o lib/ -c src/', (err, stdout, stderr) ->
- throw err if err
- callback?()
-
-compile = (callback) ->
- exec 'coffee -o lib/ -c src/', (err, stdout, stderr) ->
- throw err if err
- console.log "Compiled funcflow.coffee"
- callback?()
-
-compress = (callback) ->
- exec 'uglifyjs -o lib/funcflow.min.js lib/funcflow.js', (err, stdout, stderr) ->
- throw err if err
- console.log "Compressed funcflow.js"
- callback?()
-
+ invoke('build')
+ setTimeout(
+ (()->readFile('./tests/tests.coffee', (file)->compile(file, (file)->writeFile('./tests/tests.js', file, ()->test('./tests/tests.js', ()->console.log('Tested "funcflow.coffee" successful!')))))),
+ 1500)
+
+compile = (inputFile, callback) ->
+ coffee = require 'coffee-script'
+ callback?(coffee.compile(inputFile))
+
+compress = (inputFile, callback) ->
+ uglify = require "uglify-js"
+ ast = uglify.parser.parse(inputFile); # parse code and get the initial AST
+ ast = uglify.uglify.ast_mangle(ast); # get a new AST with mangled names
+ ast = uglify.uglify.ast_squeeze(ast); # get an AST with compression optimizations
+ callback?(uglify.uglify.gen_code(ast))
+
+ readFile = (filename, callback) ->
+ data = fs.readFile(filename, 'utf8', (err, data)-> if err then throw err else callback(data))
+
+ writeFile = (filename, data, callback) ->
+ fs.writeFile(filename, data, 'utf8', (err)-> if err then throw err else callback())
+
+#'nodeunit tests/tests.js',
+
+test = (inputFile, callback) ->
+ console.log 'Testing "funcflow.coffee"'
+ test = require("./tests/tests.js")
+ callback()
View
@@ -1,4 +1,3 @@
-// Generated by CoffeeScript 1.3.3
(function() {
var Flow, FlowStep, flow,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
@@ -14,20 +13,39 @@
this.spawn = __bind(this.spawn, this);
+ this.raise = __bind(this.raise, this);
+
+ if (!(state.catchExceptions != null)) {
+ state.catchExceptions = true;
+ }
+ this.state = state;
state.next = this.next;
state.spawn = this.spawn;
state.raise = this.raise;
this.threads = [];
this._unjoinedThreadCount = 1;
- try {
+ if (this.state.catchExceptions) {
+ try {
+ lastReturn.unshift(state);
+ func.apply(null, lastReturn);
+ } catch (ex) {
+ if (this.state.catchExceptions) {
+ this.callback([ex]);
+ } else {
+ throw ex;
+ }
+ }
+ } else {
lastReturn.unshift(state);
func.apply(null, lastReturn);
- } catch (ex) {
- this.callback([ex]);
}
}
FlowStep.prototype.raise = function(ex) {
+ this.state.catchExceptions = false;
+ if (typeof ex === "undefined" || ex === null) {
+ throw "Can not raise null or undefined exception. Call to step.raise() has failed!";
+ }
throw ex;
};
@@ -119,7 +137,7 @@
if (typeof module !== 'undefined') {
module.exports = flow;
} else {
- window.flow = flow;
+ window.funcflow = flow;
}
}).call(this);
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,6 +1,6 @@
{
"name": "funcflow"
- , "version": "1.0.3"
+ , "version": "1.1.0"
, "description": "Simplifies asynchronous control flow in javascript making making parallel code, synchronous code, and error handling simple"
, "homepage": "https://github.com/freethenation/funcflow"
, "keywords": ["async", "parallel", "synchronous", "coffee", "callback", "flow", "control", "coffeescript"]
@@ -16,7 +16,6 @@
, "devDependencies": {
"coffee-script": "1.3.3"
, "uglify-js": "1.3.0"
- , "nodeunit":"0.7.4"
}
, "main": "index"
, "engines": { "node": ">= 0.4.0" }
View
@@ -1,16 +1,26 @@
class FlowStep
constructor:(func, lastReturn, state, @callback)->
+ if !state.catchExceptions? then state.catchExceptions = true
+ @state = state
state.next = @next
state.spawn = @spawn
state.raise = @raise
@threads=[]
@_unjoinedThreadCount=1
- try
- lastReturn.unshift(state)
- func.apply(null, lastReturn)
- catch ex
- @callback([ex])
- raise:(ex)->throw ex
+ if @state.catchExceptions
+ try
+ lastReturn.unshift(state)
+ func.apply(null, lastReturn)
+ catch ex
+ if @state.catchExceptions then @callback([ex]) else throw ex
+ else
+ lastReturn.unshift(state)
+ func.apply(null, lastReturn)
+ raise:(ex)=>
+ @state.catchExceptions = false
+ if typeof ex == "undefined" or ex == null
+ throw "Can not raise null or undefined exception. Call to step.raise() has failed!"
+ throw ex
spawn:()=>
@_unjoinedThreadCount++
threadId=@threads.length
@@ -55,4 +65,4 @@ flow=(steps, state, callback)->
return
if(typeof(module)!='undefined') then module.exports = flow
-else window.flow = flow
+else window.funcflow = flow
View
@@ -19,7 +19,7 @@ test=(name, func)->
callMeBack=(func, args...)->
func.apply(null, args)
-funcflow=require('../lib/funcflow.js')
+funcflow=require("../lib/funcflow")
test "basic", ()->
steps = []
@@ -77,10 +77,12 @@ test "parallel error handling", ()->
test "bubble error handling", ()->
@expect(1)
- @throws(funcflow([
- (step, err)=>
- step.raise("some error")
- ], ()->))
+ exThrown = false
+ try
+ funcflow([(step, err)=>step.raise("some error")])
+ catch ex
+ exThrown = true
+ @ok(exThrown)
@done()
test "no callback", ()->
View
@@ -1,4 +1,3 @@
-// Generated by CoffeeScript 1.3.3
(function() {
var callMeBack, funcflow, test,
__slice = [].slice;
@@ -34,7 +33,7 @@
return func.apply(null, args);
};
- funcflow = require('../lib/funcflow.js');
+ funcflow = require("../lib/funcflow");
test("basic", function() {
var steps,
@@ -108,13 +107,20 @@
});
test("bubble error handling", function() {
- var _this = this;
+ var exThrown,
+ _this = this;
this.expect(1);
- this.throws(funcflow([
- function(step, err) {
- return step.raise("some error");
- }
- ], function() {}));
+ exThrown = false;
+ try {
+ funcflow([
+ function(step, err) {
+ return step.raise("some error");
+ }
+ ]);
+ } catch (ex) {
+ exThrown = true;
+ }
+ this.ok(exThrown);
return this.done();
});

0 comments on commit 820c997

Please sign in to comment.