about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Classes
diff options
context:
space:
mode:
Diffstat (limited to 'MatrixRoomUtils.Web/Classes')
-rw-r--r--MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs88
1 files changed, 43 insertions, 45 deletions
diff --git a/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs b/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs
index bd44f7f..e21b363 100644
--- a/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs
+++ b/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs
@@ -7,85 +7,83 @@ public partial class LocalStorageWrapper
 {
     private static SemaphoreSlim _semaphoreSlim = new(1);
     public static Settings Settings { get; set; } = new();
-    
+
     //some basic logic
-    public static async Task ReloadLocalStorage(ILocalStorageService localStorage)
-    {
-        await SaveToLocalStorage(localStorage);
-        await LoadFromLocalStorage(localStorage);
-    }
-    public static async Task LoadFromLocalStorage(ILocalStorageService localStorage)
+    public static async Task InitialiseRuntimeVariables(ILocalStorageService localStorage)
     {
-        await _semaphoreSlim.WaitAsync();
-        if (RuntimeCache.WasLoaded) return;
-        RuntimeCache.WasLoaded = true;
-        Settings = await localStorage.GetItemAsync<Settings>("rory.matrixroomutils.settings") ?? new();
-        
         //RuntimeCache stuff
         async void Save() => await SaveToLocalStorage(localStorage);
 
         RuntimeCache.Save = Save;
-        RuntimeCache.SaveObject = async (key, obj) => await localStorage.SetItemAsync(key, obj); 
-        // RuntimeCache.AccessToken = await localStorage.GetItemAsync<string>("rory.matrixroomutils.token");
-        RuntimeCache.LastUsedToken = await localStorage.GetItemAsync<string>("rory.matrixroomutils.last_used_token");
-        // RuntimeCache.CurrentHomeserver = await localStorage.GetItemAsync<string>("rory.matrixroomutils.current_homeserver");
-        RuntimeCache.LoginSessions = await localStorage.GetItemAsync<Dictionary<string, UserInfo>>("rory.matrixroomutils.user_cache") ?? new();
-        RuntimeCache.HomeserverResolutionCache = await localStorage.GetItemAsync<Dictionary<string, HomeServerResolutionResult>>("rory.matrixroomutils.homeserver_resolution_cache") ?? new();
-        Console.WriteLine($"[LocalStorageWrapper] Loaded {RuntimeCache.LoginSessions.Count} login sessions, {RuntimeCache.HomeserverResolutionCache.Count} homeserver resolution cache entries");
+        RuntimeCache.SaveObject = async (key, obj) => await localStorage.SetItemAsync(key, obj);
         if (RuntimeCache.LastUsedToken != null)
         {
             Console.WriteLine($"Access token is not null, creating authenticated home server");
             Console.WriteLine($"Homeserver cache: {RuntimeCache.HomeserverResolutionCache.Count} entries");
             // Console.WriteLine(RuntimeCache.HomeserverResolutionCache.ToJson());
-            RuntimeCache.CurrentHomeServer = await new AuthenticatedHomeServer(RuntimeCache.LoginSessions[RuntimeCache.LastUsedToken].LoginResponse.UserId, RuntimeCache.LastUsedToken, RuntimeCache.LoginSessions[RuntimeCache.LastUsedToken].LoginResponse.HomeServer).Configure();
+            RuntimeCache.CurrentHomeServer = await new AuthenticatedHomeServer(RuntimeCache.LoginSessions[RuntimeCache.LastUsedToken].LoginResponse.UserId, RuntimeCache.LastUsedToken,
+                RuntimeCache.LoginSessions[RuntimeCache.LastUsedToken].LoginResponse.HomeServer).Configure();
             Console.WriteLine("Created authenticated home server");
         }
-        RuntimeCache.GenericResponseCache = await localStorage.GetItemAsync<Dictionary<string, ObjectCache<object>>>("rory.matrixroomutils.generic_cache") ?? new();
-        
-        foreach (var s in (await localStorage.KeysAsync()).Where(x=>x.StartsWith("rory.matrixroomutils.generic_cache:")).ToList())
+    }
+
+    public static async Task LoadFromLocalStorage(ILocalStorageService localStorage)
+    {
+        await _semaphoreSlim.WaitAsync();
+        if (RuntimeCache.WasLoaded)
+        {
+            _semaphoreSlim.Release();
+            return;
+        }
+        Console.WriteLine("Loading from local storage...");
+        Settings = await localStorage.GetItemAsync<Settings>("rory.matrixroomutils.settings") ?? new();
+
+        RuntimeCache.LastUsedToken = await localStorage.GetItemAsync<string>("rory.matrixroomutils.last_used_token");
+        RuntimeCache.LoginSessions = await localStorage.GetItemAsync<Dictionary<string, UserInfo>>("rory.matrixroomutils.login_sessions") ?? new();
+        RuntimeCache.HomeserverResolutionCache = await localStorage.GetItemAsync<Dictionary<string, HomeServerResolutionResult>>("rory.matrixroomutils.homeserver_resolution_cache") ?? new();
+        Console.WriteLine($"[LocalStorageWrapper] Loaded {RuntimeCache.LoginSessions.Count} login sessions, {RuntimeCache.HomeserverResolutionCache.Count} homeserver resolution cache entries");
+
+        //RuntimeCache.GenericResponseCache = await localStorage.GetItemAsync<Dictionary<string, ObjectCache<object>>>("rory.matrixroomutils.generic_cache") ?? new();
+
+        foreach (var s in (await localStorage.KeysAsync()).Where(x => x.StartsWith("rory.matrixroomutils.generic_cache:")).ToList())
         {
             Console.WriteLine($"Loading generic cache entry {s}");
             RuntimeCache.GenericResponseCache[s.Replace("rory.matrixroomutils.generic_cache:", "")] = await localStorage.GetItemAsync<ObjectCache<object>>(s);
         }
 
+        await InitialiseRuntimeVariables(localStorage);
+        RuntimeCache.WasLoaded = true;
         _semaphoreSlim.Release();
     }
 
     public static async Task SaveToLocalStorage(ILocalStorageService localStorage)
     {
+        Console.WriteLine("Saving to local storage...");
         await localStorage.SetItemAsync("rory.matrixroomutils.settings", Settings);
-        // if(RuntimeCache.AccessToken != null) await localStorage.SetItemAsStringAsync("rory.matrixroomutils.token", RuntimeCache.AccessToken);
-        // if(RuntimeCache.CurrentHomeserver != null) await localStorage.SetItemAsync("rory.matrixroomutils.current_homeserver", RuntimeCache.CurrentHomeserver);
-        if(RuntimeCache.LoginSessions != null) await localStorage.SetItemAsync("rory.matrixroomutils.user_cache", RuntimeCache.LoginSessions);
-        if(RuntimeCache.LastUsedToken != null) await localStorage.SetItemAsync("rory.matrixroomutils.last_used_token", RuntimeCache.LastUsedToken);
-        await localStorage.SetItemAsync("rory.matrixroomutils.homeserver_resolution_cache", 
-            RuntimeCache.HomeserverResolutionCache.DistinctBy(x => x.Key)
-                .ToDictionary(x => x.Key, x => x.Value));
-        await localStorage.SetItemAsync("rory.matrixroomutils.generic_cache", RuntimeCache.GenericResponseCache);
-        // foreach (var s in RuntimeCache.GenericResponseCache.Keys)
-        // {
-            // await localStorage.SetItemAsync($"rory.matrixroomutils.generic_cache:{s}", RuntimeCache.GenericResponseCache[s]);
-        // }
+        if (RuntimeCache.LoginSessions != null) await localStorage.SetItemAsync("rory.matrixroomutils.login_sessions", RuntimeCache.LoginSessions);
+        if (RuntimeCache.LastUsedToken != null) await localStorage.SetItemAsync("rory.matrixroomutils.last_used_token", RuntimeCache.LastUsedToken);
     }
-    public static async Task SaveFieldToLocalStorage(ILocalStorageService localStorage, string key)
+
+    public static async Task SaveCacheToLocalStorage(ILocalStorageService localStorage, bool awaitSave = true, bool saveGenericCache = true)
     {
-        if (key == "rory.matrixroomutils.settings") await localStorage.SetItemAsync(key, Settings);
-        // if (key == "rory.matrixroomutils.token") await localStorage.SetItemAsStringAsync(key, RuntimeCache.AccessToken);
-        // if (key == "rory.matrixroomutils.current_homeserver") await localStorage.SetItemAsync(key, RuntimeCache.CurrentHomeserver);
-        if (key == "rory.matrixroomutils.user_cache") await localStorage.SetItemAsync(key, RuntimeCache.LoginSessions);
-        if (key == "rory.matrixroomutils.last_used_token") await localStorage.SetItemAsync(key, RuntimeCache.LastUsedToken);
-        if (key == "rory.matrixroomutils.homeserver_resolution_cache") await localStorage.SetItemAsync(key, RuntimeCache.HomeserverResolutionCache);
-        //if (key == "rory.matrixroomutils.generic_cache") await localStorage.SetItemAsync(key, RuntimeCache.GenericResponseCache);
+        await localStorage.SetItemAsync("rory.matrixroomutils.homeserver_resolution_cache",
+            RuntimeCache.HomeserverResolutionCache.DistinctBy(x => x.Key)
+                .ToDictionary(x => x.Key, x => x.Value));
+        //await localStorage.SetItemAsync("rory.matrixroomutils.generic_cache", RuntimeCache.GenericResponseCache);
+        if(saveGenericCache)
+            foreach (var s in RuntimeCache.GenericResponseCache.Keys)
+            {
+                var t = localStorage.SetItemAsync($"rory.matrixroomutils.generic_cache:{s}", RuntimeCache.GenericResponseCache[s]);
+                if (awaitSave) await t;
+            }
     }
 }
 
-
 public class Settings
 {
     public DeveloperSettings DeveloperSettings { get; set; } = new();
 }
 
-
 public class DeveloperSettings
 {
     public bool EnableLogViewers { get; set; } = false;