Browse Source

Added function for stores to respond to changes in other stores

frogsoftware 1 year ago
parent
commit
3b8c36f88c
1 changed files with 21 additions and 0 deletions
  1. 21 0
      InABox.Database/Stores/Store.cs

+ 21 - 0
InABox.Database/Stores/Store.cs

@@ -2,6 +2,7 @@
 using System.Reflection;
 using System.Text.RegularExpressions;
 using InABox.Core;
+using NPOI.OpenXmlFormats.Dml;
 using NPOI.POIFS.FileSystem;
 
 namespace InABox.Database
@@ -462,6 +463,7 @@ namespace InABox.Database
             AfterSave(entity);
 
             entity = RunScript(ScriptType.AfterSave, new[] { entity }).First();
+            NotifyListeners([entity]);
         }
 
         protected void AuditTrail(IEnumerable<Entity> entities, IEnumerable<string> notes)
@@ -590,6 +592,8 @@ namespace InABox.Database
             }
 
             entities = RunScript(ScriptType.AfterSave, entities);
+            
+            NotifyListeners(entities);
 
             //}
             //catch (Exception e)
@@ -598,6 +602,19 @@ namespace InABox.Database
             //}
         }
 
+        private static List<Tuple<Type, Action<Guid[]>>> _listeners = new List<Tuple<Type, Action<Guid[]>>>();
+
+        public static void RegisterListener<TType>(Action<Guid[]> listener)
+            => _listeners.Add(new Tuple<Type, Action<Guid[]>>(typeof(TType), listener));
+
+        private void NotifyListeners(IEnumerable<T> items)
+        {
+            var ids = items.Select(x => x.ID).ToArray();
+            foreach (var listener in _listeners.Where(x => x.Item1 == typeof(T)))
+                listener.Item2(ids);
+            
+        }
+        
         #endregion
 
         #region Delete Functions
@@ -637,6 +654,8 @@ namespace InABox.Database
                 AfterDelete(entity);
 
                 entity = RunScript(ScriptType.AfterDelete, new[] { entity }).First();
+                
+                NotifyListeners([entity]);
             }
             catch (Exception e)
             {
@@ -667,6 +686,8 @@ namespace InABox.Database
                 foreach (var entity in entities) AfterDelete(entity);
 
                 entities = RunScript(ScriptType.AfterDelete, entities);
+                
+                NotifyListeners(entities);
             }
             catch (Exception e)
             {