-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
158 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* This file is worker script for spxls (spx language server). | ||
* It runs in a Web Worker. | ||
*/ | ||
|
||
declare const self: DedicatedWorkerGlobalScope | ||
|
||
import '@/assets/wasm_exec.js' | ||
import spxlsWasmUrl from '@/assets/spxls.wasm?url' | ||
import type { Files, Message, NotificationMessage, RequestMessage, ResponseMessage, Spxls } from './spxls' | ||
|
||
export type FilesMessage = { | ||
type: 'files' | ||
files: Files | ||
} | ||
|
||
export type LSPMessage<M extends Message> = { | ||
type: 'lsp' | ||
message: M | ||
} | ||
|
||
/** Message that worker send to main thread. */ | ||
export type WorkerMessage = LSPMessage<ResponseMessage | NotificationMessage> | ||
|
||
/** Message that main thread send to worker. */ | ||
export type MainMessage = LSPMessage<RequestMessage | NotificationMessage> | FilesMessage | ||
|
||
interface IWorkerScope { | ||
postMessage(message: WorkerMessage): void | ||
addEventListener(type: 'message', listener: (event: MessageEvent<MainMessage>) => void): void | ||
} | ||
|
||
export interface IWorkerHandler { | ||
postMessage(message: MainMessage): void | ||
addEventListener(type: 'message', listener: (event: MessageEvent<WorkerMessage>) => void): void | ||
terminate(): void | ||
} | ||
|
||
class SpxlsWorker { | ||
private lsInited: Promise<Spxls> | ||
|
||
constructor(private scope: IWorkerScope) { | ||
this.lsInited = this.initLS() | ||
|
||
scope.addEventListener('message', async (event) => { | ||
const message = event.data | ||
switch (message.type) { | ||
case 'lsp': { | ||
const ls = await this.lsInited | ||
const error = ls.handleMessage(message.message) | ||
if (error != null) throw error | ||
break | ||
} | ||
case 'files': | ||
this.setFiles(message.files) | ||
return | ||
} | ||
}) | ||
} | ||
|
||
private files: Files = {} | ||
setFiles(files: Files): void { | ||
this.files = files | ||
} | ||
|
||
private async initLS(): Promise<Spxls> { | ||
const go = new Go() | ||
const { instance } = await WebAssembly.instantiateStreaming(fetch(spxlsWasmUrl), go.importObject) | ||
go.run(instance) | ||
const ls = NewSpxls( | ||
() => this.files, | ||
(message) => { | ||
this.scope.postMessage({ type: 'lsp', message }) | ||
} | ||
) | ||
if (ls instanceof Error) throw ls | ||
return ls | ||
} | ||
} | ||
|
||
new SpxlsWorker(self) |
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 |
---|---|---|
|
@@ -15,6 +15,12 @@ | |
"@/*": [ | ||
"src/*" | ||
] | ||
} | ||
}, | ||
"lib": [ | ||
"es2023", | ||
"dom", | ||
"dom.iterable", | ||
"webworker" | ||
] | ||
} | ||
} |
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