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* @since 7 19* @syscap SystemCapability.Utils.Lang 20*/ 21export interface WorkerOptions { 22 /** 23 * Mode in which the worker executes the script. 24 * @since 7 25 * @syscap SystemCapability.Utils.Lang 26 */ 27 type?: "classic" | "module"; 28 29 /** 30 * Name of the worker. 31 * @since 7 32 * @syscap SystemCapability.Utils.Lang 33 */ 34 name?: string; 35 36 /** 37 * Whether the worker is shared. 38 * @since 7 39 * @syscap SystemCapability.Utils.Lang 40 */ 41 shared?: boolean; 42} 43 44/** 45 * Defines the event. 46 * @since 7 47 * @syscap SystemCapability.Utils.Lang 48 */ 49export interface Event { 50 /** 51 * Type of the Event. 52 * @since 7 53 * @syscap SystemCapability.Utils.Lang 54 */ 55 readonly type: string; 56 57 /** 58 * Timestamp(accurate to millisecond) when the event is created. 59 * @since 7 60 * @syscap SystemCapability.Utils.Lang 61 */ 62 readonly timeStamp: number; 63} 64 65/** 66 * Provides detailed information about the exception occurred during worker execution. 67 * @since 7 68 * @syscap SystemCapability.Utils.Lang 69 */ 70export interface ErrorEvent extends Event { 71 /** 72 * Information about the exception. 73 * @since 7 74 * @syscap SystemCapability.Utils.Lang 75 */ 76 readonly message: string; 77 78 /** 79 * File where the exception is located. 80 * @since 7 81 * @syscap SystemCapability.Utils.Lang 82 */ 83 readonly filename: string; 84 85 /** 86 * Number of the line where the exception is located. 87 * @since 7 88 * @syscap SystemCapability.Utils.Lang 89 */ 90 readonly lineno: number; 91 92 /** 93 * Number of the column where the exception is located. 94 * @since 7 95 * @syscap SystemCapability.Utils.Lang 96 */ 97 readonly colno: number; 98 99 /** 100 * Type of the exception. 101 * @since 7 102 * @syscap SystemCapability.Utils.Lang 103 */ 104 readonly error: Object; 105} 106 107/** 108 * Holds the data transferred between worker threads. 109 * @since 7 110 * @syscap SystemCapability.Utils.Lang 111 */ 112export interface MessageEvent<T> extends Event { 113 /** 114 * Data transferred when an exception occurs. 115 * @since 7 116 * @syscap SystemCapability.Utils.Lang 117 */ 118 readonly data: T; 119} 120 121/** 122 * Saves the data transferred between worker thread and host thread. 123 * @since 9 124 * @syscap SystemCapability.Utils.Lang 125 */ 126 export interface MessageEvents extends Event { 127 /** 128 * Data transferred when an exception occurs. 129 * @since 9 130 * @syscap SystemCapability.Utils.Lang 131 */ 132 readonly data; 133} 134 135/** 136 * Specifies the object whose ownership need to be transferred during data transfer. 137 * The object must be ArrayBuffer. 138 * @since 7 139 * @syscap SystemCapability.Utils.Lang 140 */ 141export interface PostMessageOptions { 142 /** 143 * ArrayBuffer array used to transfer the ownership. 144 * @since 7 145 * @syscap SystemCapability.Utils.Lang 146 */ 147 transfer?: Object[]; 148} 149 150/** 151 * Implements event listening. 152 * @since 7 153 * @deprecated since 9 154 * @useinstead ohos.worker.WorkerEventListener 155 * @syscap SystemCapability.Utils.Lang 156 */ 157export interface EventListener { 158 /** 159 * Specifies the callback to invoke. 160 * @param evt Event class for the callback to invoke. 161 * @since 7 162 * @deprecated since 9 163 * @useinstead ohos.worker.WorkerEventListener.(event: Event): void | Promise<void> 164 * @syscap SystemCapability.Utils.Lang 165 */ 166 (evt: Event): void | Promise<void>; 167} 168 169/** 170 * Implements event listening. 171 * @since 9 172 * @syscap SystemCapability.Utils.Lang 173 */ 174 export interface WorkerEventListener { 175 /** 176 * Specifies the callback function to be invoked. 177 * @param event Event class for the callback to invoke. 178 * @throws {BusinessError} 401 - if the input parameters are invalid. 179 * @throws {BusinessError} 10200004 - Worker instance is not running. 180 * @throws {BusinessError} 10200005 - The invoked API is not supported in workers. 181 * @since 9 182 * @syscap SystemCapability.Utils.Lang 183 */ 184 (event: Event): void | Promise<void>; 185} 186 187/** 188 * Type of message, only "message" and "messageerror". 189 * @since 7 190 * @syscap SystemCapability.Utils.Lang 191 */ 192type MessageType = "message" | "messageerror"; 193 194/** 195 * Specific event features. 196 * @since 7 197 * @deprecated since 9 198 * @useinstead ohos.worker.WorkerEventTarget 199 * @syscap SystemCapability.Utils.Lang 200 */ 201export interface EventTarget { 202 /** 203 * Adds an event listener to the worker. 204 * @param type Type of the event to listen for. 205 * @param listener Callback to invoke when an event of the specified type occurs. 206 * @since 7 207 * @deprecated since 9 208 * @useinstead ohos.worker.WorkerEventTarget.addEventListener 209 * @syscap SystemCapability.Utils.Lang 210 */ 211 addEventListener( 212 type: string, 213 listener: EventListener 214 ): void; 215 216 /** 217 * Dispatches the event defined for the worker. 218 * @param event Event to dispatch. 219 * @since 7 220 * @deprecated since 9 221 * @useinstead ohos.worker.WorkerEventTarget.dispatchEvent 222 * @syscap SystemCapability.Utils.Lang 223 */ 224 dispatchEvent(event: Event): boolean; 225 226 /** 227 * Removes an event defined for the worker. 228 * @param type Type of the event for which the event listener is removed. 229 * @param callback Callback of the event listener to remove. 230 * @since 7 231 * @deprecated since 9 232 * @useinstead ohos.worker.WorkerEventTarget.removeEventListener 233 * @syscap SystemCapability.Utils.Lang 234 */ 235 removeEventListener( 236 type: string, 237 callback?: EventListener 238 ): void; 239 240 /** 241 * Removes all event listeners for the worker. 242 * @since 7 243 * @deprecated since 9 244 * @useinstead ohos.worker.WorkerEventTarget.removeAllListener 245 * @syscap SystemCapability.Utils.Lang 246 */ 247 removeAllListener(): void; 248} 249 250/** 251 * Specific worker event features. 252 * @since 9 253 * @syscap SystemCapability.Utils.Lang 254 */ 255export interface WorkerEventTarget { 256 /** 257 * Adds an event listener to the worker. 258 * @param type Type of the event to listen for. 259 * @param listener Callback to invoke when an event of the specified type occurs. 260 * @throws {BusinessError} 401 - if the input parameters are invalid. 261 * @throws {BusinessError} 10200004 - Worker instance is not running. 262 * @throws {BusinessError} 10200005 - The invoked API is not supported in workers. 263 * @since 9 264 * @syscap SystemCapability.Utils.Lang 265 */ 266 addEventListener(type: string, listener: WorkerEventListener): void; 267 /** 268 * Handle the event defined for the worker. 269 * @param event Event to dispatch. 270 * @throws {BusinessError} 401 - if the input parameters are invalid. 271 * @throws {BusinessError} 10200004 - Worker instance is not running. 272 * @since 9 273 * @syscap SystemCapability.Utils.Lang 274 */ 275 dispatchEvent(event: Event): boolean; 276 /** 277 * Remove an event defined for the worker. 278 * @param type Type of the event for which the event listener is cancelled. 279 * @param callback Callback of the event listener to remove. 280 * @throws {BusinessError} 401 - if the input parameters are invalid. 281 * @throws {BusinessError} 10200004 - Worker instance is not running. 282 * @since 9 283 * @syscap SystemCapability.Utils.Lang 284 */ 285 removeEventListener(type: string, callback?: WorkerEventListener): void; 286 /** 287 * Remove all event listeners for the worker. 288 * @throws {BusinessError} 10200004 - Worker instance is not running. 289 * @since 9 290 * @syscap SystemCapability.Utils.Lang 291 */ 292 removeAllListener(): void; 293} 294 295/** 296 * Specifies the worker thread running environment, which is isolated from the host thread environment. 297 * @since 7 298 * @deprecated since 9 299 * @useinstead ohos.worker.GlobalScope 300 * @syscap SystemCapability.Utils.Lang 301 */ 302declare interface WorkerGlobalScope extends EventTarget { 303 /** 304 * Worker name specified when there is a new worker. 305 * @since 7 306 * @deprecated since 9 307 * @useinstead ohos.worker.GlobalScope.name 308 * @syscap SystemCapability.Utils.Lang 309 */ 310 readonly name: string; 311 312 /** 313 * The onerror attribute of parentPort specifies 314 * the event handler to be called when an exception occurs during worker execution. 315 * The event handler is executed in the worker thread. 316 * @param ev Error data. 317 * @since 7 318 * @deprecated since 9 319 * @useinstead ohos.worker.GlobalScope.onerror 320 * @syscap SystemCapability.Utils.Lang 321 */ 322 onerror?: (ev: ErrorEvent) => void; 323 readonly self: WorkerGlobalScope & typeof globalThis; 324} 325 326/** 327 * The environment Specified in which worker threads run, which is isolated from the host thread environment. 328 * @since 9 329 * @syscap SystemCapability.Utils.Lang 330 */ 331 declare interface GlobalScope extends WorkerEventTarget { 332 /** 333 * Name of Worker specified when there is a new worker. 334 * @since 9 335 * @syscap SystemCapability.Utils.Lang 336 */ 337 readonly name: string; 338 339 /** 340 * The onerror attribute of parentPort specified. 341 * the event handler to be called when an exception occurs during worker execution. 342 * The event handler is executed in the worker thread. 343 * @param ev Error data. 344 * @since 9 345 * @syscap SystemCapability.Utils.Lang 346 */ 347 onerror?: (ev: ErrorEvent) => void; 348 /** 349 * Specify the type attribute for self. 350 * @param self type is read-only. 351 * @since 9 352 * @syscap SystemCapability.Utils.Lang 353 */ 354 readonly self: GlobalScope & typeof globalThis; 355} 356 357/** 358 * Specifies the worker thread running environment, which is isolated from the host thread environment 359 * @since 7 360 * @deprecated since 9 361 * @useinstead ohos.worker.ThreadWorkerGlobalScope 362 * @syscap SystemCapability.Utils.Lang 363 */ 364export interface DedicatedWorkerGlobalScope extends WorkerGlobalScope { 365 /** 366 * The onmessage attribute of parentPort specifies the event handler 367 * to be called then the worker thread receives a message sent by 368 * the host thread through worker postMessage. 369 * The event handler is executed in the worker thread. 370 * @param ev Message received. 371 * @since 7 372 * @deprecated since 9 373 * @useinstead ohos.worker.ThreadWorkerGlobalScope.onmessage 374 * @syscap SystemCapability.Utils.Lang 375 */ 376 onmessage?: (this: DedicatedWorkerGlobalScope, ev: MessageEvent) => void; 377 378 /** 379 * The onmessage attribute of parentPort specifies the event handler 380 * to be called then the worker receives a message that cannot be deserialized. 381 * The event handler is executed in the worker thread. 382 * @param ev Error data. 383 * @since 7 384 * @deprecated since 9 385 * @useinstead ohos.worker.ThreadWorkerGlobalScope.onmessageerror 386 * @syscap SystemCapability.Utils.Lang 387 */ 388 onmessageerror?: (this: DedicatedWorkerGlobalScope, ev: MessageEvent) => void; 389 390 /** 391 * Close the worker thread to stop the worker from receiving messages 392 * @since 7 393 * @deprecated since 9 394 * @useinstead ohos.worker.ThreadWorkerGlobalScope.close 395 * @syscap SystemCapability.Utils.Lang 396 */ 397 close(): void; 398 399 /** 400 * Send a message to be host thread from the worker 401 * @param messageObject Data to be sent to the worker 402 * @param transfer array cannot contain null. 403 * @since 7 404 * @deprecated since 9 405 * @useinstead ohos.worker.ThreadWorkerGlobalScope.postMessage 406 * @syscap SystemCapability.Utils.Lang 407 */ 408 postMessage(messageObject: Object, transfer: Transferable[]): void; 409 postMessage(messageObject: Object, options?: PostMessageOptions): void; 410 411 /** 412 * Send a message to host thread from the worker 413 * @param messageObject Data to be sent to the worker 414 * @param transfer array cannot contain null. 415 * @since 9 416 * @syscap SystemCapability.Utils.Lang 417 */ 418 postMessage(messageObject: Object, transfer: ArrayBuffer[]): void; 419} 420 421/** 422 * Specifies the thread-worker running environment, which is isolated from the host-thread environment 423 * @since 9 424 * @syscap SystemCapability.Utils.Lang 425 */ 426export interface ThreadWorkerGlobalScope extends GlobalScope { 427 /** 428 * The onmessage attribute of parentPort specifies the event handler 429 * to be called then the worker thread receives a message sent by 430 * the host thread through worker postMessage. 431 * The event handler is executed in the worker thread. 432 * @param ev Message received. 433 * @throws {BusinessError} 401 - if the input parameters are invalid. 434 * @throws {BusinessError} 10200004 - Worker instance is not running. 435 * @throws {BusinessError} 10200005 - The invoked API is not supported in workers. 436 * @since 9 437 * @syscap SystemCapability.Utils.Lang 438 */ 439 onmessage?: (this: ThreadWorkerGlobalScope, ev: MessageEvents) => void; 440 441 /** 442 * The onmessage attribute of parentPort specifies the event handler 443 * to be called then the worker receives a message that cannot be deserialized. 444 * The event handler is executed in the worker thread. 445 * @param ev Error data. 446 * @throws {BusinessError} 401 - if the input parameters are invalid. 447 * @throws {BusinessError} 10200004 - Worker instance is not running. 448 * @throws {BusinessError} 10200005 - The invoked API is not supported in workers. 449 * @since 9 450 * @syscap SystemCapability.Utils.Lang 451 */ 452 onmessageerror?: (this: ThreadWorkerGlobalScope, ev: MessageEvents) => void; 453 454 /** 455 * Close the worker thread to stop the worker from receiving messages 456 * @throws {BusinessError} 10200004 - Worker instance is not running. 457 * @since 9 458 * @syscap SystemCapability.Utils.Lang 459 */ 460 close(): void; 461 462 /** 463 * Send a message to host thread from the worker 464 * @param messageObject Data to be sent to the worker 465 * @param transfer array cannot contain null. 466 * @throws {BusinessError} 401 - if the input parameters are invalid. 467 * @throws {BusinessError} 10200004 - Worker instance is not running. 468 * @throws {BusinessError} 10200006 - Serializing an uncaught exception failed. 469 * @since 9 470 * @syscap SystemCapability.Utils.Lang 471 */ 472 postMessage(messageObject: Object, transfer: ArrayBuffer[]): void; 473 474 /** 475 * Send a message to be host thread from the worker 476 * @param messageObject Data to be sent to the worker 477 * @param options Option can be set for postmessage. 478 * @throws {BusinessError} 401 - if the input parameters are invalid. 479 * @throws {BusinessError} 10200004 - Worker instance is not running. 480 * @throws {BusinessError} 10200006 - Serializing an uncaught exception failed. 481 * @since 9 482 * @syscap SystemCapability.Utils.Lang 483 */ 484 postMessage(messageObject: Object, options?: PostMessageOptions): void; 485} 486 487/** 488 * JS cross-thread communication tool 489 * @since 7 490 * @syscap SystemCapability.Utils.Lang 491 */ 492declare namespace worker { 493 /** 494 * The ThreadWorker class contains all Worker functions. 495 * @since 9 496 * @syscap SystemCapability.Utils.Lang 497 */ 498 class ThreadWorker implements WorkerEventTarget { 499 /** 500 * Creates a worker instance 501 * @param scriptURL URL of the script to be executed by the worker 502 * @param options Options that can be set for the worker 503 * @throws {BusinessError} 401 - if the input parameters are invalid. 504 * @throws {BusinessError} 10200003 - Worker initialization failure. 505 * @throws {BusinessError} 10200007 - The worker file patch is invalid path. 506 * @since 9 507 * @syscap SystemCapability.Utils.Lang 508 */ 509 constructor(scriptURL: string, options?: WorkerOptions); 510 /** 511 * The onexit attribute of the worker specifies the event handler to be called 512 * when the worker exits. The handler is executed in the host thread. 513 * @param code Code indicating the worker exit state 514 * @throws {BusinessError} 401 - if the input parameters are invalid. 515 * @throws {BusinessError} 10200004 - Worker instance is not running. 516 * @throws {BusinessError} 10200005 - The invoked API is not supported in workers. 517 * @since 9 518 * @syscap SystemCapability.Utils.Lang 519 */ 520 onexit?: (code: number) => void; 521 /** 522 * The onerror attribute of the worker specifies the event handler to be called 523 * when an exception occurs during worker execution. 524 * The event handler is executed in the host thread. 525 * @throws {BusinessError} 401 - if the input parameters are invalid. 526 * @throws {BusinessError} 10200004 - Worker instance is not running. 527 * @throws {BusinessError} 10200005 - The invoked API is not supported in workers. 528 * @since 9 529 * @syscap SystemCapability.Utils.Lang 530 */ 531 onerror?: (err: ErrorEvent) => void; 532 /** 533 * The onmessage attribute of the worker specifies the event handler 534 * to be called then the host thread receives a message created by itself 535 * and sent by the worker through the parentPort.postMessage. 536 * The event handler is executed in the host thread. 537 * @param event Message received. 538 * @throws {BusinessError} 401 - if the input parameters are invalid. 539 * @throws {BusinessError} 10200004 - Worker instance is not running. 540 * @throws {BusinessError} 10200005 - The invoked API is not supported in workers. 541 * @since 9 542 * @syscap SystemCapability.Utils.Lang 543 */ 544 onmessage?: (event: MessageEvents) => void; 545 /** 546 * The onmessage attribute of the worker specifies the event handler 547 * when the worker receives a message that cannot be serialized. 548 * The event handler is executed in the host thread. 549 * @throws {BusinessError} 401 - if the input parameters are invalid. 550 * @throws {BusinessError} 10200004 - Worker instance is not running. 551 * @throws {BusinessError} 10200005 - The invoked API is not supported in workers. 552 * @since 9 553 * @syscap SystemCapability.Utils.Lang 554 */ 555 onmessageerror?: (event: MessageEvents) => void; 556 /** 557 * Sends a message to the worker thread. 558 * The data is transferred using the structured clone algorithm. 559 * @param message Data to be sent to the worker 560 * @param transfer ArrayBuffer instance that can be transferred. 561 * The transferList array cannot contain null. 562 * @throws {BusinessError} 401 - if the input parameters are invalid. 563 * @throws {BusinessError} 10200004 - Worker instance is not running. 564 * @throws {BusinessError} 10200006 - Serializing an uncaught exception failed. 565 * @since 9 566 * @syscap SystemCapability.Utils.Lang 567 */ 568 postMessage(message: Object, transfer: ArrayBuffer[]): void; 569 /** 570 * Sends a message to the worker thread. 571 * The data is transferred using the structured clone algorithm. 572 * @param message Data to be sent to the worker 573 * @throws {BusinessError} 401 - if the input parameters are invalid. 574 * @throws {BusinessError} 10200004 - Worker instance is not running. 575 * @throws {BusinessError} 10200006 - Serializing an uncaught exception failed. 576 * @since 9 577 * @syscap SystemCapability.Utils.Lang 578 */ 579 postMessage(message: Object, options?: PostMessageOptions): void; 580 /** 581 * Adds an event listener to the worker. 582 * @param type Adds an event listener to the worker. 583 * @param listener Callback to invoke when an event of the specified type occurs. 584 * @throws {BusinessError} 401 - if the input parameters are invalid. 585 * @throws {BusinessError} 10200004 - Worker instance is not running. 586 * @throws {BusinessError} 10200005 - The invoked API is not supported in workers. 587 * @since 9 588 * @syscap SystemCapability.Utils.Lang 589 */ 590 on(type: string, listener: WorkerEventListener): void; 591 /** 592 * Adds an event listener to the worker 593 * and removes the event listener automatically after it is invoked once. 594 * @param type Type of the event to listen for 595 * @param listener Callback to invoke when an event of the specified type occurs 596 * @throws {BusinessError} 401 - if the input parameters are invalid. 597 * @throws {BusinessError} 10200004 - Worker instance is not running. 598 * @throws {BusinessError} 10200005 - The invoked API is not supported in workers. 599 * @since 9 600 * @syscap SystemCapability.Utils.Lang 601 */ 602 once(type: string, listener: WorkerEventListener): void; 603 /** 604 * Removes an event listener to the worker. 605 * @param type Type of the event for which the event listener is removed. 606 * @param listener Callback of the event listener to remove. 607 * @throws {BusinessError} 401 - if the input parameters are invalid. 608 * @throws {BusinessError} 10200004 - Worker instance is not running. 609 * @throws {BusinessError} 10200005 - The invoked API is not supported in workers. 610 * @since 9 611 * @syscap SystemCapability.Utils.Lang 612 */ 613 off(type: string, listener?: WorkerEventListener): void; 614 /** 615 * Terminates the worker thread to stop the worker from receiving messages 616 * @throws {BusinessError} 10200004 - Worker instance is not running. 617 * @since 9 618 * @syscap SystemCapability.Utils.Lang 619 */ 620 terminate(): void; 621 /** 622 * Adds an event listener to the worker. 623 * @param type Type of the event to listen for. 624 * @param listener Callback to invoke when an event of the specified type occurs. 625 * @throws {BusinessError} 401 - if the input parameters are invalid. 626 * @throws {BusinessError} 10200004 - Worker instance is not running. 627 * @throws {BusinessError} 10200005 - The invoked API is not supported in workers. 628 * @since 9 629 * @syscap SystemCapability.Utils.Lang 630 */ 631 addEventListener(type: string, listener: WorkerEventListener): void; 632 /** 633 * Handle the event defined for the worker. 634 * @param event Event to dispatch. 635 * @throws {BusinessError} 401 - if the input parameters are invalid. 636 * @throws {BusinessError} 10200004 - Worker instance is not running. 637 * @since 9 638 * @syscap SystemCapability.Utils.Lang 639 */ 640 dispatchEvent(event: Event): boolean; 641 /** 642 * Remove an event defined for the worker. 643 * @param type Type of the event for which the event listener is cancelled. 644 * @param callback Callback of the event listener to remove. 645 * @throws {BusinessError} 401 - if the input parameters are invalid. 646 * @throws {BusinessError} 10200004 - Worker instance is not running. 647 * @since 9 648 * @syscap SystemCapability.Utils.Lang 649 */ 650 removeEventListener(type: string, callback?: WorkerEventListener): void; 651 /** 652 * Remove all event listeners for the worker. 653 * @throws {BusinessError} 10200004 - Worker instance is not running. 654 * @since 9 655 * @syscap SystemCapability.Utils.Lang 656 */ 657 removeAllListener(): void; 658 } 659 660 /** 661 * The Worker class contains all Worker functions. 662 * @since 7 663 * @deprecated since 9 664 * @useinstead ohos.worker.ThreadWorker 665 * @syscap SystemCapability.Utils.Lang 666 */ 667 class Worker implements EventTarget { 668 /** 669 * Creates a worker instance 670 * @param scriptURL URL of the script to be executed by the worker 671 * @param options Options that can be set for the worker 672 * @since 7 673 * @deprecated since 9 674 * @useinstead ohos.worker.ThreadWorker.constructor 675 * @syscap SystemCapability.Utils.Lang 676 */ 677 constructor(scriptURL: string, options?: WorkerOptions); 678 679 /** 680 * The onexit attribute of the worker specifies the event handler to be called 681 * when the worker exits. The handler is executed in the host thread. 682 * @param code Code indicating the worker exit state 683 * @since 7 684 * @deprecated since 9 685 * @useinstead ohos.worker.ThreadWorker.onexit 686 * @syscap SystemCapability.Utils.Lang 687 */ 688 onexit?: (code: number) => void; 689 690 /** 691 * The onerror attribute of the worker specifies the event handler to be called 692 * when an exception occurs during worker execution. 693 * The event handler is executed in the host thread. 694 * @since 7 695 * @deprecated since 9 696 * @useinstead ohos.worker.ThreadWorker.onerror 697 * @syscap SystemCapability.Utils.Lang 698 */ 699 onerror?: (err: ErrorEvent) => void; 700 701 /** 702 * The onmessage attribute of the worker specifies the event handler 703 * to be called then the host thread receives a message created by itself 704 * and sent by the worker through the parentPort.postMessage. 705 * The event handler is executed in the host thread. 706 * @param event Message received. 707 * @since 7 708 * @deprecated since 9 709 * @useinstead ohos.worker.ThreadWorker.onmessage 710 * @syscap SystemCapability.Utils.Lang 711 */ 712 onmessage?: (event: MessageEvent) => void; 713 714 /** 715 * The onmessage attribute of the worker specifies the event handler 716 * when the worker receives a message that cannot be serialized. 717 * The event handler is executed in the host thread. 718 * @since 7 719 * @deprecated since 9 720 * @useinstead ohos.worker.ThreadWorker.onmessageerror 721 * @syscap SystemCapability.Utils.Lang 722 */ 723 onmessageerror?: (event: MessageEvent) => void; 724 725 /** 726 * Sends a message to the worker thread. 727 * The data is transferred using the structured clone algorithm. 728 * @param message Data to be sent to the worker 729 * @param transfer ArrayBuffer instance that can be transferred. 730 * The transferList array cannot contain null. 731 * @since 7 732 * @deprecated since 9 733 * @useinstead ohos.worker.ThreadWorker.postMessage 734 * @syscap SystemCapability.Utils.Lang 735 */ 736 postMessage(message: Object, transfer: ArrayBuffer[]): void; 737 postMessage(message: Object, options?: PostMessageOptions): void; 738 739 /** 740 * Adds an event listener to the worker. 741 * @param type Adds an event listener to the worker. 742 * @param listener Callback to invoke when an event of the specified type occurs. 743 * @since 7 744 * @deprecated since 9 745 * @useinstead ohos.worker.ThreadWorker.on 746 * @syscap SystemCapability.Utils.Lang 747 */ 748 on(type: string, listener: EventListener): void; 749 750 /** 751 * Adds an event listener to the worker 752 * and removes the event listener automatically after it is invoked once. 753 * @param type Type of the event to listen for 754 * @param listener Callback to invoke when an event of the specified type occurs 755 * @since 7 756 * @deprecated since 9 757 * @useinstead ohos.worker.ThreadWorker.once 758 * @syscap SystemCapability.Utils.Lang 759 */ 760 once(type: string, listener: EventListener): void; 761 762 /** 763 * Removes an event listener to the worker. 764 * @param type Type of the event for which the event listener is removed. 765 * @param listener Callback of the event listener to remove. 766 * @since 7 767 * @deprecated since 9 768 * @useinstead ohos.worker.ThreadWorker.off 769 * @syscap SystemCapability.Utils.Lang 770 */ 771 off(type: string, listener?: EventListener): void; 772 773 /** 774 * Terminates the worker thread to stop the worker from receiving messages 775 * @since 7 776 * @deprecated since 9 777 * @useinstead ohos.worker.ThreadWorker.terminate 778 * @syscap SystemCapability.Utils.Lang 779 */ 780 terminate(): void; 781 } 782 783 /** 784 * The object used by the worker thread to communicate with the host thread. 785 * @since 7 786 * @deprecated since 9 787 * @useinstead ohos.worker.workerPort 788 * @syscap SystemCapability.Utils.Lang 789 */ 790 const parentPort: DedicatedWorkerGlobalScope; 791 792 /** 793 * The object used by the worker thread to communicate with the host thread. 794 * @since 9 795 * @syscap SystemCapability.Utils.Lang 796 */ 797 const workerPort: ThreadWorkerGlobalScope; 798} 799export default worker;