1/** 2 * Copyright (c) 2024 SwanLink (Jiangsu) Technology Development 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 16import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; 17import { TestCallObject } from '../test/utils/workerCommon'; 18 19const workerPort: ThreadWorkerGlobalScope = worker.workerPort; 20 21/** 22 * Defines the event handler to be called when the worker thread receives a message sent by the host thread. 23 * The event handler is executed in the worker thread. 24 * 25 * @param e message data 26 */ 27workerPort.onmessage = (e: MessageEvents) => { 28 let data = e.data as TestCallObject; 29 let res = false; 30 let value: string; 31 try { 32 if (data.str) { 33 value = 34 workerPort.callGlobalCallObjectMethod(data.instanceName, data.methodName, data.timeout, data.str) as string; 35 } else { 36 value = workerPort.callGlobalCallObjectMethod(data.instanceName, data.methodName, data.timeout) as string; 37 } 38 res = value == data.methodName; 39 } catch (error) { 40 console.error('worker: error code is ' + error.code + ' error message is ' + error.message); 41 res = false; 42 } 43 workerPort.postMessage(res); 44} 45 46/** 47 * Defines the event handler to be called when the worker receives a message that cannot be deserialized. 48 * The event handler is executed in the worker thread. 49 * 50 * @param e message data 51 */ 52workerPort.onmessageerror = (e: MessageEvents) => { 53} 54 55/** 56 * Defines the event handler to be called when an exception occurs during worker execution. 57 * The event handler is executed in the worker thread. 58 * 59 * @param e error message 60 */ 61workerPort.onerror = (e: ErrorEvent) => { 62}