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

webxdc api: Await calls to setUpdateListener() callback #4537

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ralphtheninja
Copy link
Member

@ralphtheninja ralphtheninja commented Jan 23, 2025

This allows an async callback to be passed to webxdc.setUpdateListener().

Let me illustrate with an example by taking the simplest example from the webxdc.org page and tweaking it a bit. So imagine this app:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
    <script src="webxdc.js"></script>
  </head>
  <body>
    <input id="input" type="text"/>
    <a href="" onclick="sendMsg(); return false;">Send</a>
    <p id="output"></p>
    <script>
      const numbers = []

      function sendMsg() {
        msg = document.getElementById("input").value;
        window.webxdc.sendUpdate({payload: msg}, 'Someone typed "'+msg+'".');
      }

      async function somethingReallyAsync () {
        return new Promise((resolve) => {
          setTimeout(() => {
            resolve()
          }, 100)
        })
      }

      async function doSomething (update) {
        const n = update.payload
        console.log('number payload', n)
        if (!numbers.includes(n)) {
          await somethingReallyAsync()
          numbers.push(n)
          document.getElementById('output').innerHTML += update.payload + "<br>";
        }
      }

      window.webxdc.setUpdateListener(async (update) => {
        await doSomething(update)
      }, 0);
    </script>
  </body>
</html>

It allows you to input numbers but only shows the ones you haven't already put in. So you can type like 1 and hit send a couple of times, then you just see a single 1, then you type a 2 maybe and hit send a couple of times. It works.

Now, when you refresh the browser, it'll show everything you have put in. This means you can't have local state (this is just a javascript array) and do something async while updating that state (exemplified by somethingReallyAsync()).

If you instead await callback() this works.

Note that if you run this in the browser and you're using the local webxdc.js variant, the setUpdateListener method needs to change to something like:

image

This allows an async callback to be passed to
webxdc.setUpdateListener()
@nicodh
Copy link
Member

nicodh commented Jan 23, 2025

The change makes sense to me. The webxdc API is not precise on the callback. In https://webxdc.org/docs/spec/setUpdateListener.html it says
"The returned promise resolves when the listener has processed all the update messages known at the time when setUpdateListener was called."
That would require a "await" for async callbacks.

@r10s Looking at Android https://github.com/deltachat/deltachat-android/blob/main/src/main/res/raw/webxdc.js the callback is called without await.

So if we fix that in Desktop it should be updated there too.

And maybe update the API:
With setUpdateListener() you define a (optionally async) callback that receives the updates sent by sendUpdate().

Copy link
Collaborator

@WofWca WofWca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • As @nicodh said, the spec needs to be updated.
  • This is gonna be a breaking change for the apps. Need to consider how we want to handle this fact. One hypothetical situation where it's breaking is where one async update listener call awaits for another update listener call.
  • Although this is also true for the current version of the webxdc spec, we probably need to say what should happen when an error occurs in the listener, or when the promise (thenable) rejects.

@Simon-Laux
Copy link
Member

Simon-Laux commented Jan 23, 2025

I made a pr to the types repo with todo where else it needs to be changed: webxdc/webxdc-types#13

@r10s
Copy link
Member

r10s commented Jan 23, 2025

@ralphtheninja can you add to the initial PR description why this change is needed and what is now possible what was not possible before.

esp. as this change requires quite some upfollownings and the impact to existing webxdc apps seems not 100% clear

also, this should not go to current release

@ralphtheninja
Copy link
Member Author

ralphtheninja commented Jan 23, 2025

@ralphtheninja can you add to the initial PR description why this change is needed and what is now possible what was not possible before.

esp. as this change requires quite some upfollownings and the impact to existing webxdc apps seems not 100% clear

also, this should not go to current release

@r10s I've updated the PR text to show more what the problem is.

@ralphtheninja
Copy link
Member Author

It's not really clear to me if just awaiting is good enough. What happens if the callback throws etc? This should be tested also. So maybe let this PR hang a bit here a while.

@Simon-Laux Simon-Laux changed the title Await calls to setUpdateListener() callback webxdc api: Await calls to setUpdateListener() callback Jan 27, 2025
@ralphtheninja
Copy link
Member Author

Side note but related: Can the listener in realtimeChannel.setListener(cb) be async?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants