BlitterStudio / MB_SubSonic

MusicBee SubSonic plugin
GNU General Public License v3.0
115 stars 13 forks source link

Background task keeps fetching even after shifting focus #56

Closed Koenvh1 closed 3 years ago

Koenvh1 commented 4 years ago

I noticed that when PreCacheAll is set to true, it will fetch the current level plus all levels below it. That is intended, however, once the focus is shifted from the top level to another level (such as a sublevel), and it was not finished fetching yet, then it will keep fetching even though the result will no longer be used. In my case, this means that I am suddenly sending a couple hundred requests to my server (or actually to Astiga) that will not be used, which seems a bit of a waste.

Now to counter that, I added a unique ID to the start of each task, and passed that along with the task. As soon as a new task is started, a new ID is generated, and the old task will abort. Like this:

private static string _taskId;
var uniqueId = Guid.NewGuid().ToString("N");
_taskId = uniqueId;
GetFolderFiles(path.Substring(0, path.IndexOf(@"\", StringComparison.Ordinal)), folderId, files, uniqueId);
private static void GetFolderFiles(string baseFolderName, string folderId, ICollection<KeyValuePair<byte, string>[]> files, string taskId)
{
    if (taskId != _taskId) return;

From my initial testing it seems to work fine. However, am I overlooking something that would break this way? I have only tested it against Astiga, not against the original Subsonic server, but I think it should work the same. I would create a pull request, but I have been experimenting around with some other things as well, and it is a bit difficult to separate those changes now.

midwan commented 4 years ago

@Koenvh1 Thanks! I'll test this, it looks like a good approach (and that area generally needs some extra love for sure).

midwan commented 3 years ago

This should be fixed with the new release: https://github.com/midwan/MB_SubSonic/releases/tag/v2.30 I've removed pre-caching altogether, as it wasn't really worth the trouble it caused. Instead, we load files/folders on-demand now, which should be faster for most people.