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(experimental): Replace invoke timeout with AbortSignal #540

Merged
merged 1 commit into from
Apr 18, 2024

Conversation

manzt
Copy link
Owner

@manzt manzt commented Apr 18, 2024

This allows more flexible control over aborting the invoke request, including delegating to third-party libraries that manage cancellation.

export default {
  async render({ model, el }) {
    const controller = new AbortController();

    // Randomly abort the request after 1 second
    setTimeout(() => Math.random() < 0.5 && controller.abort(), 1000);

    const signal = controller.signal;
    model
      .invoke("echo", "Hello, world", { signal })
      .then((result) => {
        el.innerHTML = result;
      })
      .catch((err) => {
        el.innerHTML = `Error: ${err.message}`;
      });
  },
};

Copy link

changeset-bot bot commented Apr 18, 2024

🦋 Changeset detected

Latest commit: f0aa55e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
anywidget Patch
@anywidget/types Patch
@anywidget/react Patch
@anywidget/svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@manzt
Copy link
Owner Author

manzt commented Apr 18, 2024

cc: @keller-mark @kylebarron

This means you could get pass down the signal from Deck.gl tile fetcher to abort tile requests.

@manzt manzt changed the title feat(experimental): Replace invoke timeout with AbortSignal feat(experimental): Replace invoke timeout with AbortSignal Apr 18, 2024
@kylebarron
Copy link
Contributor

kylebarron commented Apr 18, 2024

😍

I think it might be worth having both a timeout and a signal. Like set timeout to 10 seconds but stop sooner if the signal was aborted?

@manzt manzt merged commit a4b0ec0 into main Apr 18, 2024
11 checks passed
@manzt manzt deleted the abort-signal branch April 18, 2024 19:14
@github-actions github-actions bot mentioned this pull request Apr 18, 2024
@manzt
Copy link
Owner Author

manzt commented Apr 18, 2024

I think it might be worth having both a timeout and a signal. Like set timeout to 10 seconds but stop sooner if the signal was aborted?

I guess I'd defer this to the caller with AbortSignal.any. What do you think?

let res = await invoke("_get_tile", tileId, {
  signal: AbortSignal.any([deckSignal, AbortSignal.timeout(10_000)]),
});

I guess we could do this internally to have some final fallback.... but deciding on what the timeout should be generally is challenging.

@kylebarron
Copy link
Contributor

I guess I'd defer this to the caller with AbortSignal.any. What do you think?

Oh that's cool! I didn't know that existed. That seems fine!

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