From 1cfbabe64cac391e957f53ad88d49626c1244e48 Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Sat, 20 Jul 2019 13:48:19 +0100 Subject: [PATCH] notify: use data to match specific callbacks When removing a notification callback, check both the function and data pointers. This means we can reuse a single callback function with multiple data objects. --- context.c | 4 ++-- mutt/notify.c | 7 ++++--- mutt/notify.h | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/context.c b/context.c index 4385d287c..40738ef9f 100644 --- a/context.c +++ b/context.c @@ -56,7 +56,7 @@ void ctx_free(struct Context **ptr) notify_send(ctx->notify, NT_CONTEXT, NT_CONTEXT_CLOSE, IP & ev_ctx); if (ctx->mailbox) - notify_observer_remove(ctx->mailbox->notify, ctx_mailbox_observer); + notify_observer_remove(ctx->mailbox->notify, ctx_mailbox_observer, IP ctx); notify_free(&ctx->notify); @@ -86,7 +86,7 @@ void ctx_cleanup(struct Context *ctx) FREE(&ctx->pattern); mutt_pattern_free(&ctx->limit_pattern); if (ctx->mailbox) - notify_observer_remove(ctx->mailbox->notify, ctx_mailbox_observer); + notify_observer_remove(ctx->mailbox->notify, ctx_mailbox_observer, IP ctx); struct Notify *notify = ctx->notify; memset(ctx, 0, sizeof(struct Context)); diff --git a/mutt/notify.c b/mutt/notify.c index 0c7a2950e..cf2424e16 100644 --- a/mutt/notify.c +++ b/mutt/notify.c @@ -74,7 +74,7 @@ void notify_free(struct Notify **ptr) struct Notify *notify = *ptr; // NOTIFY observers - notify_observer_remove(notify, NULL); + notify_observer_remove(notify, NULL, 0); FREE(ptr); } @@ -186,11 +186,12 @@ bool notify_observer_add(struct Notify *notify, enum NotifyType type, * notify_observer_remove - Remove an observer from an object * @param notify Notification handler * @param callback Function to call on a matching event, see ::observer_t + * @param data Private data to match specific callback * @retval true If successful * * If callback is NULL, all the observers will be removed. */ -bool notify_observer_remove(struct Notify *notify, observer_t callback) +bool notify_observer_remove(struct Notify *notify, observer_t callback, intptr_t data) { if (!notify) return false; @@ -200,7 +201,7 @@ bool notify_observer_remove(struct Notify *notify, observer_t callback) struct ObserverNode *tmp = NULL; STAILQ_FOREACH_SAFE(np, ¬ify->observers, entries, tmp) { - if (!callback || (np->observer->callback == callback)) + if (!callback || ((np->observer->callback == callback) && (np->observer->data == data))) { STAILQ_REMOVE(¬ify->observers, np, ObserverNode, entries); FREE(&np->observer); diff --git a/mutt/notify.h b/mutt/notify.h index f57f3a6ea..a045527e1 100644 --- a/mutt/notify.h +++ b/mutt/notify.h @@ -37,6 +37,6 @@ void notify_set_parent(struct Notify *notify, struct Notify *parent); bool notify_send(struct Notify *notify, int type, int subtype, intptr_t data); bool notify_observer_add(struct Notify *notify, enum NotifyType type, int subtype, observer_t callback, intptr_t data); -bool notify_observer_remove(struct Notify *notify, observer_t callback); +bool notify_observer_remove(struct Notify *notify, observer_t callback, intptr_t data); #endif /* MUTT_LIB_NOTIFY_H */ -- 2.40.0