1/* 2 * Copyright (c) 2021 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 16/** 17* Provides options that can be set for the worker to create. 18* @devices phone, tablet, wearable 19* @since 7 20*/ 21export interface WorkerOptions { 22 /** 23 * Mode in which the worker executes the script. 24 * @devices phone, tablet, wearable 25 * @since 7 26 */ 27 type?: "classic" | "module"; 28 29 /** 30 * Name of the worker. 31 * @devices phone, tablet, wearable 32 * @since 7 33 */ 34 name?: string; 35 36 /** 37 * Whether the worker is shared. 38 * @devices phone, tablet, wearable 39 * @since 7 40 */ 41 shared?: boolean; 42} 43 44/** 45 * Defines the event. 46 * @devices phone, tablet, wearable 47 * @since 7 48 */ 49export interface Event { 50 /** 51 * Type of the Event. 52 * @devices phone, tablet, wearable 53 * @since 7 54 */ 55 readonly type: string; 56 57 /** 58 * Timestamp(accurate to millisecond) when the event is created. 59 * @devices phone, tablet, wearable 60 * @since 7 61 */ 62 readonly timeStamp: number; 63} 64 65/** 66 * Provides detailed information about the exception occurred during worker execution. 67 * @devices phone, tablet, wearable 68 * @since 7 69 */ 70interface ErrorEvent extends Event { 71 /** 72 * Information about the exception. 73 * @devices phone, tablet, wearable 74 * @since 7 75 */ 76 readonly message: string; 77 78 /** 79 * File where the exception is located. 80 * @devices phone, tablet, wearable 81 * @since 7 82 */ 83 readonly filename: string; 84 85 /** 86 * Number of the line where the exception is located. 87 * @devices phone, tablet, wearable 88 * @since 7 89 */ 90 readonly lineno: number; 91 92 /** 93 * Number of the column where the exception is located. 94 * @devices phone, tablet, wearable 95 * @since 7 96 */ 97 readonly colno: number; 98 99 /** 100 * Type of the exception. 101 * @devices phone, tablet, wearable 102 * @since 7 103 */ 104 readonly error: Object; 105} 106 107/** 108 * Holds the data transferred between worker threads. 109 * @devices phone, tablet, wearable 110 * @since 7 111 */ 112declare interface MessageEvent<T = Object> extends Event { 113 /** 114 * Data transferred when an exception occurs. 115 * @devices phone, tablet, wearable 116 * @since 7 117 */ 118 readonly data: T; 119} 120 121/** 122 * Specifies the object whose ownership need to be transferred during data transfer. 123 * The object must be ArrayBuffer. 124 * @devices phone, tablet, wearable 125 * @since 7 126 */ 127export interface PostMessageOptions { 128 /** 129 * ArrayBuffer array used to transfer the ownership. 130 * @devices phone, tablet, wearable 131 * @since 7 132 */ 133 transfer?: Object[]; 134} 135 136/** 137 * Implements evemt listening. 138 * @devices phone, tablet, wearable 139 * @since 7 140 */ 141export interface EventListener { 142 /** 143 * Specifies the callback to invoke. 144 * @param evt Event class for the callback to invoke. 145 * @devices phone, tablet, wearable 146 * @since 7 147 */ 148 (evt: Event): void | Promise<void>; 149} 150 151/** 152 * Type of message, only "message" and "messageerror". 153 * @devices phone, tablet, wearable 154 * @since 7 155 */ 156type MessageType = "message" | "messageerror"; 157 158/** 159 * Specific event features. 160 * @devices phone, tablet, wearable 161 * @since 7 162 */ 163declare interface EventTarget { 164 /** 165 * Adds an event listener to the worker. 166 * @param type Type of the event to listen for. 167 * @param listener Callback to invoke when an event of the specified type occurs. 168 * @devices phone, tablet, wearable 169 * @since 7 170 */ 171 addEventListener( 172 type: string, 173 listener: EventListener 174 ): void; 175 176 /** 177 * Dispatches the event defined for the worker. 178 * @param event Event to dispatch. 179 * @devices phone, tablet, wearable 180 * @since 7 181 */ 182 dispatchEvent(event: Event): boolean; 183 184 /** 185 * Removes an event defined for the worker. 186 * @param type Type of the event for which the event listener is removed. 187 * @param callback Callback of the event listener to remove. 188 * @devices phone, tablet, wearable 189 * @since 7 190 */ 191 removeEventListener( 192 type: string, 193 callback?: EventListener 194 ): void; 195 196 /** 197 * Removes all event listeners for the worker. 198 * @devices phone, tablet, wearable 199 * @since 7 200 */ 201 removeAllListener(): void; 202} 203 204/** 205 * Specifies the worker thread running environment, which is isolated from the host thread environment. 206 * @devices phone, tablet, wearable 207 * @since 7 208 */ 209declare interface WorkerGlobalScope extends EventTarget { 210 /** 211 * Worker name specified when there is a new worker. 212 * @devices phone, tablet, wearable 213 * @since 7 214 */ 215 readonly name: string; 216 217 /** 218 * The onerror attribute of parentPort specifies 219 * the event handler to be called when an exception occurs during worker execution. 220 * The event handler is executed in the worker thread. 221 * @param ev Error data. 222 * @devices phone, tablet, wearable 223 * @since 7 224 */ 225 onerror?: (ev: ErrorEvent) => void; 226 readonly self: WorkerGlobalScope & typeof globalThis; 227} 228 229/** 230 * Specifies the worker thread running environment, which is isolated from the host thread environment 231 * @devices phone, tablet, wearable 232 * @since 7 233 */ 234declare interface DedicatedWorkerGlobalScope extends WorkerGlobalScope { 235 /** 236 * The onmessage attribute of parentPort specifies the event handler 237 * to be called then the worker thread receives a message sent by 238 * the host thread through worker postMessage. 239 * The event handler is executed in the worker thread. 240 * @param ev Message received. 241 * @devices phone, tablet, wearable 242 * @since 7 243 */ 244 onmessage?: (this: DedicatedWorkerGlobalScope, ev: MessageEvent) => void; 245 246 /** 247 * The onmessage attribute of parentPort specifies the event handler 248 * to be called then the worker receives a message that cannot be deserialized. 249 * The event handler is executed in the worker thread. 250 * @param ev Error data. 251 * @devices phone, tablet, wearable 252 * @since 7 253 */ 254 onmessageerror?: (this: DedicatedWorkerGlobalScope, ev: MessageEvent) => void; 255 256 /** 257 * Close the worker thread to stop the worker from receiving messages 258 * @devices phone, tablet, wearable 259 * @since 7 260 */ 261 close(): void; 262 263 /** 264 * Send a message to be host thread from the worker 265 * @param messageObject Data to be sent to the worker 266 * @param transfer array cannot contain null. 267 * @devices phone, tablet, wearable 268 * @since 7 269 */ 270 postMessage(messageObject: Object, transfer: Transferable[]): void; 271 postMessage(messageObject: Object, options?: PostMessageOptions): void; 272} 273 274/** 275 * JS cross-thread communication tool 276 * @devices phone, tablet, wearable 277 * @since 7 278 */ 279declare namespace worker { 280 class Worker extends EventTarget { 281 /** 282 * Creates a worker instance 283 * @param scriptURL URL of the script to be executed by the worker 284 * @param options Options that can be set for the worker 285 * @devices phone, tablet, wearable 286 * @since 7 287 */ 288 constructor(scriptURL: string, options?: WorkerOptions); 289 290 /** 291 * The onexit attribute of the worker specifies the event handler to be called 292 * when the worker exits. The handler is executed in the host thread. 293 * @param code Code indicating the worker exit state 294 * @devices phone, tablet, wearable 295 * @since 7 296 */ 297 onexit?: (code: number) => void; 298 299 /** 300 * The onerror attribute of the worker specifies the event handler to be called 301 * when an exception occurs during worker execution. 302 * The event handler is executed in the host thread. 303 * @devices phone, tablet, wearable 304 * @since 7 305 */ 306 onerror?: (err: ErrorEvent) => void; 307 308 /** 309 * The onmessage attribute of the worker specifies the event handler 310 * to be called then the host thread receives a message created by itself 311 * and sent by the worker through the parentPort.postMessage. 312 * The event handler is executed in the host thread. 313 * @param event Message received. 314 * @devices phone, tablet, wearable 315 * @since 7 316 */ 317 onmessage?: (event: MessageEvent) => void; 318 319 /** 320 * The onmessage attribute of the worker specifies the event handler 321 * when the worker receives a message that cannot be serialized. 322 * The event handler is executed in the host thread. 323 * @devices phone, tablet, wearable 324 * @since 7 325 */ 326 onmessageerror?: (event: MessageEvent) => void; 327 328 /** 329 * Sends a message to the worker thread. 330 * The data is transferred using the structured clone algorithm. 331 * @param message Data to be sent to the worker 332 * @param transfer ArrayBuffer instance that can be transferred. 333 * The transferList array cannot contain null. 334 * @devices phone, tablet, wearable 335 * @since 7 336 */ 337 postMessage(message: Object, transfer: ArrayBuffer[]): void; 338 postMessage(message: Object, options?: PostMessageOptions): void; 339 340 /** 341 * Adds an event listener to the worker. 342 * @param type Adds an event listener to the worker. 343 * @param listener Callback to invoke when an event of the specified type occurs. 344 * @devices phone, tablet, wearable 345 * @since 7 346 */ 347 on(type: string, listener: EventListener): void; 348 349 /** 350 * Adds an event listener to the worker 351 * and removes the event listener automically after it is invoked once. 352 * @param type Type of the event to listen for 353 * @param listener Callback to invoke when an event of the specified type occurs 354 * @devices phone, tablet, wearable 355 * @since 7 356 */ 357 once(type: string, listener: EventListener): void; 358 359 /** 360 * Removes an event listener to the worker. 361 * @param type Type of the event for which the event listener is removed. 362 * @param listener Callback of the event listener to remove. 363 * @devices phone, tablet, wearable 364 * @since 7 365 */ 366 off(type: string, listener?: EventListener): void; 367 368 /** 369 * Terminates the worker thread to stop the worker from receiving messages 370 * @devices phone, tablet, wearable 371 * @since 7 372 */ 373 terminate(): void; 374 } 375 const parentPort: DedicatedWorkerGlobalScope; 376} 377 378export default worker;