Commit 6ed8c93b authored by Eduardo's avatar Eduardo

Apply suggestions from code review

Co-authored-by: 's avatarRobert Knight <robertknight@gmail.com>
parent 1b8e8a4d
...@@ -52,7 +52,7 @@ function hasHypothesis(iframe) { ...@@ -52,7 +52,7 @@ function hasHypothesis(iframe) {
/** /**
* Inject Hypothesis client into a frame. * Inject Hypothesis client into a frame.
* *
* IMPORTANT: This method requires that the iframe is "accessible" * IMPORTANT: This method requires that the iframe is same-origin
* (frame.contentDocument|contentWindow is not null). * (frame.contentDocument|contentWindow is not null).
* *
* This waits for the frame to finish loading before injecting the client. * This waits for the frame to finish loading before injecting the client.
......
...@@ -21,6 +21,8 @@ import { Notebook } from './notebook'; ...@@ -21,6 +21,8 @@ import { Notebook } from './notebook';
import { Sidebar } from './sidebar'; import { Sidebar } from './sidebar';
import { EventBus } from './util/emitter'; import { EventBus } from './util/emitter';
/** @typedef {import('../types/annotator').Destroyable} Destroyable */
// Look up the URL of the sidebar. This element is added to the page by the // Look up the URL of the sidebar. This element is added to the page by the
// boot script before the "annotator" bundle loads. // boot script before the "annotator" bundle loads.
const sidebarLinkElement = /** @type {HTMLLinkElement} */ ( const sidebarLinkElement = /** @type {HTMLLinkElement} */ (
...@@ -46,51 +48,46 @@ function init() { ...@@ -46,51 +48,46 @@ function init() {
const hostFrame = annotatorConfig.subFrameIdentifier ? window.parent : window; const hostFrame = annotatorConfig.subFrameIdentifier ? window.parent : window;
/** @type {Sidebar|undefined} */ /** @type {Destroyable[]} */
let sidebar; const destroyables = [];
/** @type {Notebook|undefined} */
let notebook;
/** @type {PortProvider|undefined} */
let portProvider;
if (hostFrame === window) { if (hostFrame === window) {
const sidebarConfig = getConfig('sidebar'); const sidebarConfig = getConfig('sidebar');
const hypothesisAppsOrigin = new URL(sidebarConfig.sidebarAppUrl).origin; const hypothesisAppsOrigin = new URL(sidebarConfig.sidebarAppUrl).origin;
portProvider = new PortProvider(hypothesisAppsOrigin); const portProvider = new PortProvider(hypothesisAppsOrigin);
const eventBus = new EventBus(); const eventBus = new EventBus();
sidebar = new Sidebar(document.body, eventBus, sidebarConfig); const sidebar = new Sidebar(document.body, eventBus, sidebarConfig);
notebook = new Notebook(document.body, eventBus, getConfig('notebook')); const notebook = new Notebook(
document.body,
eventBus,
getConfig('notebook')
);
portProvider.on('frameConnected', (source, port) => portProvider.on('frameConnected', (source, port) =>
/** @type {Sidebar} */ (sidebar).onFrameConnected(source, port) /** @type {Sidebar} */ (sidebar).onFrameConnected(source, port)
); );
destroyables.push(portProvider, sidebar, notebook);
} }
/** @type {Guest|undefined} */
let guest;
/** @type {VitalSourceInjector|undefined} */
let vitalSourceInjector;
/** @type {HypothesisInjector|undefined} */
let hypothesisInjector;
const vsFrameRole = vitalSourceFrameRole(); const vsFrameRole = vitalSourceFrameRole();
if (vsFrameRole === 'container') { if (vsFrameRole === 'container') {
vitalSourceInjector = new VitalSourceInjector(annotatorConfig); const vitalSourceInjector = new VitalSourceInjector(annotatorConfig);
destroyables.push(vitalSourceInjector);
} else { } else {
// Set up automatic and integration-triggered injection of client into // Set up automatic injection of the client into iframes in this frame.
// iframes in this frame. const hypothesisInjector = new HypothesisInjector(
hypothesisInjector = new HypothesisInjector(document.body, annotatorConfig); document.body,
annotatorConfig
);
// Create the guest that handles creating annotations and displaying highlights. // Create the guest that handles creating annotations and displaying highlights.
guest = new Guest(document.body, annotatorConfig, hostFrame); const guest = new Guest(document.body, annotatorConfig, hostFrame);
destroyables.push(hypothesisInjector, guest);
} }
sidebarLinkElement.addEventListener('destroy', () => { sidebarLinkElement.addEventListener('destroy', () => {
portProvider?.destroy(); destroyables.forEach(instance => instance.destroy());
sidebar?.destroy();
notebook?.destroy();
vitalSourceInjector?.destroy();
hypothesisInjector?.destroy();
guest?.destroy();
// Remove all the `<link>`, `<script>` and `<style>` elements added to the // Remove all the `<link>`, `<script>` and `<style>` elements added to the
// page by the boot script. // page by the boot script.
......
...@@ -14,7 +14,7 @@ import { ...@@ -14,7 +14,7 @@ import {
* Create the integration that handles document-type specific aspects of * Create the integration that handles document-type specific aspects of
* guest functionality. * guest functionality.
* *
* @param {Pick<Annotator, 'anchor'|'anchors'>} annotator * @param {Annotator} annotator
* @return {Integration} * @return {Integration}
*/ */
export function createIntegration(annotator) { export function createIntegration(annotator) {
......
...@@ -61,7 +61,7 @@ export function isPDF() { ...@@ -61,7 +61,7 @@ export function isPDF() {
*/ */
export class PDFIntegration { export class PDFIntegration {
/** /**
* @param {Pick<Annotator, 'anchor'|'anchors'>} annotator * @param {Annotator} annotator
* @param {object} options * @param {object} options
* @param {number} [options.reanchoringMaxWait] - Max time to wait for * @param {number} [options.reanchoringMaxWait] - Max time to wait for
* re-anchoring to complete when scrolling to an un-rendered page. * re-anchoring to complete when scrolling to an un-rendered page.
......
...@@ -39,7 +39,8 @@ export function vitalSourceFrameRole(window_ = window) { ...@@ -39,7 +39,8 @@ export function vitalSourceFrameRole(window_ = window) {
} }
/** /**
* Observe the book content iframe and load the client into this frame. * VitalSourceInjector runs in the book container frame and loads the client into
* book content frames.
*/ */
export class VitalSourceInjector { export class VitalSourceInjector {
/** /**
......
...@@ -88,7 +88,7 @@ describe('HypothesisInjector integration test', () => { ...@@ -88,7 +88,7 @@ describe('HypothesisInjector integration test', () => {
it('copies client asset locations from host frame', async () => { it('copies client asset locations from host frame', async () => {
hostJSONConfig = { hostJSONConfig = {
clientUrl: 'chrome-extension:///o', clientUrl: 'chrome-extension://abc/client/build/boot.js',
assetRoot: 'chrome-extension://abc/client', assetRoot: 'chrome-extension://abc/client',
notebookAppUrl: 'chrome-extension://abc/client/notebook.html', notebookAppUrl: 'chrome-extension://abc/client/notebook.html',
sidebarAppUrl: 'chrome-extension://abc/client/sidebar.html', sidebarAppUrl: 'chrome-extension://abc/client/sidebar.html',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment