From 58e046c1f0605316f4a5e7b1c1fabec0e2d4938f Mon Sep 17 00:00:00 2001 From: Mario Steele Date: Wed, 30 Jul 2025 12:20:50 -0500 Subject: [PATCH] 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. --- FreeTubeSyncer/Program.cs | 58 +++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/FreeTubeSyncer/Program.cs b/FreeTubeSyncer/Program.cs index 24adb90..f5fd8c8 100644 --- a/FreeTubeSyncer/Program.cs +++ b/FreeTubeSyncer/Program.cs @@ -1,7 +1,9 @@ using Avalonia; using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Linq; using System.Threading; using System.Threading.Tasks; using FreeTubeSyncer.Library; @@ -50,23 +52,49 @@ class Program Syncer? profileSyncer = null, Syncer? searchHistorySyncer = null, Syncer? settingsSyncer = null) { + var syncers = new List() + { + historySyncer, + playlistSyncer, + profileSyncer, + searchHistorySyncer, + settingsSyncer + }; + var lastTime = DateTime.Now; while (true) { - Thread.Sleep(100); - if (Process.GetProcessesByName("FreeTube").Length > 0) continue; - Console.WriteLine("FreeTube has closed, we're going to try and update."); - Thread.Sleep(1500); - - if (historySyncer is { IsDirty: true }) - historySyncer.Sync(); - if (playlistSyncer is { IsDirty: true }) - playlistSyncer.Sync(); - if (profileSyncer is { IsDirty: true}) - profileSyncer.Sync(); - if (searchHistorySyncer is { IsDirty: true}) - searchHistorySyncer.Sync(); - if (settingsSyncer is { IsDirty: true}) - settingsSyncer.Sync(); + if (syncers.Any(x => x != null && x.IsDirty() )) + { + Thread.Sleep(100); + if (lastTime - DateTime.Now > TimeSpan.FromSeconds(30)) + { + foreach (var syncer in syncers) + syncer.FetchDatabase().Wait(); + lastTime = DateTime.Now; + } + if (Process.GetProcessesByName("FreeTube").Length > 0) continue; + Console.WriteLine("FreeTube has closed and we have updates, we're going to try and update."); + Thread.Sleep(1500); + + if (historySyncer != null && historySyncer.IsDirty()) + 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()