2023-10-26 04:57:00 -04:00
|
|
|
// Migrated from static methods of Document in Document.js
|
|
|
|
|
2024-02-08 05:10:46 -05:00
|
|
|
import { DocumentContainer } from '@/features/ide-react/editor/document-container'
|
2023-10-26 04:57:00 -04:00
|
|
|
import { debugConsole } from '@/utils/debugging'
|
|
|
|
import { Socket } from '@/features/ide-react/connection/types/socket'
|
|
|
|
import { IdeEventEmitter } from '@/features/ide-react/create-ide-event-emitter'
|
|
|
|
import { EventLog } from '@/features/ide-react/editor/event-log'
|
|
|
|
import EditorWatchdogManager from '@/features/ide-react/connection/editor-watchdog-manager'
|
|
|
|
|
|
|
|
export class OpenDocuments {
|
2024-02-08 05:10:46 -05:00
|
|
|
private openDocs = new Map<string, DocumentContainer>()
|
2023-10-26 04:57:00 -04:00
|
|
|
|
|
|
|
// eslint-disable-next-line no-useless-constructor
|
|
|
|
constructor(
|
|
|
|
private readonly socket: Socket,
|
|
|
|
private readonly globalEditorWatchdogManager: EditorWatchdogManager,
|
|
|
|
private readonly events: IdeEventEmitter,
|
|
|
|
private readonly eventLog: EventLog
|
|
|
|
) {}
|
|
|
|
|
|
|
|
getDocument(docId: string) {
|
|
|
|
// Try to clean up existing docs before reopening them. If the doc has no
|
|
|
|
// buffered ops then it will be deleted by _cleanup() and a new instance
|
|
|
|
// of the document created below. This prevents us trying to follow the
|
|
|
|
// joinDoc:existing code path on an existing doc that doesn't have any
|
|
|
|
// local changes and getting an error if its version is too old.
|
|
|
|
if (this.openDocs.has(docId)) {
|
|
|
|
debugConsole.log(
|
|
|
|
`[getDocument] Cleaning up existing document instance for ${docId}`
|
|
|
|
)
|
|
|
|
this.openDocs.get(docId)?.cleanUp()
|
|
|
|
}
|
|
|
|
if (!this.openDocs.has(docId)) {
|
|
|
|
debugConsole.log(
|
|
|
|
`[getDocument] Creating new document instance for ${docId}`
|
|
|
|
)
|
|
|
|
this.createDoc(docId)
|
|
|
|
} else {
|
|
|
|
debugConsole.log(
|
|
|
|
`[getDocument] Returning existing document instance for ${docId}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return this.openDocs.get(docId)
|
|
|
|
}
|
|
|
|
|
|
|
|
private createDoc(docId: string) {
|
2024-02-08 05:10:46 -05:00
|
|
|
const doc = new DocumentContainer(
|
2023-10-26 04:57:00 -04:00
|
|
|
docId,
|
|
|
|
this.socket,
|
|
|
|
this.globalEditorWatchdogManager,
|
|
|
|
this.events,
|
2023-12-15 07:18:56 -05:00
|
|
|
this.eventLog,
|
|
|
|
this.detachDoc.bind(this)
|
2023-10-26 04:57:00 -04:00
|
|
|
)
|
|
|
|
this.openDocs.set(docId, doc)
|
2023-12-15 07:18:56 -05:00
|
|
|
}
|
|
|
|
|
2024-02-08 05:10:46 -05:00
|
|
|
detachDoc(docId: string, doc: DocumentContainer) {
|
2023-12-15 07:18:56 -05:00
|
|
|
if (this.openDocs.get(docId) === doc) {
|
2023-10-26 04:57:00 -04:00
|
|
|
debugConsole.log(
|
|
|
|
`[detach] Removing document with ID (${docId}) from openDocs`
|
|
|
|
)
|
|
|
|
this.openDocs.delete(docId)
|
2023-12-15 07:18:56 -05:00
|
|
|
} else {
|
|
|
|
// It's possible that this instance has error, and the doc has been reloaded.
|
|
|
|
// This creates a new instance in Document.openDoc with the same id. We shouldn't
|
|
|
|
// clear it because it's not this instance.
|
|
|
|
debugConsole.log(
|
|
|
|
`[_cleanUp] New instance of (${docId}) created. Not removing`
|
|
|
|
)
|
|
|
|
}
|
2023-10-26 04:57:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hasUnsavedChanges() {
|
2023-12-14 06:06:36 -05:00
|
|
|
for (const doc of this.openDocs.values()) {
|
|
|
|
if (doc.hasBufferedOps()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2023-10-26 04:57:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flushAll() {
|
2023-12-14 06:06:36 -05:00
|
|
|
for (const doc of this.openDocs.values()) {
|
|
|
|
doc.flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsavedDocIds() {
|
|
|
|
const ids = []
|
|
|
|
for (const [docId, doc] of this.openDocs) {
|
|
|
|
if (!doc.pollSavedStatus()) {
|
|
|
|
ids.push(docId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ids
|
2023-10-26 04:57:00 -04:00
|
|
|
}
|
|
|
|
}
|