Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Coroutine instances #3154

Merged
merged 3 commits into from
Aug 3, 2024
Merged

feat: Coroutine instances #3154

merged 3 commits into from
Aug 3, 2024

Conversation

eonarheim
Copy link
Member

@eonarheim eonarheim commented Aug 1, 2024

===:clipboard: PR Checklist :clipboard:===

  • 📌 issue exists in github for these changes
  • 🔬 existing tests still pass
  • 🙈 code conforms to the style guide
  • 📐 new tests written and passing / old tests updated with new scenario(s)
  • 📄 changelog entry added (or not needed)

==================

This PR gives users greater flexibility with Excalibur Coroutines!

  • Optionally ask coroutines not to start immediately

    ex.coroutine(function* () { .. }, { autostart: false });
  • New CoroutineInstance is returned that is also awaitable

    export interface CoroutineOptions {
      timing?: ScheduledCallbackTiming;
      autostart?: boolean;
    }
     
    export interface CoroutineInstance extends PromiseLike<void> {
      isRunning(): boolean;
      isComplete(): boolean;
      done: Promise<void>;
      generator: Generator<CoroutineInstance | number | Promise<any> | undefined, void, number>;
      start: () => CoroutineInstance;
      cancel: () => void;
      then: Thenable;
      [Symbol.iterator]: () => Generator<CoroutineInstance | number | Promise<any> | undefined, void, number>;
    }
    const co = ex.coroutine(function* () { 
      yield 100;
    });
    
    await co; // wait for coroutine to finish
  • start()/cancel()coroutines

    const co = ex.coroutine(function* () { 
      yield 100;
    });
    
    co.start();
    co.cancel();
    
    await co;
  • nested coroutines! (Nested coroutines do not start running, they are run with their parent)

    const result = ex.coroutine(function* () {
        yield 100;
        yield* ex.coroutine(function* () {
          const elapsed = yield 99;
        });
        yield 100;
    });

@github-actions github-actions bot added the enhancement Label applied to enhancements or improvements to existing features label Aug 1, 2024
@eonarheim eonarheim requested a review from mattjennings August 1, 2024 14:11
Copy link

cloudflare-workers-and-pages bot commented Aug 1, 2024

Deploying excaliburjs with  Cloudflare Pages  Cloudflare Pages

Latest commit: bbf5e3a
Status: ✅  Deploy successful!
Preview URL: https://96dba774.excaliburjs.pages.dev
Branch Preview URL: https://feat-coroutine-instances.excaliburjs.pages.dev

View logs

@eonarheim eonarheim merged commit 98ea167 into main Aug 3, 2024
7 of 8 checks passed
@eonarheim eonarheim deleted the feat/coroutine-instances branch August 3, 2024 00:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Label applied to enhancements or improvements to existing features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant