GitXplorerGitXplorer
r

monaco-worker-manager

public
4 stars
1 forks
0 issues

Commits

List of commits on branch main.
Verified
9348a1b939d063f21dd68ceb80ae084ef4d2c088

fix typo in README. (#2)

aaqssxlzc committed 2 years ago
Verified
1d87a5548add6572d79aa5b67c28665cf55e3881

2.0.1

rremcohaszing committed 3 years ago
Verified
8039f6f46c91e70608431f63c6da36838c11118c

Remove explicit handling of the client

rremcohaszing committed 3 years ago
Verified
3e013c34ce86ac3f1ffc92717278f479aed48732

2.0.0

rremcohaszing committed 3 years ago
Verified
8d0c34b30ed96afcb75f8f4e0a6e3a66ef827cca

Fix type issues

rremcohaszing committed 3 years ago
Verified
59b0939451e0491123e80fbc80e403ec06045f0e

Add badges to readme

rremcohaszing committed 3 years ago

README

The README file for this repository.

Monaco Worker Manager

ci workflow npm version prettier code style

A Monaco worker manager handles workers in a type safe manner.

Installation

This project has a peer dependency on monaco-editor. It’s recommded to add it explicitly.

npm install monaco-editor monaco-worker-manager

Usage

Create a module that contains a Monaco web worker, let’s call it my.worker.ts.

import { initialize } from 'monaco-worker-manager/worker';
import { doValidate } from 'my-language-service';

export interface MyWorker {
  doValidate: (uri: string) => Violation[];
}

initialize<MyWorker>((ctx, options) => {
  function getModel(uri: string): worker.IMirrorModel | undefined {
    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:

import { editor, Uri } from 'monaco-editor';
import { createWorkerManager } from 'monaco-worker-manager';

import { MyWorker } from './my.worker';

const myLabel = 'myLabel';
const myModuleId = 'my.worker';

window.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<MyWorker>({
  label: myLabel,
  moduleId: myModuleId,
});

const model = editor.createModel('Hello Monaco!', 'plaintext', Uri.parse('file:///hello.txt'));

async function main(): Promise<void> {
  const worker = await workerManager.getWorker(model.uri);
  const diagnostics = await worker.doValidate(String(model.uri));
  console.log(diagnostics);
}

main();

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