From 56cae88fe49659bbe5ffa9f4a7e8e577121b9a52 Mon Sep 17 00:00:00 2001 From: Kris Siegel Date: Sun, 11 Oct 2015 01:57:10 -0400 Subject: [PATCH] Fixed issue where registered handled of less components wouldn't be hit by emits with more components Added array deduping method with documentation --- docs/api.md | 12 +++- msngr.js | 130 +++++++++++++---------------------- msngr.min.js | 2 +- package.json | 4 +- src/main.js | 2 +- src/objects/memory.js | 115 +++++++++---------------------- src/objects/message.aspec.js | 20 ++++++ src/utils/misc.aspec.js | 8 +++ src/utils/misc.js | 13 ++++ 9 files changed, 138 insertions(+), 168 deletions(-) diff --git a/docs/api.md b/docs/api.md index d2d0002..3c941d1 100644 --- a/docs/api.md +++ b/docs/api.md @@ -449,7 +449,17 @@ Removes a value from an array. Optimized for better performance in larger arrays var myArray = [5, 17, 42, 97]; console.log(myArray); // Outputs [5, 17, 42, 97] msngr.removeFromArray(myArray, 42); -console.log(myArray); // Outputs [5, 17, 97]; +console.log(myArray); // Outputs [5, 17, 97] +``` + +### ```msngr.deDupeArray(arr)``` +Removes duplicates from an array and returns the resulting, deduped array. + +```arr (required)``` - the array to dedupe. + +```javascript +var myArray = [5, 17, 42, 56, 42, 56, 42, 97]; +console.log(msngr.deDupeArray(myArray)); // Outputs [5, 17, 42, 56, 97] ``` ### ```msngr.argumentsToArray(args)``` diff --git a/msngr.js b/msngr.js index f0a791c..4ed7b39 100644 --- a/msngr.js +++ b/msngr.js @@ -18,7 +18,7 @@ var msngr = msngr || (function() { return internal.objects.message(topic, category, subcategory); }; - external.version = "2.3.0"; + external.version = "2.4.0"; // Merge two inputs into one var twoMerge = function(input1, input2) { @@ -370,6 +370,19 @@ msngr.extend((function(external, internal) { arr[inx] = temp; } arr.pop(); + }, + deDupeArray: function (arr) { + var hash = { }; + var result = []; + var arrLength = arr.length; + for (var i = 0; i < arrLength; ++i) { + if (hash[arr[i]] === undefined) { + hash[arr[i]] = true; + result.push(arr[i]); + } + } + + return result; } }; })); @@ -560,12 +573,7 @@ msngr.extend((function(external, internal) { var id_to_message = {}; // Direct index (no partials) for message - var direct_index = { - topic_to_id: {}, - topic_cat_to_id: {}, - topic_scat_to_id: {}, - topic_cat_scat_to_id: {} - }; + var index = { }; // Message index count var index_count = 0; @@ -576,49 +584,37 @@ msngr.extend((function(external, internal) { var uuid = external.id(); id_to_message[uuid] = message; - if (direct_index.topic_to_id[message.topic] === undefined) { - direct_index.topic_to_id[message.topic] = []; + if (!external.exist(index[message.topic])) { + index[message.topic] = { + uuids: [], + category: { } + }; } - direct_index.topic_to_id[message.topic].push(uuid); - if (external.exist(message.category)) { - if (direct_index.topic_cat_to_id[message.topic] === undefined) { - direct_index.topic_cat_to_id[message.topic] = {}; + if (!external.exist(index[message.topic].category[message.category])) { + index[message.topic].category[message.category] = { + uuids: [], + subcategory: { } } + } - if (direct_index.topic_cat_to_id[message.topic][message.category] === undefined) { - direct_index.topic_cat_to_id[message.topic][message.category] = []; + if (!external.exist(index[message.topic].category[message.category].subcategory[message.subcategory])) { + index[message.topic].category[message.category].subcategory[message.subcategory] = { + uuids: [] } - - direct_index.topic_cat_to_id[message.topic][message.category].push(uuid); } - if (external.exist(message.subcategory)) { - if (direct_index.topic_scat_to_id[message.topic] === undefined) { - direct_index.topic_scat_to_id[message.topic] = {}; - } - if (direct_index.topic_scat_to_id[message.topic][message.subcategory] === undefined) { - direct_index.topic_scat_to_id[message.topic][message.subcategory] = []; - } + if (!external.exist(message.category) && !external.exist(message.subcategory)) { + index[message.topic].uuids.push(uuid); + } - direct_index.topic_scat_to_id[message.topic][message.subcategory].push(uuid); + if (external.exist(message.category) && !external.exist(message.subcategory)) { + index[message.topic].category[message.category].uuids.push(uuid); } if (external.exist(message.category) && external.exist(message.subcategory)) { - if (direct_index.topic_cat_scat_to_id[message.topic] === undefined) { - direct_index.topic_cat_scat_to_id[message.topic] = {}; - } - - if (direct_index.topic_cat_scat_to_id[message.topic][message.category] === undefined) { - direct_index.topic_cat_scat_to_id[message.topic][message.category] = {}; - } - - if (direct_index.topic_cat_scat_to_id[message.topic][message.category][message.subcategory] === undefined) { - direct_index.topic_cat_scat_to_id[message.topic][message.category][message.subcategory] = []; - } - - direct_index.topic_cat_scat_to_id[message.topic][message.category][message.subcategory].push(uuid); + index[message.topic].category[message.category].subcategory[message.subcategory].uuids.push(uuid); } index_count++; @@ -631,21 +627,9 @@ msngr.extend((function(external, internal) { if (external.exist(uuid) && external.exist(id_to_message[uuid])) { var message = id_to_message[uuid]; - if (external.exist(message.topic)) { - external.removeFromArray(direct_index.topic_to_id[message.topic], uuid); - - if (external.exist(message.category)) { - external.removeFromArray(direct_index.topic_cat_to_id[message.topic][message.category], uuid); - } - - if (external.exist(message.subcategory)) { - external.removeFromArray(direct_index.topic_scat_to_id[message.topic][message.subcategory], uuid); - } - - if (external.exist(message.category) && external.exist(message.subcategory)) { - external.removeFromArray(direct_index.topic_cat_scat_to_id[message.topic][message.category][message.subcategory], uuid); - } - } + external.removeFromArray(index[message.topic].uuids, uuid); + external.removeFromArray(index[message.topic].category[message.category].uuids, uuid); + external.removeFromArray(index[message.topic].category[message.category].subcategory[message.subcategory].uuids, uuid); delete id_to_message[uuid]; index_count--; @@ -655,43 +639,25 @@ msngr.extend((function(external, internal) { return false; }, query: function(message) { - if (external.exist(message)) { - if (external.exist(message.topic)) { - // Topic Only Results - if (!external.exist(message.category) && !external.exist(message.subcategory)) { - return direct_index.topic_to_id[message.topic] || []; - } - - // Topic + Category Results - if (external.exist(message.category) && !external.exist(message.subcategory)) { - return (direct_index.topic_cat_to_id[message.topic] || {})[message.category] || []; - } - - // Topic + Data Type Results - if (external.exist(message.subcategory) && !external.exist(message.category)) { - return (direct_index.topic_scat_to_id[message.topic] || {})[message.subcategory] || []; - } - - // Topic + Category + Data Type Results - if (external.exist(message.category) && external.exist(message.subcategory)) { - return ((direct_index.topic_cat_scat_to_id[message.topic] || {})[message.category] || {})[message.subcategory] || []; - } - } + var result = []; + if (external.exist(message) && external.exist(message.topic) && external.exist(index[message.topic])) { + var indexTopic = index[message.topic]; + var indexTopicCategory = ((indexTopic || { }).category || { })[message.category]; + var indexTopicCategorySubcategory = ((indexTopicCategory || { }).subcategory || { })[message.subcategory]; + + result = result.concat(indexTopic.uuids || []); + result = result.concat((indexTopicCategory || { }).uuids || []); + result = result.concat((indexTopicCategorySubcategory || { }).uuids || []); } - return []; + return external.deDupeArray(result); }, clear: function() { // Index for id to message objects id_to_message = {}; // Direct index (no partials) for message - direct_index = { - topic_to_id: {}, - topic_cat_to_id: {}, - topic_scat_to_id: {}, - topic_cat_scat_to_id: {} - }; + index = { }; index_count = 0; diff --git a/msngr.min.js b/msngr.min.js index 70f66bc..f9c0a4f 100644 --- a/msngr.min.js +++ b/msngr.min.js @@ -1 +1 @@ -var msngr=msngr||function(){"use strict";var internal={globalOptions:{},warnings:!0},external=function(topic,category,subcategory){return internal.objects.message(topic,category,subcategory)};external.version="2.3.0";var twoMerge=function(input1,input2){if(void 0===input1||null===input1)return input2;if(void 0===input2||null===input2)return input1;var result,type1=Object.prototype.toString.call(input1),type2=Object.prototype.toString.call(input2);if("[object Object]"===type1&&"[object Object]"===type2){result={};for(var key in input1)input1.hasOwnProperty(key)&&(result[key]=input1[key]);for(var key in input2)if(input2.hasOwnProperty(key))if("[object Object]"===Object.prototype.toString.call(input2[key]))void 0===result[key]&&(result[key]={}),result[key]=external.merge(input1[key],input2[key]);else if("[object Array]"===Object.prototype.toString.call(input1[key])&&"[object Array]"===Object.prototype.toString.call(input2[key])){result[key]=input1[key]||[];for(var i=0;i0)for(var i=0;i0?elms[0]:elms},findElements:function(selector,root){var elm;if(external.isHtmlElement(selector)&&(elm=selector),void 0===elm&&external.isString(selector)){var doc=root||document,result=doc.querySelectorAll(selector);null!==result&&(elm=result)}return elm},getDomPath:function(element){var node=external.isHtmlElement(element)?element:void 0;return void 0===node?void 0:(void 0===node.id&&(node.id=external.id()),"#"+node.id)},querySelectorAllWithEq:function(selector,root){if(void 0===selector)return null;for(var doc=root||document,queue=[],process=function(input){if(-1===input.indexOf(":eq("))return void 0;var eqlLoc=input.indexOf(":eq("),sel=input.substring(0,eqlLoc),ind=input.substring(eqlLoc+4,input.indexOf(")",eqlLoc));selector=input.substring(input.indexOf(")",eqlLoc)+1,input.length),">"===sel.charAt(0)&&(sel=sel.substring(1,sel.length)),">"===selector.charAt(0)&&(selector=selector.substring(1,selector.length)),queue.push({selector:sel,index:parseInt(ind,10)})};-1!==selector.indexOf(":eq");)process(selector);for(var result;queue.length>0;){var item=queue.shift();result=(result||doc).querySelectorAll(item.selector)[item.index]}return selector.trim().length>0?(result||doc).querySelectorAll(selector):[result]},querySelectorWithEq:function(selector,root){return external.querySelectorAllWithEq(selector,root)[0]}}}),msngr.extend(function(external,internal){"use strict";return internal.InvalidParametersException=function(str){return{name:"InvalidParametersException",severity:"unrecoverable",message:"Invalid parameters supplied to the {method} method".replace("{method}",str)}},internal.ReservedKeywordsException=function(keyword){return{name:"ReservedKeywordsException",severity:"unrecoverable",message:"Reserved keyword {keyword} supplied as action.".replace("{keyword}",keyword)}},internal.MangledException=function(variable,method){return{name:"MangledException",severity:"unrecoverable",message:"The {variable} was unexpectedly mangled in {method}.".replace("{variable}",variable).replace("{method}",method)}},{}}),msngr.extend(function(external,internal){"use strict";var nowPerformance=function(){return performance.now()},nowNode=function(){return process.hrtime()[1]/1e6},nowLegacy=function(){return(new Date).getTime()},nowExec=void 0,nowExecDebugLabel="",lastNow=void 0;return{id:function(){var d=external.now(),uuid="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(c){var r=(d+16*Math.random())%16|0;return d=Math.floor(d/16),("x"==c?r:3&r|8).toString(16)});return uuid},now:function(noDuplicate){void 0===nowExec&&("undefined"!=typeof performance?(nowExec=nowPerformance,nowExecDebugLabel="performance"):"undefined"!=typeof process?(nowExec=nowNode,nowExecDebugLabel="node"):(nowExec=nowLegacy,nowExecDebugLabel="legacy"));var now=nowExec();return noDuplicate===!0&&lastNow===now?external.now(noDuplicate):(lastNow=now,now)},removeFromArray:function(arr,value){var inx=arr.indexOf(value),endIndex=arr.length-1;if(inx!==endIndex){var temp=arr[endIndex];arr[endIndex]=arr[inx],arr[inx]=temp}arr.pop()}}}),msngr.extend(function(external,internal){"use strict";return internal.reiterativeValidation=function(validationMethod,inputs){var result=!1;if(external.exist(validationMethod)&&external.exist(inputs)){external.isArray(inputs)||(inputs=[inputs]);for(var i=0;i0)for(var i=0;i0)for(var i=0;i0)for(var i=0;ii;++i){var found=external.findElements(selectors[i],doc);found.length>0&&(elements=elements.concat(Array.prototype.slice.call(found)))}if(0===elements.length)return void 0;for(var resultMap=void 0,elmLength=elements.length,unnamedTags=0,i=0;elmLength>i;++i){var key=void 0,elm=elements[i],nameAttr=elm.getAttribute("name"),idAttr=elm.id,tagName=elm.tagName.toLowerCase(),val=elm.value;external.exist(nameAttr)&&!external.isEmptyString(nameAttr)?key=nameAttr:external.exist(idAttr)&&!external.isEmptyString(idAttr)?key=idAttr:(key=tagName+unnamedTags,unnamedTags++),void 0===resultMap&&(resultMap={}),resultMap[key]=val}return resultMap},{}}),"undefined"!=typeof module&&"undefined"!=typeof module.exports&&(module.exports=msngr); \ No newline at end of file +var msngr=msngr||function(){"use strict";var internal={globalOptions:{},warnings:!0},external=function(topic,category,subcategory){return internal.objects.message(topic,category,subcategory)};external.version="2.4.0";var twoMerge=function(input1,input2){if(void 0===input1||null===input1)return input2;if(void 0===input2||null===input2)return input1;var result,type1=Object.prototype.toString.call(input1),type2=Object.prototype.toString.call(input2);if("[object Object]"===type1&&"[object Object]"===type2){result={};for(var key in input1)input1.hasOwnProperty(key)&&(result[key]=input1[key]);for(var key in input2)if(input2.hasOwnProperty(key))if("[object Object]"===Object.prototype.toString.call(input2[key]))void 0===result[key]&&(result[key]={}),result[key]=external.merge(input1[key],input2[key]);else if("[object Array]"===Object.prototype.toString.call(input1[key])&&"[object Array]"===Object.prototype.toString.call(input2[key])){result[key]=input1[key]||[];for(var i=0;i0)for(var i=0;i0?elms[0]:elms},findElements:function(selector,root){var elm;if(external.isHtmlElement(selector)&&(elm=selector),void 0===elm&&external.isString(selector)){var doc=root||document,result=doc.querySelectorAll(selector);null!==result&&(elm=result)}return elm},getDomPath:function(element){var node=external.isHtmlElement(element)?element:void 0;return void 0===node?void 0:(void 0===node.id&&(node.id=external.id()),"#"+node.id)},querySelectorAllWithEq:function(selector,root){if(void 0===selector)return null;for(var doc=root||document,queue=[],process=function(input){if(-1===input.indexOf(":eq("))return void 0;var eqlLoc=input.indexOf(":eq("),sel=input.substring(0,eqlLoc),ind=input.substring(eqlLoc+4,input.indexOf(")",eqlLoc));selector=input.substring(input.indexOf(")",eqlLoc)+1,input.length),">"===sel.charAt(0)&&(sel=sel.substring(1,sel.length)),">"===selector.charAt(0)&&(selector=selector.substring(1,selector.length)),queue.push({selector:sel,index:parseInt(ind,10)})};-1!==selector.indexOf(":eq");)process(selector);for(var result;queue.length>0;){var item=queue.shift();result=(result||doc).querySelectorAll(item.selector)[item.index]}return selector.trim().length>0?(result||doc).querySelectorAll(selector):[result]},querySelectorWithEq:function(selector,root){return external.querySelectorAllWithEq(selector,root)[0]}}}),msngr.extend(function(external,internal){"use strict";return internal.InvalidParametersException=function(str){return{name:"InvalidParametersException",severity:"unrecoverable",message:"Invalid parameters supplied to the {method} method".replace("{method}",str)}},internal.ReservedKeywordsException=function(keyword){return{name:"ReservedKeywordsException",severity:"unrecoverable",message:"Reserved keyword {keyword} supplied as action.".replace("{keyword}",keyword)}},internal.MangledException=function(variable,method){return{name:"MangledException",severity:"unrecoverable",message:"The {variable} was unexpectedly mangled in {method}.".replace("{variable}",variable).replace("{method}",method)}},{}}),msngr.extend(function(external,internal){"use strict";var nowPerformance=function(){return performance.now()},nowNode=function(){return process.hrtime()[1]/1e6},nowLegacy=function(){return(new Date).getTime()},nowExec=void 0,nowExecDebugLabel="",lastNow=void 0;return{id:function(){var d=external.now(),uuid="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(c){var r=(d+16*Math.random())%16|0;return d=Math.floor(d/16),("x"==c?r:3&r|8).toString(16)});return uuid},now:function(noDuplicate){void 0===nowExec&&("undefined"!=typeof performance?(nowExec=nowPerformance,nowExecDebugLabel="performance"):"undefined"!=typeof process?(nowExec=nowNode,nowExecDebugLabel="node"):(nowExec=nowLegacy,nowExecDebugLabel="legacy"));var now=nowExec();return noDuplicate===!0&&lastNow===now?external.now(noDuplicate):(lastNow=now,now)},removeFromArray:function(arr,value){var inx=arr.indexOf(value),endIndex=arr.length-1;if(inx!==endIndex){var temp=arr[endIndex];arr[endIndex]=arr[inx],arr[inx]=temp}arr.pop()},deDupeArray:function(arr){for(var hash={},result=[],arrLength=arr.length,i=0;arrLength>i;++i)void 0===hash[arr[i]]&&(hash[arr[i]]=!0,result.push(arr[i]));return result}}}),msngr.extend(function(external,internal){"use strict";return internal.reiterativeValidation=function(validationMethod,inputs){var result=!1;if(external.exist(validationMethod)&&external.exist(inputs)){external.isArray(inputs)||(inputs=[inputs]);for(var i=0;i0)for(var i=0;i0)for(var i=0;i0)for(var i=0;ii;++i){var found=external.findElements(selectors[i],doc);found.length>0&&(elements=elements.concat(Array.prototype.slice.call(found)))}if(0===elements.length)return void 0;for(var resultMap=void 0,elmLength=elements.length,unnamedTags=0,i=0;elmLength>i;++i){var key=void 0,elm=elements[i],nameAttr=elm.getAttribute("name"),idAttr=elm.id,tagName=elm.tagName.toLowerCase(),val=elm.value;external.exist(nameAttr)&&!external.isEmptyString(nameAttr)?key=nameAttr:external.exist(idAttr)&&!external.isEmptyString(idAttr)?key=idAttr:(key=tagName+unnamedTags,unnamedTags++),void 0===resultMap&&(resultMap={}),resultMap[key]=val}return resultMap},{}}),"undefined"!=typeof module&&"undefined"!=typeof module.exports&&(module.exports=msngr); \ No newline at end of file diff --git a/package.json b/package.json index b7da087..9a0025b 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "msngr", "main": "msngr.js", "description": "msngr.js is a small library used to facilitate communication through messages rather than direct binding. This loose coupling allows connecting components to each other or to UI components in an abstract way on the server or the client.", - "version": "2.3.0", + "version": "2.4.0", "keywords": ["message", "messaging", "subscription", "delegation", "eventing", "dom", "binding"], "repository": { "type": "git", @@ -22,7 +22,7 @@ "grunt-mocha-test": "0.12.7", "mocha": "2.3.3", "grunt-mocha-phantomjs": "2.0.0", - "grunt-available-tasks": "0.6.0", + "grunt-available-tasks": "0.6.1", "chai": "3.3.0" }, "scripts": { diff --git a/src/main.js b/src/main.js index 257f274..ca7c012 100644 --- a/src/main.js +++ b/src/main.js @@ -18,7 +18,7 @@ var msngr = msngr || (function() { return internal.objects.message(topic, category, subcategory); }; - external.version = "2.3.0"; + external.version = "2.4.0"; // Merge two inputs into one var twoMerge = function(input1, input2) { diff --git a/src/objects/memory.js b/src/objects/memory.js index eb3e37b..4f12672 100644 --- a/src/objects/memory.js +++ b/src/objects/memory.js @@ -8,12 +8,7 @@ msngr.extend((function(external, internal) { var id_to_message = {}; // Direct index (no partials) for message - var direct_index = { - topic_to_id: {}, - topic_cat_to_id: {}, - topic_scat_to_id: {}, - topic_cat_scat_to_id: {} - }; + var index = { }; // Message index count var index_count = 0; @@ -24,49 +19,37 @@ msngr.extend((function(external, internal) { var uuid = external.id(); id_to_message[uuid] = message; - if (direct_index.topic_to_id[message.topic] === undefined) { - direct_index.topic_to_id[message.topic] = []; + if (!external.exist(index[message.topic])) { + index[message.topic] = { + uuids: [], + category: { } + }; } - direct_index.topic_to_id[message.topic].push(uuid); - if (external.exist(message.category)) { - if (direct_index.topic_cat_to_id[message.topic] === undefined) { - direct_index.topic_cat_to_id[message.topic] = {}; + if (!external.exist(index[message.topic].category[message.category])) { + index[message.topic].category[message.category] = { + uuids: [], + subcategory: { } } + } - if (direct_index.topic_cat_to_id[message.topic][message.category] === undefined) { - direct_index.topic_cat_to_id[message.topic][message.category] = []; + if (!external.exist(index[message.topic].category[message.category].subcategory[message.subcategory])) { + index[message.topic].category[message.category].subcategory[message.subcategory] = { + uuids: [] } - - direct_index.topic_cat_to_id[message.topic][message.category].push(uuid); } - if (external.exist(message.subcategory)) { - if (direct_index.topic_scat_to_id[message.topic] === undefined) { - direct_index.topic_scat_to_id[message.topic] = {}; - } - if (direct_index.topic_scat_to_id[message.topic][message.subcategory] === undefined) { - direct_index.topic_scat_to_id[message.topic][message.subcategory] = []; - } + if (!external.exist(message.category) && !external.exist(message.subcategory)) { + index[message.topic].uuids.push(uuid); + } - direct_index.topic_scat_to_id[message.topic][message.subcategory].push(uuid); + if (external.exist(message.category) && !external.exist(message.subcategory)) { + index[message.topic].category[message.category].uuids.push(uuid); } if (external.exist(message.category) && external.exist(message.subcategory)) { - if (direct_index.topic_cat_scat_to_id[message.topic] === undefined) { - direct_index.topic_cat_scat_to_id[message.topic] = {}; - } - - if (direct_index.topic_cat_scat_to_id[message.topic][message.category] === undefined) { - direct_index.topic_cat_scat_to_id[message.topic][message.category] = {}; - } - - if (direct_index.topic_cat_scat_to_id[message.topic][message.category][message.subcategory] === undefined) { - direct_index.topic_cat_scat_to_id[message.topic][message.category][message.subcategory] = []; - } - - direct_index.topic_cat_scat_to_id[message.topic][message.category][message.subcategory].push(uuid); + index[message.topic].category[message.category].subcategory[message.subcategory].uuids.push(uuid); } index_count++; @@ -79,21 +62,9 @@ msngr.extend((function(external, internal) { if (external.exist(uuid) && external.exist(id_to_message[uuid])) { var message = id_to_message[uuid]; - if (external.exist(message.topic)) { - external.removeFromArray(direct_index.topic_to_id[message.topic], uuid); - - if (external.exist(message.category)) { - external.removeFromArray(direct_index.topic_cat_to_id[message.topic][message.category], uuid); - } - - if (external.exist(message.subcategory)) { - external.removeFromArray(direct_index.topic_scat_to_id[message.topic][message.subcategory], uuid); - } - - if (external.exist(message.category) && external.exist(message.subcategory)) { - external.removeFromArray(direct_index.topic_cat_scat_to_id[message.topic][message.category][message.subcategory], uuid); - } - } + external.removeFromArray(index[message.topic].uuids, uuid); + external.removeFromArray(index[message.topic].category[message.category].uuids, uuid); + external.removeFromArray(index[message.topic].category[message.category].subcategory[message.subcategory].uuids, uuid); delete id_to_message[uuid]; index_count--; @@ -103,43 +74,25 @@ msngr.extend((function(external, internal) { return false; }, query: function(message) { - if (external.exist(message)) { - if (external.exist(message.topic)) { - // Topic Only Results - if (!external.exist(message.category) && !external.exist(message.subcategory)) { - return direct_index.topic_to_id[message.topic] || []; - } - - // Topic + Category Results - if (external.exist(message.category) && !external.exist(message.subcategory)) { - return (direct_index.topic_cat_to_id[message.topic] || {})[message.category] || []; - } - - // Topic + Data Type Results - if (external.exist(message.subcategory) && !external.exist(message.category)) { - return (direct_index.topic_scat_to_id[message.topic] || {})[message.subcategory] || []; - } - - // Topic + Category + Data Type Results - if (external.exist(message.category) && external.exist(message.subcategory)) { - return ((direct_index.topic_cat_scat_to_id[message.topic] || {})[message.category] || {})[message.subcategory] || []; - } - } + var result = []; + if (external.exist(message) && external.exist(message.topic) && external.exist(index[message.topic])) { + var indexTopic = index[message.topic]; + var indexTopicCategory = ((indexTopic || { }).category || { })[message.category]; + var indexTopicCategorySubcategory = ((indexTopicCategory || { }).subcategory || { })[message.subcategory]; + + result = result.concat(indexTopic.uuids || []); + result = result.concat((indexTopicCategory || { }).uuids || []); + result = result.concat((indexTopicCategorySubcategory || { }).uuids || []); } - return []; + return external.deDupeArray(result); }, clear: function() { // Index for id to message objects id_to_message = {}; // Direct index (no partials) for message - direct_index = { - topic_to_id: {}, - topic_cat_to_id: {}, - topic_scat_to_id: {}, - topic_cat_scat_to_id: {} - }; + index = { }; index_count = 0; diff --git a/src/objects/message.aspec.js b/src/objects/message.aspec.js index 0459029..df27e2f 100644 --- a/src/objects/message.aspec.js +++ b/src/objects/message.aspec.js @@ -174,6 +174,26 @@ describe("./objects/message.js", function() { msg.emit("WeePayloads!"); }); + it("msngr().emit() / on() - Successfully emits topic, category to a handler set to handle topic", function(done) { + msngr("MyTopic").on(function(payload) { + expect(payload).to.exist; + expect(payload).to.equal("WeePayloads!"); + done(); + }); + + msngr("MyTopic", "MyCategory").emit("WeePayloads!"); + }); + + it("msngr().emit() / on() - Successfully emits topic, category and subcategory to a handler set to handle topic and category", function(done) { + msngr("MyTopic", "MyCategory").on(function(payload) { + expect(payload).to.exist; + expect(payload).to.equal("WeePayloads!"); + done(); + }); + + msngr("MyTopic", "MyCategory", "MySubcategory").emit("WeePayloads!"); + }); + it("msngr().emit() / on() - Setup three handlers, both receive emit payload", function(done) { var handled = 0; diff --git a/src/utils/misc.aspec.js b/src/utils/misc.aspec.js index d0faa63..1066823 100644 --- a/src/utils/misc.aspec.js +++ b/src/utils/misc.aspec.js @@ -86,4 +86,12 @@ describe("./utils/misc.js", function() { expect(arr[1]).to.equal("weee"); expect(arr.length).to.equal(2); }); + + it("msngr.deDupeArray() - removes duplicates from an array", function() { + var arr1 = [4,5,6,6,6,6,6,6,6,6,7]; + expect(msngr.deDupeArray(arr1).length).to.equal(4); + + var arr2 = ["yeah", "oh", "nice", "chips", "chips", "chips"]; + expect(msngr.deDupeArray(arr2).length).to.equal(4); + }); }); diff --git a/src/utils/misc.js b/src/utils/misc.js index 12317bd..da7cbfc 100644 --- a/src/utils/misc.js +++ b/src/utils/misc.js @@ -56,6 +56,19 @@ msngr.extend((function(external, internal) { arr[inx] = temp; } arr.pop(); + }, + deDupeArray: function (arr) { + var hash = { }; + var result = []; + var arrLength = arr.length; + for (var i = 0; i < arrLength; ++i) { + if (hash[arr[i]] === undefined) { + hash[arr[i]] = true; + result.push(arr[i]); + } + } + + return result; } }; }));