diff --git a/src/UniTask.NetCoreTests/TriggerEventTest.cs b/src/UniTask.NetCoreTests/TriggerEventTest.cs index 1efc4f2..0daae06 100644 --- a/src/UniTask.NetCoreTests/TriggerEventTest.cs +++ b/src/UniTask.NetCoreTests/TriggerEventTest.cs @@ -2,12 +2,7 @@ using FluentAssertions; using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading; -using System.Threading.Channels; -using Cysharp.Threading.Tasks.Linq; -using System.Threading.Tasks; using Xunit; namespace NetCoreTests diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/TriggerEvent.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/TriggerEvent.cs index 9f46b10..ba0e40d 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/TriggerEvent.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/TriggerEvent.cs @@ -42,7 +42,6 @@ namespace Cysharp.Threading.Tasks while (h != null) { iteratingNode = h; - var next = h.Next; try { @@ -54,7 +53,9 @@ namespace Cysharp.Threading.Tasks Remove(h); } - h = next; + // If `h` itself is removed by OnNext, h.Next is null. + // Therefore, instead of looking at h.Next, the `iteratingNode` reference itself is replaced. + h = h == iteratingNode ? h.Next : iteratingNode; } iteratingNode = null; @@ -238,18 +239,20 @@ namespace Cysharp.Threading.Tasks { head = next; } - else if (handler == iteratingHead) + // when handler is head, prev indicate last so don't use it. + else if (prev != null) + { + prev.Next = next; + } + + if (handler == iteratingNode) + { + iteratingNode = next; + } + if (handler == iteratingHead) { iteratingHead = next; } - else - { - // when handler is head, prev indicate last so don't use it. - if (prev != null) - { - prev.Next = next; - } - } if (head != null) {