Skip to content
Browse files

seperated async events from util lib, renamed jsDAV_Directory to more…

… generic jsDAV_Collection and improved logging for async events
  • Loading branch information...
1 parent 29994a3 commit bbada3748019a38cb4e9e5b3a1359d09a66cf8f3 @mikedeboer committed Feb 4, 2013
View
4 lib/CardDAV/addressBook.js
@@ -7,7 +7,7 @@
*/
"use strict";
-var jsDAV_Directory = require("./../DAV/directory");
+var jsDAV_Collection = require("./../DAV/collection");
var jsCardDAV_iAddressBook = require("./interfaces/iAddressBook");
var jsDAV_iProperties = require("./../DAV/interfaces/iProperties");
var jsDAVACL_iAcl = require("./../DAVACL/interfaces/iAcl");
@@ -20,7 +20,7 @@ var Exc = require("./../shared/exceptions");
*
* The AddressBook can contain multiple vcards
*/
-var jsCardDAV_AddressBook = module.exports = jsDAV_Directory.extend(jsCardDAV_iAddressBook, jsDAV_iProperties, jsDAVACL_iAcl, {
+var jsCardDAV_AddressBook = module.exports = jsDAV_Collection.extend(jsCardDAV_iAddressBook, jsDAV_iProperties, jsDAVACL_iAcl, {
/**
* This is an array with addressbook information
*
View
4 lib/CardDAV/userAddressBooks.js
@@ -7,7 +7,7 @@
*/
"use strict";
-var jsDAV_Directory = require("./../DAV/directory");
+var jsDAV_Collection = require("./../DAV/collection");
var jsDAV_iExtendedCollection = require("./../DAV/interfaces/iExtendedCollection");
var jsDAVACL_iAcl = require("./../DAVACL/interfaces/iAcl")
var jsCardDAV_AddressBook = require("./addressBook");
@@ -21,7 +21,7 @@ var Util = require("./../shared/util");
*
* The UserAddressBooks collection contains a list of addressbooks associated with a user
*/
-var jsCardDAV_UserAddressBooks = module.exports = jsDAV_Directory.extend(jsDAV_iExtendedCollection, jsDAVACL_iAcl, {
+var jsCardDAV_UserAddressBooks = module.exports = jsDAV_Collection.extend(jsDAV_iExtendedCollection, jsDAVACL_iAcl, {
/**
* Principal uri
*
View
4 lib/DAV/backends/dropbox/directory.js
@@ -9,12 +9,12 @@
var jsDAV_Dropbox_Node = require("./node");
var jsDAV_Dropbox_File = require("./file");
-var jsDAV_Directory = require("./../../directory");
+var jsDAV_Collection = require("./../../collection");
var jsDAV_iQuota = require("./../../interfaces/iQuota");
var Exc = require("./../../../shared/exceptions");
-var jsDAV_Dropbox_Directory = module.exports = jsDAV_Dropbox_Node.extend(jsDAV_Directory, jsDAV_iQuota, {
+var jsDAV_Dropbox_Directory = module.exports = jsDAV_Dropbox_Node.extend(jsDAV_Collection, jsDAV_iQuota, {
initialize: function(path, client) {
this.path = path;
this.client = client;
View
4 lib/DAV/backends/fs/directory.js
@@ -9,15 +9,15 @@
var jsDAV_FS_Node = require("./node");
var jsDAV_FS_File = require("./file");
-var jsDAV_Directory = require("./../../directory");
+var jsDAV_Collection = require("./../../collection");
var jsDAV_iQuota = require("./../../interfaces/iQuota");
var Fs = require("fs");
var Async = require("asyncjs");
var Exc = require("./../../../shared/exceptions");
var Util = require("./../../../shared/util");
-var jsDAV_FS_Directory = module.exports = jsDAV_FS_Node.extend(jsDAV_Directory, jsDAV_iQuota, {
+var jsDAV_FS_Directory = module.exports = jsDAV_FS_Node.extend(jsDAV_Collection, jsDAV_iQuota, {
initialize: function(path) {
this.path = path;
},
View
4 lib/DAV/backends/ftp/directory.js
@@ -9,14 +9,14 @@
var jsDAV_Ftp_Node = require("./node");
var jsDAV_Ftp_File = require("./file");
-var jsDAV_Directory = require("./../../directory");
+var jsDAV_Collection = require("./../../collection");
var jsDAV_iQuota = require("./../../interfaces/iQuota");
var Path = require("path");
var Async = require("asyncjs");
var Exc = require("./../../../shared/exceptions");
-var jsDAV_Ftp_Directory = module.exports = jsDAV_Ftp_Node.extend(jsDAV_Directory, jsDAV_iQuota, {
+var jsDAV_Ftp_Directory = module.exports = jsDAV_Ftp_Node.extend(jsDAV_Collection, jsDAV_iQuota, {
initialize: function(path, ftp) {
this.path = path || "";
this.ftp = ftp;
View
4 lib/DAV/backends/sftp/directory.js
@@ -9,13 +9,13 @@
var jsDAV_SFTP_Node = require("./node");
var jsDAV_SFTP_File = require("./file");
-var jsDAV_Directory = require("./../../directory");
+var jsDAV_Collection = require("./../../collection");
var jsDAV_iQuota = require("./../../interfaces/iQuota");
var Async = require("asyncjs");
var Exc = require("./../../../shared/exceptions");
-var jsDAV_SFTP_Directory = module.exports = jsDAV_SFTP_Node.extend(jsDAV_Directory, jsDAV_iQuota, {
+var jsDAV_SFTP_Directory = module.exports = jsDAV_SFTP_Node.extend(jsDAV_Collection, jsDAV_iQuota, {
initialize: function(path, sftp) {
this.path = (path || "").replace(/[\/]+$/, "");
this.sftp = sftp;
View
3 lib/DAV/backends/sftp/tree.js
@@ -12,6 +12,7 @@ var jsDAV_SFTP_Directory = require("./directory");
var jsDAV_SFTP_File = require("./file");
var Sftp = require("node-ssh").sftp;
+var AsyncEventEmitter = require("./../../../shared/asyncEvents").EventEmitter;
var Util = require("./../../../shared/util");
var Exc = require("./../../../shared/exceptions");
@@ -39,7 +40,7 @@ var jsDAV_Tree_Sftp = module.exports = jsDAV_Tree.extend({
throw err;
});
});
- Util.EventEmitter.DEFAULT_TIMEOUT = 10000;
+ AsyncEventEmitter.DEFAULT_TIMEOUT = 10000;
},
/**
View
7 lib/DAV/directory.js → lib/DAV/collection.js
@@ -13,12 +13,13 @@ var jsDAV_iCollection = require("./interfaces/iCollection");
var Exc = require("./../shared/exceptions");
/**
- * Directory class
+ * jsDAV_Collection class
*
- * This is a helper class, that should aid in getting directory classes setup.
+ * This is a helper class, that should aid in getting collection/ directory classes
+ * setup.
* Most of its methods are implemented, and throw permission denied exceptions
*/
-var jsDAV_Directory = module.exports = jsDAV_Node.extend(jsDAV_iCollection, {
+var jsDAV_Collection = module.exports = jsDAV_Node.extend(jsDAV_iCollection, {
/**
* Returns a child object, by its name.
*
View
3 lib/DAV/handler.js
@@ -24,6 +24,7 @@ var jsDAV_iProperties = require("./interfaces/iProperties");
var Url = require("url");
var Fs = require("fs");
var Path = require("path");
+var AsyncEventEmitter = require("./../shared/asyncEvents").EventEmitter;
var Exc = require("./../shared/exceptions");
var Util = require("./../shared/util");
var Xml = require("./../shared/xml");
@@ -2445,4 +2446,4 @@ jsDAV_Handler.STATUS_MAP = {
code = String(code);
return "HTTP/1.1 " + code + " " + jsDAV_Handler.STATUS_MAP[code];
};
-}).call(jsDAV_Handler.prototype = new Util.EventEmitter());
+}).call(jsDAV_Handler.prototype = new AsyncEventEmitter());
View
5 lib/DAV/plugins/auth.js
@@ -8,7 +8,8 @@
"use strict";
var jsDAV_ServerPlugin = require("./../plugin");
-var jsDAV_Util_EventEmitter = require("./../../shared/util").EventEmitter;
+
+var AsyncEventEmitter = require("./../../shared/asyncEvents").EventEmitter;
/**
* This plugin provides Authentication for a WebDAV server.
@@ -39,7 +40,7 @@ var jsDAV_Auth_Plugin = module.exports = jsDAV_ServerPlugin.extend({
this.authBackend = handler.server.options.authBackend || null;
this.realm = handler.server.options.realm || "jsdav";
- handler.addEventListener("beforeMethod", this.beforeMethod.bind(this), jsDAV_Util_EventEmitter.PRIO_HIGH);
+ handler.addEventListener("beforeMethod", this.beforeMethod.bind(this), AsyncEventEmitter.PRIO_HIGH);
},
/**
View
4 lib/DAV/simpleDirectory.js
@@ -8,7 +8,7 @@
"use strict";
var jsDAV = require("./../jsdav");
-var jsDAV_Directory = require("./directory");
+var jsDAV_Collection = require("./collection");
var jsDAV_iNode = require("./interfaces/iNode");
var Exc = require("./../shared/exceptions");
@@ -26,7 +26,7 @@ var Exc = require("./../shared/exceptions");
* @param array children
* @return void
*/
-var jsDAV_SimpleDirectory = module.exports = jsDAV_Directory.extend({
+var jsDAV_SimpleDirectory = module.exports = jsDAV_Collection.extend({
initialize: function(name, children) {
children = children || [];
this.name = name;
View
4 lib/DAVACL/abstractPrincipalCollection.js
@@ -7,7 +7,7 @@
*/
"use strict";
-var jsDAV_Directory = require("./../DAV/directory");
+var jsDAV_Collection = require("./../DAV/collection");
var jsDAVACL_iPrincipalCollection = require("./interfaces/iPricipalCollection");
var Util = require("./../shared/util");
@@ -23,7 +23,7 @@ var Async = require("asyncjs");
*
* To use this class, simply implement the getChildForPrincipal method.
*/
-var jsDAVACL_AbstractPrincipalCollection = module.exports = jsDAV_Directory.extend(jsDAVACL_iPrincipalCollection, {
+var jsDAVACL_AbstractPrincipalCollection = module.exports = jsDAV_Collection.extend(jsDAVACL_iPrincipalCollection, {
/**
* Node or 'directory' name.
*
View
130 lib/shared/asyncEvents.js
@@ -0,0 +1,130 @@
+/*
+ * @package jsDAV
+ * @subpackage shared
+ * @copyright Copyright(c) 2011 Ajax.org B.V. <info AT ajax DOT org>
+ * @author Mike de Boer <info AT mikedeboer DOT nl>
+ * @license http://github.com/mikedeboer/jsDAV/blob/master/LICENSE MIT License
+ */
+"use strict";
+
+var jsDAV = require("./../jsdav");
+
+var Util = require("./util");
+var Async = require("asyncjs");
+
+exports.EventEmitter = function() {};
+
+exports.EventEmitter.DEFAULT_TIMEOUT = 30000; // in milliseconds
+exports.EventEmitter.PRIO_LOW = 0x0001;
+exports.EventEmitter.PRIO_NORMAL = 0x0002;
+exports.EventEmitter.PRIO_HIGH = 0x0004;
+
+var _slice = Array.prototype.slice;
+
+(function() {
+ var _ev = exports.EventEmitter;
+
+ function persistRegistry() {
+ if (this.$eventRegistry)
+ return;
+ this.$eventRegistry = {};
+ this.$eventRegistry[_ev.PRIO_LOW] = {};
+ this.$eventRegistry[_ev.PRIO_NORMAL] = {};
+ this.$eventRegistry[_ev.PRIO_HIGH] = {};
+ }
+
+ function getListeners(eventName) {
+ return (this.$eventRegistry[_ev.PRIO_HIGH][eventName] || []).concat(
+ this.$eventRegistry[_ev.PRIO_NORMAL][eventName] || []).concat(
+ this.$eventRegistry[_ev.PRIO_LOW][eventName] || []);
+ }
+
+ this.dispatchEvent = function() {
+ persistRegistry.call(this);
+
+ var e,
+ args = _slice.call(arguments),
+ eventName = args.shift().toLowerCase(),
+ listeners = getListeners.call(this, eventName),
+ cbdispatch = (typeof args[args.length - 1] == "function")
+ ? args.pop()
+ : function(){};
+ if (!listeners.length)
+ return cbdispatch();
+
+ Async.list(listeners).each(function(listener, cbnext) {
+ e = new exports.Event(eventName, args, cbnext);
+ listener.apply(null, [e].concat(args));
+ if (listener.$usetimeout > 0) {
+ clearTimeout(listener.$timeout);
+ listener.$timeout = setTimeout(function() {
+ if (!e.$done) {
+ e.next("Event callback timeout: timeout reached, no callback fired within "
+ + listener.$usetimeout + "ms");
+ }
+ }, listener.$usetimeout);
+ }
+ }).end(function(err) {
+ // everything except TRUE as an argument is an error
+ if (jsDAV.debugMode && err) {
+ if (err === true)
+ Util.log("event propagation '" + eventName + "' stopped", "info");
+ else
+ Util.log("argument after event '" + eventName + "': " + err, "error");
+ }
+ cbdispatch(err);
+ });
+ };
+
+ this.addEventListener = function(eventName, listener, prio, timeout) {
+ persistRegistry.call(this);
+
+ listener.$usetimeout = timeout === false
+ ? 0
+ : (typeof timeout == "number")
+ ? timeout
+ : exports.EventEmitter.DEFAULT_TIMEOUT;
+
+ eventName = eventName.toLowerCase();
+ prio = prio || _ev.PRIO_NORMAL;
+ var allListeners = getListeners.call(this, eventName),
+ listeners = this.$eventRegistry[prio][eventName];
+ if (!listeners)
+ listeners = this.$eventRegistry[prio][eventName] = [];
+ if (allListeners.indexOf(listener) === -1)
+ listeners.push(listener);
+ };
+
+ this.removeEventListener = function(eventName, listener) {
+ persistRegistry.call(this);
+
+ eventName = eventName.toLowerCase();
+ var _self = this;
+ [_ev.PRIO_LOW, _ev.PRIO_NORMAL, _ev.PRIO_HIGH].forEach(function(prio) {
+ var listeners = _self.$eventRegistry[prio][eventName];
+ if (!listeners)
+ return;
+ var index = listeners.indexOf(listener);
+ if (index !== -1)
+ listeners.splice(index, 1);
+ });
+ };
+}).call(exports.EventEmitter.prototype);
+
+exports.Event = function(type, args, callback) {
+ this.$event = true;
+ this.$done = false;
+ this.type = type;
+ this.returnValue = null;
+
+ this.next = function(err) {
+ if (this.$done || !callback)
+ return (!callback ? this.$done = true : false);
+ this.$done = true;
+ callback.apply(null, [err].concat(args || []));
+ };
+
+ this.stop = function() {
+ return this.next(this.returnValue = true);
+ };
+};
View
114 lib/shared/util.js
@@ -758,118 +758,6 @@ exports.uniqid = function(prefix, more_entropy) {
return retId;
};
-exports.EventEmitter = function() {};
-
-exports.EventEmitter.DEFAULT_TIMEOUT = 30000; // in milliseconds
-exports.EventEmitter.PRIO_LOW = 0x0001;
-exports.EventEmitter.PRIO_NORMAL = 0x0002;
-exports.EventEmitter.PRIO_HIGH = 0x0004;
-
-var _slice = Array.prototype.slice;
-
-(function() {
- var _ev = exports.EventEmitter;
-
- function persistRegistry() {
- if (this.$eventRegistry)
- return;
- this.$eventRegistry = {};
- this.$eventRegistry[_ev.PRIO_LOW] = {};
- this.$eventRegistry[_ev.PRIO_NORMAL] = {};
- this.$eventRegistry[_ev.PRIO_HIGH] = {};
- }
-
- function getListeners(eventName) {
- return (this.$eventRegistry[_ev.PRIO_HIGH][eventName] || []).concat(
- this.$eventRegistry[_ev.PRIO_NORMAL][eventName] || []).concat(
- this.$eventRegistry[_ev.PRIO_LOW][eventName] || []);
- }
-
- this.dispatchEvent = function() {
- persistRegistry.call(this);
-
- var e,
- args = _slice.call(arguments),
- eventName = args.shift().toLowerCase(),
- listeners = getListeners.call(this, eventName),
- cbdispatch = (typeof args[args.length - 1] == "function")
- ? args.pop()
- : function(){};
- if (!listeners.length)
- return cbdispatch();
-
- Async.list(listeners).each(function(listener, cbnext) {
- e = new exports.Event(eventName, args, cbnext);
- listener.apply(null, [e].concat(args));
- if (listener.$usetimeout > 0) {
- clearTimeout(listener.$timeout);
- listener.$timeout = setTimeout(function() {
- if (!e.$done) {
- e.next("Event callback timeout: timeout reached, no callback fired within "
- + listener.$usetimeout + "ms");
- }
- }, listener.$usetimeout);
- }
- }).end(function(err) {
- if (jsDAV.debugMode && err)
- exports.log("argument after event '" + eventName + "': " + err, "error");
- cbdispatch(err);
- });
- };
-
- this.addEventListener = function(eventName, listener, prio, timeout) {
- persistRegistry.call(this);
-
- listener.$usetimeout = timeout === false
- ? 0
- : (typeof timeout == "number")
- ? timeout
- : exports.EventEmitter.DEFAULT_TIMEOUT;
-
- eventName = eventName.toLowerCase();
- prio = prio || _ev.PRIO_NORMAL;
- var allListeners = getListeners.call(this, eventName),
- listeners = this.$eventRegistry[prio][eventName];
- if (!listeners)
- listeners = this.$eventRegistry[prio][eventName] = [];
- if (allListeners.indexOf(listener) === -1)
- listeners.push(listener);
- };
-
- this.removeEventListener = function(eventName, listener) {
- persistRegistry.call(this);
-
- eventName = eventName.toLowerCase();
- var _self = this;
- [_ev.PRIO_LOW, _ev.PRIO_NORMAL, _ev.PRIO_HIGH].forEach(function(prio) {
- var listeners = _self.$eventRegistry[prio][eventName];
- if (!listeners)
- return;
- var index = listeners.indexOf(listener);
- if (index !== -1)
- listeners.splice(index, 1);
- });
- };
-}).call(exports.EventEmitter.prototype);
-
-exports.Event = function(type, args, callback) {
- this.$event = true;
- this.$done = false;
- this.type = type;
- this.returnValue = null;
-
- this.next = function(err) {
- if (this.$done || !callback)
- return (!callback ? this.$done = true : false);
- this.$done = true;
- callback.apply(null, [err].concat(args || []));
- };
-
- this.stop = function() {
- return this.next(this.returnValue = true);
- };
-};
-
exports.concatBuffers = function(bufs) {
var buffer,
length = 0,
@@ -1009,6 +897,8 @@ var levels = {
"exit": ["\033[36m", "\033[39m"] // cyan
};
+var _slice = Array.prototype.slice;
+
exports.log = function() {
var args = _slice.call(arguments);
var lastArg = args[args.length - 1];

0 comments on commit bbada37

Please sign in to comment.
Something went wrong with that request. Please try again.