diff options
author | portix <none@none> | 2012-11-28 03:11:28 +0100 |
---|---|---|
committer | portix <none@none> | 2012-11-28 03:11:28 +0100 |
commit | 342835d1eea41964ca40c34711af9f2c171d5e91 (patch) | |
tree | 0d83b50a653972ddb3ce5fab2aa34515f164f55f /scripts/lib | |
parent | b1153dd74dfb5f4be2cd855ccef12083fcb8f17f (diff) | |
download | dwb-342835d1eea41964ca40c34711af9f2c171d5e91.zip |
Merging signals.connect and signals.connectByFunction; coding style in signals.js/dwb.js/util.js
Diffstat (limited to 'scripts/lib')
-rw-r--r-- | scripts/lib/dwb.js | 148 | ||||
-rw-r--r-- | scripts/lib/extensions.js | 3 | ||||
-rw-r--r-- | scripts/lib/signals.js | 186 | ||||
-rw-r--r-- | scripts/lib/util.js | 206 |
4 files changed, 299 insertions, 244 deletions
diff --git a/scripts/lib/dwb.js b/scripts/lib/dwb.js index d81a49b9..9ec4cf07 100644 --- a/scripts/lib/dwb.js +++ b/scripts/lib/dwb.js @@ -3,76 +3,92 @@ var _requires = {}; var _callbacks = []; var _initialized = false; - var _applyRequired = function(names, callback) { - if (names === null) - callback.call(this, _modules); - else { - var modules = []; - var name, detail; - for (var j=0, m=names.length; j<m; j++) { - name = names[j]; - if (/^\w*!/.test(name)) { - detail = name.split("!"); - name = detail[0]; - if (!_modules[name]) - include(detail.slice(1).join("!")); - } - modules.push(_modules[name]); - } - if (callback) - callback.apply(this, modules); - } + var _applyRequired = function(names, callback) + { + if (names === null) + callback.call(this, _modules); + else + { + var modules = []; + var name, detail; + for (var j=0, m=names.length; j<m; j++) + { + name = names[j]; + if (/^\w*!/.test(name)) + { + detail = name.split("!"); + name = detail[0]; + if (!_modules[name]) + include(detail.slice(1).join("!")); + } + modules.push(_modules[name]); + } + if (callback) + callback.apply(this, modules); + } }; Object.defineProperties(this, { - "provide" : { - value : function(name, module) { - if (_modules[name]) - io.debug({ - error : new Error("provide: Module " + name + " is already defined!"), - offset : 1, arguments : arguments - }); - else - _modules[name] = module; - } - }, - "replace" : { - value : function(name, module) { - if (! module && _modules[name] ) { - for (var key in _modules[name]) { - delete _modules[name][key]; - } - delete _modules[name]; - } - else - _modules[name] = module; - } - }, - "require" : { - value : function(names, callback) { - if (names !== null && ! (names instanceof Array)) { - io.debug({ error : new Error("require : invalid argument (" + JSON.stringify(names) + ")"), offset : 1, arguments : arguments }); - return; - } + "provide" : + { + value : function(name, module) + { + if (_modules[name]) + { + io.debug({ + offset : 1, arguments : arguments, + error : new Error("provide: Module " + name + " is already defined!") + }); + } + else + _modules[name] = module; + } + }, + "replace" : + { + value : function(name, module) + { + if (! module && _modules[name] ) + { + for (var key in _modules[name]) + delete _modules[name][key]; - if (!_initialized) - _callbacks.push({callback : callback, names : names}); - else - _applyRequired(names, callback); - } - }, - // Called after all scripts have been loaded and executed - // Immediately deleted from the global object, so it is not callable - // from other scripts - "_init" : { - value : function() { - _initialized = true; - for (var i=0, l=_callbacks.length; i<l; i++) - _applyRequired(_callbacks[i].names, _callbacks[i].callback); + delete _modules[name]; + } + else + _modules[name] = module; + } + }, + "require" : + { + value : function(names, callback) + { + if (names !== null && ! (names instanceof Array)) + { + io.debug({ error : new Error("require : invalid argument (" + JSON.stringify(names) + ")"), offset : 1, arguments : arguments }); + return; + } - Object.freeze(this); - }, - configurable : true - } + if (!_initialized) + _callbacks.push({callback : callback, names : names}); + else + _applyRequired(names, callback); + } + }, + // Called after all scripts have been loaded and executed + // Immediately deleted from the global object, so it is not callable + // from other scripts + "_init" : + { + value : function() + { + _initialized = true; + for (var i=0, l=_callbacks.length; i<l; i++) + _applyRequired(_callbacks[i].names, _callbacks[i].callback); + + Object.freeze(this); + }, + configurable : true + } }); })(); Object.preventExtensions(this); diff --git a/scripts/lib/extensions.js b/scripts/lib/extensions.js index 9337adb2..9825c02f 100644 --- a/scripts/lib/extensions.js +++ b/scripts/lib/extensions.js @@ -191,9 +191,8 @@ { _registered[name] = plugin; - if (plugin.exports) { + if (plugin.exports) replace(name, plugin.exports); - } extensions.message(name, "Successfully loaded and initialized."); return plugin.exports || null; diff --git a/scripts/lib/signals.js b/scripts/lib/signals.js index 9561d13d..53df11a5 100644 --- a/scripts/lib/signals.js +++ b/scripts/lib/signals.js @@ -1,95 +1,105 @@ -(function () { - var _registered = {}; - function _disconnect(sig) { - signals[sig] = null; - delete _registered[sig]; - } - Object.defineProperties(signals, { - "emit" : { - value : function(sig, args) { - var sigs = _registered[sig]; - var ret = false; - var i = 0; - do { - if (sigs[i].connected) { - ret = sigs[i].callback.apply(this, args) || ret; - i++; - } - else { - sigs.splice(i, 1); - } - } while (i<sigs.length); - if (_registered[sig].length === 0) { - _disconnect(sig); - } - return ret; - } - }, - "connect" : { - value : (function () { - var id = 0; - return function(sig, func) { - if (func === null || typeof func !== "function") { - return -1; - } - ++id; - if (_registered[sig] === undefined || _registered[sig] === null) { - _registered[sig] = []; - signals[sig] = function () { return signals.emit(sig, arguments); }; - } - _registered[sig].push({callback : func, id : id, connected : true }); - return id; - }; - })() - }, - "disconnect" : { - value : function(id) { +(function () +{ + var _registered = {}; + function _disconnect(sig) + { + signals[sig] = null; + delete _registered[sig]; + } + var _disconnectByProp = function(prop, obj) + { var sig, i, sigs; - for (sig in _registered) { - sigs = _registered[sig]; - for (i = 0; i<sigs.length; i++) { - if (sigs[i].id == id) { - if (_registered[sig].length === 1) { - _disconnect(sig); - } - else { - sigs[i].connected = false; - } - return true; + for (sig in _registered) + { + sigs = _registered[sig]; + for (i = 0; i<sigs.length; i++) + { + if (sigs[i][prop] == obj) + { + if (_registered[sig].length === 1) + { + _disconnect(sig); + } + else + { + sigs[i].connected = false; + } + return true; + } } - } } return false; - } - }, - "disconnectByFunction" : { - value : function(func) { - var sig, i, sigs, ret = false; - for (sig in _registered) { - sigs = _registered[sig]; - for (i = 0; i<sigs.length; i++) { - if (sigs[i].callback == func) { - if (_registered[sig].length === 1) { - _disconnect(sig); - } - else { - sigs[i].connected = false; - } - ret = true; + }; + Object.defineProperties(signals, + { + "emit" : + { + value : function(sig, args) + { + var sigs = _registered[sig]; + var ret = false; + var i = 0; + do + { + if (sigs[i].connected) + { + ret = sigs[i].callback.apply(this, args) || ret; + i++; + } + else + { + sigs.splice(i, 1); + } + } while (i<sigs.length); + + if (_registered[sig].length === 0) + { + _disconnect(sig); + } + return ret; + } + }, + "connect" : + { + value : (function () + { + var id = 0; + return function(sig, func) + { + if (func === null || typeof func !== "function") + { + return -1; + } + ++id; + if (_registered[sig] === undefined || _registered[sig] === null) + { + _registered[sig] = []; + signals[sig] = function () { return signals.emit(sig, arguments); }; + } + _registered[sig].push({callback : func, id : id, connected : true }); + return id; + }; + })() + }, + "disconnect" : + { + value : _disconnectByProp.bind(this, "id") + }, + "disconnectByFunction" : + { + value : _disconnectByProp.bind(this, "callback") + }, + "disconnectByName" : + { + value : function (name) + { + if (signals[name] !== null && signals[name] !== undefined) + { + _disconnect(name); + return true; + } + return false; } - } - } - return ret; - } - }, - "disconnectByName" : { - value : function (name) { - if (signals[name] !== null && signals[name] !== undefined) { - _disconnect(name); - return true; } - return false; - } - } - }); + }); })(); diff --git a/scripts/lib/util.js b/scripts/lib/util.js index 6f7fe219..02674407 100644 --- a/scripts/lib/util.js +++ b/scripts/lib/util.js @@ -1,111 +1,141 @@ (function () { var privProps = []; - function getPrivateIdx(object, key, identifier) { - var p; - for (var i=0, l=privProps.length; i<l; ++i) { - p = privProps[i]; - if (p.object == object && p.key == key && p.identifier === identifier) - return i; - } - return -1; + function getPrivateIdx(object, key, identifier) + { + var p; + for (var i=0, l=privProps.length; i<l; ++i) + { + p = privProps[i]; + if (p.object == object && p.key == key && p.identifier === identifier) + return i; + } + return -1; } - Object.defineProperties(util, { - "getBody" : { - value : function(f) { - if (f !== null && f !== undefined && f instanceof Function) { - var m = f.toString().match(/\{([\s\S]*)\}/m)[1]; - return m.replace(/^\s*\/\/.*$/mg,''); + Object.defineProperties(util, + { + "getBody" : + { + value : function(f) + { + if (f !== null && f !== undefined && f instanceof Function) + { + var m = f.toString().match(/\{([\s\S]*)\}/m)[1]; + return m.replace(/^\s*\/\/.*$/mg,''); + } + return null; } - return null; - } }, - "getSelection" : { - value : function() { - var frames = tabs.current.allFrames; - for (var i=frames.length-1; i>=0; --i) { - var selection = JSON.parse(frames[i].inject("return document.getSelection().toString()")); - if (selection.length > 0) - return selection; + "getSelection" : + { + value : function() + { + var frames = tabs.current.allFrames; + for (var i=frames.length-1; i>=0; --i) + { + var selection = JSON.parse(frames[i].inject("return document.getSelection().toString()")); + if (selection.length > 0) + return selection; + } + return null; } - return null; - } } }); Object.freeze(util); - if (Object.prototype.setPrivate === undefined && Object.prototype.getPrivate === undefined) { - Object.defineProperties(Object.prototype, { - "setPrivate" : { - value : function(key, value, identifier) { - if (!(identifier instanceof Object) && !(identifier instanceof Function)) { - throw new Error("[setPrivate] identifier is not an Object or Function"); - } - var i = getPrivateIdx(this, key, identifier); - if (i === -1) { - if (value !== undefined && value !== null) - privProps.push({ object : this, key : key, identifier : identifier, value : value }); - } - else if (value !== null) { - privProps[i].value = value; - } - else { - privProps.splice(i); + + if (Object.prototype.setPrivate === undefined && Object.prototype.getPrivate === undefined) + { + Object.defineProperties(Object.prototype, + { + "setPrivate" : + { + value : function(key, value, identifier) + { + if (!(identifier instanceof Object) && !(identifier instanceof Function)) + throw new Error("[setPrivate] identifier is not an Object or Function"); + + var i = getPrivateIdx(this, key, identifier); + if (i === -1) + { + if (value !== undefined && value !== null) + privProps.push({ object : this, key : key, identifier : identifier, value : value }); + } + else if (value !== null) + { + privProps[i].value = value; + } + else + { + privProps.splice(i); + } + } + }, + "getPrivate" : + { + value : function(key, identifier) + { + var i = getPrivateIdx(this, key, identifier); + if (i !== -1) + return privProps[i].value; + return null; + } } - } - }, - "getPrivate" : { - value : function(key, identifier) { - var i = getPrivateIdx(this, key, identifier); - if (i !== -1) - return privProps[i].value; - return null; - } - } - }); + }); } - if (Object.prototype.forEach === undefined) { - Object.defineProperty(Object.prototype, "forEach", { - value : function (callback) { - var key; - for (key in this) { - callback(key, this[key], this); + if (Object.prototype.forEach === undefined) + { + Object.defineProperty(Object.prototype, "forEach", + { + value : function (callback) + { + var key; + for (key in this) + callback(key, this[key], this); } - } - }); + }); } - if (Array.prototype.fastIndexOf === undefined) { - Object.defineProperty(Array.prototype, "fastIndexOf", { - value : function (v) { - for (var i=0, l=this.length; i<l; ++i) { - if (this[i] == v) - return i; + if (Array.prototype.fastIndexOf === undefined) + { + Object.defineProperty(Array.prototype, "fastIndexOf", + { + value : function (v) + { + for (var i=0, l=this.length; i<l; ++i) { + if (this[i] == v) + return i; + } + return -1; } - return -1; - } - }); + }); } - if (Array.prototype.fastLastIndexOf === undefined) { - Object.defineProperty(Array.prototype, "fastLastIndexOf", { - value : function (v) { - for (var i=this.length-1; i>=0; --i) { - if (this[i] == v) - return i; + if (Array.prototype.fastLastIndexOf === undefined) + { + Object.defineProperty(Array.prototype, "fastLastIndexOf", + { + value : function (v) + { + for (var i=this.length-1; i>=0; --i) { + if (this[i] == v) + return i; + } + return -1; } - return -1; - } - }); + }); } if (Function.prototype.curry === undefined) { - Object.defineProperty(Function.prototype, "curry", { - value : function() { - var args = Array.prototype.slice.call(arguments); - var self = this; - return function() { - return self.apply(this, args.concat(Array.prototype.slice.call(arguments))); - }; + Object.defineProperty(Function.prototype, "curry", + { + value : function() + { + var args = Array.prototype.slice.call(arguments); + var self = this; + return function() + { + return self.apply(this, args.concat(Array.prototype.slice.call(arguments))); + }; - } - }); + } + }); } })(); |