Skip to content

Commit

Permalink
Merge pull request #35 from KrisSiegel/2.4.0
Browse files Browse the repository at this point in the history
Fixed messaging handling and added a utility
  • Loading branch information
KrisSiegel committed Oct 11, 2015
2 parents efec13f + 56cae88 commit dcdfc3c
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 168 deletions.
12 changes: 11 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)```
Expand Down
130 changes: 48 additions & 82 deletions msngr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
};
}));
Expand Down Expand Up @@ -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;
Expand All @@ -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++;
Expand All @@ -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--;
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion msngr.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit dcdfc3c

Please sign in to comment.