UNPKG

2.81 kBTypeScriptView Raw
1/**
2 * Copyright 2017 Google Inc. All Rights Reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
12 */
13export interface Endpoint {
14 postMessage(message: any, transfer?: any[]): void;
15 addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: {}): void;
16 removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: {}): void;
17}
18declare type Promisify<T> = T extends Promise<any> ? T : Promise<T>;
19/**
20 * Symbol that gets added to objects by `Comlink.proxy()`.
21 */
22export declare const proxyValueSymbol: unique symbol;
23/**
24 * Object that was wrapped with `Comlink.proxy()`.
25 */
26export interface ProxyValue {
27 [proxyValueSymbol]: true;
28}
29/** Helper that omits all keys K from object T */
30declare type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
31/**
32 * `ProxiedObject<T>` is equivalent to `T`, except that all properties are now promises and
33 * all functions now return promises, except if they were wrapped with `Comlink.proxyValue()`.
34 * It effectively async-ifies an object.
35 */
36declare type ProxiedObject<T> = {
37 [P in keyof T]: T[P] extends (...args: infer Arguments) => infer R ? (...args: Arguments) => Promisify<R> : (T[P] extends ProxyValue ? ProxiedObject<Omit<T[P], keyof ProxyValue>> : Promisify<T[P]>);
38};
39/**
40 * ProxyResult<T> is an augmentation of ProxyObject<T> that also handles raw functions
41 * and classes correctly.
42 */
43export declare type ProxyResult<T> = ProxiedObject<T> & (T extends (...args: infer Arguments) => infer R ? (...args: Arguments) => Promisify<R> : unknown) & (T extends {
44 new (...args: infer ArgumentsType): infer InstanceType;
45} ? {
46 new (...args: ArgumentsType): Promisify<ProxiedObject<InstanceType>>;
47} : unknown);
48export declare type Proxy = Function;
49export declare type Exposable = Function | Object;
50export interface TransferHandler {
51 canHandle: (obj: {}) => Boolean;
52 serialize: (obj: {}) => {};
53 deserialize: (obj: {}) => {};
54}
55export declare const transferHandlers: Map<string, TransferHandler>;
56export declare function proxy<T = any>(endpoint: Endpoint | Window, target?: any): ProxyResult<T>;
57export declare function proxyValue<T>(obj: T): T & ProxyValue;
58export declare function expose(rootObj: Exposable, endpoint: Endpoint | Window): void;
59export {};