Skip to content

Commit

Permalink
Constructor simplification (#713)
Browse files Browse the repository at this point in the history
* Update AudioDownloader

* Update Translator

* Update ClipboardMonitor

* Update ClipboardReader

* Update PanelElement

* Update QueryParser

* Update DisplayGenerator

* Update DisplayHistory

* Update DOMDataBinder

* Remove unnecessary cast

* Update Popup types

* One declaration per line

* Remove optionals from Frontend constructor

* Fix Translator constructor
  • Loading branch information
toasted-nutbread authored Feb 20, 2024
1 parent 0e4ae92 commit fe875bb
Show file tree
Hide file tree
Showing 29 changed files with 111 additions and 203 deletions.
28 changes: 14 additions & 14 deletions ext/js/app/popup-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,26 @@ export class PopupFactory {
if (id === null) {
id = generateId(16);
}
const popup = new PopupWindow({
application: this._application,
const popup = new PopupWindow(
this._application,
id,
depth,
frameId: currentFrameId
});
currentFrameId
);
this._popups.set(id, popup);
return popup;
} else if (frameId === currentFrameId) {
// New unique id
if (id === null) {
id = generateId(16);
}
const popup = new Popup({
application: this._application,
const popup = new Popup(
this._application,
id,
depth,
frameId: currentFrameId,
currentFrameId,
childrenSupported
});
);
if (parent !== null) {
if (parent.child !== null) {
throw new Error('Parent popup already has a child');
Expand All @@ -162,13 +162,13 @@ export class PopupFactory {
childrenSupported
});
id = info.id;
const popup = new PopupProxy({
application: this._application,
const popup = new PopupProxy(
this._application,
id,
depth: info.depth,
frameId: info.frameId,
frameOffsetForwarder: useFrameOffsetForwarder ? this._frameOffsetForwarder : null
});
info.depth,
info.frameId,
useFrameOffsetForwarder ? this._frameOffsetForwarder : null
);
this._popups.set(id, popup);
return popup;
}
Expand Down
15 changes: 6 additions & 9 deletions ext/js/app/popup-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@ import {log} from '../core/log.js';
*/
export class PopupProxy extends EventDispatcher {
/**
* Creates a new instance.
* @param {import('popup').PopupProxyConstructorDetails} details Details about how to set up the instance.
* @param {import('../application.js').Application} application The main application instance.
* @param {string} id The identifier of the popup.
* @param {number} depth The depth of the popup.
* @param {number} frameId The frameId of the host frame.
* @param {?import('../comm/frame-offset-forwarder.js').FrameOffsetForwarder} frameOffsetForwarder A `FrameOffsetForwarder` instance which is used to determine frame positioning.
*/
constructor({
application,
id,
depth,
frameId,
frameOffsetForwarder
}) {
constructor(application, id, depth, frameId, frameOffsetForwarder) {
super();
/** @type {import('../application.js').Application} */
this._application = application;
Expand Down
13 changes: 5 additions & 8 deletions ext/js/app/popup-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@ import {EventDispatcher} from '../core/event-dispatcher.js';
*/
export class PopupWindow extends EventDispatcher {
/**
* Creates a new instance.
* @param {import('popup').PopupWindowConstructorDetails} details Details about how to set up the instance.
* @param {import('../application.js').Application} application The main application instance.
* @param {string} id The identifier of the popup.
* @param {number} depth The depth of the popup.
* @param {number} frameId The frameId of the host frame.
*/
constructor({
application,
id,
depth,
frameId
}) {
constructor(application, id, depth, frameId) {
super();
/** @type {import('../application.js').Application} */
this._application = application;
Expand Down
17 changes: 7 additions & 10 deletions ext/js/app/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,13 @@ import {ThemeController} from './theme-controller.js';
*/
export class Popup extends EventDispatcher {
/**
* Creates a new instance.
* @param {import('popup').PopupConstructorDetails} details The details used to construct the new instance.
*/
constructor({
application,
id,
depth,
frameId,
childrenSupported
}) {
* @param {import('../application.js').Application} application The main application instance.
* @param {string} id The identifier of the popup.
* @param {number} depth The depth of the popup.
* @param {number} frameId The frameId of the host frame.
* @param {boolean} childrenSupported Whether or not the popup is able to show child popups.
*/
constructor(application, id, depth, frameId, childrenSupported) {
super();
/** @type {import('../application.js').Application} */
this._application = application;
Expand Down
22 changes: 8 additions & 14 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,14 @@ export class Backend {
/** @type {DictionaryDatabase|DictionaryDatabaseProxy} */
this._dictionaryDatabase = new DictionaryDatabase();
/** @type {Translator|TranslatorProxy} */
this._translator = new Translator({
database: this._dictionaryDatabase
});
this._translator = new Translator(this._dictionaryDatabase);
/** @type {ClipboardReader|ClipboardReaderProxy} */
this._clipboardReader = new ClipboardReader({
this._clipboardReader = new ClipboardReader(
// eslint-disable-next-line no-undef
document: (typeof document === 'object' && document !== null ? document : null),
pasteTargetSelector: '#clipboard-paste-target',
richContentPasteTargetSelector: '#clipboard-rich-content-paste-target'
});
(typeof document === 'object' && document !== null ? document : null),
'#clipboard-paste-target',
'#clipboard-rich-content-paste-target'
);
} else {
/** @type {?OffscreenProxy} */
this._offscreen = new OffscreenProxy(webExtension);
Expand All @@ -90,9 +88,7 @@ export class Backend {
}

/** @type {ClipboardMonitor} */
this._clipboardMonitor = new ClipboardMonitor({
clipboardReader: this._clipboardReader
});
this._clipboardMonitor = new ClipboardMonitor(this._clipboardReader);
/** @type {?import('settings').Options} */
this._options = null;
/** @type {import('../data/json-schema.js').JsonSchema[]} */
Expand All @@ -102,9 +98,7 @@ export class Backend {
/** @type {RequestBuilder} */
this._requestBuilder = new RequestBuilder();
/** @type {AudioDownloader} */
this._audioDownloader = new AudioDownloader({
requestBuilder: this._requestBuilder
});
this._audioDownloader = new AudioDownloader(this._requestBuilder);
/** @type {OptionsUtil} */
this._optionsUtil = new OptionsUtil();
/** @type {AccessibilityController} */
Expand Down
14 changes: 6 additions & 8 deletions ext/js/background/offscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ export class Offscreen {
/** @type {DictionaryDatabase} */
this._dictionaryDatabase = new DictionaryDatabase();
/** @type {Translator} */
this._translator = new Translator({
database: this._dictionaryDatabase
});
this._translator = new Translator(this._dictionaryDatabase);
/** @type {ClipboardReader} */
this._clipboardReader = new ClipboardReader({
document: (typeof document === 'object' && document !== null ? document : null),
pasteTargetSelector: '#clipboard-paste-target',
richContentPasteTargetSelector: '#clipboard-rich-content-paste-target'
});
this._clipboardReader = new ClipboardReader(
(typeof document === 'object' && document !== null ? document : null),
'#clipboard-paste-target',
'#clipboard-rich-content-paste-target'
);


/* eslint-disable @stylistic/no-multi-spaces */
Expand Down
4 changes: 2 additions & 2 deletions ext/js/comm/clipboard-monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import {isStringPartiallyJapanese} from '../language/ja/japanese.js';
*/
export class ClipboardMonitor extends EventDispatcher {
/**
* @param {{clipboardReader: import('clipboard-monitor').ClipboardReaderLike}} details
* @param {import('clipboard-monitor').ClipboardReaderLike} clipboardReader
*/
constructor({clipboardReader}) {
constructor(clipboardReader) {
super();
/** @type {import('clipboard-monitor').ClipboardReaderLike} */
this._clipboardReader = clipboardReader;
Expand Down
7 changes: 4 additions & 3 deletions ext/js/comm/clipboard-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import {getFileExtensionFromImageMediaType} from '../media/media-util.js';
*/
export class ClipboardReader {
/**
* Creates a new instances of a clipboard reader.
* @param {{document: ?Document, pasteTargetSelector: ?string, richContentPasteTargetSelector: ?string}} details Details about how to set up the instance.
* @param {?Document} document
* @param {?string} pasteTargetSelector
* @param {?string} richContentPasteTargetSelector
*/
constructor({document = null, pasteTargetSelector = null, richContentPasteTargetSelector = null}) {
constructor(document, pasteTargetSelector, richContentPasteTargetSelector) {
/** @type {?Document} */
this._document = document;
/** @type {?import('environment').Browser} */
Expand Down
5 changes: 3 additions & 2 deletions ext/js/display/display-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import {StructuredContentGenerator} from './sandbox/structured-content-generator

export class DisplayGenerator {
/**
* @param {import('display').DisplayGeneratorConstructorDetails} details
* @param {import('./display-content-manager.js').DisplayContentManager} contentManager
* @param {?import('../input/hotkey-help-controller.js').HotkeyHelpController} hotkeyHelpController
*/
constructor({contentManager, hotkeyHelpController = null}) {
constructor(contentManager, hotkeyHelpController) {
/** @type {import('./display-content-manager.js').DisplayContentManager} */
this._contentManager = contentManager;
/** @type {?import('../input/hotkey-help-controller.js').HotkeyHelpController} */
Expand Down
5 changes: 3 additions & 2 deletions ext/js/display/display-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import {generateId, isObject} from '../core/utilities.js';
*/
export class DisplayHistory extends EventDispatcher {
/**
* @param {{clearable?: boolean, useBrowserHistory?: boolean}} details
* @param {boolean} clearable
* @param {boolean} useBrowserHistory
*/
constructor({clearable = true, useBrowserHistory = false}) {
constructor(clearable, useBrowserHistory) {
super();
/** @type {boolean} */
this._clearable = clearable;
Expand Down
5 changes: 1 addition & 4 deletions ext/js/display/display-profile-selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ export class DisplayProfileSelection {
/** @type {HTMLElement} */
const profilePanelElement = querySelectorNotNull(document, '#profile-panel');
/** @type {PanelElement} */
this._profilePanel = new PanelElement({
node: profilePanelElement,
closingAnimationDuration: 375 // Milliseconds; includes buffer
});
this._profilePanel = new PanelElement(profilePanelElement, 375); // Milliseconds; includes buffer
/** @type {boolean} */
this._profileListNeedsUpdate = false;
/** @type {EventListenerCollection} */
Expand Down
23 changes: 7 additions & 16 deletions ext/js/display/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,13 @@ export class Display extends EventDispatcher {
/** @type {HotkeyHelpController} */
this._hotkeyHelpController = new HotkeyHelpController();
/** @type {DisplayGenerator} */
this._displayGenerator = new DisplayGenerator({
contentManager: this._contentManager,
hotkeyHelpController: this._hotkeyHelpController
});
this._displayGenerator = new DisplayGenerator(this._contentManager, this._hotkeyHelpController);
/** @type {import('display').DirectApiMap} */
this._directApiMap = new Map();
/** @type {import('api-map').ApiMap<import('display').WindowApiSurface>} */ // import('display').WindowApiMap
this._windowApiMap = new Map();
/** @type {DisplayHistory} */
this._history = new DisplayHistory({clearable: true, useBrowserHistory: false});
this._history = new DisplayHistory(true, false);
/** @type {boolean} */
this._historyChangeIgnore = false;
/** @type {boolean} */
Expand Down Expand Up @@ -126,11 +123,7 @@ export class Display extends EventDispatcher {
/** @type {TextSourceGenerator} */
this._textSourceGenerator = new TextSourceGenerator();
/** @type {QueryParser} */
this._queryParser = new QueryParser({
api: application.api,
getSearchContext: this._getSearchContext.bind(this),
textSourceGenerator: this._textSourceGenerator
});
this._queryParser = new QueryParser(application.api, this._textSourceGenerator, this._getSearchContext.bind(this));
/** @type {HTMLElement} */
this._contentScrollElement = querySelectorNotNull(document, '#content-scroll');
/** @type {HTMLElement} */
Expand Down Expand Up @@ -1712,8 +1705,7 @@ export class Display extends EventDispatcher {
const popupFactory = new PopupFactory(this._application);
popupFactory.prepare();

/** @type {import('frontend').ConstructorDetails} */
const setupNestedPopupsOptions = {
const frontend = new Frontend({
application: this._application,
useProxyPopup,
parentPopupId,
Expand All @@ -1723,10 +1715,9 @@ export class Display extends EventDispatcher {
pageType: this._pageType,
allowRootFramePopupProxy: true,
childrenSupported: this._childrenSupported,
hotkeyHandler: this._hotkeyHandler
};

const frontend = new Frontend(setupNestedPopupsOptions);
hotkeyHandler: this._hotkeyHandler,
canUseWindowPopup: true
});
this._frontend = frontend;
await frontend.prepare();
}
Expand Down
6 changes: 4 additions & 2 deletions ext/js/display/query-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import {TextScanner} from '../language/text-scanner.js';
*/
export class QueryParser extends EventDispatcher {
/**
* @param {import('display').QueryParserConstructorDetails} details
* @param {import('../comm/api.js').API} api
* @param {import('../dom/text-source-generator').TextSourceGenerator} textSourceGenerator
* @param {import('display').GetSearchContextCallback} getSearchContext
*/
constructor({api, getSearchContext, textSourceGenerator}) {
constructor(api, textSourceGenerator, getSearchContext) {
super();
/** @type {import('../comm/api.js').API} */
this._api = api;
Expand Down
10 changes: 5 additions & 5 deletions ext/js/display/search-display-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ export class SearchDisplayController {
this._introAnimationTimer = null;
/** @type {boolean} */
this._clipboardMonitorEnabled = false;
/** @type {import('clipboard-monitor').ClipboardReaderLike} */
const clipboardReader = {
getText: this._display.application.api.clipboardGet.bind(this._display.application.api)
};
/** @type {ClipboardMonitor} */
this._clipboardMonitor = new ClipboardMonitor({
clipboardReader: {
getText: this._display.application.api.clipboardGet.bind(this._display.application.api)
}
});
this._clipboardMonitor = new ClipboardMonitor(clipboardReader);
/** @type {import('application').ApiMap} */
this._apiMap = createApiMap([
['searchDisplayControllerGetMode', this._onMessageGetMode.bind(this)],
Expand Down
13 changes: 9 additions & 4 deletions ext/js/dom/dom-data-binder.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ import {SelectorObserver} from './selector-observer.js';
*/
export class DOMDataBinder {
/**
* @param {import('dom-data-binder').ConstructorDetails<T>} details
* @param {string} selector
* @param {import('dom-data-binder').CreateElementMetadataCallback<T>} createElementMetadata
* @param {import('dom-data-binder').CompareElementMetadataCallback<T>} compareElementMetadata
* @param {import('dom-data-binder').GetValuesCallback<T>} getValues
* @param {import('dom-data-binder').SetValuesCallback<T>} setValues
* @param {import('dom-data-binder').OnErrorCallback<T>|null} [onError]
*/
constructor({selector, createElementMetadata, compareElementMetadata, getValues, setValues, onError = null}) {
constructor(selector, createElementMetadata, compareElementMetadata, getValues, setValues, onError = null) {
/** @type {string} */
this._selector = selector;
/** @type {import('dom-data-binder').CreateElementMetadataCallback<T>} */
Expand All @@ -45,14 +50,14 @@ export class DOMDataBinder {
/** @type {TaskAccumulator<import('dom-data-binder').ElementObserver<T>, import('dom-data-binder').AssignTaskValue>} */
this._assignTasks = new TaskAccumulator(this._onBulkAssign.bind(this));
/** @type {SelectorObserver<import('dom-data-binder').ElementObserver<T>>} */
this._selectorObserver = /** @type {SelectorObserver<import('dom-data-binder').ElementObserver<T>>} */ (new SelectorObserver({
this._selectorObserver = new SelectorObserver({
selector,
ignoreSelector: null,
onAdded: this._createObserver.bind(this),
onRemoved: this._removeObserver.bind(this),
onChildrenUpdated: this._onObserverChildrenUpdated.bind(this),
isStale: this._isObserverStale.bind(this)
}));
});
}

/**
Expand Down
5 changes: 3 additions & 2 deletions ext/js/dom/panel-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import {EventDispatcher} from '../core/event-dispatcher.js';
*/
export class PanelElement extends EventDispatcher {
/**
* @param {import('panel-element').ConstructorDetails} details
* @param {HTMLElement} node
* @param {number} closingAnimationDuration
*/
constructor({node, closingAnimationDuration}) {
constructor(node, closingAnimationDuration) {
super();
/** @type {HTMLElement} */
this._node = node;
Expand Down
Loading

0 comments on commit fe875bb

Please sign in to comment.