Compare commits

...

4 Commits

3 changed files with 21 additions and 84 deletions

View File

@ -42,7 +42,6 @@ namespace Cysharp.Threading.Tasks
while (h != null) while (h != null)
{ {
iteratingNode = h; iteratingNode = h;
var next = h.Next;
try try
{ {
@ -54,7 +53,9 @@ namespace Cysharp.Threading.Tasks
Remove(h); 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; iteratingNode = null;
@ -84,9 +85,9 @@ namespace Cysharp.Threading.Tasks
{ {
LogError(ex); LogError(ex);
} }
iteratingNode = null; iteratingNode = null;
var next = h.Next; var next = h == iteratingNode ? h.Next : iteratingNode;
Remove(h); Remove(h);
h = next; h = next;
} }
@ -118,9 +119,9 @@ namespace Cysharp.Threading.Tasks
{ {
LogError(ex); LogError(ex);
} }
var next = h == iteratingNode ? h.Next : iteratingNode;
iteratingNode = null; iteratingNode = null;
var next = h.Next;
Remove(h); Remove(h);
h = next; h = next;
} }
@ -152,9 +153,9 @@ namespace Cysharp.Threading.Tasks
{ {
LogError(ex); LogError(ex);
} }
var next = h == iteratingNode ? h.Next : iteratingNode;
iteratingNode = null; iteratingNode = null;
var next = h.Next;
Remove(h); Remove(h);
h = next; h = next;
} }
@ -225,7 +226,7 @@ namespace Cysharp.Threading.Tasks
public void Remove(ITriggerHandler<T> handler) public void Remove(ITriggerHandler<T> handler)
{ {
if (handler == null) throw new ArgumentNullException(nameof(handler)); if (handler == null) throw new ArgumentNullException(nameof(handler));
var prev = handler.Prev; var prev = handler.Prev;
var next = handler.Next; var next = handler.Next;
@ -238,18 +239,20 @@ namespace Cysharp.Threading.Tasks
{ {
head = next; 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; iteratingHead = next;
} }
else
{
// when handler is head, prev indicate last so don't use it.
if (prev != null)
{
prev.Next = next;
}
}
if (head != null) if (head != null)
{ {

View File

@ -1,63 +0,0 @@
using Cysharp.Threading.Tasks;
using FluentAssertions;
using System;
using System.Collections;
using System.Threading;
using UnityEngine.TestTools;
namespace Cysharp.Threading.TasksTests
{
public class AsyncReactivePropertyTest
{
private int _callCounter;
[UnityTest]
public IEnumerator WaitCancelWait() => UniTask.ToCoroutine(async () =>
{
// Test case for https://github.com/Cysharp/UniTask/issues/444
var property = new AsyncReactiveProperty<int>(0);
var cts1 = new CancellationTokenSource();
var cts2 = new CancellationTokenSource();
WaitForProperty(property, cts1.Token);
WaitForProperty(property, cts2.Token);
_callCounter = 0;
property.Value = 1;
_callCounter.Should().Be(2);
cts2.Cancel();
cts2.Dispose();
cts1.Cancel();
cts1.Dispose();
var cts3 = new CancellationTokenSource();
WaitForProperty(property, cts3.Token);
_callCounter = 0;
property.Value = 2;
_callCounter.Should().Be(1);
cts3.Cancel();
cts3.Dispose();
await UniTask.CompletedTask;
});
private async void WaitForProperty(AsyncReactiveProperty<int> property, CancellationToken token)
{
while (true)
{
try
{
await property.WaitAsync(token);
_callCounter++;
}
catch (OperationCanceledException)
{
break;
}
}
}
}
}

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 27665955eefb4448969b8cc4dd204600
timeCreated: 1676129650