Updated Program

Now utilizing syncers in an Array for each checking of IsDirty() or not,
and running code based upon the state of the syncers.  May clean this up
more.
Added polling of the database to see if there's anything new to sync
locally.
This commit is contained in:
Mario Steele 2025-07-30 12:20:50 -05:00
parent 5887bc0961
commit 58e046c1f0

View file

@ -1,7 +1,9 @@
using Avalonia; using Avalonia;
using System; using System;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using FreeTubeSyncer.Library; using FreeTubeSyncer.Library;
@ -50,23 +52,49 @@ class Program
Syncer<Profile>? profileSyncer = null, Syncer<SearchHistory>? searchHistorySyncer = null, Syncer<Profile>? profileSyncer = null, Syncer<SearchHistory>? searchHistorySyncer = null,
Syncer<Setting>? settingsSyncer = null) Syncer<Setting>? settingsSyncer = null)
{ {
var syncers = new List<ISyncer>()
{
historySyncer,
playlistSyncer,
profileSyncer,
searchHistorySyncer,
settingsSyncer
};
var lastTime = DateTime.Now;
while (true) while (true)
{ {
Thread.Sleep(100); if (syncers.Any(x => x != null && x.IsDirty() ))
if (Process.GetProcessesByName("FreeTube").Length > 0) continue; {
Console.WriteLine("FreeTube has closed, we're going to try and update."); Thread.Sleep(100);
Thread.Sleep(1500); if (lastTime - DateTime.Now > TimeSpan.FromSeconds(30))
{
if (historySyncer is { IsDirty: true }) foreach (var syncer in syncers)
historySyncer.Sync(); syncer.FetchDatabase().Wait();
if (playlistSyncer is { IsDirty: true }) lastTime = DateTime.Now;
playlistSyncer.Sync(); }
if (profileSyncer is { IsDirty: true}) if (Process.GetProcessesByName("FreeTube").Length > 0) continue;
profileSyncer.Sync(); Console.WriteLine("FreeTube has closed and we have updates, we're going to try and update.");
if (searchHistorySyncer is { IsDirty: true}) Thread.Sleep(1500);
searchHistorySyncer.Sync();
if (settingsSyncer is { IsDirty: true}) if (historySyncer != null && historySyncer.IsDirty())
settingsSyncer.Sync(); historySyncer.Sync();
if (playlistSyncer != null && playlistSyncer.IsDirty())
playlistSyncer.Sync();
if (profileSyncer != null && profileSyncer.IsDirty())
profileSyncer.Sync();
if (searchHistorySyncer != null && searchHistorySyncer.IsDirty())
searchHistorySyncer.Sync();
if (settingsSyncer != null && settingsSyncer.IsDirty())
settingsSyncer.Sync();
}
else
{
Thread.Sleep(100);
if (lastTime - DateTime.Now <= TimeSpan.FromSeconds(30)) continue;
foreach (var syncer in syncers)
syncer.FetchDatabase().Wait();
lastTime = DateTime.Now;
}
} }
} }
// public static void Main(string[] args) => BuildAvaloniaApp() // public static void Main(string[] args) => BuildAvaloniaApp()