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*/ 15import { ArkTSUtils, ErrorEvent, MessageEvents, taskpool, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; 16import { lock, tasklock, tasklockrequest, timelockshared } from '../ConcurrencyModularImportAsyncLockTest'; 17import { numberPromise, stringPromise } from '../ConcurrencyModularImportPromiseTest'; 18import { longTask, task } from '../ConcurrencyModularImportTaskTest'; 19 20const workerPort: ThreadWorkerGlobalScope = worker.workerPort; 21 22/** 23 * Defines the event handler to be called when the worker thread receives a message sent by the host thread. 24 * The event handler is executed in the worker thread. 25 * 26 * @param event message data 27 */ 28workerPort.onmessage = async (event: MessageEvents) => { 29 let str: number|string = event.data; 30 console.log('str:' + str); 31 if (str === 1) { 32 const numberResult = await numberPromise; 33 console.log('String result:', numberResult); 34 workerPort.postMessage(numberResult); 35 workerPort.close(); 36 } 37 if (str === 2) { 38 const result = await stringPromise; 39 console.log('String result:', result); 40 workerPort.postMessage(result); 41 workerPort.close(); 42 } 43 if (str === 3) { 44 await taskpool.execute(task).catch((e: Error) => { 45 workerPort.postMessage(42); 46 }) 47 workerPort.close(); 48 } 49 if (str === 4) { 50 await lock.lockAsync(async () => { 51 const p = await tasklock(); 52 workerPort.postMessage(p); 53 }, ArkTSUtils.locks.AsyncLockMode.SHARED); 54 workerPort.close(); 55 } 56 if (str === 5) { 57 await lock.lockAsync(async () => { 58 await taskpool.execute(task).catch(() => { 59 workerPort.postMessage(5); 60 }) 61 }, ArkTSUtils.locks.AsyncLockMode.EXCLUSIVE); 62 workerPort.close(); 63 } 64 if (str === 6) { 65 await lock.lockAsync(async () => { 66 const p = await timelockshared(100); 67 workerPort.postMessage(p); 68 }, ArkTSUtils.locks.AsyncLockMode.SHARED); 69 workerPort.close(); 70 } 71 72 if (str === 7) { 73 setTimeout(async () => { 74 await lock.lockAsync(async () => { 75 const p = await timelockshared(100); 76 workerPort.postMessage(p); 77 }, ArkTSUtils.locks.AsyncLockMode.SHARED); 78 workerPort.close(); 79 }, 100); 80 } 81 if (str === 8) { 82 await lock.lockAsync(async () => { 83 const p = await tasklockrequest(); 84 workerPort.postMessage(p); 85 }, ArkTSUtils.locks.AsyncLockMode.SHARED); 86 workerPort.close(); 87 } 88 if (str === 9) { 89 setTimeout(async () => { 90 await lock.lockAsync(async () => { 91 await taskpool.execute(task).catch((e: Error) => { 92 workerPort.postMessage(42); 93 }) 94 }, ArkTSUtils.locks.AsyncLockMode.EXCLUSIVE); 95 workerPort.close(); 96 }, 100); 97 } 98 if (str == 'ConcurrencyModularImportAndExportTest2900') { 99 let count: number = 42; 100 console.log('count1:' + count); 101 workerPort.postMessage(count); 102 workerPort.close(); 103 } 104}; 105/** 106 * Defines the event handler to be called when the worker receives a message that cannot be deserialized. 107 * The event handler is executed in the worker thread. 108 * 109 * @param event message data 110 */ 111workerPort.onmessageerror = (event: MessageEvents) => { 112}; 113 114/** 115 * Defines the event handler to be called when an exception occurs during worker execution. 116 * The event handler is executed in the worker thread. 117 * 118 * @param event error message 119 */ 120workerPort.onerror = (event: ErrorEvent) => { 121};