@@ -528,8 +528,8 @@ void Engine::step()
528528 m_frameActivity = !m_threads.empty ();
529529
530530 // Resolve stopped broadcast scripts
531- std::vector <Broadcast *> resolved;
532- std::vector <Thread *> resolvedThreads;
531+ std::unordered_map <Broadcast *, Thread *> stoppedBroadcasts; // sender thread
532+ std::unordered_map <Thread *, bool > runningStatus; // sender thread
533533
534534 for (const auto &[broadcast, senderThread] : m_broadcastSenders) {
535535 std::unordered_map<Broadcast *, std::vector<Script *>> *broadcastMap = nullptr ;
@@ -544,44 +544,41 @@ void Engine::step()
544544 broadcastMap = &m_broadcastMap;
545545 }
546546
547- bool found = false ;
547+ bool isRunning = false ;
548548
549549 if (broadcastMap->find (broadcast) != broadcastMap->cend ()) {
550550 const auto &scripts = (*broadcastMap)[broadcast];
551551
552552 for (auto script : scripts) {
553553 if (std::find_if (m_threads.begin (), m_threads.end (), [script](std::shared_ptr<Thread> thread) { return thread->script () == script; }) != m_threads.end ()) {
554- found = true ;
554+ isRunning = true ;
555555 break ;
556556 }
557557 }
558558 }
559559
560- if (found) {
561- // If a broadcast with the same name but different case
562- // was considered stopped before, restore the promise.
563- if (std::find (resolvedThreads.begin (), resolvedThreads.end (), senderThread) != resolvedThreads.end ()) {
564- senderThread->promise ();
565- resolvedThreads.erase (std::remove (resolvedThreads.begin (), resolvedThreads.end (), senderThread), resolvedThreads.end ());
566- }
567- } else {
568- Thread *th = senderThread;
560+ if (runningStatus.find (senderThread) == runningStatus.cend () || isRunning)
561+ runningStatus[senderThread] = isRunning;
569562
570- if (std::find_if (m_threads.begin (), m_threads.end (), [th](std::shared_ptr<Thread> thread) { return thread.get () == th; }) != m_threads.end ()) {
571- auto promise = th->promise ();
563+ if (!isRunning)
564+ stoppedBroadcasts[broadcast] = senderThread;
565+ }
572566
573- if (promise)
574- promise->resolve ();
575- }
567+ for (const auto &[broadcast, senderThread] : stoppedBroadcasts) {
568+ m_broadcastSenders.erase (broadcast);
576569
577- resolved.push_back (broadcast);
578- resolvedThreads.push_back (th);
570+ // Resolve broadcast promise
571+ Thread *th = senderThread;
572+
573+ if (std::find_if (m_threads.begin (), m_threads.end (), [th](std::shared_ptr<Thread> thread) { return thread.get () == th; }) != m_threads.end ()) {
574+ auto promise = th->promise ().get ();
575+
576+ // Resolve only if all broadcasts of the same name but different case are stopped
577+ if (promise && !runningStatus[senderThread])
578+ promise->resolve ();
579579 }
580580 }
581581
582- for (Broadcast *broadcast : resolved)
583- m_broadcastSenders.erase (broadcast);
584-
585582 m_redrawRequested = false ;
586583
587584 // Step threads
0 commit comments