New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite server code of channel sorting #1064
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -412,44 +412,40 @@ Client.prototype.open = function(socketId, target) { | |
}; | ||
|
||
Client.prototype.sort = function(data) { | ||
var self = this; | ||
const order = data.order; | ||
|
||
var type = data.type; | ||
var order = data.order || []; | ||
var sorted = []; | ||
if (!_.isArray(order)) { | ||
return; | ||
} | ||
|
||
switch (type) { | ||
switch (data.type) { | ||
case "networks": | ||
order.forEach(i => { | ||
var find = _.find(self.networks, {id: i}); | ||
if (find) { | ||
sorted.push(find); | ||
} | ||
this.networks.sort((a, b) => { | ||
return order.indexOf(a.id) > order.indexOf(b.id); | ||
}); | ||
self.networks = sorted; | ||
|
||
// Sync order to connected clients | ||
this.emit("sync_sort", {order: this.networks.map(obj => obj.id), type: data.type, target: data.target}); | ||
|
||
break; | ||
|
||
case "channels": | ||
var target = data.target; | ||
var network = _.find(self.networks, {id: target}); | ||
var network = _.find(this.networks, {id: data.target}); | ||
if (!network) { | ||
return; | ||
} | ||
order.forEach(i => { | ||
var find = _.find(network.channels, {id: i}); | ||
if (find) { | ||
sorted.push(find); | ||
} | ||
|
||
network.channels.sort((a, b) => { | ||
return order.indexOf(a.id) > order.indexOf(b.id); | ||
}); | ||
network.channels = sorted; | ||
|
||
// Sync order to connected clients | ||
this.emit("sync_sort", {order: network.channels.map(obj => obj.id), type: data.type, target: data.target}); | ||
|
||
break; | ||
} | ||
|
||
self.save(); | ||
|
||
// Sync order to connected clients | ||
const syncOrder = sorted.map(obj => obj.id); | ||
self.emit("sync_sort", {order: syncOrder, type: type, target: data.target}); | ||
this.save(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specific reason why you separated the event emit and file save? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nothing specific, just didn't want to keep the variable. |
||
}; | ||
|
||
Client.prototype.names = function(data) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming about Node's (unstable) sorting algorithm, this is at best
O(n² * log n)
. Some people hang out in easily dozens of channels: 50 channels and that's 14k operations, 100 channels is 64k operations, etc.I know this is not worse than it was before (I think), but do you think there is any way we can improve that? The user provides the order themselves, so in my understanding we shouldn't have to do a full sort, instead loop through the given order (which would be
O(n²)
, not much better, surely there is a better way...)Also, when is this needed exactly? When a user reorders their channels/networks on the UI?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't rely entirely on user given input (will have to filter it to exactly contain every channel ID on the network, missing channels have to be added, invalid ones removed. This was the simplest approach.
This could be improved by modifying order into a hash map so that no indexOf calls are required.
This method is entirely just for the UI sorting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, this can be done later though, your fix is a good thing anyway.