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