-
Notifications
You must be signed in to change notification settings - Fork 346
feat(content-preview): support externally-managed preview instance #4693
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,6 +153,13 @@ type Props = { | |
| previewExperiences?: { | ||
| [name: string]: TargetingApi, | ||
| }, | ||
| /** | ||
| * Optional externally-managed Box.Preview instance. When provided, | ||
| * ContentPreview reuses it instead of constructing its own, and on | ||
| * cleanup it only detaches its listeners rather than destroying the | ||
| * instance — the host application retains ownership of its lifecycle. | ||
| */ | ||
| previewInstance?: Object, | ||
| previewLibraryVersion: string, | ||
| previewMode?: 'default' | 'shared_file' | 'shared_folder' | 'editable_shared_file' | 'inline_feed', | ||
| requestInterceptor?: Function, | ||
|
|
@@ -426,8 +433,15 @@ class ContentPreview extends React.PureComponent<Props, State> { | |
| this.clearLoadingIndicatorDelayTimeout(); | ||
| const { onPreviewDestroy } = this.props; | ||
| if (this.preview) { | ||
| this.preview.destroy(); | ||
| this.preview.removeAllListeners(); | ||
| if (this.props.previewInstance) { | ||
| this.preview.removeListener('load', this.onPreviewLoad); | ||
| this.preview.removeListener('preload', this.endLoadingSession); | ||
| this.preview.removeListener('preview_error', this.onPreviewError); | ||
| this.preview.removeListener('preview_metric', this.onPreviewMetric); | ||
| } else { | ||
| this.preview.destroy(); | ||
| this.preview.removeAllListeners(); | ||
| } | ||
| this.preview = undefined; | ||
|
|
||
| onPreviewDestroy(shouldReset); | ||
|
|
@@ -1008,7 +1022,7 @@ class ContentPreview extends React.PureComponent<Props, State> { | |
| useHotkeys: false, | ||
| }; | ||
| const { Preview } = global.Box; | ||
| this.preview = new Preview(); | ||
| this.preview = this.props.previewInstance || new Preview(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Check componentDidUpdate and related lifecycle for previewInstance handling
rg -n 'componentDidUpdate|previewInstance' src/elements/content-preview/ContentPreview.jsRepository: box/box-ui-elements Length of output: 385 🏁 Script executed: #!/bin/bash
set -euo pipefail
ast-grep outline src/elements/content-preview/ContentPreview.js --view expanded
sed -n '480,580p' src/elements/content-preview/ContentPreview.js
sed -n '990,1060p' src/elements/content-preview/ContentPreview.jsRepository: box/box-ui-elements Length of output: 7054 🏁 Script executed: #!/bin/bash
set -euo pipefail
sed -n '580,760p' src/elements/content-preview/ContentPreview.js
rg -n 'previewInstance|destroyPreview|componentWillUnmount|removeListener|destroy\(' src/elements/content-preview/ContentPreview.jsRepository: box/box-ui-elements Length of output: 6664 🏁 Script executed: #!/bin/bash
set -euo pipefail
sed -n '420,450p' src/elements/content-preview/ContentPreview.js | cat -nRepository: box/box-ui-elements Length of output: 1393 Handle 🤖 Prompt for AI Agents |
||
| this.preview.addListener('load', this.onPreviewLoad); | ||
| this.preview.addListener('preload', this.endLoadingSession); | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: box/box-ui-elements
Length of output: 857
🏁 Script executed:
Repository: box/box-ui-elements
Length of output: 4732
🏁 Script executed:
Repository: box/box-ui-elements
Length of output: 2070
Detach all listeners when
previewInstanceis suppliedsrc/elements/content-preview/ContentPreview.js:436-444only removes 4 handlers for external instances, butContentPreviewalso registersthumbnailsOpen,thumbnailsClose,annotator_create, andadvanced_insights_report. Those stay attached to the shared preview instance after unmount/reload.🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1, why do we not use removeAllListeners?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question,
@JChan106— sincepreviewInstanceis externally owned by the host application when it's supplied,removeAllListeners()would strip out any listeners the host app itself registered on that shared instance (and any listeners other consumers might have attached), not just the onesContentPreviewadded. That would be a destructive side effect on an object the component doesn't own.So the intent is correct: only remove the specific listeners
ContentPreviewitself attached, leaving everything else on the shared instance intact for the host to manage.That said, the current cleanup only detaches 4 of the listeners registered in
loadPreview— it's missingthumbnailsOpen,thumbnailsClose,annotator_create(whenshowAnnotationsControlsis set), andadvanced_insights_report(whenadvancedContentInsightsis set). To avoid leaking those onto the shared instance, it'd be worth removing all of them symmetrically indestroyPreview, e.g.:Note that the
thumbnailsOpen/thumbnailsCloselisteners andannotator_create/advanced_insights_reportare currently registered as inline anonymous functions or closures inloadPreview, so they'd need to be hoisted to stable instance methods/references to be removable later.