ui/common/types.c

branch
newapi
changeset 395
b8277deb75b8
parent 380
aad99285865c
--- a/ui/common/types.c	Wed Nov 27 13:28:21 2024 +0100
+++ b/ui/common/types.c	Wed Nov 27 18:46:45 2024 +0100
@@ -544,3 +544,36 @@
     }
     free(list.files);
 }
+
+
+typedef struct UiObserverDestructor {
+    UiList *list;
+    UiObserver *observer;
+} UiObserverDestructor;
+
+static void observer_destructor(UiObserverDestructor *destr) {
+    UiObserver *remove_obs = destr->observer;
+    UiObserver *obs = destr->list->observers;
+    UiObserver *prev = NULL;
+    while(obs) {
+        if(obs == remove_obs) {
+            if(prev) {
+                prev->next = obs->next;
+            } else {
+                destr->list->observers = obs->next;
+            }
+            break;
+        }
+        prev = obs;
+        obs = obs->next;
+    }
+    free(remove_obs);
+}
+
+void uic_list_register_observer_destructor(UiContext *ctx, UiList *list, UiObserver *observer) {
+    CxMempool *mp = ctx->mp;
+    UiObserverDestructor *destr = cxMalloc(mp->allocator, sizeof(UiObserverDestructor));
+    destr->list = list;
+    destr->observer = observer;
+    cxMempoolSetDestructor(destr, (cx_destructor_func)observer_destructor);
+}

mercurial