Skip to content

Instantly share code, notes, and snippets.

@bbalakriz
Created May 8, 2020 06:17
Show Gist options
  • Select an option

  • Save bbalakriz/1efb2ba0b477282292070eaf5523ee2e to your computer and use it in GitHub Desktop.

Select an option

Save bbalakriz/1efb2ba0b477282292070eaf5523ee2e to your computer and use it in GitHub Desktop.
results.html
$(function () {
var tags = [];
var source = new EventSource("/hashtags/stream");
source.onmessage = function (event) {
// Parse hashtag & weight
var input = event.data.split(":");
var tag = [{ word: input[0], weight: input[1] }];
// Check if the same hashtag exists in the tag cloud and get its index in the array
const objIndex = tags.findIndex((obj => obj.word === input[0]));
//If exists, then replace just the weight for the hashtag with the new incoming weight
if (objIndex > -1) {
// create a new element with hashtag and its new weight
const updatedObj = { ...tags[objIndex], weight: input[1] };
// create a new array with the updated element;
// doing an in-place replace of the existing element
const updatedTags = [
...tags.slice(0, objIndex),
updatedObj,
...tags.slice(objIndex + 1),
];
// assign updated array to the existing array variable
tags = updatedTags;
} else {
// Add the new incoming hash tag to the tag cloud
Array.prototype.push.apply(tags, tag);
}
$("#wordCloud").jQWCloud({ words: tags });
document.getElementById("content").innerHTML = event.data;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment