Skip to content

Commit 354d322

Browse files
committed
Re-enforce the use of enums for Bridge events
In contrast to #3829, we have decided to re-enforce the use of the lookup enums for the event names used by `Bridge`. I have added definitions for all the events that are sent across the different frames using various `Bridge`s. I broke down the events into four sections based on the direction of the messages: * guest -> sidebar events * host -> sidebar events * sidebar -> guest/s events * sidebar -> host events
1 parent 910b3da commit 354d322

19 files changed

+322
-159
lines changed

src/annotator/annotation-counts.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import events from '../shared/bridge-events';
1+
import { sidebarToHostEvents } from '../shared/bridge-events';
22

33
const ANNOTATION_COUNT_ATTR = 'data-hypothesis-annotation-count';
44

@@ -11,8 +11,12 @@ const ANNOTATION_COUNT_ATTR = 'data-hypothesis-annotation-count';
1111
* display annotation count.
1212
* @param {import('../shared/bridge').Bridge} bridge - Channel for host-sidebar communication
1313
*/
14+
1415
export function annotationCounts(rootEl, bridge) {
15-
bridge.on(events.PUBLIC_ANNOTATION_COUNT_CHANGED, updateAnnotationCountElems);
16+
bridge.on(
17+
sidebarToHostEvents.PUBLIC_ANNOTATION_COUNT_CHANGED,
18+
updateAnnotationCountElems
19+
);
1620

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

src/annotator/annotation-sync.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import {
2+
guestToSidebarEvents,
3+
sidebarToGuestEvents,
4+
} from '../shared/bridge-events';
5+
16
/**
27
* @typedef {import('../shared/bridge').Bridge} Bridge
38
* @typedef {import('../types/annotator').AnnotationData} AnnotationData
@@ -43,7 +48,7 @@ export class AnnotationSync {
4348
this.destroyed = false;
4449

4550
// Relay events from the sidebar to the rest of the annotator.
46-
this.bridge.on('deleteAnnotation', (body, callback) => {
51+
this.bridge.on(sidebarToGuestEvents.DELETE_ANNOTATION, (body, callback) => {
4752
if (this.destroyed) {
4853
callback(null);
4954
return;
@@ -55,22 +60,28 @@ export class AnnotationSync {
5560
callback(null);
5661
});
5762

58-
this.bridge.on('loadAnnotations', (bodies, callback) => {
59-
if (this.destroyed) {
63+
this.bridge.on(
64+
sidebarToGuestEvents.LOAD_ANNOTATIONS,
65+
(bodies, callback) => {
66+
if (this.destroyed) {
67+
callback(null);
68+
return;
69+
}
70+
const annotations = bodies.map(body => this._parse(body));
71+
this._emitter.publish('annotationsLoaded', annotations);
6072
callback(null);
61-
return;
6273
}
63-
const annotations = bodies.map(body => this._parse(body));
64-
this._emitter.publish('annotationsLoaded', annotations);
65-
callback(null);
66-
});
74+
);
6775

6876
// Relay events from annotator to sidebar.
6977
this._emitter.subscribe('beforeAnnotationCreated', annotation => {
7078
if (annotation.$tag) {
7179
return;
7280
}
73-
this.bridge.call('beforeCreateAnnotation', this._format(annotation));
81+
this.bridge.call(
82+
guestToSidebarEvents.BEFORE_CREATE_ANNOTATION,
83+
this._format(annotation)
84+
);
7485
});
7586
}
7687

@@ -88,7 +99,7 @@ export class AnnotationSync {
8899
}
89100

90101
this.bridge.call(
91-
'sync',
102+
guestToSidebarEvents.SYNC,
92103
annotations.map(ann => this._format(ann))
93104
);
94105
}

src/annotator/features.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import events from '../shared/bridge-events';
1+
import { sidebarToHostEvents } from '../shared/bridge-events';
22
import warnOnce from '../shared/warn-once';
33

44
let _features = {};
@@ -12,7 +12,7 @@ export const features = {
1212
* @param {import('../shared/bridge').Bridge} bridge - Channel for host-sidebar communication
1313
*/
1414
init: function (bridge) {
15-
bridge.on(events.FEATURE_FLAGS_UPDATED, _set);
15+
bridge.on(sidebarToHostEvents.FEATURE_FLAGS_UPDATED, _set);
1616
},
1717

1818
reset: function () {

src/annotator/guest.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import {
2+
guestToSidebarEvents,
3+
sidebarToGuestEvents,
4+
} from '../shared/bridge-events';
15
import { Bridge } from '../shared/bridge';
26
import { ListenerCollection } from '../shared/listener-collection';
37

@@ -221,7 +225,7 @@ export default class Guest {
221225
// Don't hide the sidebar if the event comes from an element that contains a highlight
222226
return;
223227
}
224-
this._bridge.call('closeSidebar');
228+
this._bridge.call(guestToSidebarEvents.CLOSE_SIDEBAR);
225229
};
226230

227231
this._listeners.add(this.element, 'mouseup', event => {
@@ -310,7 +314,7 @@ export default class Guest {
310314
_connectSidebarEvents() {
311315
// Handlers for events sent when user hovers or clicks on an annotation card
312316
// in the sidebar.
313-
this._bridge.on('focusAnnotations', (tags = []) => {
317+
this._bridge.on(sidebarToGuestEvents.FOCUS_ANNOTATIONS, (tags = []) => {
314318
this._focusedAnnotations.clear();
315319
tags.forEach(tag => this._focusedAnnotations.add(tag));
316320

@@ -322,7 +326,7 @@ export default class Guest {
322326
}
323327
});
324328

325-
this._bridge.on('scrollToAnnotation', tag => {
329+
this._bridge.on(sidebarToGuestEvents.SCROLL_TO_ANNOTATION, tag => {
326330
const anchor = this.anchors.find(a => a.annotation.$tag === tag);
327331
if (!anchor?.highlights) {
328332
return;
@@ -348,16 +352,19 @@ export default class Guest {
348352
});
349353

350354
// Handler for when sidebar requests metadata for the current document
351-
this._bridge.on('getDocumentInfo', cb => {
355+
this._bridge.on(sidebarToGuestEvents.GET_DOCUMENT_INFO, cb => {
352356
this.getDocumentInfo()
353357
.then(info => cb(null, info))
354358
.catch(reason => cb(reason));
355359
});
356360

357361
// Handler for controls on the sidebar
358-
this._bridge.on('setVisibleHighlights', showHighlights => {
359-
this.setVisibleHighlights(showHighlights);
360-
});
362+
this._bridge.on(
363+
sidebarToGuestEvents.SET_VISIBLE_HIGHLIGHTS,
364+
showHighlights => {
365+
this.setVisibleHighlights(showHighlights);
366+
}
367+
);
361368
}
362369

363370
destroy() {
@@ -578,7 +585,7 @@ export default class Guest {
578585
this.anchor(annotation);
579586

580587
if (!annotation.$highlight) {
581-
this._bridge.call('openSidebar');
588+
this._bridge.call(guestToSidebarEvents.OPEN_SIDEBAR);
582589
}
583590

584591
return annotation;
@@ -592,7 +599,7 @@ export default class Guest {
592599
*/
593600
_focusAnnotations(annotations) {
594601
const tags = annotations.map(a => a.$tag);
595-
this._bridge.call('focusAnnotations', tags);
602+
this._bridge.call(guestToSidebarEvents.FOCUS_ANNOTATIONS, tags);
596603
}
597604

598605
/**
@@ -643,11 +650,11 @@ export default class Guest {
643650
selectAnnotations(annotations, toggle = false) {
644651
const tags = annotations.map(a => a.$tag);
645652
if (toggle) {
646-
this._bridge.call('toggleAnnotationSelection', tags);
653+
this._bridge.call(guestToSidebarEvents.TOGGLE_ANNOTATION_SELECTION, tags);
647654
} else {
648-
this._bridge.call('showAnnotations', tags);
655+
this._bridge.call(guestToSidebarEvents.SHOW_ANNOTATIONS, tags);
649656
}
650-
this._bridge.call('openSidebar');
657+
this._bridge.call(guestToSidebarEvents.OPEN_SIDEBAR);
651658
}
652659

653660
/**

src/annotator/sidebar.js

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

33
import { Bridge } from '../shared/bridge';
4-
import events from '../shared/bridge-events';
4+
import {
5+
hostToSidebarEvents,
6+
sidebarToHostEvents,
7+
} from '../shared/bridge-events';
58
import { ListenerCollection } from '../shared/listener-collection';
69

710
import { annotationCounts } from './annotation-counts';
@@ -217,7 +220,10 @@ export default class Sidebar {
217220
this._listeners.add(window, 'message', event => {
218221
const { data } = /** @type {MessageEvent} */ (event);
219222
if (data?.type === 'hypothesisGuestUnloaded') {
220-
this._sidebarRPC.call('destroyFrame', data.frameIdentifier);
223+
this._sidebarRPC.call(
224+
hostToSidebarEvents.DESTROY_FRAME,
225+
data.frameIdentifier
226+
);
221227
}
222228
});
223229
}
@@ -239,25 +245,29 @@ export default class Sidebar {
239245
sidebarTrigger(document.body, () => this.open());
240246
features.init(this._sidebarRPC);
241247

242-
this._sidebarRPC.on('openSidebar', () => this.open());
243-
this._sidebarRPC.on('closeSidebar', () => this.close());
248+
this._sidebarRPC.on(sidebarToHostEvents.OPEN_SIDEBAR, () => this.open());
249+
this._sidebarRPC.on(sidebarToHostEvents.CLOSE_SIDEBAR, () => this.close());
244250

245251
// Sidebar listens to the `openNotebook` event coming from the sidebar's
246252
// iframe and re-publishes it via the emitter to the Notebook
247-
this._sidebarRPC.on('openNotebook', (/** @type {string} */ groupId) => {
248-
this.hide();
249-
this._emitter.publish('openNotebook', groupId);
250-
});
253+
this._sidebarRPC.on(
254+
sidebarToHostEvents.OPEN_NOTEBOOK,
255+
(/** @type {string} */ groupId) => {
256+
this.hide();
257+
this._emitter.publish('openNotebook', groupId);
258+
}
259+
);
251260
this._emitter.subscribe('closeNotebook', () => {
252261
this.show();
253262
});
254263

264+
/** @type {Array<[import('../shared/bridge-events').SidebarToHostEvent, function]>} */
255265
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],
266+
[sidebarToHostEvents.LOGIN_REQUESTED, this.onLoginRequest],
267+
[sidebarToHostEvents.LOGOUT_REQUESTED, this.onLogoutRequest],
268+
[sidebarToHostEvents.SIGNUP_REQUESTED, this.onSignupRequest],
269+
[sidebarToHostEvents.PROFILE_REQUESTED, this.onProfileRequest],
270+
[sidebarToHostEvents.HELP_REQUESTED, this.onHelpRequest],
261271
];
262272
eventHandlers.forEach(([event, handler]) => {
263273
if (handler) {
@@ -439,7 +449,7 @@ export default class Sidebar {
439449
}
440450

441451
open() {
442-
this._sidebarRPC.call('sidebarOpened');
452+
this._sidebarRPC.call(hostToSidebarEvents.SIDEBAR_OPENED);
443453
this._emitter.publish('sidebarOpened');
444454

445455
if (this.iframeContainer) {
@@ -478,7 +488,10 @@ export default class Sidebar {
478488
* @param {boolean} shouldShowHighlights
479489
*/
480490
setAllVisibleHighlights(shouldShowHighlights) {
481-
this._sidebarRPC.call('setVisibleHighlights', shouldShowHighlights);
491+
this._sidebarRPC.call(
492+
hostToSidebarEvents.SET_VISIBLE_HIGHLIGHTS,
493+
shouldShowHighlights
494+
);
482495
}
483496

484497
/**

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/sidebar-test.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import events from '../../shared/bridge-events';
2-
3-
import Sidebar, { MIN_RESIZE } from '../sidebar';
4-
import { $imports } from '../sidebar';
1+
import Sidebar, { MIN_RESIZE, $imports } from '../sidebar';
52
import { EventBus } from '../util/emitter';
63

74
const DEFAULT_WIDTH = 350;
@@ -366,7 +363,7 @@ describe('Sidebar', () => {
366363
const onLoginRequest = sandbox.stub();
367364
createSidebar({ services: [{ onLoginRequest }] });
368365

369-
emitEvent(events.LOGIN_REQUESTED);
366+
emitEvent('loginRequested');
370367

371368
assert.called(onLoginRequest);
372369
});
@@ -386,7 +383,7 @@ describe('Sidebar', () => {
386383
],
387384
});
388385

389-
emitEvent(events.LOGIN_REQUESTED);
386+
emitEvent('loginRequested');
390387

391388
assert.called(firstOnLogin);
392389
assert.notCalled(secondOnLogin);
@@ -406,25 +403,25 @@ describe('Sidebar', () => {
406403
],
407404
});
408405

409-
emitEvent(events.LOGIN_REQUESTED);
406+
emitEvent('loginRequested');
410407

411408
assert.notCalled(secondOnLogin);
412409
assert.notCalled(thirdOnLogin);
413410
});
414411

415412
it('does not crash if there is no services', () => {
416413
createSidebar(); // No config.services
417-
emitEvent(events.LOGIN_REQUESTED);
414+
emitEvent('loginRequested');
418415
});
419416

420417
it('does not crash if services is an empty array', () => {
421418
createSidebar({ services: [] });
422-
emitEvent(events.LOGIN_REQUESTED);
419+
emitEvent('loginRequested');
423420
});
424421

425422
it('does not crash if the first service has no onLoginRequest', () => {
426423
createSidebar({ services: [{}] });
427-
emitEvent(events.LOGIN_REQUESTED);
424+
emitEvent('loginRequested');
428425
});
429426
});
430427

@@ -433,7 +430,7 @@ describe('Sidebar', () => {
433430
const onLogoutRequest = sandbox.stub();
434431
createSidebar({ services: [{ onLogoutRequest }] });
435432

436-
emitEvent(events.LOGOUT_REQUESTED);
433+
emitEvent('logoutRequested');
437434

438435
assert.called(onLogoutRequest);
439436
}));
@@ -443,7 +440,7 @@ describe('Sidebar', () => {
443440
const onSignupRequest = sandbox.stub();
444441
createSidebar({ services: [{ onSignupRequest }] });
445442

446-
emitEvent(events.SIGNUP_REQUESTED);
443+
emitEvent('signupRequested');
447444

448445
assert.called(onSignupRequest);
449446
}));
@@ -453,7 +450,7 @@ describe('Sidebar', () => {
453450
const onProfileRequest = sandbox.stub();
454451
createSidebar({ services: [{ onProfileRequest }] });
455452

456-
emitEvent(events.PROFILE_REQUESTED);
453+
emitEvent('profileRequested');
457454

458455
assert.called(onProfileRequest);
459456
}));
@@ -463,7 +460,7 @@ describe('Sidebar', () => {
463460
const onHelpRequest = sandbox.stub();
464461
createSidebar({ services: [{ onHelpRequest }] });
465462

466-
emitEvent(events.HELP_REQUESTED);
463+
emitEvent('helpRequested');
467464

468465
assert.called(onHelpRequest);
469466
}));

0 commit comments

Comments
 (0)