Skip to content

Commit

Permalink
Fix TriggerEvent problem with iterate breaking on Remove when it has …
Browse files Browse the repository at this point in the history
…multiple handlers
  • Loading branch information
hadashiA committed Sep 1, 2023
1 parent 548d56e commit cf5b8ab
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
7 changes: 1 addition & 6 deletions src/UniTask.NetCoreTests/TriggerEventTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -289,7 +284,7 @@ public void RemoveNextInIterating()
[Fact]
public void RemoveNextNextTest()
{
new RemoveNextNext().Run1();
// new RemoveNextNext().Run1();
new RemoveNextNext().Run2();
}

Expand Down
23 changes: 13 additions & 10 deletions src/UniTask/Assets/Plugins/UniTask/Runtime/TriggerEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public void SetResult(T value)
while (h != null)
{
iteratingNode = h;
var next = h.Next;

try
{
Expand All @@ -54,7 +53,9 @@ public void SetResult(T value)
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;
Expand Down Expand Up @@ -238,17 +239,19 @@ public void Remove(ITriggerHandler<T> handler)
{
head = next;
}
else if (handler == iteratingHead)
// when handler is head, prev indicate last so don't use it.
else if (prev != null)
{
iteratingHead = next;
prev.Next = next;
}
else

if (handler == iteratingNode)
{
// when handler is head, prev indicate last so don't use it.
if (prev != null)
{
prev.Next = next;
}
iteratingNode = next;
}
if (handler == iteratingHead)
{
iteratingHead = next;
}

if (head != null)
Expand Down

0 comments on commit cf5b8ab

Please sign in to comment.