-
Notifications
You must be signed in to change notification settings - Fork 198
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
Strengthen the types for Bridge
events
#3838
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
/** | ||
* @typedef {import('../shared/bridge').Bridge} Bridge | ||
* @typedef {import('../shared/bridge').Bridge<GuestToSidebarEvent,SidebarToGuestEvent>} SidebarBridge | ||
* @typedef {import('../types/annotator').AnnotationData} AnnotationData | ||
* @typedef {import('../types/annotator').Destroyable} Destroyable | ||
* @typedef {import('../types/bridge-events').GuestToSidebarEvent} GuestToSidebarEvent | ||
* @typedef {import('../types/bridge-events').SidebarToGuestEvent} SidebarToGuestEvent | ||
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. Are these typedefs still needed given the change to the |
||
* @typedef {import('./util/emitter').EventBus} EventBus | ||
*/ | ||
|
||
|
@@ -26,11 +28,11 @@ | |
export class AnnotationSync { | ||
/** | ||
* @param {EventBus} eventBus - Event bus for communicating with the annotator code (eg. the Guest) | ||
* @param {Bridge} bridge - Channel for communicating with the sidebar | ||
* @param {SidebarBridge} bridge - Channel for communicating with the sidebar | ||
*/ | ||
constructor(eventBus, bridge) { | ||
this._emitter = eventBus.createEmitter(); | ||
this.bridge = bridge; | ||
this._sidebar = bridge; | ||
|
||
/** | ||
* Mapping from annotation tags to annotation objects for annotations which | ||
|
@@ -43,7 +45,7 @@ export class AnnotationSync { | |
this.destroyed = false; | ||
|
||
// Relay events from the sidebar to the rest of the annotator. | ||
this.bridge.on('deleteAnnotation', (body, callback) => { | ||
this._sidebar.on('deleteAnnotation', (body, callback) => { | ||
if (this.destroyed) { | ||
callback(null); | ||
return; | ||
|
@@ -55,7 +57,7 @@ export class AnnotationSync { | |
callback(null); | ||
}); | ||
|
||
this.bridge.on('loadAnnotations', (bodies, callback) => { | ||
this._sidebar.on('loadAnnotations', (bodies, callback) => { | ||
if (this.destroyed) { | ||
callback(null); | ||
return; | ||
|
@@ -70,7 +72,7 @@ export class AnnotationSync { | |
if (annotation.$tag) { | ||
return; | ||
} | ||
this.bridge.call('beforeCreateAnnotation', this._format(annotation)); | ||
this._sidebar.call('beforeCreateAnnotation', this._format(annotation)); | ||
}); | ||
} | ||
|
||
|
@@ -87,7 +89,7 @@ export class AnnotationSync { | |
return; | ||
} | ||
|
||
this.bridge.call( | ||
this._sidebar.call( | ||
'sync', | ||
annotations.map(ann => this._format(ann)) | ||
); | ||
|
This file contains 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 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 |
---|---|---|
|
@@ -25,6 +25,8 @@ import { normalizeURI } from './util/url'; | |
* @typedef {import('../types/annotator').Destroyable} Destroyable | ||
* @typedef {import('../types/annotator').SidebarLayout} SidebarLayout | ||
* @typedef {import('../types/api').Target} Target | ||
* @typedef {import('../types/bridge-events').GuestToSidebarEvent} GuestToSidebarEvent | ||
* @typedef {import('../types/bridge-events').SidebarToGuestEvent} SidebarToGuestEvent | ||
* @typedef {import('./util/emitter').EventBus} EventBus | ||
*/ | ||
|
||
|
@@ -163,7 +165,11 @@ export default class Guest { | |
// The "top" guest instance will have this as null since it's in a top frame not a sub frame | ||
this._frameIdentifier = config.subFrameIdentifier || null; | ||
|
||
// Set up listeners for messages coming from the sidebar to this guest. | ||
/** | ||
* Channel for sidebar-guest communication. | ||
* | ||
* @type {Bridge<GuestToSidebarEvent,SidebarToGuestEvent>} | ||
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. |
||
*/ | ||
this._bridge = new Bridge(); | ||
this._bridge.onConnect(() => { | ||
this._emitter.publish('panelReady'); | ||
|
This file contains 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 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 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 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.
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 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.
Note that since no template parameters are supplied here, the type will be
Bridge<any, any>
. As a result, for calls toon
andcall
, TS won't even check if the first argument is a string.Passing in the
bridge
here is not really the best separation of responsibilities. What I would suggest we do in future is make the sidebar responsible for listening for the message and instead put only the logic for updating the page elements (the contents ofupdateAnnotationCountElems
here) in a function in a separate module.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.
The issue about
any
type doesn't seem to happen in version 4.5:https://www.staging-typescript.org/play?ts=4.5.0-dev.20211015&filetype=js#code/PQKhCgAIUgBAXApgWwA4BsCGTIG8DkAFouugPb4A++ARgJ6L4C+kAKlDLKpgE6bJ5WLTB2DgAxlgDOUyMjoBhabNxRI60BHXrO3PgNxDII7dDGnxmUgApMASlWmLZAHZSy6RADpyAc1t2ANxq6kzgYeDgmnDwdKiIePJKmDIAPEQk5PgAfCwgYuKuUvCQAJZu8Jgu4ogAjJAAvJAuiADucorK1kHg5cVVNbVeljYZpBQ9UWAxcQm4Scrp9Iy5ZhJFJX2V1YgATI3NbR3JMt3BWwN7w1bo1rQM+JOFFWUVlwDMBy3tCylSZ703jt3tdRsRxo9zkCaiCRrd7owekA
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.
I agree.