https://github.com/remcohaszing/monaco-worker-manager
Easily deal with monaco workers
https://github.com/remcohaszing/monaco-worker-manager
monaco monaco-editor webworker
Last synced: 11 months ago
JSON representation
Easily deal with monaco workers
- Host: GitHub
- URL: https://github.com/remcohaszing/monaco-worker-manager
- Owner: remcohaszing
- License: mit
- Created: 2022-04-03T12:30:04.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-06-20T07:16:18.000Z (about 1 year ago)
- Last Synced: 2025-07-07T09:02:13.040Z (12 months ago)
- Topics: monaco, monaco-editor, webworker
- Language: TypeScript
- Homepage:
- Size: 176 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Monaco Worker Manager
[](https://github.com/remcohaszing/monaco-worker-manager/actions/workflows/ci.yaml)
[](https://www.npmjs.com/package/monaco-worker-manager)
[](https://www.npmjs.com/package/monaco-worker-manager)
A Monaco worker manager handles workers in a type safe manner.
## Table of Contents
- [Installation](#installation)
- [Usage](#usage)
- [API](#api)
- [`monaco-worker-manager#createWorkerManager(options)`](#monaco-worker-managercreateworkermanageroptions)
- [`monaco-worker-manager/worker#initialize(fn)`](#monaco-worker-managerworkerinitializefn)
- [License](#license)
## Installation
This project has a peer dependency on `monaco-editor`. It’s recommded to add it explicitly.
```sh
npm install monaco-editor monaco-worker-manager
```
## Usage
Create a module that contains a Monaco web worker, let’s call it `my.worker.ts`.
```typescript
import { initialize } from 'monaco-worker-manager/worker'
import { doValidate } from 'my-language-service'
export interface MyWorker {
doValidate: (uri: string) => Violation[]
}
initialize((ctx, options) => {
function getModel(uri: string): undefined | worker.IMirrorModel {
for (const model of ctx.getMirrorModels()) {
if (String(model.uri) === uri) {
return model
}
}
}
return {
doValidate(uri) {
const model = getModel(uri)
if (!model) {
return []
}
return doValidate(model, options)
}
}
})
```
Now create a monaco environment and create a worker manager in the main thread:
```typescript
import { editor, Uri } from 'monaco-editor'
import { createWorkerManager } from 'monaco-worker-manager'
import { type MyWorker } from './my.worker'
const myLabel = 'myLabel'
const myModuleId = 'my.worker'
globalThis.MonacoEnvironment = {
getWorker(moduleId, label) {
switch (label) {
case 'editorWorkerService':
return new Worker(new URL('monaco-editor/esm/vs/editor/editor.worker', import.meta.url))
case myLabel:
return new Worker(new URL('my.worker', import.meta.url))
default:
throw new Error(`Unknown label ${label}`)
}
}
}
const workerManager = createWorkerManager({
label: myLabel,
moduleId: myModuleId
})
const model = editor.createModel('Hello Monaco!', 'plaintext', Uri.parse('file:///hello.txt'))
const worker = await workerManager.getWorker(model.uri)
const diagnostics = await worker.doValidate(String(model.uri))
```
## API
This project exposes 2 modules: one to use in the main thread, another to create your own worker.
### `monaco-worker-manager#createWorkerManager(options)`
Create a worker manager.
A worker manager is an object which deals with Monaco based web workers, such as cleanups and idle
timeouts.
#### Options
- `options.createData`: The data to send over when creating the worker. (Optional)
- `options.interval` How often to check if a worker is idle in milliseconds. (Optional, default:
`30_000`)
- `options.label`: A label to be used to identify the web worker.
- `options.moduleId`: The module id to be used to identify the web worker.
- `options.stopWhenIdleFor`: The worker is stopped after this time has passed in milliseconds. Set
to Infinity to never stop the worker. (Optional, default: `120_000`)
#### Return value
A disposable object with the following keys:
- `getWorker(...resources: Uri[])`: An unbound method for getting the web worker.
- `updateCreateData(newCreateData)`: An unbound method which updates the create data and reloads the
worker.
### `monaco-worker-manager/worker#initialize(fn)`
Create a web worker in a type safe manner.
The function will be called with the following arguments:
1. The Monaco worker context.
2. The create data defined by the worker manager.
## License
[MIT](https://github.com/remcohaszing/monaco-worker-manager/blob/main/LICENSE.md)