1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 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 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16The definitions of some interfaces implemented in jsapi/workers/worker.cpp are released under Mozilla license. 17 18The definitions and functions of these interfaces are consistent with the standard interfaces under mozila license, 19but the implementation of specific functions is independent and self-developed. 20 21All interfaces are described in d.ts, the following is the interface written in d.ts under to Mozilla license 22 23export interface WorkerOptions { 24 type?: "classic" | "module"; 25 name?: string; 26 shared?: boolean; 27} 28 29export interface Event { 30 readonly type: string; 31 readonly timeStamp: number; 32} 33 34interface ErrorEvent extends Event { 35 readonly message: string; 36 readonly filename: string; 37 readonly lineno: number; 38 readonly colno: number; 39 readonly error: Object; 40} 41 42declare interface MessageEvent<T = Object> extends Event { 43 readonly data: T; 44} 45 46export interface PostMessageOptions { 47 transfer?: Object[]; 48} 49 50export interface EventListener { 51 (evt: Event): void | Promise<void>; 52} 53 54type MessageType = "message" | "messageerror"; 55 56declare interface EventTarget { 57 addEventListener( 58 type: string, 59 listener: EventListener 60 ): void; 61 dispatchEvent(event: Event): boolean; 62 removeEventListener( 63 type: string, 64 callback?: EventListener 65 ): void; 66 removeAllListener(): void; 67} 68 69declare interface WorkerGlobalScope extends EventTarget { 70 readonly name: string; 71 onerror?: (ev: ErrorEvent) => void; 72 readonly self: WorkerGlobalScope & typeof globalThis; 73} 74 75declare interface DedicatedWorkerGlobalScope extends WorkerGlobalScope { 76 onmessage?: (this: DedicatedWorkerGlobalScope, ev: MessageEvent) => void; 77 onmessageerror?: (this: DedicatedWorkerGlobalScope, ev: MessageEvent) => void; 78 close(): void; 79 postMessage(messageObject: Object, transfer: Transferable[]): void; 80 postMessage(messageObject: Object, options?: PostMessageOptions): void; 81} 82 83declare namespace worker { 84 class Worker extends EventTarget { 85 constructor(scriptURL: string, options?: WorkerOptions); 86 onexit?: (code: number) => void; 87 onerror?: (err: ErrorEvent) => void; 88 onmessage?: (event: MessageEvent) => void; 89 onmessageerror?: (event: MessageEvent) => void; 90 postMessage(message: Object, transfer: ArrayBuffer[]): void; 91 postMessage(message: Object, options?: PostMessageOptions): void; 92 terminate(): void; 93 } 94 const parentPort: DedicatedWorkerGlobalScope; 95}