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

Fix race condition when setting up Strada components #27

Merged
merged 4 commits into from
Mar 1, 2024

Conversation

dginsburg
Copy link
Contributor

@dginsburg dginsburg commented Jan 4, 2024

There is a race condition that has been discussed in #20. The symptom is that messages aren't received in the native adapter from Strada JS components after pulling to refresh.

Racer 1

The JS shim is injected .atDocumentStart

return WKUserScript(source: source, injectionTime: .atDocumentStart, forMainFrameOnly: true)

ready message is posted to the native adapter

window.nativeBridge = new NativeBridge()
window.nativeBridge.postMessage("ready")

It goes through the Swift code and eventually calls register to set the supportedComponents

register(component) {
if (Array.isArray(component)) {
this.supportedComponents = this.supportedComponents.concat(component)
} else {
this.supportedComponents.push(component)
}
this.notifyBridgeOfSupportedComponentsUpdate()
}

Racer 2

strada-web dispatches web-bridge: ready
https://github.com/hotwired/strada-web/blob/df1c2b40234286dad55815aaf78b31f8691693e9/src/bridge.js#L18-L20

The native app calls setAdapter upon receiving that event

document.addEventListener("web-bridge:ready", () => this.webBridge.setAdapter(this))

Once the adapter is set, strada-web is free to send messages. Before it sends a message it calls supportsComponent.

supportsComponent(component) {
return this.supportedComponents.includes(component)
}

Solution

Racer 1 is setting supportedComponents and Racer 2 requires it to already be set. The two racers aren't correlated and either path can get there first.

I think something like this PR is the best way to solve it. It's NativeBridge's responsibility to be fully set up before calling setAdapter.

I'm not a JS dev and used AI ✨ to help me write a Promise. Tell me or just change it if you think there is a better way.

@dginsburg dginsburg marked this pull request as ready for review January 5, 2024 14:41
@svara
Copy link
Collaborator

svara commented Mar 1, 2024

It's NativeBridge's responsibility to be fully set up before calling setAdapter.

Agree, and I like the proposed solution.

@jayohms @joemasilotti Could you guys take a look at the code please?

@joemasilotti
Copy link
Member

Approved assuming that when this gets merged on top of #32 the tests will pass.

@joemasilotti joemasilotti merged commit e2e8a7c into hotwired:main Mar 1, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants