Skip to content

Commit e6b39e5

Browse files
committed
Strengthen the types for Bridge events
I have created type definitions for all the event names that are sent across the different frames using various `Bridge`s. It is based on the previous `bridge-events.js`. I broke down the events in four sections based on the direction of the messages: * guest -> sidebar events * host -> sidebar events * sidebar -> guest/s events * sidebar -> host events For those events that didn't have a description I added one. This is more stringent and less verbose than the previous lookup system.
1 parent 910b3da commit e6b39e5

22 files changed

+276
-166
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@
123123
"scripts": {
124124
"build": "cross-env NODE_ENV=production gulp build",
125125
"lint": "eslint .",
126-
"checkformatting": "prettier --check '**/*.{js,scss}'",
127-
"format": "prettier --list-different --write '**/*.{js,scss}'",
126+
"checkformatting": "prettier --check '**/*.{js,scss,d.ts}'",
127+
"format": "prettier --list-different --write '**/*.{js,scss,d.ts}'",
128128
"test": "gulp test",
129129
"typecheck": "tsc --build tsconfig.json",
130130
"report-coverage": "codecov -f coverage/coverage-final.json",

src/annotator/annotation-counts.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import events from '../shared/bridge-events';
2-
31
const ANNOTATION_COUNT_ATTR = 'data-hypothesis-annotation-count';
42

53
/**
@@ -11,8 +9,9 @@ const ANNOTATION_COUNT_ATTR = 'data-hypothesis-annotation-count';
119
* display annotation count.
1210
* @param {import('../shared/bridge').Bridge} bridge - Channel for host-sidebar communication
1311
*/
12+
1413
export function annotationCounts(rootEl, bridge) {
15-
bridge.on(events.PUBLIC_ANNOTATION_COUNT_CHANGED, updateAnnotationCountElems);
14+
bridge.on('publicAnnotationCountChanged', updateAnnotationCountElems);
1615

1716
function updateAnnotationCountElems(newCount) {
1817
const elems = rootEl.querySelectorAll('[' + ANNOTATION_COUNT_ATTR + ']');

src/annotator/annotation-sync.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
/**
2-
* @typedef {import('../shared/bridge').Bridge} Bridge
32
* @typedef {import('../types/annotator').AnnotationData} AnnotationData
43
* @typedef {import('../types/annotator').Destroyable} Destroyable
4+
* @typedef {import('../types/bridge-events').GuestToSidebarEvent} GuestToSidebarEvent
5+
* @typedef {import('../types/bridge-events').SidebarToGuestEvent} SidebarToGuestEvent
56
* @typedef {import('./util/emitter').EventBus} EventBus
67
*/
78

9+
/**
10+
* @template {GuestToSidebarEvent} T
11+
* @template {SidebarToGuestEvent} U
12+
* @typedef {import('../shared/bridge').Bridge<T,U>} Bridge
13+
*/
14+
815
/**
916
* Message sent between the sidebar and annotator about an annotation that has
1017
* changed.
@@ -26,11 +33,11 @@
2633
export class AnnotationSync {
2734
/**
2835
* @param {EventBus} eventBus - Event bus for communicating with the annotator code (eg. the Guest)
29-
* @param {Bridge} bridge - Channel for communicating with the sidebar
36+
* @param {Bridge<GuestToSidebarEvent,SidebarToGuestEvent>} bridge - Channel for communicating with the sidebar
3037
*/
3138
constructor(eventBus, bridge) {
3239
this._emitter = eventBus.createEmitter();
33-
this.bridge = bridge;
40+
this._sidebar = bridge;
3441

3542
/**
3643
* Mapping from annotation tags to annotation objects for annotations which
@@ -43,7 +50,7 @@ export class AnnotationSync {
4350
this.destroyed = false;
4451

4552
// Relay events from the sidebar to the rest of the annotator.
46-
this.bridge.on('deleteAnnotation', (body, callback) => {
53+
this._sidebar.on('deleteAnnotation', (body, callback) => {
4754
if (this.destroyed) {
4855
callback(null);
4956
return;
@@ -55,7 +62,7 @@ export class AnnotationSync {
5562
callback(null);
5663
});
5764

58-
this.bridge.on('loadAnnotations', (bodies, callback) => {
65+
this._sidebar.on('loadAnnotations', (bodies, callback) => {
5966
if (this.destroyed) {
6067
callback(null);
6168
return;
@@ -70,7 +77,7 @@ export class AnnotationSync {
7077
if (annotation.$tag) {
7178
return;
7279
}
73-
this.bridge.call('beforeCreateAnnotation', this._format(annotation));
80+
this._sidebar.call('beforeCreateAnnotation', this._format(annotation));
7481
});
7582
}
7683

@@ -87,7 +94,7 @@ export class AnnotationSync {
8794
return;
8895
}
8996

90-
this.bridge.call(
97+
this._sidebar.call(
9198
'sync',
9299
annotations.map(ann => this._format(ann))
93100
);

src/annotator/features.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import events from '../shared/bridge-events';
21
import warnOnce from '../shared/warn-once';
32

43
let _features = {};
@@ -12,7 +11,7 @@ export const features = {
1211
* @param {import('../shared/bridge').Bridge} bridge - Channel for host-sidebar communication
1312
*/
1413
init: function (bridge) {
15-
bridge.on(events.FEATURE_FLAGS_UPDATED, _set);
14+
bridge.on('featureFlagsUpdated', _set);
1615
},
1716

1817
reset: function () {

src/annotator/guest.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import { normalizeURI } from './util/url';
2525
* @typedef {import('../types/annotator').Destroyable} Destroyable
2626
* @typedef {import('../types/annotator').SidebarLayout} SidebarLayout
2727
* @typedef {import('../types/api').Target} Target
28+
* @typedef {import('../types/bridge-events').GuestToSidebarEvent} GuestToSidebarEvent
29+
* @typedef {import('../types/bridge-events').SidebarToGuestEvent} SidebarToGuestEvent
2830
* @typedef {import('./util/emitter').EventBus} EventBus
2931
*/
3032

@@ -163,7 +165,11 @@ export default class Guest {
163165
// The "top" guest instance will have this as null since it's in a top frame not a sub frame
164166
this._frameIdentifier = config.subFrameIdentifier || null;
165167

166-
// Set up listeners for messages coming from the sidebar to this guest.
168+
/**
169+
* Channel for sidebar-guest communication.
170+
*
171+
* @type {Bridge<GuestToSidebarEvent,SidebarToGuestEvent>}
172+
*/
167173
this._bridge = new Bridge();
168174
this._bridge.onConnect(() => {
169175
this._emitter.publish('panelReady');

src/annotator/sidebar.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Hammer from 'hammerjs';
22

33
import { Bridge } from '../shared/bridge';
4-
import events from '../shared/bridge-events';
54
import { ListenerCollection } from '../shared/listener-collection';
65

76
import { annotationCounts } from './annotation-counts';
@@ -14,6 +13,8 @@ import { createShadowRoot } from './util/shadow-root';
1413

1514
/**
1615
* @typedef {import('./guest').default} Guest
16+
* @typedef {import('../types/bridge-events').HostToSidebarEvent} HostToSidebarEvent
17+
* @typedef {import('../types/bridge-events').SidebarToHostEvent} SidebarToHostEvent
1718
* @typedef {import('../types/annotator').SidebarLayout} SidebarLayout
1819
* @typedef {import('../types/annotator').Destroyable} Destroyable
1920
*/
@@ -64,6 +65,11 @@ export default class Sidebar {
6465
constructor(element, eventBus, guest, config = {}) {
6566
this._emitter = eventBus.createEmitter();
6667

68+
/**
69+
* Channel for sidebar-host communication.
70+
*
71+
* @type {Bridge<HostToSidebarEvent,SidebarToHostEvent>}
72+
*/
6773
this._sidebarRPC = new Bridge();
6874

6975
/**
@@ -252,12 +258,13 @@ export default class Sidebar {
252258
this.show();
253259
});
254260

261+
/** @type {Array<[import('../types/bridge-events').SidebarToHostEvent, function]>} */
255262
const eventHandlers = [
256-
[events.LOGIN_REQUESTED, this.onLoginRequest],
257-
[events.LOGOUT_REQUESTED, this.onLogoutRequest],
258-
[events.SIGNUP_REQUESTED, this.onSignupRequest],
259-
[events.PROFILE_REQUESTED, this.onProfileRequest],
260-
[events.HELP_REQUESTED, this.onHelpRequest],
263+
['loginRequested', this.onLoginRequest],
264+
['logoutRequested', this.onLogoutRequest],
265+
['signupRequested', this.onSignupRequest],
266+
['profileRequested', this.onProfileRequest],
267+
['helpRequested', this.onHelpRequest],
261268
];
262269
eventHandlers.forEach(([event, handler]) => {
263270
if (handler) {

src/annotator/test/annotation-sync-test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { EventBus } from '../util/emitter';
2-
31
import { AnnotationSync } from '../annotation-sync';
2+
import { EventBus } from '../util/emitter';
43

54
describe('AnnotationSync', () => {
65
let createAnnotationSync;
@@ -49,7 +48,7 @@ describe('AnnotationSync', () => {
4948
assert.calledWith(eventStub, ann);
5049
});
5150

52-
it("calls the 'deleteAnnotation' event's callback function", done => {
51+
it('calls the "deleteAnnotation" event\'s callback function', done => {
5352
const ann = { id: 1, $tag: 'tag1' };
5453
const callback = function (err, result) {
5554
assert.isNull(err);

src/annotator/test/features-test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import events from '../../shared/bridge-events';
21
import { features, $imports } from '../features';
32

43
describe('features - annotation layer', () => {
@@ -22,7 +21,7 @@ describe('features - annotation layer', () => {
2221

2322
features.init({
2423
on: function (topic, handler) {
25-
if (topic === events.FEATURE_FLAGS_UPDATED) {
24+
if (topic === 'featureFlagsUpdated') {
2625
featureFlagsUpdateHandler = handler;
2726
}
2827
},

src/annotator/test/guest-test.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import Guest from '../guest';
1+
import Guest, { $imports } from '../guest';
22
import { EventBus } from '../util/emitter';
3-
import { $imports } from '../guest';
43

54
class FakeAdder {
65
constructor(container, options) {
@@ -234,7 +233,7 @@ describe('Guest', () => {
234233
});
235234

236235
describe('events from sidebar', () => {
237-
const emitGuestEvent = (event, ...args) => {
236+
const emitSidebarEvent = (event, ...args) => {
238237
for (let [evt, fn] of fakeBridge.on.args) {
239238
if (event === evt) {
240239
fn(...args);
@@ -252,7 +251,7 @@ describe('Guest', () => {
252251
{ annotation: { $tag: 'tag2' }, highlights: [highlight1] },
253252
];
254253

255-
emitGuestEvent('focusAnnotations', ['tag1']);
254+
emitSidebarEvent('focusAnnotations', ['tag1']);
256255

257256
assert.calledWith(
258257
highlighter.setHighlightsFocused,
@@ -270,7 +269,7 @@ describe('Guest', () => {
270269
{ annotation: { $tag: 'tag2' }, highlights: [highlight1] },
271270
];
272271

273-
emitGuestEvent('focusAnnotations', ['tag1']);
272+
emitSidebarEvent('focusAnnotations', ['tag1']);
274273

275274
assert.calledWith(
276275
highlighter.setHighlightsFocused,
@@ -282,8 +281,8 @@ describe('Guest', () => {
282281
it('updates focused tag set', () => {
283282
const guest = createGuest();
284283

285-
emitGuestEvent('focusAnnotations', ['tag1']);
286-
emitGuestEvent('focusAnnotations', ['tag2', 'tag3']);
284+
emitSidebarEvent('focusAnnotations', ['tag1']);
285+
emitSidebarEvent('focusAnnotations', ['tag2', 'tag3']);
287286

288287
assert.deepEqual([...guest.focusedAnnotationTags], ['tag2', 'tag3']);
289288
});
@@ -302,7 +301,7 @@ describe('Guest', () => {
302301
},
303302
];
304303

305-
emitGuestEvent('scrollToAnnotation', 'tag1');
304+
emitSidebarEvent('scrollToAnnotation', 'tag1');
306305

307306
assert.called(fakeIntegration.scrollToAnchor);
308307
assert.calledWith(fakeIntegration.scrollToAnchor, guest.anchors[0]);
@@ -326,7 +325,7 @@ describe('Guest', () => {
326325
resolve();
327326
});
328327

329-
emitGuestEvent('scrollToAnnotation', 'tag1');
328+
emitSidebarEvent('scrollToAnnotation', 'tag1');
330329
});
331330
});
332331

@@ -345,7 +344,7 @@ describe('Guest', () => {
345344
event.preventDefault()
346345
);
347346

348-
emitGuestEvent('scrollToAnnotation', 'tag1');
347+
emitSidebarEvent('scrollToAnnotation', 'tag1');
349348

350349
assert.notCalled(fakeIntegration.scrollToAnchor);
351350
});
@@ -354,7 +353,7 @@ describe('Guest', () => {
354353
const guest = createGuest();
355354

356355
guest.anchors = [{ annotation: { $tag: 'tag1' } }];
357-
emitGuestEvent('scrollToAnnotation', 'tag1');
356+
emitSidebarEvent('scrollToAnnotation', 'tag1');
358357

359358
assert.notCalled(fakeIntegration.scrollToAnchor);
360359
});
@@ -374,7 +373,7 @@ describe('Guest', () => {
374373
const eventEmitted = sandbox.stub();
375374
guest.element.addEventListener('scrolltorange', eventEmitted);
376375

377-
emitGuestEvent('scrollToAnnotation', 'tag1');
376+
emitSidebarEvent('scrollToAnnotation', 'tag1');
378377

379378
assert.notCalled(eventEmitted);
380379
assert.notCalled(fakeIntegration.scrollToAnchor);
@@ -408,7 +407,7 @@ describe('Guest', () => {
408407

409408
fakeIntegration.getMetadata.resolves(metadata);
410409

411-
emitGuestEvent(
410+
emitSidebarEvent(
412411
'getDocumentInfo',
413412
createCallback('https://example.com/test.pdf', metadata, done)
414413
);
@@ -419,14 +418,14 @@ describe('Guest', () => {
419418
it('sets visibility of highlights in document', () => {
420419
const guest = createGuest();
421420

422-
emitGuestEvent('setVisibleHighlights', true);
421+
emitSidebarEvent('setVisibleHighlights', true);
423422
assert.calledWith(
424423
highlighter.setHighlightsVisible,
425424
guest.element,
426425
true
427426
);
428427

429-
emitGuestEvent('setVisibleHighlights', false);
428+
emitSidebarEvent('setVisibleHighlights', false);
430429
assert.calledWith(
431430
highlighter.setHighlightsVisible,
432431
guest.element,

0 commit comments

Comments
 (0)