-
Notifications
You must be signed in to change notification settings - Fork 29
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
129 additions
and
37 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,74 @@ | ||
/** | ||
* 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 | ||
} | ||
|
||
const scope: IWorkerScope = self | ||
const lsIniting = initLS() | ||
let files: Files = {} | ||
|
||
async function initLS(): Promise<Spxls> { | ||
const go = new Go() | ||
const { instance } = await WebAssembly.instantiateStreaming(fetch(spxlsWasmUrl), go.importObject) | ||
go.run(instance) | ||
const ls = NewSpxls( | ||
() => files, | ||
(message) => { | ||
scope.postMessage({ type: 'lsp', message }) | ||
} | ||
) | ||
if (ls instanceof Error) throw ls | ||
return ls | ||
} | ||
|
||
function main() { | ||
scope.addEventListener('message', async (event) => { | ||
const message = event.data | ||
switch (message.type) { | ||
case 'lsp': { | ||
const ls = await lsIniting | ||
const error = ls.handleMessage(message.message) | ||
if (error != null) throw error | ||
break | ||
} | ||
case 'files': | ||
files = message.files | ||
return | ||
} | ||
}) | ||
} | ||
|
||
main() |
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