npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

monaco-worker-manager

v2.0.1

Published

Easily deal with monaco workers

Downloads

750,227

Readme

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-editor-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