Skip to content

Commit 887ee2a

Browse files
authored
refactor(superdoc): mechanical residual checkJs fixes (SD-2867) (#3088)
* refactor(superdoc): mechanical residual checkJs fixes (SD-2867) * docs(superdoc): correct cancelWebsocketRetry rationale comment * fix(superdoc): drop storeDocs annotations on untested upgrade methods
1 parent 67b5c37 commit 887ee2a

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

packages/superdoc/src/core/SuperDoc.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,10 @@ export class SuperDoc extends EventEmitter {
478478

479479
#initDocuments() {
480480
const doc = this.config.document;
481-
const hasDocumentConfig = !!doc && typeof doc === 'object' && Object.keys(this.config.document)?.length;
481+
// Pass the narrowed `doc` to `Object.keys` so the `!!doc && typeof doc === 'object'`
482+
// gate carries through; refetching `this.config.document` re-widens to
483+
// `string | object | File | Blob | undefined` and trips the overload.
484+
const hasDocumentConfig = !!doc && typeof doc === 'object' && Object.keys(doc)?.length;
482485
const hasDocumentUrl = !!doc && typeof doc === 'string' && doc.length > 0;
483486
const hasDocumentFile = !!doc && typeof File === 'function' && doc instanceof File;
484487
const hasDocumentBlob = !!doc && doc instanceof Blob && !(doc instanceof File);
@@ -1339,6 +1342,7 @@ export class SuperDoc extends EventEmitter {
13391342
* @returns {Promise<boolean>} Whether the target was found and navigated to.
13401343
*/
13411344
async navigateTo(target) {
1345+
/** @type {RuntimeDocument[] | undefined} */
13421346
const storeDocs = this.superdocStore?.documents;
13431347
if (!storeDocs?.length) return false;
13441348
const presentationEditor = storeDocs[0].getPresentationEditor?.();
@@ -1364,6 +1368,7 @@ export class SuperDoc extends EventEmitter {
13641368
* await superdoc.scrollToElement('imported-25def254');
13651369
*/
13661370
async scrollToElement(elementId) {
1371+
/** @type {RuntimeDocument[] | undefined} */
13671372
const storeDocs = this.superdocStore?.documents;
13681373
if (!storeDocs?.length) return false;
13691374
const presentationEditor = storeDocs[0].getPresentationEditor?.();
@@ -1551,6 +1556,7 @@ export class SuperDoc extends EventEmitter {
15511556
});
15521557
}
15531558

1559+
/** @type {RuntimeDocument[] | undefined} */
15541560
const docs = this.superdocStore?.documents;
15551561
if (Array.isArray(docs) && docs.length > 0) {
15561562
docs.forEach((doc) => {
@@ -1804,7 +1810,7 @@ export class SuperDoc extends EventEmitter {
18041810
this.pendingCollaborationSaves++;
18051811
this.#log(`After increment - Doc ${index}: pending = ${this.pendingCollaborationSaves}`);
18061812
const metaMap = doc.ydoc.getMap('meta');
1807-
metaMap.observe((event) => {
1813+
metaMap.observe((/** @type {import('yjs').YMapEvent<unknown>} */ event) => {
18081814
if (event.changes.keys.has('immediate-save-finished')) {
18091815
this.pendingCollaborationSaves--;
18101816
if (this.pendingCollaborationSaves <= 0) {
@@ -1850,7 +1856,12 @@ export class SuperDoc extends EventEmitter {
18501856
}
18511857

18521858
const cfg = /** @type {InternalConfig} */ (this.config);
1853-
cfg.socket?.cancelWebsocketRetry();
1859+
// `cancelWebsocketRetry` is set on `HocuspocusProviderWebsocket` only
1860+
// while a reconnect timer is pending, and Hocuspocus clears it back to
1861+
// `undefined` after firing. Destroy from the "already connected, no
1862+
// pending retry" path lands here with the method absent, so the
1863+
// optional chain on the method is required to avoid a `TypeError`.
1864+
cfg.socket?.cancelWebsocketRetry?.();
18541865
cfg.socket?.disconnect();
18551866
cfg.socket?.destroy();
18561867

0 commit comments

Comments
 (0)