-
Notifications
You must be signed in to change notification settings - Fork 200
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
Changes from 1 commit
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 |
---|---|---|
@@ -1,10 +1,17 @@ | ||
/** | ||
* @typedef {import('../shared/bridge').Bridge} Bridge | ||
* @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 | ||
*/ | ||
|
||
/** | ||
* @template {GuestToSidebarEvent} T | ||
* @template {SidebarToGuestEvent} U | ||
* @typedef {import('../shared/bridge').Bridge<T,U>} Bridge | ||
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. Since we only ever use /**
* @typedef {import('../shared/bridge').Bridge<GuestToSidebarEvent, SidebarToGuestEvent>} SidebarBridge
*/ 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. 👍🏼 |
||
*/ | ||
|
||
/** | ||
* Message sent between the sidebar and annotator about an annotation that has | ||
* changed. | ||
|
@@ -26,11 +33,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 {Bridge<GuestToSidebarEvent,SidebarToGuestEvent>} 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 +50,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 +62,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 +77,7 @@ export class AnnotationSync { | |
if (annotation.$tag) { | ||
return; | ||
} | ||
this.bridge.call('beforeCreateAnnotation', this._format(annotation)); | ||
this._sidebar.call('beforeCreateAnnotation', this._format(annotation)); | ||
}); | ||
} | ||
|
||
|
@@ -87,7 +94,7 @@ export class AnnotationSync { | |
return; | ||
} | ||
|
||
this.bridge.call( | ||
this._sidebar.call( | ||
'sync', | ||
annotations.map(ann => this._format(ann)) | ||
); | ||
|
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'); | ||
|
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.