feat: refactor to _useSession
semantics
#726
Merged
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.
getSession()
is problematic. The issue comes because whenever the library itself does anything with the session, it may be very quick < 10ms or very slow >= 10s. For example, if a session needs to be refreshed, when you callgetSession()
the response may be very quick or take very long.When such long processing is required, a race condition arises between multiple windows or tabs concurrently doing the same processing. In reality, only one such tab / window (in further text "process") needs to do the processing. Therefore, just using a concept like a method call to
getSession()
is not advisable.With this change, the internals of the library use a new function called
_useSession()
which expects a callback to be provided. So long as the callback's promise has not resolved, the session is "being used." In follow up PRs, locking semantics will be used to ensure that only one_useSession
callback is active amongst all processes.The existing
getSession()
computation is put behind__loadSession
which should not be used outside of_useSession()
. A debug-mode stack check is performed to make sure that issues like that are caught.This PR also adds two new utility functions
stackGuard
andisInStackGuard
. These can be used to detect recursive calls or inappropriate use of methods. For example, in this PR a stack guard is used at debug time to make sure that__loadSession
is only called within_useSession
, as that is the only valid use of the method. In follow up versions this will become a runtime exception.It works because when using async/await JS engines like browsers and Node will respect stack traces. A stack guard is a special string that shows up in the stack trace of the
Error
object, which if seen (byisInStackGuard
) can be used to detect whether a function was called from the correct parent-caller or whether there is a recursive call.It's not possible to use plain functions because of JS minification, which mangles the name of virtually all functions, so a trick has to be used to dynamically generate a function name which is the special stack guard string.