comlink

  • Version 4.4.1
  • Published
  • 248 kB
  • No dependencies
  • Apache-2.0 license

Install

npm i comlink
yarn add comlink
pnpm add comlink

Overview

Comlink makes WebWorkers enjoyable

Index

Variables

variable createEndpoint

const createEndpoint: Symbol;

    variable finalizer

    const finalizer: Symbol;

      variable proxyMarker

      const proxyMarker: Symbol;

        variable releaseProxy

        const releaseProxy: Symbol;

          variable transferHandlers

          const transferHandlers: Map<string, TransferHandler<unknown, unknown>>;
          • Allows customizing the serialization of certain values.

          Functions

          function expose

          expose: (obj: any, ep?: Endpoint, allowedOrigins?: (string | RegExp)[]) => void;

            function proxy

            proxy: <T extends {}>(obj: T) => T & ProxyMarked;

              function transfer

              transfer: <T>(obj: T, transfers: Transferable[]) => T;

                function windowEndpoint

                windowEndpoint: (
                w: PostMessageWithOrigin,
                context?: EventSource,
                targetOrigin?: string
                ) => Endpoint;

                  function wrap

                  wrap: <T>(ep: Endpoint, target?: any) => Remote<T>;

                    Interfaces

                    interface Endpoint

                    interface Endpoint extends EventSource {}

                      property start

                      start?: () => void;

                        method postMessage

                        postMessage: (message: any, transfer?: Transferable[]) => void;

                          interface ProxyMarked

                          interface ProxyMarked {}
                          • Interface of values that were marked to be proxied with comlink.proxy(). Can also be implemented by classes.

                          property [proxyMarker]

                          [proxyMarker]: true;

                            interface ProxyMethods

                            interface ProxyMethods {}
                            • Additional special comlink methods available on each proxy returned by Comlink.wrap().

                            property [createEndpoint]

                            [createEndpoint]: () => Promise<MessagePort>;

                              property [releaseProxy]

                              [releaseProxy]: () => void;

                                interface TransferHandler

                                interface TransferHandler<T, S> {}
                                • Customizes the serialization of certain values as determined by canHandle().

                                  T The input type being handled by this transfer handler. S The serialized type sent over the wire.

                                method canHandle

                                canHandle: (value: unknown) => value is T;
                                • Gets called for every value to determine whether this transfer handler should serialize the value, which includes checking that it is of the right type (but can perform checks beyond that as well).

                                method deserialize

                                deserialize: (value: S) => T;
                                • Gets called to deserialize an incoming value that was serialized in the other thread with this transfer handler (known through the name it was registered under).

                                method serialize

                                serialize: (value: T) => [S, Transferable[]];
                                • Gets called with the value if canHandle() returned true to produce a value that can be sent in a message, consisting of structured-cloneable values and/or transferrable objects.

                                Type Aliases

                                type Local

                                type Local<T> = Omit<LocalObject<T>, keyof ProxyMethods> &
                                (T extends (...args: infer TArguments) => infer TReturn
                                ? (
                                ...args: {
                                [I in keyof TArguments]: ProxyOrClone<TArguments[I]>;
                                }
                                ) => MaybePromise<UnproxyOrClone<Unpromisify<TReturn>>>
                                : unknown) &
                                (T extends {
                                new (...args: infer TArguments): infer TInstance;
                                }
                                ? {
                                new (
                                ...args: {
                                [I in keyof TArguments]: ProxyOrClone<TArguments[I]>;
                                }
                                ): MaybePromise<Local<Unpromisify<TInstance>>>;
                                }
                                : unknown);
                                • Takes the raw type of a remote object, function or class as a remote thread would see it through a proxy (e.g. when passed in as a function argument) and returns the type the local thread has to supply.

                                  This is the inverse of Remote<T>. It takes a Remote<T> and returns its original input T.

                                type LocalObject

                                type LocalObject<T> = {
                                [P in keyof T]: LocalProperty<T[P]>;
                                };
                                • Takes the type of an object as a remote thread would see it through a proxy (e.g. when passed in as a function argument) and returns the type that the local thread has to supply.

                                  This does not handle call signatures, which is handled by the more general Local<T> type.

                                  This is the inverse of RemoteObject<T>.

                                  T The type of a proxied object.

                                type ProxyOrClone

                                type ProxyOrClone<T> = T extends ProxyMarked ? Remote<T> : T;
                                • Proxies T if it is a ProxyMarked, clones it otherwise (as handled by structured cloning and transfer handlers).

                                type Remote

                                type Remote<T> = RemoteObject<T> &
                                (T extends (...args: infer TArguments) => infer TReturn
                                ? (
                                ...args: {
                                [I in keyof TArguments]: UnproxyOrClone<TArguments[I]>;
                                }
                                ) => Promisify<ProxyOrClone<Unpromisify<TReturn>>>
                                : unknown) &
                                (T extends {
                                new (...args: infer TArguments): infer TInstance;
                                }
                                ? {
                                new (
                                ...args: {
                                [I in keyof TArguments]: UnproxyOrClone<TArguments[I]>;
                                }
                                ): Promisify<Remote<TInstance>>;
                                }
                                : unknown) &
                                ProxyMethods;
                                • Takes the raw type of a remote object, function or class in the other thread and returns the type as it is visible to the local thread from the proxy return value of Comlink.wrap() or Comlink.proxy().

                                type RemoteObject

                                type RemoteObject<T> = {
                                [P in keyof T]: RemoteProperty<T[P]>;
                                };
                                • Takes the raw type of a remote object in the other thread and returns the type as it is visible to the local thread when proxied with Comlink.proxy().

                                  This does not handle call signatures, which is handled by the more general Remote<T> type.

                                  T The raw type of a remote object as seen in the other thread.

                                type UnproxyOrClone

                                type UnproxyOrClone<T> = T extends RemoteObject<ProxyMarked> ? Local<T> : T;
                                • Inverse of ProxyOrClone<T>.

                                Package Files (2)

                                Dependencies (0)

                                No dependencies.

                                Dev Dependencies (19)

                                Peer Dependencies (0)

                                No peer dependencies.

                                Badge

                                To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/comlink.

                                • Markdown
                                  [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/comlink)
                                • HTML
                                  <a href="https://www.jsdocs.io/package/comlink"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>