Comparing version
#[Promises](./comb_Promise.html) | ||
`comb` provides an async wrapper called a [promise](http://en.wikipedia.org/wiki/Futures_and_promises). Promises are a way of handling async and sync behavior by encapsulating it in a wrapper. One of the benefits of promises is that it encourages the separation of success and failure logic, and provides a value that can be passed around rather than providing callbacks for every async action. | ||
`comb` provides an async wrapper called a [promise](http://en.wikipedia.org/wiki/Futures_and_promises). Promises are a way of handling async and sync behavior by encapsulating it in a wrapper. One of the benefits of promises is that they encourage the separation of success and failure logic. Promises provide a value that can be passed around rather than providing callbacks for every async action. | ||
@@ -17,3 +17,3 @@ ##[comb.Promise](./comb_Promise.html) | ||
When working with a promise you must resolve the promise in order to callbacks/errbacks to be invoked. Resolution can happen before for after callbacks/errbacks have been registered. The methods to resolve a promise are `callback`, `errback`, and `resolve`. | ||
When working with a promise you must resolve the promise in order for callbacks/errbacks to be invoked. Resolution can happen before for after callbacks/errbacks have been registered. The methods to resolve a promise are `callback`, `errback`, and `resolve`. | ||
@@ -75,3 +75,3 @@ ###callback and errback | ||
###Listening for Promise Resolutions. | ||
###Listening for Promise Resolutions | ||
@@ -149,3 +149,3 @@ To listen for the resolution of promises there are the following methods. For the following examples we will use the `readFile` method defined above. | ||
The chain method is used to chain promises together so they execute ir order. The chain method is different from the [comb.serial](./comb.html#.serial) method in that it pipes the results from one promise into the next. | ||
The chain method is used to chain promises together so they execute in order. The chain method is different from the [comb.serial](./comb.html#.serial) method in that it pipes the results from one promise into the next. | ||
@@ -185,6 +185,6 @@ In the following example we chain the results of different promise actions into a single result. | ||
If you do not provide an `errback` for each chain then it will be propogated to the final promise | ||
If you do not provide an `errback` for each chain then it will be propogated to the final promise. | ||
``` | ||
new Promise().callbac() | ||
new Promise().callback() | ||
.chain(function(){ | ||
@@ -203,3 +203,3 @@ return new comb.Promise().errback(new Error("error")); | ||
Chain also allows you to catch errors so you can handle them successfully | ||
Chain also allows you to catch errors so you can handle them successfully. | ||
@@ -227,5 +227,6 @@ ``` | ||
console.log(str); //"caught error and handled" | ||
}); | ||
``` | ||
If you still cannot handle the error you can rethrow the error. | ||
If you still cannot handle the error you can rethrow the error. It will handle errors thrown asynchronously or synchronously. | ||
@@ -237,2 +238,3 @@ ``` | ||
}, function(){ | ||
// return async error | ||
return new Promise().errback(new Error("error not handled")); | ||
@@ -242,2 +244,3 @@ }) | ||
console.log(err.message); //"error not handled" | ||
}); | ||
``` | ||
@@ -254,2 +257,3 @@ | ||
console.log(err.message); //"error not handled" | ||
}); | ||
``` | ||
@@ -313,3 +317,2 @@ | ||
return comb.when(files); | ||
} | ||
@@ -438,3 +441,3 @@ | ||
}).then(function(){ | ||
console.log(myNewArr) //[[1,0], [2,1], [3,2], [4,3], [5,4]] | ||
console.log(myNewArr); //[[1,0], [2,1], [3,2], [4,3], [5,4]] | ||
}); | ||
@@ -473,3 +476,3 @@ ``` | ||
}).then(function(){ | ||
console.log(myNewArr) //[2,4,6,8,10]; | ||
console.log(myNewArr); //[2,4,6,8,10]; | ||
}); | ||
@@ -508,3 +511,3 @@ ``` | ||
}).then(function(){ | ||
console.log(myNewArr) //[1,3,5]; | ||
console.log(myNewArr); //[1,3,5]; | ||
}) | ||
@@ -542,3 +545,3 @@ ``` | ||
}).then(function(){ | ||
console.log(myNewArr) //false; | ||
console.log(myNewArr); //false; | ||
}) | ||
@@ -577,3 +580,3 @@ ``` | ||
}).then(function(){ | ||
console.log(myNewArr) //false; | ||
console.log(myNewArr); //false; | ||
}) | ||
@@ -580,0 +583,0 @@ ``` |
@@ -0,1 +1,6 @@ | ||
# 0.3.5 | ||
* Fixed issue where messages were not propagated down to appenders even if there level was set to accept them | ||
* Added error logging to rolling file appender if there was an issue rolling over. | ||
# 0.3.4 | ||
@@ -2,0 +7,0 @@ |
@@ -10,6 +10,6 @@ var define = require("../../define.js").define, base = require("../../base"), string = base.string, style = string.style, format = string.format, Appender = require("./appender"), Level = require("../level"); | ||
*/ | ||
define(Appender, { | ||
instance:{ | ||
Appender.extend({ | ||
instance: { | ||
constructor:function (options) { | ||
constructor: function (options) { | ||
options = options || {}; | ||
@@ -20,3 +20,3 @@ !options.name && (options.name = "consoleAppender"); | ||
append:function (event) { | ||
append: function (event) { | ||
if (this._canAppend(event)) { | ||
@@ -23,0 +23,0 @@ var message = format(this.__pattern, event); |
@@ -49,6 +49,6 @@ var define = require("../../define.js").define, | ||
*/ | ||
define(Appender, { | ||
instance:{ | ||
Appender.extend({ | ||
instance: { | ||
constructor:function (options) { | ||
constructor: function (options) { | ||
options = options || {}; | ||
@@ -59,3 +59,6 @@ !options.name && (options.name = "fileAppender"); | ||
this.__overwrite = options.overwrite || false; | ||
this.__writeStream = options.writeStream || fs.createWriteStream(this.__file, { flags:this.__overwrite ? "w" : 'a', encoding:this.__encoding}); | ||
this.__writeStream = options.writeStream || fs.createWriteStream(this.__file, { | ||
flags: this.__overwrite ? "w" : 'a', | ||
encoding: this.__encoding | ||
}); | ||
this._super([options]); | ||
@@ -67,3 +70,3 @@ this.__pattern += "\n"; | ||
__onExit:function () { | ||
__onExit: function () { | ||
var ret = new Promise(); | ||
@@ -77,3 +80,3 @@ var ws = this.__writeStream; | ||
append:function (event) { | ||
append: function (event) { | ||
var ws = this.__writeStream; | ||
@@ -80,0 +83,0 @@ if (this._canAppend(event) && ws && ws.writable) { |
@@ -53,3 +53,3 @@ var define = require("../../define.js").define, | ||
*/ | ||
define(FileAppender, { | ||
FileAppender.extend({ | ||
instance:{ | ||
@@ -56,0 +56,0 @@ |
@@ -17,5 +17,5 @@ var define = require("../../define.js").define, | ||
var conversion = { | ||
MB:1048576, | ||
KB:1024, | ||
GB:1073741824 | ||
MB: 1048576, | ||
KB: 1024, | ||
GB: 1073741824 | ||
}; | ||
@@ -79,8 +79,8 @@ var DEFAULT_SIZE = "10MB"; | ||
*/ | ||
define(FileAppender, { | ||
instance:{ | ||
FileAppender.extend({ | ||
instance: { | ||
__watching:false, | ||
__watching: false, | ||
constructor:function (options) { | ||
constructor: function (options) { | ||
options = options || {}; | ||
@@ -95,3 +95,3 @@ this.maxSize = options.maxSize || DEFAULT_SIZE; | ||
__startCheck:function () { | ||
__startCheck: function () { | ||
if (!this.__watching) { | ||
@@ -106,4 +106,4 @@ this.__watching = true; | ||
__checkFile:function (stats) { | ||
var ret = new Promise(); | ||
__checkFile: function (stats) { | ||
var ret = new Promise(), self = this; | ||
if (!this.__inRollover) { | ||
@@ -113,13 +113,27 @@ if (stats.size >= this.maxSize) { | ||
this.__inRollover = true; | ||
this.__onExit().chain(hitch(this, "__rollover")).then(hitch(this, function () { | ||
var ws = fs.createWriteStream(this.__file, { flags:"w", encoding:this.__encoding}); | ||
ws.on("open", hitch(this, function () { | ||
this.__writeStream = ws; | ||
this.__inRollover = false; | ||
this.__checkQueue(); | ||
ret.callback(); | ||
})); | ||
}), hitch(ret, "errback", new Error("comb.logging.appenders.RollingFileAppender : error rolling over files"))); | ||
this.__onExit() | ||
.chain(function () { | ||
return self.__rollover(); | ||
}) | ||
.chain(function () { | ||
var ws = fs.createWriteStream(self.__file, {flags: "w", encoding: self.__encoding}); | ||
ws.on("open", function () { | ||
self.__writeStream = ws; | ||
self.__inRollover = false; | ||
self.__checkQueue(); | ||
ret.callback(); | ||
}); | ||
}, function (err) { | ||
process.stderr.write("comb.logging.appenders.RollingFileAppender : error rolling over files resuming writing"); | ||
process.stderr.write(err.stack); | ||
var ws = fs.createWriteStream(self.__file, {flags: "w", encoding: self.__encoding}); | ||
ws.on("open", function () { | ||
self.__writeStream = ws; | ||
self.__inRollover = false; | ||
self.__checkQueue(); | ||
ret.callback(); | ||
}); | ||
}); | ||
} else { | ||
this.__writeStream = fs.createWriteStream(this.__file, { flags:"w", encoding:this.__encoding}); | ||
this.__writeStream = fs.createWriteStream(this.__file, {flags: "w", encoding: this.__encoding}); | ||
ret.callback(); | ||
@@ -137,3 +151,3 @@ } | ||
append:function (event) { | ||
append: function (event) { | ||
if (this._canAppend(event)) { | ||
@@ -150,3 +164,3 @@ !this.__watching && this.__startCheck(); | ||
__checkQueue:function () { | ||
__checkQueue: function () { | ||
this.__queue.forEach(this.append, this); | ||
@@ -156,47 +170,51 @@ this.__queue.length = 0; | ||
__rollover:function () { | ||
var ret = new Promise(), file = this.__file; | ||
__rollover: function () { | ||
var ret = new Promise(), file = this.__file, self = this; | ||
var dir = path.dirname(file), baseName = new RegExp("(" + escape(path.basename(path.basename(file))) + ")(?:\\.(\\d*))*"); | ||
fs.readdir(dir, hitch(this, function (err, files) { | ||
files = files.filter( | ||
function (f) { | ||
var match = f.match(baseName); | ||
if (match) { | ||
return true; | ||
} else { | ||
return false; | ||
fs.readdir(dir, function (err, files) { | ||
if (err) { | ||
ret.errback(err); | ||
} else { | ||
files = files.filter( | ||
function (f) { | ||
var match = f.match(baseName); | ||
if (match) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
}); | ||
files = files.sort(function (a, b) { | ||
var ret = 0; | ||
if (a > b) { | ||
ret = 0; | ||
} else if (a < b) { | ||
ret = 1; | ||
} | ||
return ret; | ||
}); | ||
files = files.sort(function (a, b) { | ||
var ret = 0; | ||
if (a > b) { | ||
ret = 0; | ||
} else if (a < b) { | ||
ret = 1; | ||
} | ||
return ret; | ||
}); | ||
var count = files.length, i = 0; | ||
var checkFile = hitch(this, function () { | ||
if (count > 0) { | ||
var f = dir + "/" + files[i++]; | ||
if (count > this.maxBackupIndex) { | ||
//drop the file; | ||
count--; | ||
fs.unlink(f, function (err) { | ||
err ? ret.errback(err) : checkFile(); | ||
}); | ||
var count = files.length, i = 0; | ||
var checkFile = function () { | ||
if (count > 0) { | ||
var f = dir + "/" + files[i++]; | ||
if (count > self.maxBackupIndex) { | ||
//drop the file; | ||
count--; | ||
fs.unlink(f, function (err) { | ||
err ? ret.errback(err) : checkFile(); | ||
}); | ||
} else { | ||
//rename the file | ||
var rn = self.__file + "." + count--; | ||
fs.rename(f, rn, function (err) { | ||
err ? ret.errback(err) : checkFile(); | ||
}); | ||
} | ||
} else { | ||
//rename the file | ||
var rn = this.__file + "." + count--; | ||
fs.rename(f, rn, function (err) { | ||
err ? ret.errback(err) : checkFile(); | ||
}); | ||
ret.callback(); | ||
} | ||
} else { | ||
ret.callback(); | ||
} | ||
}); | ||
checkFile(); | ||
})); | ||
}; | ||
checkFile(); | ||
} | ||
}); | ||
return ret.promise(); | ||
@@ -206,5 +224,5 @@ }, | ||
getters:{ | ||
getters: { | ||
maxSize:function () { | ||
maxSize: function () { | ||
return this.__maxSize; | ||
@@ -214,4 +232,4 @@ } | ||
setters:{ | ||
maxSize:function (size) { | ||
setters: { | ||
maxSize: function (size) { | ||
this.__maxSize = size ? convertToBytes(size) : DEFAULT_SIZE; | ||
@@ -218,0 +236,0 @@ } |
@@ -261,5 +261,3 @@ var os = require("os"), | ||
info: function (message) { | ||
if (this.isInfo) { | ||
return this.log.apply(this, [Level.INFO].concat(argsToArray(arguments))); | ||
} | ||
return this.log.apply(this, [Level.INFO].concat(argsToArray(arguments))); | ||
}, | ||
@@ -275,5 +273,3 @@ | ||
debug: function (message) { | ||
if (this.isDebug) { | ||
return this.log.apply(this, [Level.DEBUG].concat(argsToArray(arguments))); | ||
} | ||
return this.log.apply(this, [Level.DEBUG].concat(argsToArray(arguments))); | ||
}, | ||
@@ -289,5 +285,3 @@ | ||
error: function (message) { | ||
if (this.isError) { | ||
return this.log.apply(this, [Level.ERROR].concat(argsToArray(arguments))); | ||
} | ||
return this.log.apply(this, [Level.ERROR].concat(argsToArray(arguments))); | ||
}, | ||
@@ -303,5 +297,3 @@ | ||
warn: function (message) { | ||
if (this.isWarn) { | ||
return this.log.apply(this, [Level.WARN].concat(argsToArray(arguments))); | ||
} | ||
return this.log.apply(this, [Level.WARN].concat(argsToArray(arguments))); | ||
}, | ||
@@ -317,5 +309,3 @@ | ||
trace: function (message) { | ||
if (this.isTrace) { | ||
return this.log.apply(this, [Level.TRACE].concat(argsToArray(arguments))); | ||
} | ||
return this.log.apply(this, [Level.TRACE].concat(argsToArray(arguments))); | ||
}, | ||
@@ -331,5 +321,3 @@ | ||
fatal: function (message) { | ||
if (this.isFatal) { | ||
return this.log.apply(this, [Level.FATAL].concat(argsToArray(arguments))); | ||
} | ||
return this.log.apply(this, [Level.FATAL].concat(argsToArray(arguments))); | ||
}, | ||
@@ -368,3 +356,3 @@ | ||
level = Level.toLevel(level); | ||
if (level.isGreaterOrEqualToo(this.level)) { | ||
if (this.hasLevelGt(level)) { | ||
var args = argsToArray(arguments, 1); | ||
@@ -500,2 +488,17 @@ if (args.length > 1) { | ||
hasLevelGt: function (level) { | ||
var ret = level.isGreaterOrEqualToo(this.level), i, appenders, keys, l; | ||
if (!ret) { | ||
appenders = this.__appenders; | ||
keys = Object.keys(appenders); | ||
l = keys.length; | ||
i = -1; | ||
while (++i < l && !ret) { | ||
ret = level.isGreaterOrEqualToo(appenders[keys[i]].level); | ||
} | ||
} | ||
return ret; | ||
}, | ||
/** | ||
@@ -502,0 +505,0 @@ * @ignore |
{ | ||
"name": "comb", | ||
"description": "A framework for node", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"keywords": ["OO", "Object Oriented", "Collections", "Tree", "HashTable", "Pool", "Logging", "Promise", "Promises", "Proxy"], | ||
@@ -6,0 +6,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2955816
0.19%11989
0.18%