Files
SubMusic/source/Sync/AudioSync.mc
T
Memen 1f3fba2289 fixed settings storage having 0 Bytes
fixed some rare crashes on receiving invalid response
added setting to enable debug logging
removed default debug output (speed improvement)
2023-02-20 15:42:22 +01:00

116 lines
2.9 KiB
Mason

class AudioSync extends Deferrable {
private var d_provider = SubMusic.Provider.get();
private var d_audio = null; // front of the todos
private var d_failed = []; // array of all failed
private var f_progress; // callback on progress
// store sync size
private var d_todo = []; // array of IAudio objects
private var d_todo_total; // the number of items that had to be synced
function initialize(progress, done, fail) {
if ($.debug) {
System.println("AudioSync::initialize()");
}
Deferrable.initialize(method(:sync), done, fail); // make sync the deferred task
f_progress = progress;
d_provider.setFallback(method(:onError));
d_provider.setProgressCallback(method(:onProgress));
// first delete the todeletes
var ids = SongStore.getDeletes();
for (var idx = 0; idx < ids.size(); ++idx) {
var id = ids[idx];
var isong = new ISong(id);
isong.remove(); // remove from Store
}
// now get the todos from songs and episodes
var types = [ Audio.SONG, Audio.PODCAST_EPISODE];
ids = [ SongStore.getIds(), EpisodeStore.getIds() ];
for (var typ = 0; typ != Audio.END; ++typ) {
for (var idx = 0; idx != ids[typ].size(); ++idx) {
var audio = new Audio(ids[typ][idx], typ);
// only add to todo if not yet stored
if (audio.refId() == null) {
d_todo.add(audio);
}
}
}
d_todo_total = d_todo.size();
}
function sync() {
// if songs all finished, complete this task
if (d_todo.size() == 0) {
return Deferrable.complete(); // set complete
}
// update progress
f_progress.invoke(progress());
// start download
d_provider.getRefId(d_todo[0].id(), d_todo[0].mime(), d_todo[0].type(), method(:onDownloaded));
return Deferrable.defer();
}
function progress() {
// determine what is left to do
var todo = 0;
if (d_todo != null) { todo = d_todo.size(); }
var done = d_todo_total - todo;
var progress = (100 * done) / d_todo_total.toFloat();
return progress;
}
// handle callback on intermediate progress
function onProgress(progress) {
if ($.debug) {
System.println("AudioSync::onProgress( progress: " + progress + " )");
}
progress /= d_todo_total.toFloat();
f_progress.invoke(progress() + progress);
}
// Callback for when a song is downloaded
function onDownloaded(refId) {
// update refId
d_todo[0].setRefId(refId);
// continue to next song
d_todo.removeAll(d_todo[0]);
sync();
}
function onError(error) {
if ($.debug) {
System.println("AudioSync::onError(" + error.shortString() + " : " + error.toString() + ")");
}
// indicate failed sync
// d_playlist.setError(error); TODO
d_failed.add(d_todo[0]);
// check if song in progress
if (d_todo[0] == null) {
Deferrable.cancel(error);
return;
}
// record the cause of failure
// d_todo[0].setError(error); TODO
// remove first element from todo list
d_todo.removeAll(d_todo[0]);
sync();
return;
}
}