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 * @file 18 * @kit ArkTS 19 */ 20 21/** 22 * @typedef WorkerOptions 23 * Provides options that can be set for the worker to create. 24 * @syscap SystemCapability.Utils.Lang 25 * @since 7 26 */ 27/** 28 * @typedef WorkerOptions 29 * Provides options that can be set for the worker to create. 30 * @syscap SystemCapability.Utils.Lang 31 * @crossplatform 32 * @since 10 33 */ 34/** 35 * @typedef WorkerOptions 36 * Provides options that can be set for the worker to create. 37 * @syscap SystemCapability.Utils.Lang 38 * @crossplatform 39 * @atomicservice 40 * @since 11 41 */ 42export interface WorkerOptions { 43 /** 44 * Mode in which the worker executes the script. 45 * 46 * @syscap SystemCapability.Utils.Lang 47 * @since 7 48 */ 49 /** 50 * Mode in which the worker executes the script. 51 * 52 * @syscap SystemCapability.Utils.Lang 53 * @crossplatform 54 * @since 10 55 */ 56 /** 57 * Mode in which the worker executes the script. 58 * 59 * @type { ?('classic' | 'module') } 60 * @syscap SystemCapability.Utils.Lang 61 * @crossplatform 62 * @atomicservice 63 * @since 11 64 */ 65 type?: 'classic' | 'module'; 66 67 /** 68 * Name of the worker. 69 * 70 * @syscap SystemCapability.Utils.Lang 71 * @since 7 72 */ 73 /** 74 * Name of the worker. 75 * 76 * @syscap SystemCapability.Utils.Lang 77 * @crossplatform 78 * @since 10 79 */ 80 /** 81 * Name of the worker. 82 * 83 * @type { ?string } 84 * @syscap SystemCapability.Utils.Lang 85 * @crossplatform 86 * @atomicservice 87 * @since 11 88 */ 89 name?: string; 90 91 /** 92 * Whether the worker is shared. 93 * 94 * @syscap SystemCapability.Utils.Lang 95 * @since 7 96 */ 97 /** 98 * Whether the worker is shared. 99 * 100 * @syscap SystemCapability.Utils.Lang 101 * @crossplatform 102 * @since 10 103 */ 104 /** 105 * Whether the worker is shared. 106 * 107 * @type { ?boolean } 108 * @syscap SystemCapability.Utils.Lang 109 * @crossplatform 110 * @atomicservice 111 * @since 12 112 */ 113 shared?: boolean; 114} 115 116/** 117 * @typedef Event 118 * Defines the event. 119 * @syscap SystemCapability.Utils.Lang 120 * @since 7 121 */ 122/** 123 * @typedef Event 124 * Defines the event. 125 * @syscap SystemCapability.Utils.Lang 126 * @crossplatform 127 * @since 10 128 */ 129/** 130 * @typedef Event 131 * Defines the event. 132 * @syscap SystemCapability.Utils.Lang 133 * @crossplatform 134 * @atomicservice 135 * @since 11 136 */ 137export interface Event { 138 /** 139 * Type of the Event. 140 * 141 * @syscap SystemCapability.Utils.Lang 142 * @since 7 143 */ 144 /** 145 * Type of the Event. 146 * 147 * @syscap SystemCapability.Utils.Lang 148 * @crossplatform 149 * @since 10 150 */ 151 /** 152 * Type of the Event. 153 * 154 * @type { string } 155 * @readonly 156 * @syscap SystemCapability.Utils.Lang 157 * @crossplatform 158 * @atomicservice 159 * @since 12 160 */ 161 readonly type: string; 162 163 /** 164 * Timestamp(accurate to millisecond) when the event is created. 165 * 166 * @syscap SystemCapability.Utils.Lang 167 * @since 7 168 */ 169 /** 170 * Timestamp(accurate to millisecond) when the event is created. 171 * 172 * @syscap SystemCapability.Utils.Lang 173 * @crossplatform 174 * @since 10 175 */ 176 /** 177 * Timestamp(accurate to millisecond) when the event is created. 178 * 179 * @type { number } 180 * @readonly 181 * @syscap SystemCapability.Utils.Lang 182 * @crossplatform 183 * @atomicservice 184 * @since 12 185 */ 186 readonly timeStamp: number; 187} 188 189/** 190 * Provides detailed information about the exception occurred during worker execution. 191 * @typedef ErrorEvent 192 * @syscap SystemCapability.Utils.Lang 193 * @since 7 194 */ 195/** 196 * Provides detailed information about the exception occurred during worker execution. 197 * @typedef ErrorEvent 198 * @syscap SystemCapability.Utils.Lang 199 * @crossplatform 200 * @since 10 201 */ 202/** 203 * Provides detailed information about the exception occurred during worker execution. 204 * @typedef ErrorEvent 205 * @extends Event 206 * @syscap SystemCapability.Utils.Lang 207 * @crossplatform 208 * @atomicservice 209 * @since 11 210 */ 211export interface ErrorEvent extends Event { 212 /** 213 * Information about the exception. 214 * 215 * @syscap SystemCapability.Utils.Lang 216 * @since 7 217 */ 218 /** 219 * Information about the exception. 220 * 221 * @syscap SystemCapability.Utils.Lang 222 * @crossplatform 223 * @since 10 224 */ 225 /** 226 * Information about the exception. 227 * 228 * @type { string } 229 * @readonly 230 * @syscap SystemCapability.Utils.Lang 231 * @crossplatform 232 * @atomicservice 233 * @since 11 234 */ 235 readonly message: string; 236 237 /** 238 * File where the exception is located. 239 * 240 * @syscap SystemCapability.Utils.Lang 241 * @since 7 242 */ 243 /** 244 * File where the exception is located. 245 * 246 * @syscap SystemCapability.Utils.Lang 247 * @crossplatform 248 * @since 10 249 */ 250 /** 251 * File where the exception is located. 252 * 253 * @type { string } 254 * @readonly 255 * @syscap SystemCapability.Utils.Lang 256 * @crossplatform 257 * @atomicservice 258 * @since 11 259 */ 260 readonly filename: string; 261 262 /** 263 * Number of the line where the exception is located. 264 * 265 * @syscap SystemCapability.Utils.Lang 266 * @since 7 267 */ 268 /** 269 * Number of the line where the exception is located. 270 * 271 * @syscap SystemCapability.Utils.Lang 272 * @crossplatform 273 * @since 10 274 */ 275 /** 276 * Number of the line where the exception is located. 277 * 278 * @type { number } 279 * @readonly 280 * @syscap SystemCapability.Utils.Lang 281 * @crossplatform 282 * @atomicservice 283 * @since 11 284 */ 285 readonly lineno: number; 286 287 /** 288 * Number of the column where the exception is located. 289 * 290 * @syscap SystemCapability.Utils.Lang 291 * @since 7 292 */ 293 /** 294 * Number of the column where the exception is located. 295 * 296 * @syscap SystemCapability.Utils.Lang 297 * @crossplatform 298 * @since 10 299 */ 300 /** 301 * Number of the column where the exception is located. 302 * 303 * @type { number } 304 * @readonly 305 * @syscap SystemCapability.Utils.Lang 306 * @crossplatform 307 * @atomicservice 308 * @since 11 309 */ 310 readonly colno: number; 311 312 /** 313 * Type of the exception. 314 * 315 * @syscap SystemCapability.Utils.Lang 316 * @since 7 317 */ 318 /** 319 * Type of the exception. 320 * 321 * @syscap SystemCapability.Utils.Lang 322 * @crossplatform 323 * @since 10 324 */ 325 /** 326 * Type of the exception. 327 * 328 * @type { Object } 329 * @readonly 330 * @syscap SystemCapability.Utils.Lang 331 * @crossplatform 332 * @atomicservice 333 * @since 11 334 */ 335 readonly error: Object; 336} 337 338/** 339 * Holds the data transferred between worker threads. 340 * @typedef MessageEvent<T> 341 * @syscap SystemCapability.Utils.Lang 342 * @since 7 343 */ 344/** 345 * Holds the data transferred between worker threads. 346 * @typedef MessageEvent<T> 347 * @syscap SystemCapability.Utils.Lang 348 * @crossplatform 349 * @since 10 350 */ 351/** 352 * Holds the data transferred between worker threads. 353 * @typedef MessageEvent<T> 354 * @extends Event 355 * @syscap SystemCapability.Utils.Lang 356 * @crossplatform 357 * @atomicservice 358 * @since 12 359 */ 360export interface MessageEvent<T> extends Event { 361 /** 362 * Data transferred when an exception occurs. 363 * 364 * @syscap SystemCapability.Utils.Lang 365 * @since 7 366 */ 367 /** 368 * Data transferred when an exception occurs. 369 * 370 * @syscap SystemCapability.Utils.Lang 371 * @crossplatform 372 * @since 10 373 */ 374 /** 375 * Data transferred when an exception occurs. 376 * 377 * @type { T } 378 * @readonly 379 * @syscap SystemCapability.Utils.Lang 380 * @crossplatform 381 * @atomicservice 382 * @since 12 383 */ 384 readonly data: T; 385} 386 387/** 388 * Saves the data transferred between worker thread and host thread. 389 * @typedef MessageEvents 390 * @syscap SystemCapability.Utils.Lang 391 * @since 9 392 */ 393/** 394 * Saves the data transferred between worker thread and host thread. 395 * @typedef MessageEvents 396 * @syscap SystemCapability.Utils.Lang 397 * @crossplatform 398 * @since 10 399 */ 400/** 401 * Saves the data transferred between worker thread and host thread. 402 * @typedef MessageEvents 403 * @extends Event 404 * @syscap SystemCapability.Utils.Lang 405 * @crossplatform 406 * @atomicservice 407 * @since 11 408 */ 409export interface MessageEvents extends Event { 410 /** 411 * Data transferred when an exception occurs. 412 * 413 * @type { any } 414 * @syscap SystemCapability.Utils.Lang 415 * @since 9 416 */ 417 /** 418 * Data transferred when an exception occurs. 419 * 420 * @type { any } 421 * @syscap SystemCapability.Utils.Lang 422 * @crossplatform 423 * @since 10 424 */ 425 /** 426 * Data transferred when an exception occurs. 427 * 428 * @type { any } 429 * @readonly 430 * @syscap SystemCapability.Utils.Lang 431 * @crossplatform 432 * @atomicservice 433 * @since 11 434 */ 435 readonly data: any; 436} 437 438/** 439 * @typedef PostMessageOptions 440 * Specifies the object whose ownership need to be transferred during data transfer. 441 * The object must be ArrayBuffer. 442 * @syscap SystemCapability.Utils.Lang 443 * @since 7 444 */ 445/** 446 * @typedef PostMessageOptions 447 * Specifies the object whose ownership need to be transferred during data transfer. 448 * The object must be ArrayBuffer. 449 * @syscap SystemCapability.Utils.Lang 450 * @crossplatform 451 * @since 10 452 */ 453/** 454 * @typedef PostMessageOptions 455 * Specifies the object whose ownership need to be transferred during data transfer. 456 * The object must be ArrayBuffer. 457 * @syscap SystemCapability.Utils.Lang 458 * @crossplatform 459 * @atomicservice 460 * @since 11 461 */ 462export interface PostMessageOptions { 463 /** 464 * ArrayBuffer array used to transfer the ownership. 465 * 466 * @syscap SystemCapability.Utils.Lang 467 * @since 7 468 */ 469 /** 470 * ArrayBuffer array used to transfer the ownership. 471 * 472 * @syscap SystemCapability.Utils.Lang 473 * @crossplatform 474 * @since 10 475 */ 476 /** 477 * ArrayBuffer array used to transfer the ownership. 478 * 479 * @type { ?Object[] } 480 * @syscap SystemCapability.Utils.Lang 481 * @crossplatform 482 * @atomicservice 483 * @since 11 484 */ 485 transfer?: Object[]; 486} 487 488/** 489 * @typedef EventListener 490 * Implements event listening. 491 * @syscap SystemCapability.Utils.Lang 492 * @since 7 493 * @deprecated since 9 494 * @useinstead ohos.worker.WorkerEventListener 495 */ 496export interface EventListener { 497 /** 498 * Specifies the callback to invoke. 499 * 500 * @param { Event } evt - evt evt Event class for the callback to invoke. 501 * @returns { void | Promise<void> } 502 * @syscap SystemCapability.Utils.Lang 503 * @since 7 504 * @deprecated since 9 505 * @useinstead ohos.worker.WorkerEventListener.(event: Event) 506 */ 507 (evt: Event): void | Promise<void>; 508} 509 510/** 511 * @typedef WorkerEventListener 512 * Implements event listening. 513 * @syscap SystemCapability.Utils.Lang 514 * @since 9 515 */ 516/** 517 * @typedef WorkerEventListener 518 * Implements event listening. 519 * @syscap SystemCapability.Utils.Lang 520 * @crossplatform 521 * @since 10 522 */ 523/** 524 * @typedef WorkerEventListener 525 * Implements event listening. 526 * @syscap SystemCapability.Utils.Lang 527 * @crossplatform 528 * @atomicservice 529 * @since 12 530 */ 531export interface WorkerEventListener { 532 /** 533 * Specifies the callback function to be invoked. 534 * 535 * @param { Event } event - event Event class for the callback to invoke. 536 * @returns { void | Promise<void> } 537 * @throws { BusinessError } 401 - Parameter error. Possible causes: 538 * 1.Mandatory parameters are left unspecified; 539 * 2.Incorrect parameter types; 540 * 3.Parameter verification failed. 541 * @throws { BusinessError } 10200004 - The Worker instance is not running. 542 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 543 * @syscap SystemCapability.Utils.Lang 544 * @since 9 545 */ 546 /** 547 * Specifies the callback function to be invoked. 548 * 549 * @param { Event } event - event Event class for the callback to invoke. 550 * @returns { void | Promise<void> } 551 * @throws { BusinessError } 401 - Parameter error. Possible causes: 552 * 1.Mandatory parameters are left unspecified; 553 * 2.Incorrect parameter types; 554 * 3.Parameter verification failed. 555 * @throws { BusinessError } 10200004 - The Worker instance is not running. 556 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 557 * @syscap SystemCapability.Utils.Lang 558 * @crossplatform 559 * @since 10 560 */ 561 /** 562 * Specifies the callback function to be invoked. 563 * 564 * @param { Event } event - event Event class for the callback to invoke. 565 * @returns { void | Promise<void> } 566 * @throws { BusinessError } 401 - Parameter error. Possible causes: 567 * 1.Mandatory parameters are left unspecified; 568 * 2.Incorrect parameter types; 569 * 3.Parameter verification failed. 570 * @throws { BusinessError } 10200004 - Worker instance is not running. 571 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 572 * @syscap SystemCapability.Utils.Lang 573 * @crossplatform 574 * @atomicservice 575 * @since 12 576 */ 577 (event: Event): void | Promise<void>; 578} 579 580/** 581 * Type of message, only "message" and "messageerror". 582 * 583 * @syscap SystemCapability.Utils.Lang 584 * @since 7 585 */ 586/** 587 * Type of message, only "message" and "messageerror". 588 * 589 * @syscap SystemCapability.Utils.Lang 590 * @crossplatform 591 * @since 10 592 */ 593/** 594 * Type of message, only "message" and "messageerror". 595 * 596 * @typedef { 'message' | 'messageerror' } 597 * @syscap SystemCapability.Utils.Lang 598 * @crossplatform 599 * @atomicservice 600 * @since 12 601 */ 602type MessageType = 'message' | 'messageerror'; 603 604/** 605 * @typedef EventTarget 606 * Specific event features. 607 * @syscap SystemCapability.Utils.Lang 608 * @since 7 609 * @deprecated since 9 610 * @useinstead ohos.worker.WorkerEventTarget 611 */ 612export interface EventTarget { 613 /** 614 * Adds an event listener to the worker. 615 * 616 * @param { string } type - type Type of the event to listen for. 617 * @param { EventListener } listener - listener Callback to invoke when an event of the specified type occurs. 618 * @syscap SystemCapability.Utils.Lang 619 * @since 7 620 * @deprecated since 9 621 * @useinstead ohos.worker.WorkerEventTarget.addEventListener 622 */ 623 addEventListener(type: string, listener: EventListener): void; 624 625 /** 626 * Dispatches the event defined for the worker. 627 * 628 * @param { Event } event - event Event to dispatch. 629 * @returns { boolean } 630 * @syscap SystemCapability.Utils.Lang 631 * @since 7 632 * @deprecated since 9 633 * @useinstead ohos.worker.WorkerEventTarget.dispatchEvent 634 */ 635 dispatchEvent(event: Event): boolean; 636 637 /** 638 * Removes an event defined for the worker. 639 * 640 * @param { string } type - type Type of the event for which the event listener is removed. 641 * @param { EventListener } callback - callback Callback of the event listener to remove. 642 * @syscap SystemCapability.Utils.Lang 643 * @since 7 644 * @deprecated since 9 645 * @useinstead ohos.worker.WorkerEventTarget.removeEventListener 646 */ 647 removeEventListener(type: string, callback?: EventListener): void; 648 649 /** 650 * Removes all event listeners for the worker. 651 * 652 * @syscap SystemCapability.Utils.Lang 653 * @since 7 654 * @deprecated since 9 655 * @useinstead ohos.worker.WorkerEventTarget.removeAllListener 656 */ 657 removeAllListener(): void; 658} 659 660/** 661 * @typedef WorkerEventTarget 662 * Specific worker event features. 663 * @syscap SystemCapability.Utils.Lang 664 * @since 9 665 */ 666/** 667 * @typedef WorkerEventTarget 668 * Specific worker event features. 669 * @syscap SystemCapability.Utils.Lang 670 * @crossplatform 671 * @since 10 672 */ 673/** 674 * @typedef WorkerEventTarget 675 * Specific worker event features. 676 * @syscap SystemCapability.Utils.Lang 677 * @crossplatform 678 * @atomicservice 679 * @since 11 680 */ 681export interface WorkerEventTarget { 682 /** 683 * Adds an event listener to the worker. 684 * 685 * @param { string } type - type Type of the event to listen for. 686 * @param { WorkerEventListener } listener - listener Callback to invoke when an event of the specified type occurs. 687 * @throws { BusinessError } 401 - Parameter error. Possible causes: 688 * 1.Mandatory parameters are left unspecified; 689 * 2.Incorrect parameter types; 690 * 3.Parameter verification failed. 691 * @throws { BusinessError } 10200004 - The Worker instance is not running. 692 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 693 * @syscap SystemCapability.Utils.Lang 694 * @since 9 695 */ 696 /** 697 * Adds an event listener to the worker. 698 * 699 * @param { string } type - type Type of the event to listen for. 700 * @param { WorkerEventListener } listener - listener Callback to invoke when an event of the specified type occurs. 701 * @throws { BusinessError } 401 - Parameter error. Possible causes: 702 * 1.Mandatory parameters are left unspecified; 703 * 2.Incorrect parameter types; 704 * 3.Parameter verification failed. 705 * @throws { BusinessError } 10200004 - The Worker instance is not running. 706 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 707 * @syscap SystemCapability.Utils.Lang 708 * @since 10 709 */ 710 /** 711 * Adds an event listener to the worker. 712 * 713 * @param { string } type - type Type of the event to listen for. 714 * @param { WorkerEventListener } listener - listener Callback to invoke when an event of the specified type occurs. 715 * @throws { BusinessError } 401 - Parameter error. Possible causes: 716 * 1.Mandatory parameters are left unspecified; 717 * 2.Incorrect parameter types; 718 * 3.Parameter verification failed. 719 * @throws { BusinessError } 10200004 - The Worker instance is not running. 720 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 721 * @syscap SystemCapability.Utils.Lang 722 * @crossplatform 723 * @since 11 724 */ 725 /** 726 * Adds an event listener to the worker. 727 * 728 * @param { string } type - type Type of the event to listen for. 729 * @param { WorkerEventListener } listener - listener Callback to invoke when an event of the specified type occurs. 730 * @throws { BusinessError } 401 - Parameter error. Possible causes: 731 * 1.Mandatory parameters are left unspecified; 732 * 2.Incorrect parameter types; 733 * 3.Parameter verification failed. 734 * @throws { BusinessError } 10200004 - The Worker instance is not running. 735 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 736 * @syscap SystemCapability.Utils.Lang 737 * @crossplatform 738 * @atomicservice 739 * @since 12 740 */ 741 addEventListener(type: string, listener: WorkerEventListener): void; 742 /** 743 * Handle the event defined for the worker. 744 * 745 * @param { Event } event - event Event to dispatch. 746 * @returns { boolean } 747 * @throws { BusinessError } 401 - Parameter error. Possible causes: 748 * 1.Mandatory parameters are left unspecified; 749 * 2.Incorrect parameter types; 750 * 3.Parameter verification failed. 751 * @throws { BusinessError } 10200004 - The Worker instance is not running. 752 * @syscap SystemCapability.Utils.Lang 753 * @since 9 754 */ 755 /** 756 * Handle the event defined for the worker. 757 * 758 * @param { Event } event - event Event to dispatch. 759 * @returns { boolean } 760 * @throws { BusinessError } 401 - Parameter error. Possible causes: 761 * 1.Mandatory parameters are left unspecified; 762 * 2.Incorrect parameter types; 763 * 3.Parameter verification failed. 764 * @throws { BusinessError } 10200004 - The Worker instance is not running. 765 * @syscap SystemCapability.Utils.Lang 766 * @crossplatform 767 * @since 10 768 */ 769 /** 770 * Handle the event defined for the worker. 771 * 772 * @param { Event } event - event Event to dispatch. 773 * @returns { boolean } 774 * @throws { BusinessError } 401 - Parameter error. Possible causes: 775 * 1.Mandatory parameters are left unspecified; 776 * 2.Incorrect parameter types; 777 * 3.Parameter verification failed. 778 * @throws { BusinessError } 10200004 - The Worker instance is not running. 779 * @syscap SystemCapability.Utils.Lang 780 * @crossplatform 781 * @atomicservice 782 * @since 12 783 */ 784 dispatchEvent(event: Event): boolean; 785 /** 786 * Remove an event defined for the worker. 787 * 788 * @param { string } type - type Type of the event for which the event listener is cancelled. 789 * @param { WorkerEventListener } [callback] - callback Callback of the event listener to remove. 790 * @throws { BusinessError } 401 - Parameter error. Possible causes: 791 * 1.Mandatory parameters are left unspecified; 792 * 2.Incorrect parameter types; 793 * 3.Parameter verification failed. 794 * @throws { BusinessError } 10200004 - The Worker instance is not running. 795 * @syscap SystemCapability.Utils.Lang 796 * @since 9 797 */ 798 /** 799 * Remove an event defined for the worker. 800 * 801 * @param { string } type - type Type of the event for which the event listener is cancelled. 802 * @param { WorkerEventListener } [callback] - callback Callback of the event listener to remove. 803 * @throws { BusinessError } 401 - Parameter error. Possible causes: 804 * 1.Mandatory parameters are left unspecified; 805 * 2.Incorrect parameter types; 806 * 3.Parameter verification failed. 807 * @throws { BusinessError } 10200004 - The Worker instance is not running. 808 * @syscap SystemCapability.Utils.Lang 809 * @crossplatform 810 * @since 10 811 */ 812 /** 813 * Remove an event defined for the worker. 814 * 815 * @param { string } type - type Type of the event for which the event listener is cancelled. 816 * @param { WorkerEventListener } [callback] - callback Callback of the event listener to remove. 817 * @throws { BusinessError } 401 - Parameter error. Possible causes: 818 * 1.Mandatory parameters are left unspecified; 819 * 2.Incorrect parameter types; 820 * 3.Parameter verification failed. 821 * @throws { BusinessError } 10200004 - The Worker instance is not running. 822 * @syscap SystemCapability.Utils.Lang 823 * @crossplatform 824 * @atomicservice 825 * @since 12 826 */ 827 removeEventListener(type: string, callback?: WorkerEventListener): void; 828 /** 829 * Remove all event listeners for the worker. 830 * 831 * @throws { BusinessError } 10200004 - The Worker instance is not running. 832 * @syscap SystemCapability.Utils.Lang 833 * @since 9 834 */ 835 /** 836 * Remove all event listeners for the worker. 837 * 838 * @throws { BusinessError } 10200004 - The Worker instance is not running. 839 * @syscap SystemCapability.Utils.Lang 840 * @crossplatform 841 * @since 10 842 */ 843 /** 844 * Remove all event listeners for the worker. 845 * 846 * @throws { BusinessError } 10200004 - The Worker instance is not running. 847 * @syscap SystemCapability.Utils.Lang 848 * @crossplatform 849 * @atomicservice 850 * @since 12 851 */ 852 removeAllListener(): void; 853} 854 855/** 856 * @typedef WorkerGlobalScope 857 * Specifies the worker thread running environment, which is isolated from the host thread environment. 858 * @syscap SystemCapability.Utils.Lang 859 * @since 7 860 * @deprecated since 9 861 * @useinstead ohos.worker.GlobalScope 862 */ 863declare interface WorkerGlobalScope extends EventTarget { 864 /** 865 * Worker name specified when there is a new worker. 866 * 867 * @syscap SystemCapability.Utils.Lang 868 * @since 7 869 * @deprecated since 9 870 * @useinstead ohos.worker.GlobalScope.name 871 */ 872 readonly name: string; 873 874 /** 875 * The onerror attribute of parentPort specifies 876 * the event handler to be called when an exception occurs during worker execution. 877 * The event handler is executed in the worker thread. 878 * 879 * @syscap SystemCapability.Utils.Lang 880 * @since 7 881 * @deprecated since 9 882 * @useinstead ohos.worker.GlobalScope.onerror 883 */ 884 onerror?: (ev: ErrorEvent) => void; 885 886 /** 887 * Specify the type attribute for self. 888 * 889 * @syscap SystemCapability.Utils.Lang 890 * @since 7 891 * @deprecated since 9 892 * @useinstead ohos.worker.GlobalScope.self 893 */ 894 readonly self: WorkerGlobalScope & typeof globalThis; 895} 896 897/** 898 * The environment Specified in which worker threads run, which is isolated from the host thread environment. 899 * @typedef GlobalScope 900 * @syscap SystemCapability.Utils.Lang 901 * @since 9 902 */ 903/** 904 * The environment Specified in which worker threads run, which is isolated from the host thread environment. 905 * @typedef GlobalScope 906 * @syscap SystemCapability.Utils.Lang 907 * @crossplatform 908 * @since 10 909 */ 910/** 911 * The environment Specified in which worker threads run, which is isolated from the host thread environment. 912 * @typedef GlobalScope 913 * @extends WorkerEventTarget 914 * @syscap SystemCapability.Utils.Lang 915 * @crossplatform 916 * @atomicservice 917 * @since 11 918 */ 919declare interface GlobalScope extends WorkerEventTarget { 920 /** 921 * Name of Worker specified when there is a new worker. 922 * 923 * @syscap SystemCapability.Utils.Lang 924 * @since 9 925 */ 926 /** 927 * Name of Worker specified when there is a new worker. 928 * 929 * @syscap SystemCapability.Utils.Lang 930 * @crossplatform 931 * @since 10 932 */ 933 /** 934 * Name of Worker specified when there is a new worker. 935 * 936 * @type { string } 937 * @readonly 938 * @syscap SystemCapability.Utils.Lang 939 * @crossplatform 940 * @atomicservice 941 * @since 11 942 */ 943 readonly name: string; 944 945 /** 946 * The onerror attribute of parentPort specified. 947 * the event handler to be called when an exception occurs during worker execution. 948 * The event handler is executed in the worker thread. 949 * 950 * @syscap SystemCapability.Utils.Lang 951 * @since 9 952 */ 953 /** 954 * The onerror attribute of parentPort specified. 955 * the event handler to be called when an exception occurs during worker execution. 956 * The event handler is executed in the worker thread. 957 * 958 * @syscap SystemCapability.Utils.Lang 959 * @crossplatform 960 * @since 10 961 */ 962 /** 963 * The onerror attribute of parentPort specified. 964 * the event handler to be called when an exception occurs during worker execution. 965 * The event handler is executed in the worker thread. 966 * 967 * @type { ?function } 968 * @syscap SystemCapability.Utils.Lang 969 * @crossplatform 970 * @atomicservice 971 * @since 11 972 */ 973 onerror?: (ev: ErrorEvent) => void; 974 /** 975 * Specify the type attribute for self. 976 * 977 * @syscap SystemCapability.Utils.Lang 978 * @since 9 979 */ 980 /** 981 * Specify the type attribute for self. 982 * 983 * @syscap SystemCapability.Utils.Lang 984 * @crossplatform 985 * @since 10 986 */ 987 /** 988 * Specify the type attribute for self. 989 * 990 * @type { GlobalScope & typeof globalThis } 991 * @readonly 992 * @syscap SystemCapability.Utils.Lang 993 * @crossplatform 994 * @atomicservice 995 * @since 11 996 */ 997 readonly self: GlobalScope & typeof globalThis; 998} 999 1000/** 1001 * @typedef DedicatedWorkerGlobalScope 1002 * Specifies the worker thread running environment, which is isolated from the host thread environment 1003 * @syscap SystemCapability.Utils.Lang 1004 * @since 7 1005 * @deprecated since 9 1006 * @useinstead ohos.worker.ThreadWorkerGlobalScope 1007 */ 1008export interface DedicatedWorkerGlobalScope extends WorkerGlobalScope { 1009 /** 1010 * The onmessage attribute of parentPort specifies the event handler 1011 * to be called then the worker thread receives a message sent by 1012 * the host thread through worker postMessage. 1013 * The event handler is executed in the worker thread. 1014 * 1015 * @syscap SystemCapability.Utils.Lang 1016 * @since 7 1017 * @deprecated since 9 1018 * @useinstead ohos.worker.ThreadWorkerGlobalScope.onmessage 1019 */ 1020 onmessage?: (this: DedicatedWorkerGlobalScope, ev: MessageEvent) => void; 1021 1022 /** 1023 * The onmessage attribute of parentPort specifies the event handler 1024 * to be called then the worker receives a message that cannot be deserialized. 1025 * The event handler is executed in the worker thread. 1026 * 1027 * @syscap SystemCapability.Utils.Lang 1028 * @since 7 1029 * @deprecated since 9 1030 * @useinstead ohos.worker.ThreadWorkerGlobalScope.onmessageerror 1031 */ 1032 onmessageerror?: (this: DedicatedWorkerGlobalScope, ev: MessageEvent) => void; 1033 1034 /** 1035 * Close the worker thread to stop the worker from receiving messages 1036 * 1037 * @syscap SystemCapability.Utils.Lang 1038 * @since 7 1039 * @deprecated since 9 1040 * @useinstead ohos.worker.ThreadWorkerGlobalScope.close 1041 */ 1042 close(): void; 1043 1044 /** 1045 * Send a message to be host thread from the worker 1046 * 1047 * @param { Object } messageObject - messageObject Data to be sent to the worker 1048 * @param { Transferable[] } transfer - transfer array cannot contain null. 1049 * @syscap SystemCapability.Utils.Lang 1050 * @since 7 1051 * @deprecated since 9 1052 * @useinstead ohos.worker.ThreadWorkerGlobalScope.postMessage 1053 */ 1054 postMessage(messageObject: Object, transfer: Transferable[]): void; 1055 1056 /** 1057 * Send a message to be host thread from the worker 1058 * 1059 * @param { Object } messageObject - messageObject Data to be sent to the worker 1060 * @param { PostMessageOptions } [options] - options Option can be set for postmessage. 1061 * @syscap SystemCapability.Utils.Lang 1062 * @since 7 1063 * @deprecated since 9 1064 * @useinstead ohos.worker.ThreadWorkerGlobalScope.postMessage 1065 */ 1066 postMessage(messageObject: Object, options?: PostMessageOptions): void; 1067 1068 /** 1069 * Send a message to host thread from the worker 1070 * 1071 * @param { Object } messageObject - messageObject Data to be sent to the worker 1072 * @param { ArrayBuffer[] } transfer - transfer array cannot contain null. 1073 * @syscap SystemCapability.Utils.Lang 1074 * @since 9 1075 * @deprecated since 9 1076 */ 1077 postMessage(messageObject: Object, transfer: ArrayBuffer[]): void; 1078} 1079 1080/** 1081 * Specifies the thread-worker running environment, which is isolated from the host-thread environment 1082 * @typedef ThreadWorkerGlobalScope 1083 * @syscap SystemCapability.Utils.Lang 1084 * @since 9 1085 */ 1086/** 1087 * Specifies the thread-worker running environment, which is isolated from the host-thread environment 1088 * @typedef ThreadWorkerGlobalScope 1089 * @syscap SystemCapability.Utils.Lang 1090 * @crossplatform 1091 * @since 10 1092 */ 1093/** 1094 * Specifies the thread-worker running environment, which is isolated from the host-thread environment 1095 * @typedef ThreadWorkerGlobalScope 1096 * @extends GlobalScope 1097 * @syscap SystemCapability.Utils.Lang 1098 * @crossplatform 1099 * @atomicservice 1100 * @since 11 1101 */ 1102export interface ThreadWorkerGlobalScope extends GlobalScope { 1103 /** 1104 * The onmessage attribute of parentPort specifies the event handler 1105 * to be called then the worker thread receives a message sent by 1106 * the host thread through worker postMessage. 1107 * The event handler is executed in the worker thread. 1108 * 1109 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Incorrect parameter types. 1110 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1111 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1112 * @syscap SystemCapability.Utils.Lang 1113 * @since 9 1114 */ 1115 /** 1116 * The onmessage attribute of parentPort specifies the event handler 1117 * to be called then the worker thread receives a message sent by 1118 * the host thread through worker postMessage. 1119 * The event handler is executed in the worker thread. 1120 * 1121 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Incorrect parameter types. 1122 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1123 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1124 * @syscap SystemCapability.Utils.Lang 1125 * @crossplatform 1126 * @since 10 1127 */ 1128 /** 1129 * The onmessage attribute of parentPort specifies the event handler 1130 * to be called then the worker thread receives a message sent by 1131 * the host thread through worker postMessage. 1132 * The event handler is executed in the worker thread. 1133 * 1134 * @type { ?function } 1135 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Incorrect parameter types. 1136 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1137 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1138 * @syscap SystemCapability.Utils.Lang 1139 * @crossplatform 1140 * @atomicservice 1141 * @since 11 1142 */ 1143 onmessage?: (this: ThreadWorkerGlobalScope, ev: MessageEvents) => void; 1144 1145 /** 1146 * The onmessage attribute of parentPort specifies the event handler 1147 * to be called then the worker receives a message that cannot be deserialized. 1148 * The event handler is executed in the worker thread. 1149 * 1150 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Incorrect parameter types. 1151 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1152 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1153 * @syscap SystemCapability.Utils.Lang 1154 * @since 9 1155 */ 1156 /** 1157 * The onmessage attribute of parentPort specifies the event handler 1158 * to be called then the worker receives a message that cannot be deserialized. 1159 * The event handler is executed in the worker thread. 1160 * 1161 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Incorrect parameter types. 1162 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1163 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1164 * @syscap SystemCapability.Utils.Lang 1165 * @crossplatform 1166 * @since 10 1167 */ 1168 /** 1169 * The onmessage attribute of parentPort specifies the event handler 1170 * to be called then the worker receives a message that cannot be deserialized. 1171 * The event handler is executed in the worker thread. 1172 * 1173 * @type { ?function } 1174 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Incorrect parameter types. 1175 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1176 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1177 * @syscap SystemCapability.Utils.Lang 1178 * @crossplatform 1179 * @atomicservice 1180 * @since 11 1181 */ 1182 onmessageerror?: (this: ThreadWorkerGlobalScope, ev: MessageEvents) => void; 1183 1184 /** 1185 * Close the worker thread to stop the worker from receiving messages 1186 * 1187 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1188 * @syscap SystemCapability.Utils.Lang 1189 * @since 9 1190 */ 1191 /** 1192 * Close the worker thread to stop the worker from receiving messages 1193 * 1194 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1195 * @syscap SystemCapability.Utils.Lang 1196 * @crossplatform 1197 * @since 10 1198 */ 1199 /** 1200 * Close the worker thread to stop the worker from receiving messages 1201 * 1202 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1203 * @syscap SystemCapability.Utils.Lang 1204 * @crossplatform 1205 * @atomicservice 1206 * @since 11 1207 */ 1208 close(): void; 1209 1210 /** 1211 * Send a message to host thread from the worker 1212 * 1213 * @param { Object } messageObject - messageObject Data to be sent to the worker 1214 * @param { ArrayBuffer[] } transfer - transfer array cannot contain null. 1215 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1216 * 1.Mandatory parameters are left unspecified; 1217 * 2.Incorrect parameter types; 1218 * 3.Parameter verification failed. 1219 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1220 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1221 * @syscap SystemCapability.Utils.Lang 1222 * @since 9 1223 */ 1224 /** 1225 * Send a message to host thread from the worker 1226 * 1227 * @param { Object } messageObject - messageObject Data to be sent to the worker 1228 * @param { ArrayBuffer[] } transfer - transfer array cannot contain null. 1229 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1230 * 1.Mandatory parameters are left unspecified; 1231 * 2.Incorrect parameter types; 1232 * 3.Parameter verification failed. 1233 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1234 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1235 * @syscap SystemCapability.Utils.Lang 1236 * @crossplatform 1237 * @since 10 1238 */ 1239 /** 1240 * Send a message to host thread from the worker 1241 * 1242 * @param { Object } messageObject - messageObject Data to be sent to the worker 1243 * @param { ArrayBuffer[] } transfer - transfer array cannot contain null. 1244 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1245 * 1.Mandatory parameters are left unspecified; 1246 * 2.Incorrect parameter types; 1247 * 3.Parameter verification failed. 1248 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1249 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1250 * @syscap SystemCapability.Utils.Lang 1251 * @crossplatform 1252 * @atomicservice 1253 * @since 11 1254 */ 1255 postMessage(messageObject: Object, transfer: ArrayBuffer[]): void; 1256 1257 /** 1258 * Send a message to be host thread from the worker 1259 * 1260 * @param { Object } messageObject - messageObject Data to be sent to the worker 1261 * @param { PostMessageOptions } [options] - options Option can be set for postmessage. 1262 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1263 * 1.Mandatory parameters are left unspecified; 1264 * 2.Incorrect parameter types; 1265 * 3.Parameter verification failed. 1266 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1267 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1268 * @syscap SystemCapability.Utils.Lang 1269 * @since 9 1270 */ 1271 /** 1272 * Send a message to be host thread from the worker 1273 * 1274 * @param { Object } messageObject - messageObject Data to be sent to the worker 1275 * @param { PostMessageOptions } [options] - options Option can be set for postmessage. 1276 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1277 * 1.Mandatory parameters are left unspecified; 1278 * 2.Incorrect parameter types; 1279 * 3.Parameter verification failed. 1280 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1281 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1282 * @syscap SystemCapability.Utils.Lang 1283 * @crossplatform 1284 * @since 10 1285 */ 1286 /** 1287 * Send a message to be host thread from the worker 1288 * 1289 * @param { Object } messageObject - messageObject Data to be sent to the worker 1290 * @param { PostMessageOptions } [options] - options Option can be set for postmessage. 1291 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1292 * 1.Mandatory parameters are left unspecified; 1293 * 2.Incorrect parameter types; 1294 * 3.Parameter verification failed. 1295 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1296 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1297 * @syscap SystemCapability.Utils.Lang 1298 * @crossplatform 1299 * @atomicservice 1300 * @since 11 1301 */ 1302 postMessage(messageObject: Object, options?: PostMessageOptions): void; 1303 1304 /** 1305 * Send a message to the host thread from the worker thread. 1306 * If there're sendable objects included in the message, they will be passed through references. 1307 * Non-sendable objects are passed through serialization. 1308 * 1309 * @param { Object } message - Data to be sent to the worker thread. 1310 * @param { ArrayBuffer[] } [transfer] - ArrayBuffer instance that can be transferred. 1311 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1312 * 1.Mandatory parameters are left unspecified; 1313 * 2.Incorrect parameter types; 1314 * 3.Parameter verification failed. 1315 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1316 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1317 * @syscap SystemCapability.Utils.Lang 1318 * @crossplatform 1319 * @atomicservice 1320 * @since 12 1321 */ 1322 postMessageWithSharedSendable(message: Object, transfer?: ArrayBuffer[]): void; 1323 1324 /** 1325 * Send a global call on registered globalCallObject on host side and return the result synchronously 1326 * 1327 * @param { string } instanceName - the exact key used in registration 1328 * @param { string } methodName - a string which is same to the method called on globalCallObject. 1329 * @param { number } timeout - the specific milliseconds that will wait for result to return, between 0 and 5000. 1330 * @param { Object[] } args - the method argument called on registered globalCallObject. 1331 * @returns { Object } Return the result of method if it has a return value, otherwise return void. 1332 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1333 * 1.Mandatory parameters are left unspecified; 1334 * 2.Incorrect parameter types; 1335 * 3.Parameter verification failed. 1336 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1337 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1338 * @throws { BusinessError } 10200019 - The globalCallObject is not registered. 1339 * @throws { BusinessError } 10200020 - The method to be called is not callable or is an async method or a generator. 1340 * @throws { BusinessError } 10200021 - Waiting for a global call timed out. 1341 * @syscap SystemCapability.Utils.Lang 1342 * @crossplatform 1343 * @since 11 1344 */ 1345 /** 1346 * Send a global call on registered globalCallObject on host side and return the result synchronously 1347 * 1348 * @param { string } instanceName - the exact key used in registration 1349 * @param { string } methodName - a string which is same to the method called on globalCallObject. 1350 * @param { number } timeout - the specific milliseconds that will wait for result to return, between 0 and 5000. 1351 * @param { Object[] } args - the method argument called on registered globalCallObject. 1352 * @returns { Object } Return the result of method if it has a return value, otherwise return void. 1353 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1354 * 1.Mandatory parameters are left unspecified; 1355 * 2.Incorrect parameter types; 1356 * 3.Parameter verification failed. 1357 * @throws { BusinessError } 10200004 - Worker instance is not running. 1358 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1359 * @throws { BusinessError } 10200019 - The globalCallObject is not registered. 1360 * @throws { BusinessError } 10200020 - The method to be called is not callable or is an async method or a generator. 1361 * @throws { BusinessError } 10200021 - The global call exceeds the timeout. 1362 * @syscap SystemCapability.Utils.Lang 1363 * @crossplatform 1364 * @atomicservice 1365 * @since 12 1366 */ 1367 callGlobalCallObjectMethod(instanceName: string, methodName: string, timeout: number, ...args: Object[]): Object; 1368} 1369 1370/** 1371 * JS cross-thread communication tool 1372 * 1373 * @namespace worker 1374 * @syscap SystemCapability.Utils.Lang 1375 * @since 7 1376 */ 1377/** 1378 * JS cross-thread communication tool 1379 * 1380 * @namespace worker 1381 * @syscap SystemCapability.Utils.Lang 1382 * @crossplatform 1383 * @since 10 1384 */ 1385/** 1386 * JS cross-thread communication tool 1387 * 1388 * @namespace worker 1389 * @syscap SystemCapability.Utils.Lang 1390 * @crossplatform 1391 * @atomicservice 1392 * @since 11 1393 */ 1394declare namespace worker { 1395 /** 1396 * The ThreadWorker class contains all Worker functions. 1397 * 1398 * @syscap SystemCapability.Utils.Lang 1399 * @since 9 1400 */ 1401 /** 1402 * The ThreadWorker class contains all Worker functions. 1403 * 1404 * @syscap SystemCapability.Utils.Lang 1405 * @crossplatform 1406 * @since 10 1407 */ 1408 /** 1409 * The ThreadWorker class contains all Worker functions. 1410 * 1411 * @implements WorkerEventTarget 1412 * @syscap SystemCapability.Utils.Lang 1413 * @crossplatform 1414 * @atomicservice 1415 * @since 11 1416 */ 1417 class ThreadWorker implements WorkerEventTarget { 1418 /** 1419 * Creates a worker instance 1420 * 1421 * @param { string } scriptURL - scriptURL URL of the script to be executed by the worker 1422 * @param { WorkerOptions } [options] - options Options that can be set for the worker 1423 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1424 * 1.Mandatory parameters are left unspecified; 1425 * 2.Incorrect parameter types; 1426 * 3.Parameter verification failed. 1427 * @throws { BusinessError } 10200003 - Worker initialization failed. 1428 * @throws { BusinessError } 10200007 - The worker file path is invalid. 1429 * @syscap SystemCapability.Utils.Lang 1430 * @since 9 1431 */ 1432 /** 1433 * Creates a worker instance 1434 * 1435 * @param { string } scriptURL - scriptURL URL of the script to be executed by the worker 1436 * @param { WorkerOptions } [options] - options Options that can be set for the worker 1437 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1438 * 1.Mandatory parameters are left unspecified; 1439 * 2.Incorrect parameter types; 1440 * 3.Parameter verification failed. 1441 * @throws { BusinessError } 10200003 - Worker initialization failed. 1442 * @throws { BusinessError } 10200007 - The worker file path is invalid. 1443 * @syscap SystemCapability.Utils.Lang 1444 * @crossplatform 1445 * @since 10 1446 */ 1447 /** 1448 * Creates a worker instance 1449 * 1450 * @param { string } scriptURL - scriptURL URL of the script to be executed by the worker 1451 * @param { WorkerOptions } [options] - options Options that can be set for the worker 1452 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1453 * 1.Mandatory parameters are left unspecified; 1454 * 2.Incorrect parameter types; 1455 * 3.Parameter verification failed. 1456 * @throws { BusinessError } 10200003 - Worker initialization failed. 1457 * @throws { BusinessError } 10200007 - The worker file path is invalid. 1458 * @syscap SystemCapability.Utils.Lang 1459 * @crossplatform 1460 * @atomicservice 1461 * @since 11 1462 */ 1463 constructor(scriptURL: string, options?: WorkerOptions); 1464 /** 1465 * The onexit attribute of the worker specifies the event handler to be called 1466 * when the worker exits. The handler is executed in the host thread. 1467 * 1468 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Incorrect parameter types. 1469 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1470 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1471 * @syscap SystemCapability.Utils.Lang 1472 * @since 9 1473 */ 1474 /** 1475 * The onexit attribute of the worker specifies the event handler to be called 1476 * when the worker exits. The handler is executed in the host thread. 1477 * 1478 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Incorrect parameter types. 1479 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1480 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1481 * @syscap SystemCapability.Utils.Lang 1482 * @crossplatform 1483 * @since 10 1484 */ 1485 /** 1486 * The onexit attribute of the worker specifies the event handler to be called 1487 * when the worker exits. The handler is executed in the host thread. 1488 * 1489 * @type { ?function } 1490 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Incorrect parameter types. 1491 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1492 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1493 * @syscap SystemCapability.Utils.Lang 1494 * @crossplatform 1495 * @atomicservice 1496 * @since 11 1497 */ 1498 onexit?: (code: number) => void; 1499 /** 1500 * The onerror attribute of the worker specifies the event handler to be called 1501 * when an exception occurs during worker execution. 1502 * The event handler is executed in the host thread. 1503 * 1504 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Incorrect parameter types. 1505 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1506 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1507 * @syscap SystemCapability.Utils.Lang 1508 * @since 9 1509 */ 1510 /** 1511 * The onerror attribute of the worker specifies the event handler to be called 1512 * when an exception occurs during worker execution. 1513 * The event handler is executed in the host thread. 1514 * 1515 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Incorrect parameter types. 1516 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1517 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1518 * @syscap SystemCapability.Utils.Lang 1519 * @crossplatform 1520 * @since 10 1521 */ 1522 /** 1523 * The onerror attribute of the worker specifies the event handler to be called 1524 * when an exception occurs during worker execution. 1525 * The event handler is executed in the host thread. 1526 * 1527 * @type { ?function } 1528 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Incorrect parameter types. 1529 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1530 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1531 * @syscap SystemCapability.Utils.Lang 1532 * @crossplatform 1533 * @atomicservice 1534 * @since 11 1535 */ 1536 onerror?: (err: ErrorEvent) => void; 1537 /** 1538 * The onmessage attribute of the worker specifies the event handler 1539 * to be called then the host thread receives a message created by itself 1540 * and sent by the worker through the parentPort.postMessage. 1541 * The event handler is executed in the host thread. 1542 * 1543 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Incorrect parameter types. 1544 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1545 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1546 * @syscap SystemCapability.Utils.Lang 1547 * @since 9 1548 */ 1549 /** 1550 * The onmessage attribute of the worker specifies the event handler 1551 * to be called then the host thread receives a message created by itself 1552 * and sent by the worker through the parentPort.postMessage. 1553 * The event handler is executed in the host thread. 1554 * 1555 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Incorrect parameter types. 1556 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1557 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1558 * @syscap SystemCapability.Utils.Lang 1559 * @crossplatform 1560 * @since 10 1561 */ 1562 /** 1563 * The onmessage attribute of the worker specifies the event handler 1564 * to be called then the host thread receives a message created by itself 1565 * and sent by the worker through the parentPort.postMessage. 1566 * The event handler is executed in the host thread. 1567 * 1568 * @type { ?function } 1569 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Incorrect parameter types. 1570 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1571 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1572 * @syscap SystemCapability.Utils.Lang 1573 * @crossplatform 1574 * @atomicservice 1575 * @since 11 1576 */ 1577 onmessage?: (event: MessageEvents) => void; 1578 /** 1579 * The onmessage attribute of the worker specifies the event handler 1580 * when the worker receives a message that cannot be serialized. 1581 * The event handler is executed in the host thread. 1582 * 1583 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Incorrect parameter types. 1584 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1585 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1586 * @syscap SystemCapability.Utils.Lang 1587 * @since 9 1588 */ 1589 /** 1590 * The onmessage attribute of the worker specifies the event handler 1591 * when the worker receives a message that cannot be serialized. 1592 * The event handler is executed in the host thread. 1593 * 1594 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Incorrect parameter types. 1595 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1596 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1597 * @syscap SystemCapability.Utils.Lang 1598 * @crossplatform 1599 * @since 10 1600 */ 1601 /** 1602 * The onmessage attribute of the worker specifies the event handler 1603 * when the worker receives a message that cannot be serialized. 1604 * The event handler is executed in the host thread. 1605 * 1606 * @type { ?function } 1607 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Incorrect parameter types. 1608 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1609 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1610 * @syscap SystemCapability.Utils.Lang 1611 * @crossplatform 1612 * @atomicservice 1613 * @since 11 1614 */ 1615 onmessageerror?: (event: MessageEvents) => void; 1616 /** 1617 * Sends a message to the worker thread. 1618 * The data is transferred using the structured clone algorithm. 1619 * 1620 * @param { Object } message - message Data to be sent to the worker 1621 * @param { ArrayBuffer[] } transfer - transfer ArrayBuffer instance that can be transferred. 1622 * The transferList array cannot contain null. 1623 * @throws { BusinessError } 401 - if the input parameters are invalid. 1624 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1625 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1626 * @syscap SystemCapability.Utils.Lang 1627 * @since 9 1628 */ 1629 /** 1630 * Sends a message to the worker thread. 1631 * The data is transferred using the structured clone algorithm. 1632 * 1633 * @param { Object } message - message Data to be sent to the worker 1634 * @param { ArrayBuffer[] } transfer - transfer ArrayBuffer instance that can be transferred. 1635 * The transferList array cannot contain null. 1636 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1637 * 1.Mandatory parameters are left unspecified; 1638 * 2.Incorrect parameter types; 1639 * 3.Parameter verification failed. 1640 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1641 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1642 * @syscap SystemCapability.Utils.Lang 1643 * @crossplatform 1644 * @since 10 1645 */ 1646 /** 1647 * Sends a message to the worker thread. 1648 * The data is transferred using the structured clone algorithm. 1649 * 1650 * @param { Object } message - message Data to be sent to the worker 1651 * @param { ArrayBuffer[] } transfer - transfer ArrayBuffer instance that can be transferred. 1652 * The transferList array cannot contain null. 1653 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1654 * 1.Mandatory parameters are left unspecified; 1655 * 2.Incorrect parameter types; 1656 * 3.Parameter verification failed. 1657 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1658 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1659 * @syscap SystemCapability.Utils.Lang 1660 * @crossplatform 1661 * @atomicservice 1662 * @since 11 1663 */ 1664 postMessage(message: Object, transfer: ArrayBuffer[]): void; 1665 /** 1666 * Sends a message to the worker thread. 1667 * The data is transferred using the structured clone algorithm. 1668 * 1669 * @param { Object } message - message Data to be sent to the worker 1670 * @param { PostMessageOptions } [options] - options 1671 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1672 * 1.Mandatory parameters are left unspecified; 1673 * 2.Incorrect parameter types; 1674 * 3.Parameter verification failed. 1675 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1676 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1677 * @syscap SystemCapability.Utils.Lang 1678 * @since 9 1679 */ 1680 /** 1681 * Sends a message to the worker thread. 1682 * The data is transferred using the structured clone algorithm. 1683 * 1684 * @param { Object } message - message Data to be sent to the worker 1685 * @param { PostMessageOptions } [options] - options 1686 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1687 * 1.Mandatory parameters are left unspecified; 1688 * 2.Incorrect parameter types; 1689 * 3.Parameter verification failed. 1690 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1691 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1692 * @syscap SystemCapability.Utils.Lang 1693 * @crossplatform 1694 * @since 10 1695 */ 1696 /** 1697 * Sends a message to the worker thread. 1698 * The data is transferred using the structured clone algorithm. 1699 * 1700 * @param { Object } message - message Data to be sent to the worker 1701 * @param { PostMessageOptions } [options] - options 1702 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1703 * 1.Mandatory parameters are left unspecified; 1704 * 2.Incorrect parameter types; 1705 * 3.Parameter verification failed. 1706 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1707 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1708 * @syscap SystemCapability.Utils.Lang 1709 * @crossplatform 1710 * @atomicservice 1711 * @since 11 1712 */ 1713 postMessage(message: Object, options?: PostMessageOptions): void; 1714 1715 /** 1716 * Sends a message to the worker thread from the host thread. 1717 * If there're sendable objects included in the message, they will be passed through references. 1718 * Non-sendable objects are passed through serialization. 1719 * 1720 * @param { Object } message - Data to be sent to the worker thread. 1721 * @param { ArrayBuffer[] } [transfer] - ArrayBuffer instance that can be transferred. 1722 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1723 * 1.Mandatory parameters are left unspecified; 1724 * 2.Incorrect parameter types; 1725 * 3.Parameter verification failed. 1726 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1727 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1728 * @syscap SystemCapability.Utils.Lang 1729 * @crossplatform 1730 * @atomicservice 1731 * @since 12 1732 */ 1733 postMessageWithSharedSendable(message: Object, transfer?: ArrayBuffer[]): void; 1734 1735 /** 1736 * Adds an event listener to the worker. 1737 * 1738 * @param { string } type - type Adds an event listener to the worker. 1739 * @param { WorkerEventListener } listener - listener Callback to invoke when an event of the specified type occurs. 1740 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1741 * 1.Mandatory parameters are left unspecified; 1742 * 2.Incorrect parameter types; 1743 * 3.Parameter verification failed. 1744 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1745 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1746 * @syscap SystemCapability.Utils.Lang 1747 * @since 9 1748 */ 1749 /** 1750 * Adds an event listener to the worker. 1751 * 1752 * @param { string } type - type Adds an event listener to the worker. 1753 * @param { WorkerEventListener } listener - listener Callback to invoke when an event of the specified type occurs. 1754 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1755 * 1.Mandatory parameters are left unspecified; 1756 * 2.Incorrect parameter types; 1757 * 3.Parameter verification failed. 1758 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1759 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1760 * @syscap SystemCapability.Utils.Lang 1761 * @crossplatform 1762 * @since 10 1763 */ 1764 /** 1765 * Adds an event listener to the worker. 1766 * 1767 * @param { string } type - type Adds an event listener to the worker. 1768 * @param { WorkerEventListener } listener - listener Callback to invoke when an event of the specified type occurs. 1769 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1770 * 1.Mandatory parameters are left unspecified; 1771 * 2.Incorrect parameter types; 1772 * 3.Parameter verification failed. 1773 * @throws { BusinessError } 10200004 - Worker instance is not running. 1774 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 1775 * @syscap SystemCapability.Utils.Lang 1776 * @crossplatform 1777 * @atomicservice 1778 * @since 12 1779 */ 1780 on(type: string, listener: WorkerEventListener): void; 1781 /** 1782 * Adds an event listener to the worker 1783 * and removes the event listener automatically after it is invoked once. 1784 * 1785 * @param { string } type - type Type of the event to listen for 1786 * @param { WorkerEventListener } listener - listener Callback to invoke when an event of the specified type occurs 1787 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1788 * 1.Mandatory parameters are left unspecified; 1789 * 2.Incorrect parameter types; 1790 * 3.Parameter verification failed. 1791 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1792 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1793 * @syscap SystemCapability.Utils.Lang 1794 * @since 9 1795 */ 1796 /** 1797 * Adds an event listener to the worker 1798 * and removes the event listener automatically after it is invoked once. 1799 * 1800 * @param { string } type - type Type of the event to listen for 1801 * @param { WorkerEventListener } listener - listener Callback to invoke when an event of the specified type occurs 1802 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1803 * 1.Mandatory parameters are left unspecified; 1804 * 2.Incorrect parameter types; 1805 * 3.Parameter verification failed. 1806 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1807 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1808 * @syscap SystemCapability.Utils.Lang 1809 * @crossplatform 1810 * @since 10 1811 */ 1812 /** 1813 * Adds an event listener to the worker 1814 * and removes the event listener automatically after it is invoked once. 1815 * 1816 * @param { string } type - type Type of the event to listen for 1817 * @param { WorkerEventListener } listener - listener Callback to invoke when an event of the specified type occurs 1818 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1819 * 1.Mandatory parameters are left unspecified; 1820 * 2.Incorrect parameter types; 1821 * 3.Parameter verification failed. 1822 * @throws { BusinessError } 10200004 - Worker instance is not running. 1823 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 1824 * @syscap SystemCapability.Utils.Lang 1825 * @crossplatform 1826 * @atomicservice 1827 * @since 12 1828 */ 1829 once(type: string, listener: WorkerEventListener): void; 1830 /** 1831 * Removes an event listener to the worker. 1832 * 1833 * @param { string } type - type Type of the event for which the event listener is removed. 1834 * @param { WorkerEventListener } [listener] - listener Callback of the event listener to remove. 1835 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1836 * 1.Mandatory parameters are left unspecified; 1837 * 2.Incorrect parameter types; 1838 * 3.Parameter verification failed. 1839 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1840 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1841 * @syscap SystemCapability.Utils.Lang 1842 * @since 9 1843 */ 1844 /** 1845 * Removes an event listener to the worker. 1846 * 1847 * @param { string } type - type Type of the event for which the event listener is removed. 1848 * @param { WorkerEventListener } [listener] - listener Callback of the event listener to remove. 1849 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1850 * 1.Mandatory parameters are left unspecified; 1851 * 2.Incorrect parameter types; 1852 * 3.Parameter verification failed. 1853 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1854 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1855 * @syscap SystemCapability.Utils.Lang 1856 * @crossplatform 1857 * @since 10 1858 */ 1859 /** 1860 * Removes an event listener to the worker. 1861 * 1862 * @param { string } type - type Type of the event for which the event listener is removed. 1863 * @param { WorkerEventListener } [listener] - listener Callback of the event listener to remove. 1864 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1865 * 1.Mandatory parameters are left unspecified; 1866 * 2.Incorrect parameter types; 1867 * 3.Parameter verification failed. 1868 * @throws { BusinessError } 10200004 - Worker instance is not running. 1869 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 1870 * @syscap SystemCapability.Utils.Lang 1871 * @crossplatform 1872 * @atomicservice 1873 * @since 12 1874 */ 1875 off(type: string, listener?: WorkerEventListener): void; 1876 /** 1877 * Terminates the worker thread to stop the worker from receiving messages 1878 * 1879 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1880 * @syscap SystemCapability.Utils.Lang 1881 * @since 9 1882 */ 1883 /** 1884 * Terminates the worker thread to stop the worker from receiving messages 1885 * 1886 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1887 * @syscap SystemCapability.Utils.Lang 1888 * @crossplatform 1889 * @since 10 1890 */ 1891 /** 1892 * Terminates the worker thread to stop the worker from receiving messages 1893 * 1894 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1895 * @syscap SystemCapability.Utils.Lang 1896 * @crossplatform 1897 * @atomicservice 1898 * @since 11 1899 */ 1900 terminate(): void; 1901 /** 1902 * Adds an event listener to the worker. 1903 * 1904 * @param { string } type - type Type of the event to listen for. 1905 * @param { WorkerEventListener } listener Callback to invoke when an event of the specified type occurs. 1906 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1907 * 1.Mandatory parameters are left unspecified; 1908 * 2.Incorrect parameter types; 1909 * 3.Parameter verification failed. 1910 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1911 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1912 * @syscap SystemCapability.Utils.Lang 1913 * @since 9 1914 */ 1915 /** 1916 * Adds an event listener to the worker. 1917 * 1918 * @param { string } type - type Type of the event to listen for. 1919 * @param { WorkerEventListener } listener Callback to invoke when an event of the specified type occurs. 1920 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1921 * 1.Mandatory parameters are left unspecified; 1922 * 2.Incorrect parameter types; 1923 * 3.Parameter verification failed. 1924 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1925 * @throws { BusinessError } 10200005 - The called API is not supported in the worker thread. 1926 * @syscap SystemCapability.Utils.Lang 1927 * @crossplatform 1928 * @since 10 1929 */ 1930 /** 1931 * Adds an event listener to the worker. 1932 * 1933 * @param { string } type - type Type of the event to listen for. 1934 * @param { WorkerEventListener } listener Callback to invoke when an event of the specified type occurs. 1935 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1936 * 1.Mandatory parameters are left unspecified; 1937 * 2.Incorrect parameter types; 1938 * 3.Parameter verification failed. 1939 * @throws { BusinessError } 10200004 - Worker instance is not running. 1940 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 1941 * @syscap SystemCapability.Utils.Lang 1942 * @crossplatform 1943 * @atomicservice 1944 * @since 12 1945 */ 1946 addEventListener(type: string, listener: WorkerEventListener): void; 1947 /** 1948 * Handle the event defined for the worker. 1949 * 1950 * @param { Event } event - event Event to dispatch. 1951 * @returns { boolean } 1952 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1953 * 1.Mandatory parameters are left unspecified; 1954 * 2.Incorrect parameter types; 1955 * 3.Parameter verification failed. 1956 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1957 * @syscap SystemCapability.Utils.Lang 1958 * @since 9 1959 */ 1960 /** 1961 * Handle the event defined for the worker. 1962 * 1963 * @param { Event } event - event Event to dispatch. 1964 * @returns { boolean } 1965 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1966 * 1.Mandatory parameters are left unspecified; 1967 * 2.Incorrect parameter types; 1968 * 3.Parameter verification failed. 1969 * @throws { BusinessError } 10200004 - The Worker instance is not running. 1970 * @syscap SystemCapability.Utils.Lang 1971 * @crossplatform 1972 * @since 10 1973 */ 1974 /** 1975 * Handle the event defined for the worker. 1976 * 1977 * @param { Event } event - event Event to dispatch. 1978 * @returns { boolean } 1979 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1980 * 1.Mandatory parameters are left unspecified; 1981 * 2.Incorrect parameter types; 1982 * 3.Parameter verification failed. 1983 * @throws { BusinessError } 10200004 - Worker instance is not running. 1984 * @syscap SystemCapability.Utils.Lang 1985 * @crossplatform 1986 * @atomicservice 1987 * @since 12 1988 */ 1989 dispatchEvent(event: Event): boolean; 1990 /** 1991 * Remove an event defined for the worker. 1992 * 1993 * @param { string } type - type Type of the event for which the event listener is cancelled. 1994 * @param { WorkerEventListener } [callback] - callback Callback of the event listener to remove. 1995 * @throws { BusinessError } 401 - Parameter error. Possible causes: 1996 * 1.Mandatory parameters are left unspecified; 1997 * 2.Incorrect parameter types; 1998 * 3.Parameter verification failed. 1999 * @throws { BusinessError } 10200004 - The Worker instance is not running. 2000 * @syscap SystemCapability.Utils.Lang 2001 * @since 9 2002 */ 2003 /** 2004 * Remove an event defined for the worker. 2005 * 2006 * @param { string } type - type Type of the event for which the event listener is cancelled. 2007 * @param { WorkerEventListener } [callback] - callback Callback of the event listener to remove. 2008 * @throws { BusinessError } 401 - Parameter error. Possible causes: 2009 * 1.Mandatory parameters are left unspecified; 2010 * 2.Incorrect parameter types; 2011 * 3.Parameter verification failed. 2012 * @throws { BusinessError } 10200004 - The Worker instance is not running. 2013 * @syscap SystemCapability.Utils.Lang 2014 * @crossplatform 2015 * @since 10 2016 */ 2017 /** 2018 * Remove an event defined for the worker. 2019 * 2020 * @param { string } type - type Type of the event for which the event listener is cancelled. 2021 * @param { WorkerEventListener } [callback] - callback Callback of the event listener to remove. 2022 * @throws { BusinessError } 401 - Parameter error. Possible causes: 2023 * 1.Mandatory parameters are left unspecified; 2024 * 2.Incorrect parameter types; 2025 * 3.Parameter verification failed. 2026 * @throws { BusinessError } 10200004 - Worker instance is not running. 2027 * @syscap SystemCapability.Utils.Lang 2028 * @crossplatform 2029 * @atomicservice 2030 * @since 12 2031 */ 2032 removeEventListener(type: string, callback?: WorkerEventListener): void; 2033 /** 2034 * Remove all event listeners for the worker. 2035 * 2036 * @throws { BusinessError } 10200004 - The Worker instance is not running. 2037 * @syscap SystemCapability.Utils.Lang 2038 * @since 9 2039 */ 2040 /** 2041 * Remove all event listeners for the worker. 2042 * 2043 * @throws { BusinessError } 10200004 - The Worker instance is not running. 2044 * @syscap SystemCapability.Utils.Lang 2045 * @crossplatform 2046 * @since 10 2047 */ 2048 /** 2049 * Remove all event listeners for the worker. 2050 * 2051 * @throws { BusinessError } 10200004 - Worker instance is not running. 2052 * @syscap SystemCapability.Utils.Lang 2053 * @crossplatform 2054 * @atomicservice 2055 * @since 12 2056 */ 2057 removeAllListener(): void; 2058 2059 /** 2060 * Register globalCallObject for global call. 2061 * @param { string } instanceName - The key to register globalCallObject. 2062 * @param { Object } globalCallObject - The globalCallObject that will be registered. 2063 * @throws { BusinessError } 401 - Parameter error. Possible causes: 2064 * 1.Mandatory parameters are left unspecified; 2065 * 2.Incorrect parameter types; 2066 * 3.Parameter verification failed. 2067 * @throws { BusinessError } 10200004 - The Worker instance is not running. 2068 * @syscap SystemCapability.Utils.Lang 2069 * @crossplatform 2070 * @since 11 2071 */ 2072 /** 2073 * Register globalCallObject for global call. 2074 * @param { string } instanceName - The key to register globalCallObject. 2075 * @param { Object } globalCallObject - The globalCallObject that will be registered. 2076 * @throws { BusinessError } 401 - Parameter error. Possible causes: 2077 * 1.Mandatory parameters are left unspecified; 2078 * 2.Incorrect parameter types; 2079 * 3.Parameter verification failed. 2080 * @throws { BusinessError } 10200004 - Worker instance is not running. 2081 * @syscap SystemCapability.Utils.Lang 2082 * @crossplatform 2083 * @atomicservice 2084 * @since 12 2085 */ 2086 registerGlobalCallObject(instanceName: string, globalCallObject: Object): void; 2087 2088 /** 2089 * Remove registered globalCallObject and release strong reference to registered object. 2090 * @param { string } [instanceName] - The exact key that used in registration. 2091 * @throws { BusinessError } 401 - Parameter error. Possible causes: 2092 * 1.Mandatory parameters are left unspecified; 2093 * 2.Incorrect parameter types; 2094 * 3.Parameter verification failed. 2095 * @throws { BusinessError } 10200004 - The Worker instance is not running. 2096 * @syscap SystemCapability.Utils.Lang 2097 * @crossplatform 2098 * @since 11 2099 */ 2100 /** 2101 * Remove registered globalCallObject and release strong reference to registered object. 2102 * @param { string } [instanceName] - The exact key that used in registration. 2103 * @throws { BusinessError } 401 - Parameter error. Possible causes: 2104 * 1.Mandatory parameters are left unspecified; 2105 * 2.Incorrect parameter types; 2106 * 3.Parameter verification failed. 2107 * @throws { BusinessError } 10200004 - Worker instance is not running. 2108 * @syscap SystemCapability.Utils.Lang 2109 * @crossplatform 2110 * @atomicservice 2111 * @since 12 2112 */ 2113 unregisterGlobalCallObject(instanceName?: string): void; 2114 } 2115 2116 /** 2117 * The RestrictedWorker class contains all Worker functions. 2118 * 2119 * @extends ThreadWorker 2120 * @syscap SystemCapability.Utils.Lang 2121 * @since 11 2122 */ 2123 /** 2124 * The RestrictedWorker class contains all Worker functions. 2125 * 2126 * @extends ThreadWorker 2127 * @syscap SystemCapability.Utils.Lang 2128 * @systemapi 2129 * @since 12 2130 */ 2131 class RestrictedWorker extends ThreadWorker { 2132 /** 2133 * Creates a worker instance 2134 * 2135 * @param { string } scriptURL - scriptURL URL of the script to be executed by the worker 2136 * @param { WorkerOptions } [options] - options Options that can be set for the worker 2137 * @throws { BusinessError } 401 - Parameter error. Possible causes: 2138 * 1.Mandatory parameters are left unspecified; 2139 * 2.Incorrect parameter types; 2140 * 3.Parameter verification failed. 2141 * @throws { BusinessError } 10200003 - Worker initialization failed. 2142 * @throws { BusinessError } 10200007 - The worker file path is invalid. 2143 * @syscap SystemCapability.Utils.Lang 2144 * @since 11 2145 */ 2146 /** 2147 * Creates a worker instance 2148 * 2149 * @param { string } scriptURL - scriptURL URL of the script to be executed by the worker 2150 * @param { WorkerOptions } [options] - options Options that can be set for the worker 2151 * @throws { BusinessError } 401 - Parameter error. Possible causes: 2152 * 1.Mandatory parameters are left unspecified; 2153 * 2.Incorrect parameter types; 2154 * 3.Parameter verification failed. 2155 * @throws { BusinessError } 10200003 - Worker initialization failure. 2156 * @throws { BusinessError } 10200007 - The worker file patch is invalid path. 2157 * @syscap SystemCapability.Utils.Lang 2158 * @systemapi 2159 * @since 12 2160 */ 2161 constructor(scriptURL: string, options?: WorkerOptions); 2162 } 2163 2164 /** 2165 * The Worker class contains all Worker functions. 2166 * 2167 * @syscap SystemCapability.Utils.Lang 2168 * @since 7 2169 * @deprecated since 9 2170 * @useinstead ohos.worker.ThreadWorker 2171 */ 2172 class Worker implements EventTarget { 2173 /** 2174 * Creates a worker instance 2175 * 2176 * @param { string } scriptURL - scriptURL URL of the script to be executed by the worker 2177 * @param { WorkerOptions } options - options Options that can be set for the worker 2178 * @syscap SystemCapability.Utils.Lang 2179 * @since 7 2180 * @deprecated since 9 2181 * @useinstead ohos.worker.ThreadWorker.constructor 2182 */ 2183 constructor(scriptURL: string, options?: WorkerOptions); 2184 2185 /** 2186 * The onexit attribute of the worker specifies the event handler to be called 2187 * when the worker exits. The handler is executed in the host thread. 2188 * 2189 * @syscap SystemCapability.Utils.Lang 2190 * @since 7 2191 * @deprecated since 9 2192 * @useinstead ohos.worker.ThreadWorker.onexit 2193 */ 2194 onexit?: (code: number) => void; 2195 2196 /** 2197 * The onerror attribute of the worker specifies the event handler to be called 2198 * when an exception occurs during worker execution. 2199 * The event handler is executed in the host thread. 2200 * 2201 * @syscap SystemCapability.Utils.Lang 2202 * @since 7 2203 * @deprecated since 9 2204 * @useinstead ohos.worker.ThreadWorker.onerror 2205 */ 2206 onerror?: (err: ErrorEvent) => void; 2207 2208 /** 2209 * The onmessage attribute of the worker specifies the event handler 2210 * to be called then the host thread receives a message created by itself 2211 * and sent by the worker through the parentPort.postMessage. 2212 * The event handler is executed in the host thread. 2213 * 2214 * @syscap SystemCapability.Utils.Lang 2215 * @since 7 2216 * @deprecated since 9 2217 * @useinstead ohos.worker.ThreadWorker.onmessage 2218 */ 2219 onmessage?: (event: MessageEvent) => void; 2220 2221 /** 2222 * The onmessage attribute of the worker specifies the event handler 2223 * when the worker receives a message that cannot be serialized. 2224 * The event handler is executed in the host thread. 2225 * 2226 * @syscap SystemCapability.Utils.Lang 2227 * @since 7 2228 * @deprecated since 9 2229 * @useinstead ohos.worker.ThreadWorker.onmessageerror 2230 */ 2231 onmessageerror?: (event: MessageEvent) => void; 2232 2233 /** 2234 * Sends a message to the worker thread. 2235 * The data is transferred using the structured clone algorithm. 2236 * 2237 * @param { Object } message - message - message Data to be sent to the worker 2238 * @param { ArrayBuffer[] } transfer - transfer ArrayBuffer instance that can be transferred. 2239 * The transferList array cannot contain null. 2240 * @syscap SystemCapability.Utils.Lang 2241 * @since 7 2242 * @deprecated since 9 2243 * @useinstead ohos.worker.ThreadWorker.postMessage 2244 */ 2245 postMessage(message: Object, transfer: ArrayBuffer[]): void; 2246 2247 /** 2248 * Sends a message to the worker thread. 2249 * The data is transferred using the structured clone algorithm. 2250 * 2251 * @param { Object } message - message - message Data to be sent to the worker 2252 * @param { PostMessageOptions } [options] - options Option can be set for postmessage. 2253 * The transferList array cannot contain null. 2254 * @syscap SystemCapability.Utils.Lang 2255 * @since 7 2256 * @deprecated since 9 2257 * @useinstead ohos.worker.ThreadWorker.postMessage 2258 */ 2259 postMessage(message: Object, options?: PostMessageOptions): void; 2260 2261 /** 2262 * Adds an event listener to the worker. 2263 * 2264 * @param { string } type - type Adds an event listener to the worker. 2265 * @param { EventListener } listener - listener Callback to invoke when an event of the specified type occurs. 2266 * @syscap SystemCapability.Utils.Lang 2267 * @since 7 2268 * @deprecated since 9 2269 * @useinstead ohos.worker.ThreadWorker.on 2270 */ 2271 on(type: string, listener: EventListener): void; 2272 2273 /** 2274 * Adds an event listener to the worker 2275 * and removes the event listener automatically after it is invoked once. 2276 * 2277 * @param { string } type - type Type of the event to listen for 2278 * @param { EventListener } listener - listener Callback to invoke when an event of the specified type occurs 2279 * @syscap SystemCapability.Utils.Lang 2280 * @since 7 2281 * @deprecated since 9 2282 * @useinstead ohos.worker.ThreadWorker.once 2283 */ 2284 once(type: string, listener: EventListener): void; 2285 2286 /** 2287 * Removes an event listener to the worker. 2288 * 2289 * @param { string } type - type Type of the event for which the event listener is removed. 2290 * @param { EventListener } listener - listener Callback of the event listener to remove. 2291 * @syscap SystemCapability.Utils.Lang 2292 * @since 7 2293 * @deprecated since 9 2294 * @useinstead ohos.worker.ThreadWorker.off 2295 */ 2296 off(type: string, listener?: EventListener): void; 2297 2298 /** 2299 * Terminates the worker thread to stop the worker from receiving messages 2300 * 2301 * @syscap SystemCapability.Utils.Lang 2302 * @since 7 2303 * @deprecated since 9 2304 * @useinstead ohos.worker.ThreadWorker.terminate 2305 */ 2306 terminate(): void; 2307 } 2308 2309 /** 2310 * The object used by the worker thread to communicate with the host thread. 2311 * 2312 * @constant 2313 * @syscap SystemCapability.Utils.Lang 2314 * @since 7 2315 * @deprecated since 9 2316 * @useinstead ohos.worker.workerPort 2317 */ 2318 const parentPort: DedicatedWorkerGlobalScope; 2319 2320 /** 2321 * The object used by the worker thread to communicate with the host thread. 2322 * 2323 * @constant 2324 * @syscap SystemCapability.Utils.Lang 2325 * @since 9 2326 */ 2327 /** 2328 * The object used by the worker thread to communicate with the host thread. 2329 * 2330 * @constant 2331 * @syscap SystemCapability.Utils.Lang 2332 * @crossplatform 2333 * @since 10 2334 */ 2335 /** 2336 * The object used by the worker thread to communicate with the host thread. 2337 * 2338 * @syscap SystemCapability.Utils.Lang 2339 * @crossplatform 2340 * @atomicservice 2341 * @since 11 2342 */ 2343 const workerPort: ThreadWorkerGlobalScope; 2344} 2345export default worker; 2346