mirror of
https://github.com/Ellpeck/Coroutine.git
synced 2024-11-24 06:18:33 +01:00
fixed cancellation causing the wrong coroutines to be stopped
This commit is contained in:
parent
53e10da875
commit
fa4d021368
2 changed files with 28 additions and 27 deletions
|
@ -14,36 +14,45 @@ namespace Coroutine {
|
|||
this.enumerator.MoveNext();
|
||||
}
|
||||
|
||||
public bool Cancel() {
|
||||
if (this.IsFinished || this.WasCanceled)
|
||||
return false;
|
||||
this.WasCanceled = true;
|
||||
this.IsFinished = true;
|
||||
this.OnFinished?.Invoke(this);
|
||||
return true;
|
||||
}
|
||||
|
||||
internal bool Tick(double deltaSeconds) {
|
||||
var curr = this.enumerator.Current;
|
||||
if (curr != null && curr.Tick(deltaSeconds))
|
||||
this.MoveNext();
|
||||
if (!this.WasCanceled) {
|
||||
var curr = this.enumerator.Current;
|
||||
if (curr != null && curr.Tick(deltaSeconds))
|
||||
this.MoveNext();
|
||||
}
|
||||
return this.IsFinished;
|
||||
}
|
||||
|
||||
internal bool OnEvent(Event evt) {
|
||||
var curr = this.enumerator.Current;
|
||||
if (curr != null && curr.OnEvent(evt))
|
||||
this.MoveNext();
|
||||
if (!this.WasCanceled) {
|
||||
var curr = this.enumerator.Current;
|
||||
if (curr != null && curr.OnEvent(evt))
|
||||
this.MoveNext();
|
||||
}
|
||||
return this.IsFinished;
|
||||
}
|
||||
|
||||
internal void Finish(bool cancel) {
|
||||
this.IsFinished = true;
|
||||
this.WasCanceled = cancel;
|
||||
this.OnFinished?.Invoke(this, cancel);
|
||||
}
|
||||
|
||||
private void MoveNext() {
|
||||
if (!this.enumerator.MoveNext())
|
||||
this.Finish(false);
|
||||
if (!this.enumerator.MoveNext()) {
|
||||
this.IsFinished = true;
|
||||
this.OnFinished?.Invoke(this);
|
||||
}
|
||||
}
|
||||
|
||||
internal WaitType GetCurrentType() {
|
||||
return this.enumerator.Current.GetWaitType();
|
||||
}
|
||||
|
||||
public delegate void FinishCallback(ActiveCoroutine coroutine, bool canceled);
|
||||
public delegate void FinishCallback(ActiveCoroutine coroutine);
|
||||
|
||||
}
|
||||
}
|
|
@ -18,14 +18,6 @@ namespace Coroutine {
|
|||
return inst;
|
||||
}
|
||||
|
||||
public static bool Stop(ActiveCoroutine coroutine) {
|
||||
if (TickingCoroutines.Remove(coroutine) || EventCoroutines.Remove(coroutine)) {
|
||||
coroutine.Finish(true);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void InvokeLater(IWait wait, Action action) {
|
||||
Start(InvokeLaterImpl(wait, action));
|
||||
}
|
||||
|
@ -34,9 +26,9 @@ namespace Coroutine {
|
|||
for (var i = TickingCoroutines.Count - 1; i >= 0; i--) {
|
||||
var coroutine = TickingCoroutines[i];
|
||||
if (coroutine.Tick(deltaSeconds)) {
|
||||
TickingCoroutines.Remove(coroutine);
|
||||
TickingCoroutines.RemoveAt(i);
|
||||
} else if (coroutine.GetCurrentType() != WaitType.Tick) {
|
||||
TickingCoroutines.Remove(coroutine);
|
||||
TickingCoroutines.RemoveAt(i);
|
||||
EventCoroutines.Add(coroutine);
|
||||
}
|
||||
}
|
||||
|
@ -46,9 +38,9 @@ namespace Coroutine {
|
|||
for (var i = EventCoroutines.Count - 1; i >= 0; i--) {
|
||||
var coroutine = EventCoroutines[i];
|
||||
if (coroutine.OnEvent(evt)) {
|
||||
EventCoroutines.Remove(coroutine);
|
||||
EventCoroutines.RemoveAt(i);
|
||||
} else if (coroutine.GetCurrentType() != WaitType.Event) {
|
||||
EventCoroutines.Remove(coroutine);
|
||||
EventCoroutines.RemoveAt(i);
|
||||
TickingCoroutines.Add(coroutine);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue