Skip to content

Commit

Permalink
fix: call deltaExtension as few times as possible
Browse files Browse the repository at this point in the history
to improve perfs, and also call it sequentially to prevent race conditions
  • Loading branch information
Loïc Mangeonjean committed Jul 10, 2023
1 parent 1bde13b commit 83d9cc5
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,32 @@ async function deltaExtensions (toAdd: IExtensionDescription[], toRemove: IExten
}
}

let deltaExtensionPromise = Promise.resolve()
function deltaExtensionsSequential (toAdd: IExtensionDescription[], toRemove: IExtensionDescription[]) {
async function call () {
await deltaExtensions(toAdd, toRemove)
}
deltaExtensionPromise = deltaExtensionPromise.then(call, call)
}

let toAdd: IExtensionDescription[] = []
let toRemove: IExtensionDescription[] = []
let deltaExtensionTimeout: number | undefined

function deltaExtensionsDebounced (_toAdd: IExtensionDescription[], _toRemove: IExtensionDescription[]) {
toAdd.push(..._toAdd)
toRemove.push(..._toRemove)
if (deltaExtensionTimeout != null) {
window.clearTimeout(deltaExtensionTimeout)
}
deltaExtensionTimeout = window.setTimeout(() => {
deltaExtensionTimeout = undefined
deltaExtensionsSequential(toAdd, toRemove)
toAdd = []
toRemove = []
}, 0)
}

interface RegisterExtensionResult extends IDisposable {
api: typeof vscode
registerFile: (path: string, getContent: () => Promise<Uint8Array | string>) => IDisposable
Expand Down Expand Up @@ -127,7 +153,7 @@ export function registerExtension (manifest: IExtensionManifest, defaultNLS?: IT
}
const extensionDescription = toExtensionDescription(extension)

void deltaExtensions([extensionDescription], [])
void deltaExtensionsDebounced([extensionDescription], [])

const api = createApi(extensionDescription)

Expand All @@ -142,7 +168,7 @@ export function registerExtension (manifest: IExtensionManifest, defaultNLS?: IT
return registerExtensionFile(location, path, async () => content)
},
dispose () {
void deltaExtensions([], [extensionDescription])
void deltaExtensionsDebounced([], [extensionDescription])
}
}
}
Expand Down

0 comments on commit 83d9cc5

Please sign in to comment.