From 86998438026342451685c9e310b216431f080496 Mon Sep 17 00:00:00 2001 From: Didymus Date: Sun, 14 Apr 2024 18:13:02 +0800 Subject: [PATCH] [ark-event] fix: copy events each tick to process events available per tick --- ArkKit/ark-event-kit/ArkEventManager.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ArkKit/ark-event-kit/ArkEventManager.swift b/ArkKit/ark-event-kit/ArkEventManager.swift index ea1bd2f..635331e 100644 --- a/ArkKit/ark-event-kit/ArkEventManager.swift +++ b/ArkKit/ark-event-kit/ArkEventManager.swift @@ -39,10 +39,14 @@ class ArkEventManager: ArkEventContext { } func processEvents() { - while !eventQueue.isEmpty { - guard let datedEvent = eventQueue.dequeue() else { + // copy eventQueue so that events emited during processing are processed at the next tick + var eventsFromQueue = eventQueue + while !eventsFromQueue.isEmpty { + guard let datedEvent = eventsFromQueue.dequeue() else { fatalError("[ArkEventManager.processEvents()] dequeue failed: Expected event, found nil.") } + // dequeue from copy and actual eventQueue so that they are in sync + _ = eventQueue.dequeue() guard let listenersToExecute = listeners[ObjectIdentifier(type(of: datedEvent.event))] else { continue }