mirror of
https://github.com/Ellpeck/Coroutine.git
synced 2024-11-24 14:18:34 +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();
|
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) {
|
internal bool Tick(double deltaSeconds) {
|
||||||
var curr = this.enumerator.Current;
|
if (!this.WasCanceled) {
|
||||||
if (curr != null && curr.Tick(deltaSeconds))
|
var curr = this.enumerator.Current;
|
||||||
this.MoveNext();
|
if (curr != null && curr.Tick(deltaSeconds))
|
||||||
|
this.MoveNext();
|
||||||
|
}
|
||||||
return this.IsFinished;
|
return this.IsFinished;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool OnEvent(Event evt) {
|
internal bool OnEvent(Event evt) {
|
||||||
var curr = this.enumerator.Current;
|
if (!this.WasCanceled) {
|
||||||
if (curr != null && curr.OnEvent(evt))
|
var curr = this.enumerator.Current;
|
||||||
this.MoveNext();
|
if (curr != null && curr.OnEvent(evt))
|
||||||
|
this.MoveNext();
|
||||||
|
}
|
||||||
return this.IsFinished;
|
return this.IsFinished;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Finish(bool cancel) {
|
|
||||||
this.IsFinished = true;
|
|
||||||
this.WasCanceled = cancel;
|
|
||||||
this.OnFinished?.Invoke(this, cancel);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void MoveNext() {
|
private void MoveNext() {
|
||||||
if (!this.enumerator.MoveNext())
|
if (!this.enumerator.MoveNext()) {
|
||||||
this.Finish(false);
|
this.IsFinished = true;
|
||||||
|
this.OnFinished?.Invoke(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal WaitType GetCurrentType() {
|
internal WaitType GetCurrentType() {
|
||||||
return this.enumerator.Current.GetWaitType();
|
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;
|
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) {
|
public static void InvokeLater(IWait wait, Action action) {
|
||||||
Start(InvokeLaterImpl(wait, action));
|
Start(InvokeLaterImpl(wait, action));
|
||||||
}
|
}
|
||||||
|
@ -34,9 +26,9 @@ namespace Coroutine {
|
||||||
for (var i = TickingCoroutines.Count - 1; i >= 0; i--) {
|
for (var i = TickingCoroutines.Count - 1; i >= 0; i--) {
|
||||||
var coroutine = TickingCoroutines[i];
|
var coroutine = TickingCoroutines[i];
|
||||||
if (coroutine.Tick(deltaSeconds)) {
|
if (coroutine.Tick(deltaSeconds)) {
|
||||||
TickingCoroutines.Remove(coroutine);
|
TickingCoroutines.RemoveAt(i);
|
||||||
} else if (coroutine.GetCurrentType() != WaitType.Tick) {
|
} else if (coroutine.GetCurrentType() != WaitType.Tick) {
|
||||||
TickingCoroutines.Remove(coroutine);
|
TickingCoroutines.RemoveAt(i);
|
||||||
EventCoroutines.Add(coroutine);
|
EventCoroutines.Add(coroutine);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,9 +38,9 @@ namespace Coroutine {
|
||||||
for (var i = EventCoroutines.Count - 1; i >= 0; i--) {
|
for (var i = EventCoroutines.Count - 1; i >= 0; i--) {
|
||||||
var coroutine = EventCoroutines[i];
|
var coroutine = EventCoroutines[i];
|
||||||
if (coroutine.OnEvent(evt)) {
|
if (coroutine.OnEvent(evt)) {
|
||||||
EventCoroutines.Remove(coroutine);
|
EventCoroutines.RemoveAt(i);
|
||||||
} else if (coroutine.GetCurrentType() != WaitType.Event) {
|
} else if (coroutine.GetCurrentType() != WaitType.Event) {
|
||||||
EventCoroutines.Remove(coroutine);
|
EventCoroutines.RemoveAt(i);
|
||||||
TickingCoroutines.Add(coroutine);
|
TickingCoroutines.Add(coroutine);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue