From 548850e453c7f8000c5bc5d38b66576139542462 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 3 Jul 2024 19:03:25 -0700 Subject: [PATCH] terminal: RefCountedSet should call deleted on upsert --- src/terminal/ref_counted_set.zig | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/terminal/ref_counted_set.zig b/src/terminal/ref_counted_set.zig index ba1e0afd1..0fcb84b57 100644 --- a/src/terminal/ref_counted_set.zig +++ b/src/terminal/ref_counted_set.zig @@ -476,9 +476,13 @@ pub fn RefCountedSet( /// is ignored and the existing item's ID is returned. fn upsert(self: *Self, base: anytype, value: T, new_id: Id, ctx: Context) Id { // If the item already exists, return it. - // TODO: we should probably call deleted here on value since - // we're using the value already in the map - if (self.lookup(base, value, ctx)) |id| return id; + if (self.lookup(base, value, ctx)) |id| { + // Notify the context that the value is "deleted" because + // we're reusing the existing value in the set. This allows + // callers to clean up any resources associated with the value. + if (comptime @hasDecl(Context, "deleted")) ctx.deleted(value); + return id; + } const table = self.table.ptr(base); const items = self.items.ptr(base);