about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Helpers/SyncHelper.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-28 10:38:45 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-28 10:38:45 +0200
commita331eb2f118e0051c6c6744a20d6b0934c4d6d6f (patch)
treea7798d94a3553106aad40507e2dc04ff9d2f9efd /MatrixRoomUtils.Core/Helpers/SyncHelper.cs
parentDependency injection stuff (diff)
downloadMatrixUtils-a331eb2f118e0051c6c6744a20d6b0934c4d6d6f.tar.xz
Update stuff
Diffstat (limited to 'MatrixRoomUtils.Core/Helpers/SyncHelper.cs')
-rw-r--r--MatrixRoomUtils.Core/Helpers/SyncHelper.cs28
1 files changed, 16 insertions, 12 deletions
diff --git a/MatrixRoomUtils.Core/Helpers/SyncHelper.cs b/MatrixRoomUtils.Core/Helpers/SyncHelper.cs
index bb4ddd5..1b9c598 100644
--- a/MatrixRoomUtils.Core/Helpers/SyncHelper.cs
+++ b/MatrixRoomUtils.Core/Helpers/SyncHelper.cs
@@ -3,6 +3,7 @@ using System.Net.Http.Json;
 using System.Text.Json.Serialization;
 using MatrixRoomUtils.Core.Interfaces;
 using MatrixRoomUtils.Core.Responses;
+using MatrixRoomUtils.Core.Responses.Admin;
 using MatrixRoomUtils.Core.Services;
 using MatrixRoomUtils.Core.StateEventTypes;
 
@@ -45,21 +46,24 @@ public class SyncHelper {
     [SuppressMessage("ReSharper", "FunctionNeverReturns")]
     public async Task RunSyncLoop(CancellationToken? cancellationToken = null, bool skipInitialSyncEvents = true) {
         SyncResult? sync = null;
+        string? nextBatch = null;
         while (cancellationToken is null || !cancellationToken.Value.IsCancellationRequested) {
-            sync = await Sync(sync?.NextBatch, cancellationToken);
-            Console.WriteLine($"Got sync, next batch: {sync?.NextBatch}!");
-            if (sync == null) continue;
+            sync = await Sync(nextBatch, cancellationToken);
+            nextBatch = sync?.NextBatch ?? nextBatch;
+            if(sync is null) continue;
+            Console.WriteLine($"Got sync, next batch: {nextBatch}!");
 
             if (sync.Rooms is { Invite.Count: > 0 }) {
                 foreach (var roomInvite in sync.Rooms.Invite) {
-                    Console.WriteLine(roomInvite.Value.GetType().Name);
-                    InviteReceived?.Invoke(this, roomInvite);
+                    var tasks = InviteReceivedHandlers.Select(x => x(roomInvite)).ToList();
+                    await Task.WhenAll(tasks);
                 }
             }
 
             if (sync.AccountData is { Events: { Count: > 0 } }) {
                 foreach (var accountDataEvent in sync.AccountData.Events) {
-                    AccountDataReceived?.Invoke(this, accountDataEvent);
+                    var tasks = AccountDataReceivedHandlers.Select(x => x(accountDataEvent)).ToList();
+                    await Task.WhenAll(tasks);
                 }
             }
 
@@ -73,7 +77,8 @@ public class SyncHelper {
                 foreach (var updatedRoom in sync.Rooms.Join) {
                     foreach (var stateEventResponse in updatedRoom.Value.Timeline.Events) {
                         stateEventResponse.RoomId = updatedRoom.Key;
-                        TimelineEventReceived?.Invoke(this, stateEventResponse);
+                        var tasks = TimelineEventHandlers.Select(x => x(stateEventResponse)).ToList();
+                        await Task.WhenAll(tasks);
                     }
                 }
             }
@@ -83,11 +88,10 @@ public class SyncHelper {
     /// <summary>
     /// Event fired when a room invite is received
     /// </summary>
-    public event EventHandler<KeyValuePair<string, SyncResult.RoomsDataStructure.InvitedRoomDataStructure>>?
-        InviteReceived;
-
-    public event EventHandler<StateEventResponse>? TimelineEventReceived;
-    public event EventHandler<StateEventResponse>? AccountDataReceived;
+    public List<Func<KeyValuePair<string, SyncResult.RoomsDataStructure.InvitedRoomDataStructure>, Task>> 
+        InviteReceivedHandlers { get; }  = new();
+    public List<Func<StateEventResponse, Task>> TimelineEventHandlers { get; }  = new();
+    public List<Func<StateEventResponse, Task>> AccountDataReceivedHandlers { get; }  = new();
 }
 
 public class SyncResult {