Skip to content

Commit

Permalink
Don't wait for the slowest linter before showing results
Browse files Browse the repository at this point in the history
FIX: When multiple linters are installed, start displaying results from ones that return quickly
even if others are slow to return.

See https://discuss.codemirror.net/t/mixing-async-linter-with-immediate-linter/8654
  • Loading branch information
marijnh committed Sep 24, 2024
1 parent c13331c commit 35644c1
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,10 @@ const lintPlugin = ViewPlugin.fromClass(class {
} else {
this.set = false
let {state} = this.view, {sources} = state.facet(lintConfig)
if (sources.length) Promise.all(sources.map(source => Promise.resolve(source(this.view)))).then(
annotations => {
let all = annotations.reduce((a, b) => a.concat(b))
if (this.view.state.doc == state.doc)
this.view.dispatch(setDiagnostics(this.view.state, all))
},
error => { logException(this.view.state, error) }
)
if (sources.length) batchResults(sources.map(s => Promise.resolve(s(this.view))), annotations => {
if (this.view.state.doc == state.doc)
this.view.dispatch(setDiagnostics(this.view.state, annotations.reduce((a, b) => a.concat(b))))
}, error => { logException(this.view.state, error) })
}
}

Expand Down Expand Up @@ -339,6 +335,16 @@ const lintPlugin = ViewPlugin.fromClass(class {
}
})

function batchResults<T>(promises: readonly Promise<T>[], sink: (values: T[]) => void, error: (reason: any) => void) {
let collected: T[] = [], timeout = -1
for (let p of promises) p.then(value => {
collected.push(value)
clearTimeout(timeout)
if (collected.length == promises.length) sink(collected)
else setTimeout(() => sink(collected), 200)

This comment has been minimized.

Copy link
@hubgit

hubgit Nov 27, 2024

Contributor

@marijnh I think this line is meant to be setting timeout?

This comment has been minimized.

Copy link
@marijnh

marijnh Nov 28, 2024

Author Member

Looks like you're right. See 124f997

}, error)
}

const lintConfig = Facet.define<{source: LintSource | null, config: LintConfig},
Required<LintConfig> & {sources: readonly LintSource[]}>({
combine(input) {
Expand Down

0 comments on commit 35644c1

Please sign in to comment.