From dc00cad270858ab48af76b4b87646fec75c5d85c Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 20 Jan 2025 15:23:36 +0100 Subject: [PATCH] Update to frontend-shared 8.16 --- package.json | 2 +- src/annotator/bucket-bar-client.ts | 3 +- src/annotator/guest.ts | 2 +- .../integrations/image-text-layer.ts | 2 +- src/annotator/integrations/pdf.tsx | 2 +- src/annotator/integrations/vitalsource.ts | 2 +- src/annotator/selection-observer.ts | 3 +- src/annotator/sidebar.tsx | 2 +- src/annotator/util/drag-handler.tsx | 3 +- src/annotator/util/navigation-observer.ts | 2 +- src/shared/listener-collection.ts | 75 ------------------- src/shared/messaging/port-finder.ts | 3 +- src/shared/messaging/port-provider.ts | 2 +- src/shared/messaging/port-rpc.ts | 3 +- src/shared/test/integration/messaging-test.js | 2 +- src/shared/test/listener-collection-test.js | 72 ------------------ src/sidebar/components/ThreadList.tsx | 2 +- src/sidebar/services/frame-sync.ts | 2 +- src/sidebar/util/observe-element-size.ts | 2 +- yarn.lock | 10 +-- 20 files changed, 27 insertions(+), 169 deletions(-) delete mode 100644 src/shared/listener-collection.ts delete mode 100644 src/shared/test/listener-collection-test.js diff --git a/package.json b/package.json index 99e7dc99e2a..a49722d728d 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "@babel/preset-react": "^7.0.0", "@babel/preset-typescript": "^7.16.7", "@hypothesis/frontend-build": "^3.0.0", - "@hypothesis/frontend-shared": "^8.13.0", + "@hypothesis/frontend-shared": "^8.16.0", "@hypothesis/frontend-testing": "^1.3.1", "@npmcli/arborist": "^9.0.0", "@octokit/rest": "^21.0.0", diff --git a/src/annotator/bucket-bar-client.ts b/src/annotator/bucket-bar-client.ts index 230e255aab3..edc915bfb2b 100644 --- a/src/annotator/bucket-bar-client.ts +++ b/src/annotator/bucket-bar-client.ts @@ -1,4 +1,5 @@ -import { ListenerCollection } from '../shared/listener-collection'; +import { ListenerCollection } from '@hypothesis/frontend-shared'; + import type { PortRPC } from '../shared/messaging'; import type { Anchor, Destroyable } from '../types/annotator'; import type { diff --git a/src/annotator/guest.ts b/src/annotator/guest.ts index 3db9a526f86..595def53b4a 100644 --- a/src/annotator/guest.ts +++ b/src/annotator/guest.ts @@ -1,6 +1,6 @@ +import { ListenerCollection } from '@hypothesis/frontend-shared'; import { TinyEmitter } from 'tiny-emitter'; -import { ListenerCollection } from '../shared/listener-collection'; import { PortFinder, PortRPC } from '../shared/messaging'; import { generateHexString } from '../shared/random'; import { matchShortcut } from '../shared/shortcut'; diff --git a/src/annotator/integrations/image-text-layer.ts b/src/annotator/integrations/image-text-layer.ts index 1b73daa404f..c6e7770c11d 100644 --- a/src/annotator/integrations/image-text-layer.ts +++ b/src/annotator/integrations/image-text-layer.ts @@ -1,7 +1,7 @@ +import { ListenerCollection } from '@hypothesis/frontend-shared'; import debounce from 'lodash.debounce'; import type { DebouncedFunction } from 'lodash.debounce'; -import { ListenerCollection } from '../../shared/listener-collection'; import { rectCenter, rectsOverlapHorizontally, diff --git a/src/annotator/integrations/pdf.tsx b/src/annotator/integrations/pdf.tsx index b5fe04b13b3..dcf8eddcd4a 100644 --- a/src/annotator/integrations/pdf.tsx +++ b/src/annotator/integrations/pdf.tsx @@ -1,7 +1,7 @@ +import { ListenerCollection } from '@hypothesis/frontend-shared'; import debounce from 'lodash.debounce'; import { TinyEmitter } from 'tiny-emitter'; -import { ListenerCollection } from '../../shared/listener-collection'; import type { Anchor, AnnotationData, diff --git a/src/annotator/integrations/vitalsource.ts b/src/annotator/integrations/vitalsource.ts index 02e1f853d68..fa88f77b0f7 100644 --- a/src/annotator/integrations/vitalsource.ts +++ b/src/annotator/integrations/vitalsource.ts @@ -1,7 +1,7 @@ +import { ListenerCollection } from '@hypothesis/frontend-shared'; import { TinyEmitter } from 'tiny-emitter'; import { documentCFI, stripCFIAssertions } from '../../shared/cfi'; -import { ListenerCollection } from '../../shared/listener-collection'; import type { Anchor, AnnotationData, diff --git a/src/annotator/selection-observer.ts b/src/annotator/selection-observer.ts index 9d81c8fc65d..b8ed1202a61 100644 --- a/src/annotator/selection-observer.ts +++ b/src/annotator/selection-observer.ts @@ -1,4 +1,5 @@ -import { ListenerCollection } from '../shared/listener-collection'; +import { ListenerCollection } from '@hypothesis/frontend-shared'; + import { selectedRange } from './range-util'; /** diff --git a/src/annotator/sidebar.tsx b/src/annotator/sidebar.tsx index e08635ea519..44e70787c19 100644 --- a/src/annotator/sidebar.tsx +++ b/src/annotator/sidebar.tsx @@ -1,10 +1,10 @@ import type { ToastMessage } from '@hypothesis/frontend-shared'; +import { ListenerCollection } from '@hypothesis/frontend-shared'; import classnames from 'classnames'; import { render } from 'preact'; import { addConfigFragment } from '../shared/config-fragment'; import { sendErrorsTo } from '../shared/frame-error-capture'; -import { ListenerCollection } from '../shared/listener-collection'; import { PortRPC } from '../shared/messaging'; import type { AnchorPosition, diff --git a/src/annotator/util/drag-handler.tsx b/src/annotator/util/drag-handler.tsx index 1c24c5a52fd..08b700293b4 100644 --- a/src/annotator/util/drag-handler.tsx +++ b/src/annotator/util/drag-handler.tsx @@ -1,4 +1,5 @@ -import { ListenerCollection } from '../../shared/listener-collection'; +import { ListenerCollection } from '@hypothesis/frontend-shared'; + import type { Destroyable } from '../../types/annotator'; /** diff --git a/src/annotator/util/navigation-observer.ts b/src/annotator/util/navigation-observer.ts index 5f5e76aed7b..62313ff3e2d 100644 --- a/src/annotator/util/navigation-observer.ts +++ b/src/annotator/util/navigation-observer.ts @@ -1,5 +1,5 @@ /* global Navigation */ -import { ListenerCollection } from '../../shared/listener-collection'; +import { ListenerCollection } from '@hypothesis/frontend-shared'; /** * Monkey-patch an object to observe calls to a method. diff --git a/src/shared/listener-collection.ts b/src/shared/listener-collection.ts deleted file mode 100644 index d7b4ca8dfed..00000000000 --- a/src/shared/listener-collection.ts +++ /dev/null @@ -1,75 +0,0 @@ -type Listener = { - eventTarget: EventTarget; - eventType: string; - listener: (event: Event) => void; - options?: AddEventListenerOptions; -}; - -/** - * Return the event type that a listener will receive. - * - * For example `EventType` evaluates to `KeyboardEvent`. - * - * The event type is extracted from the target's `on${Type}` property (eg. - * `HTMLElement.onkeydown` here) If there is no such property, the type defaults - * to `Event`. - */ -type EventType< - Target extends EventTarget, - Type extends string, -> = `on${Type}` extends keyof Target - ? Target[`on${Type}`] extends ((...args: any[]) => void) | null - ? Parameters>[0] - : Event - : Event; - -/** - * Utility that provides a way to conveniently remove a set of DOM event - * listeners when they are no longer needed. - */ -export class ListenerCollection { - private _listeners: Map; - - constructor() { - this._listeners = new Map(); - } - - /** - * Add a listener and return an ID that can be used to remove it later - */ - add( - eventTarget: Target, - eventType: Type, - listener: (event: EventType) => void, - options?: AddEventListenerOptions, - ) { - eventTarget.addEventListener(eventType, listener as EventListener, options); - const symbol = Symbol(); - this._listeners.set(symbol, { - eventTarget, - eventType, - listener: listener as EventListener, - options, - }); - return symbol; - } - - /** - * Remove a specific listener. - */ - remove(listenerId: symbol) { - const event = this._listeners.get(listenerId); - if (event) { - const { eventTarget, eventType, listener, options } = event; - eventTarget.removeEventListener(eventType, listener, options); - this._listeners.delete(listenerId); - } - } - - removeAll() { - this._listeners.forEach(({ eventTarget, eventType, listener, options }) => { - eventTarget.removeEventListener(eventType, listener, options); - }); - this._listeners.clear(); - } -} diff --git a/src/shared/messaging/port-finder.ts b/src/shared/messaging/port-finder.ts index 7eb3334aa55..de6b59c971e 100644 --- a/src/shared/messaging/port-finder.ts +++ b/src/shared/messaging/port-finder.ts @@ -1,5 +1,6 @@ +import { ListenerCollection } from '@hypothesis/frontend-shared'; + import type { Destroyable } from '../../types/annotator'; -import { ListenerCollection } from '../listener-collection'; import { generateHexString } from '../random'; import { isMessage } from './port-util'; import type { Frame } from './port-util'; diff --git a/src/shared/messaging/port-provider.ts b/src/shared/messaging/port-provider.ts index 15ef33caba1..0cd4b55f716 100644 --- a/src/shared/messaging/port-provider.ts +++ b/src/shared/messaging/port-provider.ts @@ -1,8 +1,8 @@ +import { ListenerCollection } from '@hypothesis/frontend-shared'; import { TinyEmitter } from 'tiny-emitter'; import type { Destroyable } from '../../types/annotator'; import { captureErrors, sendError } from '../frame-error-capture'; -import { ListenerCollection } from '../listener-collection'; import { isMessage, isMessageEqual, isSourceWindow } from './port-util'; import type { Message } from './port-util'; diff --git a/src/shared/messaging/port-rpc.ts b/src/shared/messaging/port-rpc.ts index fb76a095a77..3f3927a13ab 100644 --- a/src/shared/messaging/port-rpc.ts +++ b/src/shared/messaging/port-rpc.ts @@ -1,5 +1,6 @@ +import { ListenerCollection } from '@hypothesis/frontend-shared'; + import type { Destroyable } from '../../types/annotator'; -import { ListenerCollection } from '../listener-collection'; /* This module was adapted from `index.js` in https://github.com/substack/frame-rpc. diff --git a/src/shared/test/integration/messaging-test.js b/src/shared/test/integration/messaging-test.js index 27aa4909be6..51880e06674 100644 --- a/src/shared/test/integration/messaging-test.js +++ b/src/shared/test/integration/messaging-test.js @@ -1,6 +1,6 @@ +import { ListenerCollection as ListenerCollection_ } from '@hypothesis/frontend-shared'; import { delay } from '@hypothesis/frontend-testing'; -import { ListenerCollection as ListenerCollection_ } from '../../listener-collection'; import { PortFinder as PortFinder_, PortProvider as PortProvider_, diff --git a/src/shared/test/listener-collection-test.js b/src/shared/test/listener-collection-test.js deleted file mode 100644 index f3f8063378f..00000000000 --- a/src/shared/test/listener-collection-test.js +++ /dev/null @@ -1,72 +0,0 @@ -import { ListenerCollection } from '../listener-collection'; - -describe('ListenerCollection', () => { - let listeners; - - beforeEach(() => { - listeners = new ListenerCollection(); - }); - - afterEach(() => { - listeners.removeAll(); - }); - - describe('#add', () => { - it('registers and triggers event listener', () => { - const listener = sinon.stub(); - listeners.add(window, 'resize', listener); - - window.dispatchEvent(new Event('resize')); - assert.calledOnce(listener); - }); - }); - - describe('#remove', () => { - it('unregisters the specified listener', () => { - const listener1 = sinon.stub(); - const listener2 = sinon.stub(); - listeners.add(window, 'resize', listener1); - const listenerId = listeners.add(window, 'resize', listener2); - listeners.remove(listenerId); - - window.dispatchEvent(new Event('resize')); - assert.calledOnce(listener1); - assert.notCalled(listener2); - }); - - it('removes listeners with non-default options', () => { - const listener = sinon.stub(); - const listenerId = listeners.add(window, 'resize', listener, { - capture: true, - }); - - window.dispatchEvent(new Event('resize')); - assert.calledOnce(listener); - - listeners.remove(listenerId); - listener.resetHistory(); - - window.dispatchEvent(new Event('resize')); - assert.notCalled(listener); - }); - }); - - describe('#removeAll', () => { - it('unregisters all event listeners', () => { - const listener1 = sinon.stub(); - const listener2 = sinon.stub(); - const listener3 = sinon.stub(); - - listeners.add(window, 'resize', listener1); - listeners.add(window, 'resize', listener2); - listeners.add(window, 'resize', listener3, { capture: true }); - - listeners.removeAll(); - window.dispatchEvent(new Event('resize')); - - assert.notCalled(listener1); - assert.notCalled(listener2); - assert.notCalled(listener3); - }); - }); -}); diff --git a/src/sidebar/components/ThreadList.tsx b/src/sidebar/components/ThreadList.tsx index 0e02a238440..0f20042a059 100644 --- a/src/sidebar/components/ThreadList.tsx +++ b/src/sidebar/components/ThreadList.tsx @@ -1,8 +1,8 @@ +import { ListenerCollection } from '@hypothesis/frontend-shared'; import classnames from 'classnames'; import debounce from 'lodash.debounce'; import { useEffect, useLayoutEffect, useMemo, useState } from 'preact/hooks'; -import { ListenerCollection } from '../../shared/listener-collection'; import type { Annotation, EPUBContentSelector } from '../../types/api'; import type { Thread } from '../helpers/build-thread'; import { diff --git a/src/sidebar/services/frame-sync.ts b/src/sidebar/services/frame-sync.ts index d91b6bdeb23..8d61a0e2988 100644 --- a/src/sidebar/services/frame-sync.ts +++ b/src/sidebar/services/frame-sync.ts @@ -1,9 +1,9 @@ import type { ToastMessage } from '@hypothesis/frontend-shared'; +import { ListenerCollection } from '@hypothesis/frontend-shared'; import debounce from 'lodash.debounce'; import type { DebouncedFunction } from 'lodash.debounce'; import shallowEqual from 'shallowequal'; -import { ListenerCollection } from '../../shared/listener-collection'; import { PortFinder, PortRPC, diff --git a/src/sidebar/util/observe-element-size.ts b/src/sidebar/util/observe-element-size.ts index 7a4d6b539b9..6e1b2a171b1 100644 --- a/src/sidebar/util/observe-element-size.ts +++ b/src/sidebar/util/observe-element-size.ts @@ -1,4 +1,4 @@ -import { ListenerCollection } from '../../shared/listener-collection'; +import { ListenerCollection } from '@hypothesis/frontend-shared'; /** * Watch for changes in the size (`clientWidth` and `clientHeight`) of diff --git a/yarn.lock b/yarn.lock index 63695675ea1..ff16256ccdb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2228,15 +2228,15 @@ __metadata: languageName: node linkType: hard -"@hypothesis/frontend-shared@npm:^8.13.0": - version: 8.15.0 - resolution: "@hypothesis/frontend-shared@npm:8.15.0" +"@hypothesis/frontend-shared@npm:^8.16.0": + version: 8.16.0 + resolution: "@hypothesis/frontend-shared@npm:8.16.0" dependencies: highlight.js: ^11.6.0 wouter-preact: ^3.0.0 peerDependencies: preact: ^10.25.1 - checksum: 7489c92e3f0887a01ad541983a097e717fb57155bbaea1c83caaf26476c98f17061e4b195331b8a800790a523ed801e0fe93c0aa65b58d6918bdaca932691b79 + checksum: 3eaebe55fed28cae226405a2d847020d9ecfa3991a0628a4df1b09b8133455151172467824abad20b704f22c55b66c0e11ec0083571987ecc348d10f03d27bd5 languageName: node linkType: hard @@ -8705,7 +8705,7 @@ __metadata: "@babel/preset-react": ^7.0.0 "@babel/preset-typescript": ^7.16.7 "@hypothesis/frontend-build": ^3.0.0 - "@hypothesis/frontend-shared": ^8.13.0 + "@hypothesis/frontend-shared": ^8.16.0 "@hypothesis/frontend-testing": ^1.3.1 "@npmcli/arborist": ^9.0.0 "@octokit/rest": ^21.0.0