Fix race condition when setting up Strada components #27
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
strada-ios/Source/Bridge.swift
Line 147 in d8c1bcf
ready
message is posted to the native adapterstrada-ios/Source/strada.js
Lines 70 to 71 in d8c1bcf
It goes through the Swift code and eventually calls
register
to set thesupportedComponents
strada-ios/Source/strada.js
Lines 11 to 19 in d8c1bcf
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 eventstrada-ios/Source/strada.js
Line 8 in d8c1bcf
Once the adapter is set, strada-web is free to send messages. Before it sends a message it calls
supportsComponent
.strada-ios/Source/strada.js
Lines 35 to 37 in d8c1bcf
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 callingsetAdapter
.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.