• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (C) 2025 HiHope Open Source Organization.
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
16import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS';
17import {
18  JSpromiseTest27,
19  JSpromiseTest28,
20  JSpromiseTest29,
21} from 'library1/src/main/ets/components/JSProjectPromiseMethod';
22
23const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
24
25/**
26 * Defines the event handler to be called when the worker thread receives a message sent by the host thread.
27 * The event handler is executed in the worker thread.
28 *
29 * @param event message data
30 */
31workerPort.onmessage = async (event: MessageEvents) => {
32  const str: string = event.data;
33  console.log('str:' + str);
34  if (str === 'JSProjectPromiseTest1600') {
35    const p: number = await JSpromiseTest27;
36    workerPort.postMessage(p);
37  }
38
39  if (str == 'JSProjectPromiseTest1700') {
40    let p: number = await JSpromiseTest28;
41    workerPort.postMessage(p);
42    workerPort.close();
43  }
44
45  if (str == 'JSProjectPromiseTest1800') {
46    let p: number = await JSpromiseTest29;
47    workerPort.postMessage(p);
48    workerPort.close();
49  }
50};
51
52/**
53 * Defines the event handler to be called when the worker receives a message that cannot be deserialized.
54 * The event handler is executed in the worker thread.
55 *
56 * @param event message data
57 */
58workerPort.onmessageerror = (event: MessageEvents) => {
59};
60
61/**
62 * Defines the event handler to be called when an exception occurs during worker execution.
63 * The event handler is executed in the worker thread.
64 *
65 * @param event error message
66 */
67workerPort.onerror = (event: ErrorEvent) => {
68};