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

Add SDL_WaitEvent and SDL_WaitEventTimeout #14

Merged
merged 4 commits into from
Jul 21, 2023

Conversation

jingkaimori
Copy link
Contributor

@jingkaimori jingkaimori commented Jul 18, 2023

These two api will wait a long time until next event arrives. #7

An async iterator using SDL_WaitEvent shown here:

const SDLEvents: AsyncIterator<SDL.Event, never, SDL.Event | undefined> = (() => {
  let _event = new SDL.Event();
  return {
    next(event): Promise<IteratorResult<SDL.Event, never>> {
      if (event) {
        _event = event;
      }
      return new Promise<IteratorResult<SDL.Event, never>>((resolve, reject) => {
        const res = SDL.WaitEvent(_event);
        if (res == 0) {
          reject(SDL.GetError());
        } else {
          resolve({
            done: false,
            value: _event,
          });
        }
      });
    },
  };
})();

@smack0007
Copy link
Owner

smack0007 commented Jul 18, 2023

Thinking: There is a Memory class that contains right now only a single method to read memory. Maybe your AsyncIterator example can be packed into an Events class. Maybe something like:

for await (const _event of Events.asyncIterator()) {
// ...
}

src/events.ts Outdated Show resolved Hide resolved
@smack0007
Copy link
Owner

smack0007 commented Jul 19, 2023

I couldn't quite get this to work. SDL_WaitEvent works on it's own but Events.asyncIterator doesn't seem to. Did you test Events.asyncIterator() locally? Can you please provide a simple example project?

You can see my testing here: https://github.com/smack0007/SDL_ts/tree/wait-event
I tried to modify the hello-world example to use the Events.asyncIterator() method. I'm not exactly a whiz at async iterators but I would like to know that the Events.asyncIterator() method actually works before merging.

Nevermind I got it to work.

@smack0007
Copy link
Owner

Please copy the hello-world-async example from here: https://github.com/smack0007/SDL_ts/tree/wait-event/examples/hello-world-async

You'll also need to export Events from mod.ts:

export * from "./src/events.ts";

@smack0007 smack0007 merged commit 2bb4e28 into smack0007:main Jul 21, 2023
@smack0007
Copy link
Owner

Ok looks good to me now. Thank you for your time and contribution.

@smack0007 smack0007 mentioned this pull request Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants