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 * @typedef WorkerOptions 18 * Provides options that can be set for the worker to create. 19 * @syscap SystemCapability.Utils.Lang 20 * @since 7 21 */ 22/** 23 * @typedef WorkerOptions 24 * Provides options that can be set for the worker to create. 25 * @syscap SystemCapability.Utils.Lang 26 * @crossplatform 27 * @since 10 28 */ 29export interface WorkerOptions { 30 /** 31 * Mode in which the worker executes the script. 32 * 33 * @syscap SystemCapability.Utils.Lang 34 * @since 7 35 */ 36 /** 37 * Mode in which the worker executes the script. 38 * 39 * @syscap SystemCapability.Utils.Lang 40 * @crossplatform 41 * @since 10 42 */ 43 type?: 'classic' | 'module'; 44 45 /** 46 * Name of the worker. 47 * 48 * @syscap SystemCapability.Utils.Lang 49 * @since 7 50 */ 51 /** 52 * Name of the worker. 53 * 54 * @syscap SystemCapability.Utils.Lang 55 * @crossplatform 56 * @since 10 57 */ 58 name?: string; 59 60 /** 61 * Whether the worker is shared. 62 * 63 * @syscap SystemCapability.Utils.Lang 64 * @since 7 65 */ 66 /** 67 * Whether the worker is shared. 68 * 69 * @syscap SystemCapability.Utils.Lang 70 * @crossplatform 71 * @since 10 72 */ 73 shared?: boolean; 74} 75 76/** 77 * @typedef Event 78 * Defines the event. 79 * @syscap SystemCapability.Utils.Lang 80 * @since 7 81 */ 82/** 83 * @typedef Event 84 * Defines the event. 85 * @syscap SystemCapability.Utils.Lang 86 * @crossplatform 87 * @since 10 88 */ 89export interface Event { 90 /** 91 * Type of the Event. 92 * 93 * @syscap SystemCapability.Utils.Lang 94 * @since 7 95 */ 96 /** 97 * Type of the Event. 98 * 99 * @syscap SystemCapability.Utils.Lang 100 * @crossplatform 101 * @since 10 102 */ 103 readonly type: string; 104 105 /** 106 * Timestamp(accurate to millisecond) when the event is created. 107 * 108 * @syscap SystemCapability.Utils.Lang 109 * @since 7 110 */ 111 /** 112 * Timestamp(accurate to millisecond) when the event is created. 113 * 114 * @syscap SystemCapability.Utils.Lang 115 * @crossplatform 116 * @since 10 117 */ 118 readonly timeStamp: number; 119} 120 121/** 122 * @typedef ErrorEvent 123 * Provides detailed information about the exception occurred during worker execution. 124 * @syscap SystemCapability.Utils.Lang 125 * @since 7 126 */ 127/** 128 * @typedef ErrorEvent 129 * Provides detailed information about the exception occurred during worker execution. 130 * @syscap SystemCapability.Utils.Lang 131 * @crossplatform 132 * @since 10 133 */ 134export interface ErrorEvent extends Event { 135 /** 136 * Information about the exception. 137 * 138 * @syscap SystemCapability.Utils.Lang 139 * @since 7 140 */ 141 /** 142 * Information about the exception. 143 * 144 * @syscap SystemCapability.Utils.Lang 145 * @crossplatform 146 * @since 10 147 */ 148 readonly message: string; 149 150 /** 151 * File where the exception is located. 152 * 153 * @syscap SystemCapability.Utils.Lang 154 * @since 7 155 */ 156 /** 157 * File where the exception is located. 158 * 159 * @syscap SystemCapability.Utils.Lang 160 * @crossplatform 161 * @since 10 162 */ 163 readonly filename: string; 164 165 /** 166 * Number of the line where the exception is located. 167 * 168 * @syscap SystemCapability.Utils.Lang 169 * @since 7 170 */ 171 /** 172 * Number of the line where the exception is located. 173 * 174 * @syscap SystemCapability.Utils.Lang 175 * @crossplatform 176 * @since 10 177 */ 178 readonly lineno: number; 179 180 /** 181 * Number of the column where the exception is located. 182 * 183 * @syscap SystemCapability.Utils.Lang 184 * @since 7 185 */ 186 /** 187 * Number of the column where the exception is located. 188 * 189 * @syscap SystemCapability.Utils.Lang 190 * @crossplatform 191 * @since 10 192 */ 193 readonly colno: number; 194 195 /** 196 * Type of the exception. 197 * 198 * @syscap SystemCapability.Utils.Lang 199 * @since 7 200 */ 201 /** 202 * Type of the exception. 203 * 204 * @syscap SystemCapability.Utils.Lang 205 * @crossplatform 206 * @since 10 207 */ 208 readonly error: Object; 209} 210 211/** 212 * @typedef MessageEvent 213 * Holds the data transferred between worker threads. 214 * @syscap SystemCapability.Utils.Lang 215 * @since 7 216 */ 217/** 218 * @typedef MessageEvent 219 * Holds the data transferred between worker threads. 220 * @syscap SystemCapability.Utils.Lang 221 * @crossplatform 222 * @since 10 223 */ 224export interface MessageEvent<T> extends Event { 225 /** 226 * Data transferred when an exception occurs. 227 * 228 * @syscap SystemCapability.Utils.Lang 229 * @since 7 230 */ 231 /** 232 * Data transferred when an exception occurs. 233 * 234 * @syscap SystemCapability.Utils.Lang 235 * @crossplatform 236 * @since 10 237 */ 238 readonly data: T; 239} 240 241/** 242 * @typedef MessageEvents 243 * Saves the data transferred between worker thread and host thread. 244 * @syscap SystemCapability.Utils.Lang 245 * @since 9 246 */ 247/** 248 * @typedef MessageEvents 249 * Saves the data transferred between worker thread and host thread. 250 * @syscap SystemCapability.Utils.Lang 251 * @crossplatform 252 * @since 10 253 */ 254export interface MessageEvents extends Event { 255 /** 256 * Data transferred when an exception occurs. 257 * 258 * @syscap SystemCapability.Utils.Lang 259 * @since 9 260 */ 261 /** 262 * Data transferred when an exception occurs. 263 * 264 * @syscap SystemCapability.Utils.Lang 265 * @crossplatform 266 * @since 10 267 */ 268 readonly data; 269} 270 271/** 272 * @typedef PostMessageOptions 273 * Specifies the object whose ownership need to be transferred during data transfer. 274 * The object must be ArrayBuffer. 275 * @syscap SystemCapability.Utils.Lang 276 * @since 7 277 */ 278/** 279 * @typedef PostMessageOptions 280 * Specifies the object whose ownership need to be transferred during data transfer. 281 * The object must be ArrayBuffer. 282 * @syscap SystemCapability.Utils.Lang 283 * @crossplatform 284 * @since 10 285 */ 286export interface PostMessageOptions { 287 /** 288 * ArrayBuffer array used to transfer the ownership. 289 * 290 * @syscap SystemCapability.Utils.Lang 291 * @since 7 292 */ 293 /** 294 * ArrayBuffer array used to transfer the ownership. 295 * 296 * @syscap SystemCapability.Utils.Lang 297 * @crossplatform 298 * @since 10 299 */ 300 transfer?: Object[]; 301} 302 303/** 304 * @typedef EventListener 305 * Implements event listening. 306 * @syscap SystemCapability.Utils.Lang 307 * @since 7 308 * @deprecated since 9 309 * @useinstead ohos.worker.WorkerEventListener 310 */ 311export interface EventListener { 312 /** 313 * Specifies the callback to invoke. 314 * 315 * @param { Event } evt - evt evt Event class for the callback to invoke. 316 * @returns { void | Promise<void> } 317 * @syscap SystemCapability.Utils.Lang 318 * @since 7 319 * @deprecated since 9 320 * @useinstead ohos.worker.WorkerEventListener.(event: Event) 321 */ 322 (evt: Event): void | Promise<void>; 323} 324 325/** 326 * @typedef WorkerEventListener 327 * Implements event listening. 328 * @syscap SystemCapability.Utils.Lang 329 * @since 9 330 */ 331/** 332 * @typedef WorkerEventListener 333 * Implements event listening. 334 * @syscap SystemCapability.Utils.Lang 335 * @crossplatform 336 * @since 10 337 */ 338export interface WorkerEventListener { 339 /** 340 * Specifies the callback function to be invoked. 341 * 342 * @param { Event } event - event Event class for the callback to invoke. 343 * @returns { void | Promise<void> } 344 * @throws { BusinessError } 401 - if the input parameters are invalid. 345 * @throws { BusinessError } 10200004 - Worker instance is not running. 346 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 347 * @syscap SystemCapability.Utils.Lang 348 * @since 9 349 */ 350 /** 351 * Specifies the callback function to be invoked. 352 * 353 * @param { Event } event - event Event class for the callback to invoke. 354 * @returns { void | Promise<void> } 355 * @throws { BusinessError } 401 - if the input parameters are invalid. 356 * @throws { BusinessError } 10200004 - Worker instance is not running. 357 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 358 * @syscap SystemCapability.Utils.Lang 359 * @crossplatform 360 * @since 10 361 */ 362 (event: Event): void | Promise<void>; 363} 364 365/** 366 * Type of message, only "message" and "messageerror". 367 * 368 * @syscap SystemCapability.Utils.Lang 369 * @since 7 370 */ 371/** 372 * Type of message, only "message" and "messageerror". 373 * 374 * @syscap SystemCapability.Utils.Lang 375 * @crossplatform 376 * @since 10 377 */ 378type MessageType = 'message' | 'messageerror'; 379 380/** 381 * @typedef EventTarget 382 * Specific event features. 383 * @syscap SystemCapability.Utils.Lang 384 * @since 7 385 * @deprecated since 9 386 * @useinstead ohos.worker.WorkerEventTarget 387 */ 388export interface EventTarget { 389 /** 390 * Adds an event listener to the worker. 391 * 392 * @param { string } type - type Type of the event to listen for. 393 * @param { EventListener } listener - listener Callback to invoke when an event of the specified type occurs. 394 * @syscap SystemCapability.Utils.Lang 395 * @since 7 396 * @deprecated since 9 397 * @useinstead ohos.worker.WorkerEventTarget.addEventListener 398 */ 399 addEventListener(type: string, listener: EventListener): void; 400 401 /** 402 * Dispatches the event defined for the worker. 403 * 404 * @param { Event } event - event Event to dispatch. 405 * @returns { boolean } 406 * @syscap SystemCapability.Utils.Lang 407 * @since 7 408 * @deprecated since 9 409 * @useinstead ohos.worker.WorkerEventTarget.dispatchEvent 410 */ 411 dispatchEvent(event: Event): boolean; 412 413 /** 414 * Removes an event defined for the worker. 415 * 416 * @param { string } type - type Type of the event for which the event listener is removed. 417 * @param { EventListener } callback - callback Callback of the event listener to remove. 418 * @syscap SystemCapability.Utils.Lang 419 * @since 7 420 * @deprecated since 9 421 * @useinstead ohos.worker.WorkerEventTarget.removeEventListener 422 */ 423 removeEventListener(type: string, callback?: EventListener): void; 424 425 /** 426 * Removes all event listeners for the worker. 427 * 428 * @syscap SystemCapability.Utils.Lang 429 * @since 7 430 * @deprecated since 9 431 * @useinstead ohos.worker.WorkerEventTarget.removeAllListener 432 */ 433 removeAllListener(): void; 434} 435 436/** 437 * @typedef WorkerEventTarget 438 * Specific worker event features. 439 * @syscap SystemCapability.Utils.Lang 440 * @since 9 441 */ 442/** 443 * @typedef WorkerEventTarget 444 * Specific worker event features. 445 * @syscap SystemCapability.Utils.Lang 446 * @crossplatform 447 * @since 10 448 */ 449export interface WorkerEventTarget { 450 /** 451 * Adds an event listener to the worker. 452 * 453 * @param { string } type - type Type of the event to listen for. 454 * @param { WorkerEventListener } listener - listener Callback to invoke when an event of the specified type occurs. 455 * @throws { BusinessError } 401 - if the input parameters are invalid. 456 * @throws { BusinessError } 10200004 - Worker instance is not running. 457 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 458 * @syscap SystemCapability.Utils.Lang 459 * @since 9 460 */ 461 /** 462 * Adds an event listener to the worker. 463 * 464 * @param { string } type - type Type of the event to listen for. 465 * @param { WorkerEventListener } listener - listener Callback to invoke when an event of the specified type occurs. 466 * @throws { BusinessError } 401 - if the input parameters are invalid. 467 * @throws { BusinessError } 10200004 - Worker instance is not running. 468 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 469 * @syscap SystemCapability.Utils.Lang 470 * @since 10 471 */ 472 addEventListener(type: string, listener: WorkerEventListener): void; 473 /** 474 * Handle the event defined for the worker. 475 * 476 * @param { Event } event - event Event to dispatch. 477 * @returns { boolean } 478 * @throws { BusinessError } 401 - if the input parameters are invalid. 479 * @throws { BusinessError } 10200004 - Worker instance is not running. 480 * @syscap SystemCapability.Utils.Lang 481 * @since 9 482 */ 483 /** 484 * Handle the event defined for the worker. 485 * 486 * @param { Event } event - event Event to dispatch. 487 * @returns { boolean } 488 * @throws { BusinessError } 401 - if the input parameters are invalid. 489 * @throws { BusinessError } 10200004 - Worker instance is not running. 490 * @syscap SystemCapability.Utils.Lang 491 * @crossplatform 492 * @since 10 493 */ 494 dispatchEvent(event: Event): boolean; 495 /** 496 * Remove an event defined for the worker. 497 * 498 * @param { string } type - type Type of the event for which the event listener is cancelled. 499 * @param { WorkerEventListener } callback - callback Callback of the event listener to remove. 500 * @throws { BusinessError } 401 - if the input parameters are invalid. 501 * @throws { BusinessError } 10200004 - Worker instance is not running. 502 * @syscap SystemCapability.Utils.Lang 503 * @since 9 504 */ 505 /** 506 * Remove an event defined for the worker. 507 * 508 * @param { string } type - type Type of the event for which the event listener is cancelled. 509 * @param { WorkerEventListener } callback - callback Callback of the event listener to remove. 510 * @throws { BusinessError } 401 - if the input parameters are invalid. 511 * @throws { BusinessError } 10200004 - Worker instance is not running. 512 * @syscap SystemCapability.Utils.Lang 513 * @crossplatform 514 * @since 10 515 */ 516 removeEventListener(type: string, callback?: WorkerEventListener): void; 517 /** 518 * Remove all event listeners for the worker. 519 * 520 * @throws { BusinessError } 10200004 - Worker instance is not running. 521 * @syscap SystemCapability.Utils.Lang 522 * @since 9 523 */ 524 /** 525 * Remove all event listeners for the worker. 526 * 527 * @throws { BusinessError } 10200004 - Worker instance is not running. 528 * @syscap SystemCapability.Utils.Lang 529 * @crossplatform 530 * @since 10 531 */ 532 removeAllListener(): void; 533} 534 535/** 536 * @typedef WorkerGlobalScope 537 * Specifies the worker thread running environment, which is isolated from the host thread environment. 538 * @syscap SystemCapability.Utils.Lang 539 * @since 7 540 * @deprecated since 9 541 * @useinstead ohos.worker.GlobalScope 542 */ 543declare interface WorkerGlobalScope extends EventTarget { 544 /** 545 * Worker name specified when there is a new worker. 546 * 547 * @syscap SystemCapability.Utils.Lang 548 * @since 7 549 * @deprecated since 9 550 * @useinstead ohos.worker.GlobalScope.name 551 */ 552 readonly name: string; 553 554 /** 555 * The onerror attribute of parentPort specifies 556 * the event handler to be called when an exception occurs during worker execution. 557 * The event handler is executed in the worker thread. 558 * 559 * @syscap SystemCapability.Utils.Lang 560 * @since 7 561 * @deprecated since 9 562 * @useinstead ohos.worker.GlobalScope.onerror 563 */ 564 onerror?: (ev: ErrorEvent) => void; 565 readonly self: WorkerGlobalScope & typeof globalThis; 566} 567 568/** 569 * @typedef GlobalScope 570 * The environment Specified in which worker threads run, which is isolated from the host thread environment. 571 * @syscap SystemCapability.Utils.Lang 572 * @since 9 573 */ 574/** 575 * @typedef GlobalScope 576 * The environment Specified in which worker threads run, which is isolated from the host thread environment. 577 * @syscap SystemCapability.Utils.Lang 578 * @crossplatform 579 * @since 10 580 */ 581declare interface GlobalScope extends WorkerEventTarget { 582 /** 583 * Name of Worker specified when there is a new worker. 584 * 585 * @syscap SystemCapability.Utils.Lang 586 * @since 9 587 */ 588 /** 589 * Name of Worker specified when there is a new worker. 590 * 591 * @syscap SystemCapability.Utils.Lang 592 * @crossplatform 593 * @since 10 594 */ 595 readonly name: string; 596 597 /** 598 * The onerror attribute of parentPort specified. 599 * the event handler to be called when an exception occurs during worker execution. 600 * The event handler is executed in the worker thread. 601 * 602 * @syscap SystemCapability.Utils.Lang 603 * @since 9 604 */ 605 /** 606 * The onerror attribute of parentPort specified. 607 * the event handler to be called when an exception occurs during worker execution. 608 * The event handler is executed in the worker thread. 609 * 610 * @syscap SystemCapability.Utils.Lang 611 * @crossplatform 612 * @since 10 613 */ 614 onerror?: (ev: ErrorEvent) => void; 615 /** 616 * Specify the type attribute for self. 617 * 618 * @syscap SystemCapability.Utils.Lang 619 * @since 9 620 */ 621 /** 622 * Specify the type attribute for self. 623 * 624 * @syscap SystemCapability.Utils.Lang 625 * @crossplatform 626 * @since 10 627 */ 628 readonly self: GlobalScope & typeof globalThis; 629} 630 631/** 632 * @typedef DedicatedWorkerGlobalScope 633 * Specifies the worker thread running environment, which is isolated from the host thread environment 634 * @syscap SystemCapability.Utils.Lang 635 * @since 7 636 * @deprecated since 9 637 * @useinstead ohos.worker.ThreadWorkerGlobalScope 638 */ 639export interface DedicatedWorkerGlobalScope extends WorkerGlobalScope { 640 /** 641 * The onmessage attribute of parentPort specifies the event handler 642 * to be called then the worker thread receives a message sent by 643 * the host thread through worker postMessage. 644 * The event handler is executed in the worker thread. 645 * 646 * @syscap SystemCapability.Utils.Lang 647 * @since 7 648 * @deprecated since 9 649 * @useinstead ohos.worker.ThreadWorkerGlobalScope.onmessage 650 */ 651 onmessage?: (this: DedicatedWorkerGlobalScope, ev: MessageEvent) => void; 652 653 /** 654 * The onmessage attribute of parentPort specifies the event handler 655 * to be called then the worker receives a message that cannot be deserialized. 656 * The event handler is executed in the worker thread. 657 * 658 * @syscap SystemCapability.Utils.Lang 659 * @since 7 660 * @deprecated since 9 661 * @useinstead ohos.worker.ThreadWorkerGlobalScope.onmessageerror 662 */ 663 onmessageerror?: (this: DedicatedWorkerGlobalScope, ev: MessageEvent) => void; 664 665 /** 666 * Close the worker thread to stop the worker from receiving messages 667 * 668 * @syscap SystemCapability.Utils.Lang 669 * @since 7 670 * @deprecated since 9 671 * @useinstead ohos.worker.ThreadWorkerGlobalScope.close 672 */ 673 close(): void; 674 675 /** 676 * Send a message to be host thread from the worker 677 * 678 * @param { Object } messageObject - messageObject Data to be sent to the worker 679 * @param { Transferable[] } transfer - transfer array cannot contain null. 680 * @syscap SystemCapability.Utils.Lang 681 * @since 7 682 * @deprecated since 9 683 * @useinstead ohos.worker.ThreadWorkerGlobalScope.postMessage 684 */ 685 postMessage(messageObject: Object, transfer: Transferable[]): void; 686 postMessage(messageObject: Object, options?: PostMessageOptions): void; 687 688 /** 689 * Send a message to host thread from the worker 690 * 691 * @param { Object } messageObject - messageObject Data to be sent to the worker 692 * @param { ArrayBuffer[] } transfer - transfer array cannot contain null. 693 * @syscap SystemCapability.Utils.Lang 694 * @since 9 695 * @deprecated since 9 696 */ 697 postMessage(messageObject: Object, transfer: ArrayBuffer[]): void; 698} 699 700/** 701 * @typedef ThreadWorkerGlobalScope 702 * Specifies the thread-worker running environment, which is isolated from the host-thread environment 703 * @syscap SystemCapability.Utils.Lang 704 * @since 9 705 */ 706/** 707 * @typedef ThreadWorkerGlobalScope 708 * Specifies the thread-worker running environment, which is isolated from the host-thread environment 709 * @syscap SystemCapability.Utils.Lang 710 * @crossplatform 711 * @since 10 712 */ 713export interface ThreadWorkerGlobalScope extends GlobalScope { 714 /** 715 * The onmessage attribute of parentPort specifies the event handler 716 * to be called then the worker thread receives a message sent by 717 * the host thread through worker postMessage. 718 * The event handler is executed in the worker thread. 719 * 720 * @throws { BusinessError } 401 - if the input parameters are invalid. 721 * @throws { BusinessError } 10200004 - Worker instance is not running. 722 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 723 * @syscap SystemCapability.Utils.Lang 724 * @since 9 725 */ 726 /** 727 * The onmessage attribute of parentPort specifies the event handler 728 * to be called then the worker thread receives a message sent by 729 * the host thread through worker postMessage. 730 * The event handler is executed in the worker thread. 731 * 732 * @throws { BusinessError } 401 - if the input parameters are invalid. 733 * @throws { BusinessError } 10200004 - Worker instance is not running. 734 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 735 * @syscap SystemCapability.Utils.Lang 736 * @crossplatform 737 * @since 10 738 */ 739 onmessage?: (this: ThreadWorkerGlobalScope, ev: MessageEvents) => void; 740 741 /** 742 * The onmessage attribute of parentPort specifies the event handler 743 * to be called then the worker receives a message that cannot be deserialized. 744 * The event handler is executed in the worker thread. 745 * 746 * @throws { BusinessError } 401 - if the input parameters are invalid. 747 * @throws { BusinessError } 10200004 - Worker instance is not running. 748 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 749 * @syscap SystemCapability.Utils.Lang 750 * @since 9 751 */ 752 /** 753 * The onmessage attribute of parentPort specifies the event handler 754 * to be called then the worker receives a message that cannot be deserialized. 755 * The event handler is executed in the worker thread. 756 * 757 * @throws { BusinessError } 401 - if the input parameters are invalid. 758 * @throws { BusinessError } 10200004 - Worker instance is not running. 759 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 760 * @syscap SystemCapability.Utils.Lang 761 * @crossplatform 762 * @since 10 763 */ 764 onmessageerror?: (this: ThreadWorkerGlobalScope, ev: MessageEvents) => void; 765 766 /** 767 * Close the worker thread to stop the worker from receiving messages 768 * 769 * @throws { BusinessError } 10200004 - Worker instance is not running. 770 * @syscap SystemCapability.Utils.Lang 771 * @since 9 772 */ 773 /** 774 * Close the worker thread to stop the worker from receiving messages 775 * 776 * @throws { BusinessError } 10200004 - Worker instance is not running. 777 * @syscap SystemCapability.Utils.Lang 778 * @crossplatform 779 * @since 10 780 */ 781 close(): void; 782 783 /** 784 * Send a message to host thread from the worker 785 * 786 * @param { Object } messageObject - messageObject Data to be sent to the worker 787 * @param { ArrayBuffer[] } transfer - transfer array cannot contain null. 788 * @throws { BusinessError } 401 - if the input parameters are invalid. 789 * @throws { BusinessError } 10200004 - Worker instance is not running. 790 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 791 * @syscap SystemCapability.Utils.Lang 792 * @since 9 793 */ 794 /** 795 * Send a message to host thread from the worker 796 * 797 * @param { Object } messageObject - messageObject Data to be sent to the worker 798 * @param { ArrayBuffer[] } transfer - transfer array cannot contain null. 799 * @throws { BusinessError } 401 - if the input parameters are invalid. 800 * @throws { BusinessError } 10200004 - Worker instance is not running. 801 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 802 * @syscap SystemCapability.Utils.Lang 803 * @crossplatform 804 * @since 10 805 */ 806 postMessage(messageObject: Object, transfer: ArrayBuffer[]): void; 807 808 /** 809 * Send a message to be host thread from the worker 810 * 811 * @param { Object } messageObject - messageObject Data to be sent to the worker 812 * @param { PostMessageOptions } options - options Option can be set for postmessage. 813 * @throws { BusinessError } 401 - if the input parameters are invalid. 814 * @throws { BusinessError } 10200004 - Worker instance is not running. 815 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 816 * @syscap SystemCapability.Utils.Lang 817 * @since 9 818 */ 819 /** 820 * Send a message to be host thread from the worker 821 * 822 * @param { Object } messageObject - messageObject Data to be sent to the worker 823 * @param { PostMessageOptions } options - options Option can be set for postmessage. 824 * @throws { BusinessError } 401 - if the input parameters are invalid. 825 * @throws { BusinessError } 10200004 - Worker instance is not running. 826 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 827 * @syscap SystemCapability.Utils.Lang 828 * @crossplatform 829 * @since 10 830 */ 831 postMessage(messageObject: Object, options?: PostMessageOptions): void; 832} 833 834/** 835 * JS cross-thread communication tool 836 * 837 * @namespace worker 838 * @syscap SystemCapability.Utils.Lang 839 * @since 7 840 */ 841/** 842 * JS cross-thread communication tool 843 * 844 * @namespace worker 845 * @syscap SystemCapability.Utils.Lang 846 * @crossplatform 847 * @since 10 848 */ 849declare namespace worker { 850 /** 851 * The ThreadWorker class contains all Worker functions. 852 * 853 * @syscap SystemCapability.Utils.Lang 854 * @since 9 855 */ 856 /** 857 * The ThreadWorker class contains all Worker functions. 858 * 859 * @syscap SystemCapability.Utils.Lang 860 * @crossplatform 861 * @since 10 862 */ 863 class ThreadWorker implements WorkerEventTarget { 864 /** 865 * Creates a worker instance 866 * 867 * @param { string } scriptURL - scriptURL URL of the script to be executed by the worker 868 * @param { WorkerOptions } options - options Options that can be set for the worker 869 * @throws { BusinessError } 401 - if the input parameters are invalid. 870 * @throws { BusinessError } 10200003 - Worker initialization failure. 871 * @throws { BusinessError } 10200007 - The worker file patch is invalid path. 872 * @syscap SystemCapability.Utils.Lang 873 * @since 9 874 */ 875 /** 876 * Creates a worker instance 877 * 878 * @param { string } scriptURL - scriptURL URL of the script to be executed by the worker 879 * @param { WorkerOptions } options - options Options that can be set for the worker 880 * @throws { BusinessError } 401 - if the input parameters are invalid. 881 * @throws { BusinessError } 10200003 - Worker initialization failure. 882 * @throws { BusinessError } 10200007 - The worker file patch is invalid path. 883 * @syscap SystemCapability.Utils.Lang 884 * @crossplatform 885 * @since 10 886 */ 887 constructor(scriptURL: string, options?: WorkerOptions); 888 /** 889 * The onexit attribute of the worker specifies the event handler to be called 890 * when the worker exits. The handler is executed in the host thread. 891 * 892 * @throws { BusinessError } 401 - if the input parameters are invalid. 893 * @throws { BusinessError } 10200004 - Worker instance is not running. 894 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 895 * @syscap SystemCapability.Utils.Lang 896 * @since 9 897 */ 898 /** 899 * The onexit attribute of the worker specifies the event handler to be called 900 * when the worker exits. The handler is executed in the host thread. 901 * 902 * @throws { BusinessError } 401 - if the input parameters are invalid. 903 * @throws { BusinessError } 10200004 - Worker instance is not running. 904 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 905 * @syscap SystemCapability.Utils.Lang 906 * @crossplatform 907 * @since 10 908 */ 909 onexit?: (code: number) => void; 910 /** 911 * The onerror attribute of the worker specifies the event handler to be called 912 * when an exception occurs during worker execution. 913 * The event handler is executed in the host thread. 914 * 915 * @throws { BusinessError } 401 - if the input parameters are invalid. 916 * @throws { BusinessError } 10200004 - Worker instance is not running. 917 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 918 * @syscap SystemCapability.Utils.Lang 919 * @since 9 920 */ 921 /** 922 * The onerror attribute of the worker specifies the event handler to be called 923 * when an exception occurs during worker execution. 924 * The event handler is executed in the host thread. 925 * 926 * @throws { BusinessError } 401 - if the input parameters are invalid. 927 * @throws { BusinessError } 10200004 - Worker instance is not running. 928 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 929 * @syscap SystemCapability.Utils.Lang 930 * @crossplatform 931 * @since 10 932 */ 933 onerror?: (err: ErrorEvent) => void; 934 /** 935 * The onmessage attribute of the worker specifies the event handler 936 * to be called then the host thread receives a message created by itself 937 * and sent by the worker through the parentPort.postMessage. 938 * The event handler is executed in the host thread. 939 * 940 * @throws { BusinessError } 401 - if the input parameters are invalid. 941 * @throws { BusinessError } 10200004 - Worker instance is not running. 942 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 943 * @syscap SystemCapability.Utils.Lang 944 * @since 9 945 */ 946 /** 947 * The onmessage attribute of the worker specifies the event handler 948 * to be called then the host thread receives a message created by itself 949 * and sent by the worker through the parentPort.postMessage. 950 * The event handler is executed in the host thread. 951 * 952 * @throws { BusinessError } 401 - if the input parameters are invalid. 953 * @throws { BusinessError } 10200004 - Worker instance is not running. 954 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 955 * @syscap SystemCapability.Utils.Lang 956 * @crossplatform 957 * @since 10 958 */ 959 onmessage?: (event: MessageEvents) => void; 960 /** 961 * The onmessage attribute of the worker specifies the event handler 962 * when the worker receives a message that cannot be serialized. 963 * The event handler is executed in the host thread. 964 * 965 * @throws { BusinessError } 401 - if the input parameters are invalid. 966 * @throws { BusinessError } 10200004 - Worker instance is not running. 967 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 968 * @syscap SystemCapability.Utils.Lang 969 * @since 9 970 */ 971 /** 972 * The onmessage attribute of the worker specifies the event handler 973 * when the worker receives a message that cannot be serialized. 974 * The event handler is executed in the host thread. 975 * 976 * @throws { BusinessError } 401 - if the input parameters are invalid. 977 * @throws { BusinessError } 10200004 - Worker instance is not running. 978 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 979 * @syscap SystemCapability.Utils.Lang 980 * @crossplatform 981 * @since 10 982 */ 983 onmessageerror?: (event: MessageEvents) => void; 984 /** 985 * Sends a message to the worker thread. 986 * The data is transferred using the structured clone algorithm. 987 * 988 * @param { Object } message - message Data to be sent to the worker 989 * @param { ArrayBuffer[] } transfer - transfer ArrayBuffer instance that can be transferred. 990 * The transferList array cannot contain null. 991 * @throws { BusinessError } 401 - if the input parameters are invalid. 992 * @throws { BusinessError } 10200004 - Worker instance is not running. 993 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 994 * @syscap SystemCapability.Utils.Lang 995 * @since 9 996 */ 997 /** 998 * Sends a message to the worker thread. 999 * The data is transferred using the structured clone algorithm. 1000 * 1001 * @param { Object } message - message Data to be sent to the worker 1002 * @param { ArrayBuffer[] } transfer - transfer ArrayBuffer instance that can be transferred. 1003 * The transferList array cannot contain null. 1004 * @throws { BusinessError } 401 - if the input parameters are invalid. 1005 * @throws { BusinessError } 10200004 - Worker instance is not running. 1006 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1007 * @syscap SystemCapability.Utils.Lang 1008 * @crossplatform 1009 * @since 10 1010 */ 1011 postMessage(message: Object, transfer: ArrayBuffer[]): void; 1012 /** 1013 * Sends a message to the worker thread. 1014 * The data is transferred using the structured clone algorithm. 1015 * 1016 * @param { Object } message - message Data to be sent to the worker 1017 * @param { PostMessageOptions } options - options 1018 * @throws { BusinessError } 401 - if the input parameters are invalid. 1019 * @throws { BusinessError } 10200004 - Worker instance is not running. 1020 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1021 * @syscap SystemCapability.Utils.Lang 1022 * @since 9 1023 */ 1024 /** 1025 * Sends a message to the worker thread. 1026 * The data is transferred using the structured clone algorithm. 1027 * 1028 * @param { Object } message - message Data to be sent to the worker 1029 * @param { PostMessageOptions } options - options 1030 * @throws { BusinessError } 401 - if the input parameters are invalid. 1031 * @throws { BusinessError } 10200004 - Worker instance is not running. 1032 * @throws { BusinessError } 10200006 - An exception occurred during serialization. 1033 * @syscap SystemCapability.Utils.Lang 1034 * @crossplatform 1035 * @since 10 1036 */ 1037 postMessage(message: Object, options?: PostMessageOptions): void; 1038 /** 1039 * Adds an event listener to the worker. 1040 * 1041 * @param { string } type - type Adds an event listener to the worker. 1042 * @param { WorkerEventListener } listener - listener Callback to invoke when an event of the specified type occurs. 1043 * @throws { BusinessError } 401 - if the input parameters are invalid. 1044 * @throws { BusinessError } 10200004 - Worker instance is not running. 1045 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 1046 * @syscap SystemCapability.Utils.Lang 1047 * @since 9 1048 */ 1049 /** 1050 * Adds an event listener to the worker. 1051 * 1052 * @param { string } type - type Adds an event listener to the worker. 1053 * @param { WorkerEventListener } listener - listener Callback to invoke when an event of the specified type occurs. 1054 * @throws { BusinessError } 401 - if the input parameters are invalid. 1055 * @throws { BusinessError } 10200004 - Worker instance is not running. 1056 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 1057 * @syscap SystemCapability.Utils.Lang 1058 * @crossplatform 1059 * @since 10 1060 */ 1061 on(type: string, listener: WorkerEventListener): void; 1062 /** 1063 * Adds an event listener to the worker 1064 * and removes the event listener automatically after it is invoked once. 1065 * 1066 * @param { string } type - type Type of the event to listen for 1067 * @param { WorkerEventListener } listener - listener Callback to invoke when an event of the specified type occurs 1068 * @throws { BusinessError } 401 - if the input parameters are invalid. 1069 * @throws { BusinessError } 10200004 - Worker instance is not running. 1070 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 1071 * @syscap SystemCapability.Utils.Lang 1072 * @since 9 1073 */ 1074 /** 1075 * Adds an event listener to the worker 1076 * and removes the event listener automatically after it is invoked once. 1077 * 1078 * @param { string } type - type Type of the event to listen for 1079 * @param { WorkerEventListener } listener - listener Callback to invoke when an event of the specified type occurs 1080 * @throws { BusinessError } 401 - if the input parameters are invalid. 1081 * @throws { BusinessError } 10200004 - Worker instance is not running. 1082 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 1083 * @syscap SystemCapability.Utils.Lang 1084 * @crossplatform 1085 * @since 10 1086 */ 1087 once(type: string, listener: WorkerEventListener): void; 1088 /** 1089 * Removes an event listener to the worker. 1090 * 1091 * @param { string } type - type Type of the event for which the event listener is removed. 1092 * @param { WorkerEventListener } listener - listener Callback of the event listener to remove. 1093 * @throws { BusinessError } 401 - if the input parameters are invalid. 1094 * @throws { BusinessError } 10200004 - Worker instance is not running. 1095 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 1096 * @syscap SystemCapability.Utils.Lang 1097 * @since 9 1098 */ 1099 /** 1100 * Removes an event listener to the worker. 1101 * 1102 * @param { string } type - type Type of the event for which the event listener is removed. 1103 * @param { WorkerEventListener } listener - listener Callback of the event listener to remove. 1104 * @throws { BusinessError } 401 - if the input parameters are invalid. 1105 * @throws { BusinessError } 10200004 - Worker instance is not running. 1106 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 1107 * @syscap SystemCapability.Utils.Lang 1108 * @crossplatform 1109 * @since 10 1110 */ 1111 off(type: string, listener?: WorkerEventListener): void; 1112 /** 1113 * Terminates the worker thread to stop the worker from receiving messages 1114 * 1115 * @throws { BusinessError } 10200004 - Worker instance is not running. 1116 * @syscap SystemCapability.Utils.Lang 1117 * @since 9 1118 */ 1119 /** 1120 * Terminates the worker thread to stop the worker from receiving messages 1121 * 1122 * @throws { BusinessError } 10200004 - Worker instance is not running. 1123 * @syscap SystemCapability.Utils.Lang 1124 * @crossplatform 1125 * @since 10 1126 */ 1127 terminate(): void; 1128 /** 1129 * Adds an event listener to the worker. 1130 * 1131 * @param { string } type - type Type of the event to listen for. 1132 * @param { WorkerEventListener } listener Callback to invoke when an event of the specified type occurs. 1133 * @throws { BusinessError } 401 - if the input parameters are invalid. 1134 * @throws { BusinessError } 10200004 - Worker instance is not running. 1135 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 1136 * @syscap SystemCapability.Utils.Lang 1137 * @since 9 1138 */ 1139 /** 1140 * Adds an event listener to the worker. 1141 * 1142 * @param { string } type - type Type of the event to listen for. 1143 * @param { WorkerEventListener } listener Callback to invoke when an event of the specified type occurs. 1144 * @throws { BusinessError } 401 - if the input parameters are invalid. 1145 * @throws { BusinessError } 10200004 - Worker instance is not running. 1146 * @throws { BusinessError } 10200005 - The invoked API is not supported in workers. 1147 * @syscap SystemCapability.Utils.Lang 1148 * @crossplatform 1149 * @since 10 1150 */ 1151 addEventListener(type: string, listener: WorkerEventListener): void; 1152 /** 1153 * Handle the event defined for the worker. 1154 * 1155 * @param { Event } event - event Event to dispatch. 1156 * @returns { boolean } 1157 * @throws { BusinessError } 401 - if the input parameters are invalid. 1158 * @throws { BusinessError } 10200004 - Worker instance is not running. 1159 * @syscap SystemCapability.Utils.Lang 1160 * @since 9 1161 */ 1162 /** 1163 * Handle the event defined for the worker. 1164 * 1165 * @param { Event } event - event Event to dispatch. 1166 * @returns { boolean } 1167 * @throws { BusinessError } 401 - if the input parameters are invalid. 1168 * @throws { BusinessError } 10200004 - Worker instance is not running. 1169 * @syscap SystemCapability.Utils.Lang 1170 * @crossplatform 1171 * @since 10 1172 */ 1173 dispatchEvent(event: Event): boolean; 1174 /** 1175 * Remove an event defined for the worker. 1176 * 1177 * @param { string } type - type Type of the event for which the event listener is cancelled. 1178 * @param { WorkerEventListener } callback - callback Callback of the event listener to remove. 1179 * @throws { BusinessError } 401 - if the input parameters are invalid. 1180 * @throws { BusinessError } 10200004 - Worker instance is not running. 1181 * @syscap SystemCapability.Utils.Lang 1182 * @since 9 1183 */ 1184 /** 1185 * Remove an event defined for the worker. 1186 * 1187 * @param { string } type - type Type of the event for which the event listener is cancelled. 1188 * @param { WorkerEventListener } callback - callback Callback of the event listener to remove. 1189 * @throws { BusinessError } 401 - if the input parameters are invalid. 1190 * @throws { BusinessError } 10200004 - Worker instance is not running. 1191 * @syscap SystemCapability.Utils.Lang 1192 * @crossplatform 1193 * @since 10 1194 */ 1195 removeEventListener(type: string, callback?: WorkerEventListener): void; 1196 /** 1197 * Remove all event listeners for the worker. 1198 * 1199 * @throws { BusinessError } 10200004 - Worker instance is not running. 1200 * @syscap SystemCapability.Utils.Lang 1201 * @since 9 1202 */ 1203 /** 1204 * Remove all event listeners for the worker. 1205 * 1206 * @throws { BusinessError } 10200004 - Worker instance is not running. 1207 * @syscap SystemCapability.Utils.Lang 1208 * @crossplatform 1209 * @since 10 1210 */ 1211 removeAllListener(): void; 1212 } 1213 1214 /** 1215 * The Worker class contains all Worker functions. 1216 * 1217 * @syscap SystemCapability.Utils.Lang 1218 * @since 7 1219 * @deprecated since 9 1220 * @useinstead ohos.worker.ThreadWorker 1221 */ 1222 class Worker implements EventTarget { 1223 /** 1224 * Creates a worker instance 1225 * 1226 * @param { string } scriptURL - scriptURL URL of the script to be executed by the worker 1227 * @param { WorkerOptions } options - options Options that can be set for the worker 1228 * @syscap SystemCapability.Utils.Lang 1229 * @since 7 1230 * @deprecated since 9 1231 * @useinstead ohos.worker.ThreadWorker.constructor 1232 */ 1233 constructor(scriptURL: string, options?: WorkerOptions); 1234 1235 /** 1236 * The onexit attribute of the worker specifies the event handler to be called 1237 * when the worker exits. The handler is executed in the host thread. 1238 * 1239 * @syscap SystemCapability.Utils.Lang 1240 * @since 7 1241 * @deprecated since 9 1242 * @useinstead ohos.worker.ThreadWorker.onexit 1243 */ 1244 onexit?: (code: number) => void; 1245 1246 /** 1247 * The onerror attribute of the worker specifies the event handler to be called 1248 * when an exception occurs during worker execution. 1249 * The event handler is executed in the host thread. 1250 * 1251 * @syscap SystemCapability.Utils.Lang 1252 * @since 7 1253 * @deprecated since 9 1254 * @useinstead ohos.worker.ThreadWorker.onerror 1255 */ 1256 onerror?: (err: ErrorEvent) => void; 1257 1258 /** 1259 * The onmessage attribute of the worker specifies the event handler 1260 * to be called then the host thread receives a message created by itself 1261 * and sent by the worker through the parentPort.postMessage. 1262 * The event handler is executed in the host thread. 1263 * 1264 * @syscap SystemCapability.Utils.Lang 1265 * @since 7 1266 * @deprecated since 9 1267 * @useinstead ohos.worker.ThreadWorker.onmessage 1268 */ 1269 onmessage?: (event: MessageEvent) => void; 1270 1271 /** 1272 * The onmessage attribute of the worker specifies the event handler 1273 * when the worker receives a message that cannot be serialized. 1274 * The event handler is executed in the host thread. 1275 * 1276 * @syscap SystemCapability.Utils.Lang 1277 * @since 7 1278 * @deprecated since 9 1279 * @useinstead ohos.worker.ThreadWorker.onmessageerror 1280 */ 1281 onmessageerror?: (event: MessageEvent) => void; 1282 1283 /** 1284 * Sends a message to the worker thread. 1285 * The data is transferred using the structured clone algorithm. 1286 * 1287 * @param { Object } message - message - message Data to be sent to the worker 1288 * @param { ArrayBuffer[] } transfer - transfer ArrayBuffer instance that can be transferred. 1289 * The transferList array cannot contain null. 1290 * @syscap SystemCapability.Utils.Lang 1291 * @since 7 1292 * @deprecated since 9 1293 * @useinstead ohos.worker.ThreadWorker.postMessage 1294 */ 1295 postMessage(message: Object, transfer: ArrayBuffer[]): void; 1296 postMessage(message: Object, options?: PostMessageOptions): void; 1297 1298 /** 1299 * Adds an event listener to the worker. 1300 * 1301 * @param { string } type - type Adds an event listener to the worker. 1302 * @param { EventListener } listener - listener Callback to invoke when an event of the specified type occurs. 1303 * @syscap SystemCapability.Utils.Lang 1304 * @since 7 1305 * @deprecated since 9 1306 * @useinstead ohos.worker.ThreadWorker.on 1307 */ 1308 on(type: string, listener: EventListener): void; 1309 1310 /** 1311 * Adds an event listener to the worker 1312 * and removes the event listener automatically after it is invoked once. 1313 * 1314 * @param { string } type - type Type of the event to listen for 1315 * @param { EventListener } listener - listener Callback to invoke when an event of the specified type occurs 1316 * @syscap SystemCapability.Utils.Lang 1317 * @since 7 1318 * @deprecated since 9 1319 * @useinstead ohos.worker.ThreadWorker.once 1320 */ 1321 once(type: string, listener: EventListener): void; 1322 1323 /** 1324 * Removes an event listener to the worker. 1325 * 1326 * @param { string } type - type Type of the event for which the event listener is removed. 1327 * @param { EventListener } listener - listener Callback of the event listener to remove. 1328 * @syscap SystemCapability.Utils.Lang 1329 * @since 7 1330 * @deprecated since 9 1331 * @useinstead ohos.worker.ThreadWorker.off 1332 */ 1333 off(type: string, listener?: EventListener): void; 1334 1335 /** 1336 * Terminates the worker thread to stop the worker from receiving messages 1337 * 1338 * @syscap SystemCapability.Utils.Lang 1339 * @since 7 1340 * @deprecated since 9 1341 * @useinstead ohos.worker.ThreadWorker.terminate 1342 */ 1343 terminate(): void; 1344 } 1345 1346 /** 1347 * The object used by the worker thread to communicate with the host thread. 1348 * 1349 * @constant 1350 * @syscap SystemCapability.Utils.Lang 1351 * @since 7 1352 * @deprecated since 9 1353 * @useinstead ohos.worker.workerPort 1354 */ 1355 const parentPort: DedicatedWorkerGlobalScope; 1356 1357 /** 1358 * The object used by the worker thread to communicate with the host thread. 1359 * 1360 * @constant 1361 * @syscap SystemCapability.Utils.Lang 1362 * @since 9 1363 */ 1364 /** 1365 * The object used by the worker thread to communicate with the host thread. 1366 * 1367 * @constant 1368 * @syscap SystemCapability.Utils.Lang 1369 * @crossplatform 1370 * @since 10 1371 */ 1372 const workerPort: ThreadWorkerGlobalScope; 1373} 1374export default worker; 1375