chronosjs
Version:
JS Channels Mechanism
542 lines (505 loc) • 20.8 kB
JavaScript
;(function (root, factory) {
"use strict";
/* istanbul ignore if */
//<amd>
if ("function" === typeof define && define.amd) {
// AMD. Register as an anonymous module.
define("Chronos.EventsUtil", [], function () {
return factory(root, root, true);
});
return;
}
//</amd>
/* istanbul ignore next */
if ("object" === typeof exports) {
// CommonJS
factory(root, exports);
}
/* istanbul ignore next */
else {
// Browser globals
root.Chronos = root.Chronos || {};
factory(root, root.Chronos);
}
}(typeof ChronosRoot === "undefined" ? this : ChronosRoot, function (root, exports, hide) {
"use strict";
function getListeners(lstnrs, eventName, appName) {
var callBacks = [];
if (lstnrs[eventName] && lstnrs[eventName].length) {
for (var i = 0; i < lstnrs[eventName].length; i++) {
if ((!appName || "*" === lstnrs[eventName][i].appName) ||//generic event // &&
lstnrs[eventName][i].appName === appName) {//Specific events for a named instance
callBacks.push(lstnrs[eventName][i]);
}
}
}
if (lstnrs["*"]) {
for (var k = 0; k < lstnrs["*"].length; k++) {
if ((!appName || "*" === lstnrs["*"][k].appName) ||//generic event // &&
lstnrs["*"][k].appName === appName) {//Specific events for a named instance
callBacks.push(lstnrs["*"][k]);
}
}
}
return callBacks;
}
/* istanbul ignore next */
function log(msg, level, app) {
if (root && "function" === typeof root.log) {
root.log(msg, level, app);
}
}
/**
* var eventObj = {
* unbindObj: unbindObj,
* attrName: "eventName",
* loggerName: "Events",
* lstnrs: {}
* };
*/
function unbind(eventObj) {
var cmdName = eventObj.unbindObj[eventObj.attrName];
var unBound = false;
var updatedListeners;
if (!eventObj.unbindObj) {
log("CMD listen id not spec for unbind", "ERROR", eventObj.loggerName);
return null;
}
if (typeof eventObj.unbindObj === "string") {//Data is of type commandId
return _unregister(eventObj.lstnrs, eventObj.unbindObj);
}
else if (!eventObj.unbindObj.func && !eventObj.unbindObj.context && !eventObj.unbindObj.appName) {//No data passed in to help us find unbind
return false;
}
var listeners = eventObj.lstnrs;
if (cmdName) {
listeners = {};
listeners[cmdName] = eventObj.lstnrs[cmdName];
}
for (var key in listeners) {
if (listeners.hasOwnProperty(key)) {
if (listeners[key] && listeners[key].length) {
updatedListeners = _unbind(listeners[key], eventObj.unbindObj.func, eventObj.unbindObj.context, eventObj.unbindObj.appName);
if (updatedListeners.length !== listeners[key].length) {
eventObj.lstnrs[key] = updatedListeners;
unBound = true;
}
}
}
}
return unBound;
}
/**
* Clones objects and properties (everything except functions)
* @param cloneObj - the object we want to clone
* @return {Object}
*/
function cloneEventData(cloneObj) {
var resultObj = {};
if (cloneObj.constructor === Object) {//If this is an object
for (var key in cloneObj) {
//noinspection JSUnfilteredForInLoop
if (cloneObj.hasOwnProperty(key) && cloneObj[key] !== null && cloneObj[key] !== undefined) {//Make sure we have some data that's object specific
//noinspection JSUnfilteredForInLoop
if (typeof cloneObj[key] === "object" && cloneObj[key].constructor !== Array) {
//noinspection JSUnfilteredForInLoop
resultObj[key] = cloneEventData(cloneObj[key]);
}
else { //noinspection JSUnfilteredForInLoop
if (cloneObj[key].constructor === Array) {
//noinspection JSUnfilteredForInLoop
resultObj[key] = cloneObj[key].slice(0) || [];
}
else { //noinspection JSUnfilteredForInLoop
if (typeof cloneObj[key] !== "function") {
//noinspection JSUnfilteredForInLoop
resultObj[key] = cloneObj[key] !== null && cloneObj[key] !== undefined ? cloneObj[key] : "";
}
}
}
}
}
} else {//Return copy of Array or primitive type in case of no Object
if (cloneObj.constructor === Array) {
resultObj = cloneObj.slice(0) || [];
}
else if (typeof cloneObj !== "function") {
resultObj = cloneObj;
}
}
return resultObj;
}
function hasFired(fired, app, evName) {
if ((typeof (evName) === "undefined" || evName === "*") && app === "*") {
return fired;
}
var firedEvents = [];
for (var n = 0; n < fired.length; n++) {
if (fired[n].eventName === evName || evName === "*") {
if ((app && app === fired[n].appName) ||//For events specific to a caller
(!fired[n].appName || fired[n].appName === "*") || app === "*") { //For all events that don't have a specific appName
firedEvents.push(fired[n]);
}
}
}
return firedEvents;
}
/**
* Stores events so we can later ask for them, can be set to a limited store by defaults on instantiation
* @param data = {
* triggerData: triggerData,
* eventBufferLimit: eventBufferLimit,
* attrName: attrName,
* fired: fired,
* index: index
* }
*/
function storeEventData(data) {
//noinspection JSUnresolvedVariable
if (data.eventBufferLimit === 0 || (data.triggerData.data && !!data.triggerData.data.doNotStore)) {//No events should be stored or this event should not be stored
data = null;
return;
}
var firedEventData = {eventName: data.triggerData[data.attrName], appName: data.triggerData.appName};
firedEventData.data = data.triggerData.passDataByRef ? data.triggerData.data : cloneEventData(data.triggerData.data);
if (data.eventBufferLimit > 0) {//Limiting Array size to what was decided on creation
if (data.index >= data.eventBufferLimit) {
data.index = 0;
}
data.fired[data.index] = firedEventData;
data.index++;
}
else {//All events should be stored
data.fired.push(firedEventData);
}
data = null;
}
function _unregister(lstnrs, eventId) {
var unBound = false;
if (!eventId) {
log("Ev listen id not spec for unregister", "ERROR", "Events");
return null;
}
for (var eventName in lstnrs) {
if (lstnrs.hasOwnProperty(eventName)) {
for (var i = 0; i < lstnrs[eventName].length; i++) {
if (lstnrs[eventName][i].id == eventId) {
lstnrs[eventName].splice(i, 1);
log("Ev listen=" + eventId + " and name=" + eventName + " unregister", "DEBUG", "Events");
unBound = true;
break;
}
}
}
}
if (!unBound) {
log("Ev listen not found " + eventId + " unregister", "DEBUG", "Events");
}
return unBound;
}
/**
* The actual unbinding of the callbacks from the events mechanism
* @param listeners - the array of listeners that match this query
* @param func - the function we want to unbind
* @param context - the context we want to unbind
* @param appName - the specific appName we want to unbind (UID)
* @return {Array} - the new array of listeners we want to use
*/
function _unbind(listeners, func, context, appName) {
var newListeners = [];
if (listeners && listeners.length) {
for (var i = 0; i < listeners.length; i++) {
try {
var sameFunc = (!context && listeners[i].func === func);//If this fits the function and no context was passed
var sameContext = (!func && context && listeners[i].context === context);//If this fits the context and no function was passed
var sameFuncAndContext = (func && context && listeners[i].func === func && listeners[i].context === context);//if this fits the function and context
var hasSameAppName = (appName && appName === listeners[i].appName);//If we're unbinding a specific appName
var hasGlobalAppName = (listeners[i].appName === "*");//If we're unbinding a general appName
if ((sameFunc || sameContext || sameFuncAndContext)) {
if (hasSameAppName || hasGlobalAppName) {
continue;//This is a callback to remove
}
if (sameContext) {
continue;
}
}
else if (!func && !context && hasSameAppName) {//This should only happen if nothing but an appName was passed in
continue;//This is a callback to remove
}
newListeners.push(listeners[i]);//This is callbacks we keep
} catch (err) {
log("Error in unbind e=" + err.message, "ERROR", "Events");
}
}
}
return newListeners;
}
// attach properties to the exports object to define
// the exported module properties.
var ret = {
getListeners: getListeners,
log: log,
unbind: unbind,
hasFired: hasFired,
cloneEventData: cloneEventData,
storeEventData: storeEventData
};
if (!hide) {
exports.EventsUtil = exports.EventsUtil || ret;
}
return ret;
}));
;(function (root, factory) {
"use strict";
/* istanbul ignore if */
//<amd>
if ("function" === typeof define && define.amd) {
// AMD. Register as an anonymous module.
define("Chronos.CommandsUtil", ["Chronos.EventsUtil"], function (EventsUtil) {
return factory(root, root, EventsUtil, true);
});
return;
}
//</amd>
/* istanbul ignore next */
if ("object" === typeof exports) {
// CommonJS
factory(root, exports, require("./EventsUtil").EventsUtil);
}
/* istanbul ignore next */
else {
/**
* @depend ./EventsUtil.js
*/
// Browser globals
root.Chronos = root.Chronos || {};
factory(root, root.Chronos, root.Chronos.EventsUtil);
}
}(typeof ChronosRoot === "undefined" ? this : ChronosRoot, function (root, exports, evUtil, hide) {
"use strict";
/**
* var cmdObj = {
* cmd: cmd,
* attrName: "cmdName",
* loggerName: "Commands",
* prefix: "_somePrefix",
* id: commandId,
* lstnrs: {}
* };
*/
function bind(cmdObj) {
var cmdName = cmdObj.cmd[cmdObj.attrName];
if (!cmdName || !cmdObj.cmd.func || "function" !== typeof cmdObj.cmd.func || !valid(cmdObj.cmd, cmdName)) {
evUtil.log("comply: has invalid params: command=[" + cmdName + "]", "ERROR", cmdObj.loggerName);
return null;
}
if (cmdObj.lstnrs[cmdName] && cmdObj.lstnrs[cmdName].length) {
evUtil.log("comply: cannot comply because command already exist command=" + cmdName, "ERROR", cmdObj.loggerName);
return null;
}
var cmdId = cmdObj.prefix + (cmdObj.id++);
var newObj = {
id: cmdId,
func: cmdObj.cmd.func,
context: cmdObj.cmd.context || null,
appName: cmdObj.cmd.appName
};
cmdObj.lstnrs[cmdName] = cmdObj.lstnrs[cmdName] || [];
cmdObj.lstnrs[cmdName].push(newObj);
evUtil.log("Cmd comply: evName=[" + cmdName + "] appName=" + newObj.appName, "DEBUG", cmdObj.loggerName);
return cmdId;
}
function valid(cmd, name) {
return !((name && name === "*") || (cmd.appName && cmd.appName === "*"));
}
// attach properties to the exports object to define
// the exported module properties.
var ret = {
bind: bind,
valid: valid
};
if (!hide) {
exports.CommandsUtil = exports.CommandsUtil || ret;
}
return ret;
}));
;(function (root, factory) {
"use strict";
/* istanbul ignore if */
//<amd>
if ("function" === typeof define && define.amd) {
// AMD. Register as an anonymous module.
define("Chronos.Commands", ["Chronos.EventsUtil", "Chronos.CommandsUtil"], function (EventsUtil, CommandsUtil) {
return factory(root, root, EventsUtil, CommandsUtil, true);
});
return;
}
//</amd>
/* istanbul ignore next */
if ("object" === typeof exports) {
// CommonJS
factory(root, exports, require("./util/EventsUtil").EventsUtil, require("./util/CommandsUtil").CommandsUtil);
}
/* istanbul ignore next */
else {
/**
* @depend ./util/EventsUtil.js
* @depend ./util/CommandsUtil.js
*/
// Browser globals
root.Chronos = root.Chronos || {};
factory(root, root.Chronos, root.Chronos.EventsUtil, root.Chronos.CommandsUtil);
}
}(typeof ChronosRoot === "undefined" ? this : ChronosRoot, function (root, exports, evUtil, cmdUtil, hide) {
"use strict";
function Commands(defaults) {
var appName = "Commands",
attrName = "cmdName",
commandId = 0,
commands = {},
fired = [],
prefix = "cmdId_",
indexer = 0,
cloneData,
eventBufferLimit,
defaultAppName;
defaultAppName = defaults && defaults.appName || "*";
cloneData = (defaults && typeof defaults.cloneEventData === "boolean" ? defaults.cloneEventData : false);
eventBufferLimit = (defaults && !isNaN(defaults.eventBufferLimit) ? defaults.eventBufferLimit : -1);
/**
* This function allows registering for command with the following structure:
* @param cmd = {
* cmdName: string that is the name of the event that will be triggered like 'get'
* appName: string that specifies an added identifier for multiple instances of the same event name (click by button1, click by button 2)
* func: function - the callback function which the event data will be passed to
* context: the context which the event data will be run with
* }
*
* @return {String} - command Id.
*/
function comply(cmd) {
if ("*" !== defaultAppName) {
cmd.appName = cmd.appName || defaultAppName;
}
return cmdUtil.bind({
cmd: cmd,
attrName: attrName,
loggerName: appName,
prefix: prefix,
id: commandId,
lstnrs: commands
});
}
/**
* This function allows unbinding according to a permutation of the three parameters
* @param unbindObj
* cmdName - the eventName you want to unbind
* func - the pointer to the function you want to unbind
* context - the context you want to unbind
* appName - the specific appName we want to unbind
* OR - commandId
* @return {Boolean} - has stopped complying.
*/
function stopComplying(unbindObj) {
if ("*" !== defaultAppName) {
unbindObj.appName = unbindObj.appName || defaultAppName;
}
return evUtil.unbind({
unbindObj: unbindObj,
attrName: attrName,
loggerName: appName,
lstnrs: commands
});
}
/**
* firedEventData can pass two request parameters
* @param app name
* @param cmdName = command name
* @return {Array}
*/
function hasFired(app, cmdName) {
if ("undefined" === typeof cmdName) {
cmdName = app;
app = defaultAppName;
}
return evUtil.hasFired(fired, app, cmdName);
}
/**
* This triggers a command
* @param cmd = {
* cmdName - the name of the command triggered
* appName - optional specifies the identifier it is bound to
* passDataByRef: boolean flag whether this callback will get the reference information of the event or a copy (this allows control of data manipulation)
* data - optional event parameters to be passed to the listeners
* }
*
* @param cb - optional callback to notify when finished
* @return {*}
*/
function command(cmd, cb) {
if (!cmd || typeof (cmd.cmdName) === "undefined" || !cmdUtil.valid(cmd, cmd.cmdName)) {
evUtil.log("CMD name not spec for command", "ERROR", "Commands");
return null;
}
if ("*" !== defaultAppName) {
cmd.appName = cmd.appName || defaultAppName;
}
cmd.passDataByRef = cmd.passDataByRef || !cloneData;
_storeEventData(cmd);
if (!commands[cmd.cmdName]) {
return false;
}
var callBacks = evUtil.getListeners(commands, cmd.cmdName, cmd.appName);
if (callBacks.length > 0) {
for (var j = 0; j < callBacks.length; j++) {
var cmdData = cmd.passDataByRef ? cmd.data : evUtil.cloneEventData(cmd.data);//Clone the event data if there was not an explicit request to passByRef
var callBack = callBacks[j];
try {
if ("function" === typeof cb) {
callBack.func.call(callBack.context, cmdData, cb);
} else {
callBack.func.call(callBack.context, cmdData);
}
cmdData = null;//Delete local pointer
callBack = null;
} catch (err) {
if ("function" === typeof cb) {
try {
cb(err);
} catch (e) {
evUtil.log("Error executing callback on error, " +cmd.cmdName + " commandId: " + callBack.id + "e=" + e.message, "ERROR", "Commands");
}
}
//noinspection JSUnresolvedVariable
evUtil.log("Error executing " + cmd.cmdName + " commandId: " + callBack.id + "e=" + err.message, "ERROR", "Commands");
}
}
}
return (callBacks.length > 0);
}
//------------------- Private methods ------------------------------//
/**
* Stores commands so we can later ask for them, can be set to a limited store by defaults on instantiation
* @param triggerData
*/
function _storeEventData(triggerData) {
evUtil.storeEventData({
triggerData: triggerData,
eventBufferLimit: eventBufferLimit,
attrName: attrName,
fired: fired,
index: indexer
});
}
this.hasFired = hasFired;
this.comply = comply;
this.stopComplying = stopComplying;
this.command = command;
}
// attach properties to the exports object to define
// the exported module properties.
if (!hide) {
exports.Commands = exports.Commands || Commands;
}
return Commands;
}));