-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fork type-checker to separate process
- Loading branch information
Showing
11 changed files
with
100 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {ConfigurationLoader} from '../../backend/config-loader' | ||
import {reportDiagnostics} from '../../backend/reporter' | ||
import {LanguageService} from '../../backend/service' | ||
|
||
let serviceConfig: ConfigurationLoader<LanguageService>|null = null | ||
|
||
export async function typeCheck(...files: string[]) { | ||
if(files.length === 0) { | ||
return | ||
} | ||
|
||
if(serviceConfig === null) { | ||
serviceConfig = new ConfigurationLoader(files[0], config => new LanguageService(config)) | ||
} | ||
|
||
const service = await serviceConfig.wait() | ||
|
||
files.forEach(file => | ||
reportDiagnostics(service.parse(file)) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {CheckFileMessage} from '../../interfaces' | ||
import {Batcher} from '../../utils/batcher' | ||
|
||
import {typeCheck} from './checker' | ||
|
||
// The batcher will batch all check queries each 50 milliseconds. | ||
const batcher = new Batcher<CheckFileMessage>(messages => typeCheck(...messages.map(({file}) => file)), 50) | ||
|
||
process.on('message', (message: CheckFileMessage) => batcher.emit(message)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,27 @@ | ||
import {Diagnostic} from 'typescript' | ||
|
||
export interface Message<T extends string, U extends {} = {}> { | ||
__parcelTypeScript: U & { | ||
type: T | ||
} | ||
export type Message<T extends string, U extends {} = {}> = U & { | ||
type: T | ||
} | ||
|
||
export type CheckFileMessage = Message<'check-file', { | ||
file: string | ||
}> | ||
|
||
export type InjectedMessage = CheckFileMessage | ||
export interface InjectedMessage { | ||
__parcelTypeScript: CheckFileMessage | ||
} | ||
|
||
export interface TranspilationResult { | ||
export interface TranspileResult { | ||
sources: { | ||
js: string | ||
sourceMap?: string | ||
} | ||
} | ||
|
||
export interface TypeCheckingResult { | ||
export interface TypeCheckResult { | ||
syntacticDiagnostics: Diagnostic[] | ||
semanticDiagnostics: Diagnostic[] | ||
|
||
transpile(): TranspilationResult | ||
transpile(): TranspileResult | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export class Batcher<T> { | ||
private timeout: NodeJS.Timer|null = null | ||
private readonly queue: T[] = [] | ||
|
||
constructor( | ||
private readonly work: (data: T[]) => void, | ||
private readonly delay: number = 0 | ||
) {} | ||
|
||
public emit(data: T) { | ||
this.queue.push(data) | ||
|
||
if(this.timeout === null) { | ||
this.timeout = setTimeout(this.batch, this.delay) | ||
} | ||
} | ||
|
||
public clear(): void { | ||
if(this.timeout != null) { | ||
clearTimeout(this.timeout) | ||
} | ||
|
||
this.drain() | ||
} | ||
|
||
private drain(): T[] { | ||
return this.queue.splice(0) | ||
} | ||
|
||
private readonly batch = () => { | ||
this.timeout = null | ||
|
||
if(this.queue.length > 0) { | ||
this.work(this.drain()) | ||
} | ||
} | ||
} |