Skip to content

Commit

Permalink
Merge pull request #16 from lightpanda-io/dispatch-event-remove-listener
Browse files Browse the repository at this point in the history
handle listeners events removing themselves
  • Loading branch information
krichprollsch authored Apr 18, 2024
2 parents 7f2e157 + 293790b commit 5f945d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/events/event_target.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,17 @@ dom_exception _dom_event_target_dispatch(dom_event_target *et,
* break */
if (evt->stop_now == true)
break;

// A listener can modify the event target listeners list
// during the loop. At this stage, `le` can be removed from
// the list and its memory freed.
// First we need to detect if `le` still exists.
if (!list_has((struct list_entry *)eti->listeners, (struct list_entry *)le)) {
// `le` has been deleted.
break;
}
}
}

le = (struct listener_entry *) le->list.next;
} while (le != eti->listeners);
}
Expand Down
18 changes: 18 additions & 0 deletions src/utils/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define dom_utils_list_h_

#include <stddef.h>
#include <stdbool.h>

struct list_entry {
struct list_entry *prev;
Expand Down Expand Up @@ -58,4 +59,21 @@ static inline void list_del(struct list_entry *ent)
ent->next = ent;
}

/**
* Detect if an entry is in the list
*
* \param entry The entry need to be deleted from the list
*/
static inline bool list_has(struct list_entry *head, struct list_entry *ent)
{
struct list_entry *cur = head;
do {
if (cur == ent) {
return true;
}
cur = cur->next;
} while (cur != head);
return false;
}

#endif

0 comments on commit 5f945d6

Please sign in to comment.