10 public partial class Fiber {
12 public Fiber
WaitFor(IEnumerator enumerator) =>
13 AddAction(_ => LoadWithPayload(enumerator, 0),
"WaitFor(IEnumerator)");
16 public Fiber
WaitFor(Func<Fiber, IEnumerator> getter) =>
17 AddAction(_ => LoadWithPayload(getter(
this), 0),
"WaitFor(IEnumerator)");
20 public Fiber
WaitFor(
int framesBetweenChecks, IEnumerator enumerator) =>
21 AddAction(_ => LoadWithPayload(enumerator, framesBetweenChecks),
"WaitFor(framesBetweenChecks, IEnumerator)");
23 private Fiber LoadWithPayload(IEnumerator enumerator,
int skipFrames) {
24 var payload =
new EnumeratorWorker.Payload {Enumerator = enumerator,
SkipFrames = skipFrames};
25 return EnumeratorWorker.
Instance.Load(
this, payload);
29 private class EnumeratorWorker : Worker<EnumeratorWorker.Payload> {
33 protected override void Recycle() => Cache<EnumeratorWorker>.
Dispose(
this);
37 internal IEnumerator Enumerator;
38 internal int SkipFrames;
41 private int nextStepFrame;
43 protected override bool Prepare() =>
true;
45 protected override int CompareTo(
Worker other) =>
46 Seed.SkipFrames.CompareTo((other as EnumeratorWorker)?.Seed.SkipFrames);
48 public override void Step() {
49 if (Time.frameCount < nextStepFrame)
return;
51 nextStepFrame = Time.frameCount + Seed.SkipFrames;
53 if (Seed.Enumerator.MoveNext()) {
54 switch (Seed.Enumerator.Current) {
55 case IEnumerator coroutine:
62 nextStepFrame = Time.frameCount + frames;
64 case YieldInstruction _:
66 $
"YieldInstruction {Seed.Enumerator.Current.GetType()} only works with Unity coroutines");
Abstract code to implement a worker
lightweight cooperative multi-tasking
Fiber WaitFor(IClosure closure)
Helper that is the same as fiber.WaitFor(closure.OnComplete)
void Dispose()
Cleans up Fiber before it goes into the recycling
static Fiber Instance
Precompile an instance of a fiber command
Fiber SkipFrames(int framesToSkip)
Wait a specific count of Update, FixedUpdate or LateUpdate frames