Worklet support

Open
#29,130 5 comments 14 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
angular, typescript
Domain
build-system

Research direction

Start by reading the compiler's existing web-worker support and comparing it with the Worklet API described in the issue. The work is done when Angular provides equivalent TypeScript compilation and bundling support for worklets, with coverage for the requested loading flow.

Written by the indexing model from the issue text.

Description

angular/build:application area: @angular/build feature feature: insufficient votes
Which @angular/* package(s) are relevant/related to the feature request?

compiler

Description

The worklets are light weighted version of web workers. Currently Angular supports web workers, but not worklets, this makes us unable to make use of Angular's Typescript and bundling support when writing a worklet (i.e. we have to write the worklet in pure Javasctript, or deal with compiling and bundling ourselves).

Proposed solution

Support worklet like web workers.

Alternatives considered

For now we have come to a hacky workaround:

  1. Write the worklet as if it's a web worker. This is perfecly supported by Angular. Now the problem is, to load the compiled worker script, we don't have a trivial way to get its URL.

  2. Wrap the worker code like this:

if (typeof self !== "undefined") {
  postMessage(self.location.href);
} else {
  // do the worklet thing
}

When self is not undefined, we know this script is loaded as a webworker (in a worklet context it would be undefined). In this case, send the script's location by postMessage.

  1. When trying to load the worklet, first load it as a web worker, wait for it to report the location, then terminate the worker and use the location to load the worklet.

E.g. load an audio worklet:

// probe worker url
const worker = new Worker(
  new URL("path/to/the/worker", import.meta.url),
);

const url = await new Promise<string>((resolve, reject) => {
  worker.onmessage = (e) => {
    resolve(e.data);
    worker.terminate();
  };

  worker.onmessageerror = reject;
});

await audioContext.audioWorklet.addModule(url);
Dominant language
TypeScript
Stars
27k
Forks
11.8k
Avg merge
16h 35m
Merged PRs (30d)
176

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from angular/angular-cli

All issues in angular/angular-cli

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.