Skip to content

Commit

Permalink
fixes error on opening files
Browse files Browse the repository at this point in the history
  • Loading branch information
outercloudstudio committed Oct 3, 2023
1 parent 1e50fbc commit dcf3e52
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
29 changes: 29 additions & 0 deletions src/components/FileSystem/FileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ export class FileSystem extends Signal<void> {
return this._baseDirectory
}

// To prevent multiple files from saving to the same file at once, we will delay saving until all previous save calls are finished
protected savingQueue: Map<
string,
{
currentSaveID: number
lastSaveID: number
}
> = new Map()

constructor(baseDirectory?: AnyDirectoryHandle) {
super()
if (baseDirectory) this.setup(baseDirectory)
Expand Down Expand Up @@ -236,11 +245,31 @@ export class FileSystem extends Signal<void> {
// await handle.writable.getWriter().write(data)
// handle.close()
// } else {
if (!this.savingQueue.has(fileHandle.name))
this.savingQueue.set(fileHandle.name, {
currentSaveID: 0,
lastSaveID: 0,
})

const savingQueueEntry = this.savingQueue.get(fileHandle.name)!
const currentSaveID = savingQueueEntry.lastSaveID
savingQueueEntry.lastSaveID++

while (savingQueueEntry.currentSaveID !== currentSaveID) {
await new Promise<void>((resolve) => setTimeout(resolve, 1))
}

const writable = await fileHandle.createWritable({
keepExistingData: false,
})

await writable.write(data)
await writable.close()

savingQueueEntry.currentSaveID++

if (savingQueueEntry.currentSaveID === savingQueueEntry.lastSaveID)
this.savingQueue.delete(fileHandle.name)
// }
}

Expand Down
12 changes: 6 additions & 6 deletions src/components/TabSystem/TabSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class TabSystem extends MonacoHolder {
}

async add(tab: Tab, selectTab = true, noTabExistanceCheck = false) {
this.closeAllTemporary()
await this.closeAllTemporary()

if (!noTabExistanceCheck) {
for (const currentTab of this.tabs.value) {
Expand Down Expand Up @@ -155,7 +155,7 @@ export class TabSystem extends MonacoHolder {

return tab
}
remove(tab: Tab, destroyEditor = true, selectNewTab = true) {
async remove(tab: Tab, destroyEditor = true, selectNewTab = true) {
tab.onDeactivate()
const tabIndex = this.tabs.value.findIndex((current) => current === tab)
if (tabIndex === -1) return
Expand All @@ -165,7 +165,7 @@ export class TabSystem extends MonacoHolder {

if (selectNewTab && tab === this.selectedTab)
this.select(this.tabs.value[tabIndex === 0 ? 0 : tabIndex - 1])
if (!tab.isForeignFile) this.openedFiles.remove(tab.getPath())
if (!tab.isForeignFile) await this.openedFiles.remove(tab.getPath())

this.project.updateTabFolders()

Expand All @@ -179,7 +179,7 @@ export class TabSystem extends MonacoHolder {

return (await unsavedWin.fired) !== 'cancel'
} else {
this.remove(tab)
await this.remove(tab)
return true
}
}
Expand Down Expand Up @@ -282,11 +282,11 @@ export class TabSystem extends MonacoHolder {

app.windows.loadingWindow.close()
}
closeAllTemporary() {
async closeAllTemporary() {
for (const tab of [...this.tabs.value]) {
if (!tab.isTemporary) continue

this.remove(tab, true, false)
await this.remove(tab, true, false)
}
}

Expand Down

0 comments on commit dcf3e52

Please sign in to comment.