Fix timing issues with post-Reflex lifecycle callbacks #299
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.
Type of PR (feature, enhancement, bug fix, etc.)
Bug fix
Description
This PR tackles the issue of afterReflex callbacks firing before CableReady operations can be completed, as well as some other housekeeping concerns.
When we added
data-reflex-root
support to SR, we introduced thelast
flag to the CableReady payload. This was always brittle and should have been removed when we addedreflexId
as a concept. Instead, we had many users reporting intermittent issues with their afterReflex callbacks either not firing, or firing but not seeing visual evidence. This is because each morphdom process takes several ms. The fix introduced in #286 was unfortunately ineffective as a remedy.I introduced the concept of attaching
totalOperations
andcompletedOperations
to the promise tracking the Reflex, and removed thelast
flag entirely as it is no longer required. Each time CableReady finishes an operation, thecompletedOperations
count is increased until all have been accounted for, and the callbacks are executed.I also made sure that the callback events, promises and functions all receive
reflexId
. You can access thereflexId
of an unresolved promise by accessingpromise.reflexId
, which is pretty fun. Developers can now confidently track the status of reflex operations as uniquely identified transactions. The reflexId is the new last argument of every function. Developers might need to specify an unusederror
argument placeholder in their event handlers to access thereflexId
; this is not a breaking change because arity was not changed.As part of this update, I modified the
createSubscription
function to be aware of bothmorph
andinnerHtml
functions, as per changes in theSelectorBroadcaster
class.Finally, I removed the unused
morphMode
key from the promise response.Fixes #281
Why should this be added
This should unblock v3.3 for final review and testing.
Checklist