-
Notifications
You must be signed in to change notification settings - Fork 9
Fix RollbarContext without a payload (#102), make onRender work and document it (#88) #166
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
Open
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0407cc3
Fix RollbarContext without a payload (#102), make onRender work and d…
e5456cd
Convert RollbarContext tests to TSX now that #162's typings are in
d722ee7
Keep the innermost context when nested RollbarContexts change
brianr 7eced8f
Key the context stack by order instead of mutating entries
brianr 751938b
Don't leave an onRender context behind when React discards the render
brianr abca1d9
Correct the note on restoring '' for an unset context on the server
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // The RollbarContext components and useRollbarContext hooks that are mounted | ||
| // and setting a context, per Rollbar client. The innermost one decides the | ||
| // client's context. When the last one goes away, the context from before the | ||
| // first one is restored. | ||
| const stacks = new WeakMap(); | ||
|
|
||
| let lastOrder = 0; | ||
|
|
||
| // Parents render before their children, so an order number taken during the | ||
| // first render ranks nested contexts from outermost to innermost, even though | ||
| // React mounts (and runs effects for) children first. A context that mounts | ||
| // later under an existing one also ranks after it. | ||
| export function nextContextOrder() { | ||
| lastOrder += 1; | ||
| return lastOrder; | ||
| } | ||
|
|
||
| function getStack(rollbar) { | ||
| let stack = stacks.get(rollbar); | ||
| if (!stack) { | ||
| // rollbar.js has no default payload, so options.payload is undefined | ||
| // unless the config sets it. | ||
| stack = { base: rollbar.options.payload?.context, contexts: new Map() }; | ||
| stacks.set(rollbar, stack); | ||
| } | ||
| return stack; | ||
| } | ||
|
|
||
| function applyStack(rollbar, stack) { | ||
| if (stack.contexts.size) { | ||
| const innermost = Math.max(...stack.contexts.keys()); | ||
| rollbar.configure({ payload: { context: stack.contexts.get(innermost) } }); | ||
| return; | ||
| } | ||
| stacks.delete(rollbar); | ||
| // configure() ignores undefined values and there's no way to remove the key, | ||
| // so restoring an unset context needs ''. In the browser that's sent the same | ||
| // as an unset context. On the server it isn't: rollbar.js takes the context | ||
| // from the request's route, and payload.context, even '', replaces it. Only | ||
| // onRender restores on the server, since nothing mounts there; the README | ||
| // covers this. | ||
| rollbar.configure({ payload: { context: stack.base ?? '' } }); | ||
| } | ||
|
|
||
| // Adds the context for `order`, or applies its new value if it's already | ||
| // there. | ||
| export function setContext(rollbar, order, context) { | ||
| const stack = getStack(rollbar); | ||
| stack.contexts.set(order, context); | ||
| applyStack(rollbar, stack); | ||
| } | ||
|
|
||
| export function removeContext(rollbar, order) { | ||
| const stack = stacks.get(rollbar); | ||
| if (stack?.contexts.delete(order)) { | ||
| applyStack(rollbar, stack); | ||
| } | ||
| } | ||
|
|
||
| // For RollbarContext's onRender: sets `context` while the component renders, | ||
| // before it has mounted and been added with setContext. React can throw that | ||
| // render away without mounting anything, for example when an ErrorBoundary | ||
| // around the component catches an error from its children, and nothing mounts | ||
| // on the server. So a microtask applies the context of whatever is mounted | ||
| // again. React commits in the same task that it finishes rendering in, and an | ||
| // ErrorBoundary reports during the commit, so the microtask runs after both. | ||
| // If a transition yields partway through rendering, the microtask runs then, | ||
| // but when a child throws, React renders again from the start, synchronously, | ||
| // before it commits. | ||
| export function setRenderContext(rollbar, context) { | ||
| // Taken before the context changes, so that it's the one restored if | ||
| // nothing is mounted. | ||
| const stack = getStack(rollbar); | ||
| rollbar.configure({ payload: { context } }); | ||
| Promise.resolve().then(() => { | ||
| if (stacks.get(rollbar) === stack) { | ||
| applyStack(rollbar, stack); | ||
| } | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.