Giter Site home page Giter Site logo

Comments (3)

twschiller avatar twschiller commented on August 22, 2024 1

I've been wondering if we should move to a more stateful approach — if we can at all.

Will take a look at this more this evening. At first glance, the proposal is reinventing ports: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/connect

from webext-messenger.

fregante avatar fregante commented on August 22, 2024 1

I mentioned that in the same comment. The problem with that is that either:

  • the user has to manually connect and hold onto the port explicitly, bringing this complexity back into the app (point 3️⃣), or
  • if we don't change the API, it means we still need to attempt to connect every time a target is messaged but no open ports are found (point 1️⃣)

runtime.connect might be useful, but we still need to know how to adapt the messenger API/behavior around it. Just replacing runtime.sendMessage doesn't solve these issues.

from webext-messenger.

fregante avatar fregante commented on August 22, 2024

I've been wondering if we should move to a more stateful approach — if we can at all.

The Messenger has been built around two core concepts:

  • messaging opaqueness: call a function at an address (target), hide the transport protocol
  • automatic retrying for missing and "unready" targets

The first part makes the communication stateless and independent, which means that the caller doesn't need to know much about the target and its life cycle (blessing and a curse).

The second part appears to be incredibly complicated and variable, which is why we've been tweaking it ever since.


Thinking out loud:

There are a few possible solutions to this, with varying degrees of complexity (none of this grants us bug armistice)

  • runtime.connect():
    • retry only the connection, connect once, reuse connection, know when the target is gone
    • ok, what happens when it closes? We must reopen if it's a background page, but we never try again for {target: "this", page: "sidebar.html"}? Doesn't seem like a direct solution to all
  • webNavigation API:
    • track and find frame IDs, then use a more exact chrome.tabs.sendMessage(tab, frame) message instead of named targets
    • this helps, but seems complex when it's time to retry on a new sidebar in the same tab for example
  • move target tracking to the user side:
    • expose a stateful obtainTarget({tab: 1}) API, then use it in the following communication, as openSidebar(target) and closeSidebar(target). Once the target is closed, it's gone, and you need a new target
    • this forces the user to have to keep track of these targets internally and having to know when to attempt to obtain a new target

This feels like we're going to increase complexity exponentially and makes me long for a more straightforward approach instead. I'd love to be able to drop retrying and moving at least that part to the user side via internal API, like:

const sidebar = {target: "this", page: "sidebar.html"};
await messenger.expectTarget(sidebar); // Pings until it connects

await showPanel(sidebar); // Never retries
await updatePanel(sidebar); // Never retries

If the methods fail, they fail, the target is gone, we deal with it in the application as usual. I think this helps to give us control over when the trying happens "per target" instead of "per function call".

At any given time, we kind of know whether a target should exist or "will open soon". expectTarget in this case would also replace a user land PING_SIDEBAR method and other similar methods we have. It would probably help us emit more exact errors.

from webext-messenger.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.