react-native-webview-comlink
Version:
Add JavaScript interface for react-native-webview, based on Comlink
67 lines (66 loc) • 2.03 kB
TypeScript
export declare const enum WireValueType {
RAW = "R",
PROXY = "P",
THROW = "T"
}
interface RawWireValue {
type: WireValueType.RAW;
val: unknown;
}
interface ThrowWireValue {
type: WireValueType.THROW;
val: unknown;
}
interface ProxyWireValue {
type: WireValueType.PROXY;
id: number;
/**
* vid (version id) is the version of the proxy.
* it is used to detect if the proxy is being send to remote
* and local function cannot be released.
*/
vid: number;
}
export declare type WireValue = RawWireValue | ThrowWireValue | ProxyWireValue;
export declare const enum MessageType {
REQUEST = "REQ",
RESPONSE = "RSP",
RELEASE = "RLS"
}
export interface RequestMessage {
type: MessageType.REQUEST;
id: number;
rid: number;
args: WireValue[];
}
export interface ResponseMessage {
type: MessageType.RESPONSE;
rid: number;
ret?: WireValue;
}
export interface ReleaseMessage {
type: MessageType.RELEASE;
id: number;
vid: number;
}
export declare type Message = RequestMessage | ResponseMessage | ReleaseMessage;
export declare type Remote<T> = T & {
release(): number;
};
export declare type RemoteFunction = Remote<Function> & {
addRef(vid: number): number;
cleanup(): void;
};
export interface Channel {
notifyRelease(msg: ReleaseMessage): void;
requestResponse(msg: RequestMessage): Promise<WireValue>;
registerLocalFunction(id: number, vid: number, localFunction: Function): void;
tryGetLocalFuntionID(localFunction: Function): number;
registerRemoteFunction(id: number, remoteFunction: RemoteFunction): void;
unregisterRemoteFunction(id: number): void;
tryGetRemoteFunction(id: number): RemoteFunction;
}
export declare function toWireValue(value: unknown, channel: Channel): WireValue;
export declare function fromWireValue(value: WireValue, channel: Channel): unknown;
export declare function createRemoteFunction(id: number, vid: number, channel: Channel): RemoteFunction;
export {};