• 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*/
15import { collections, MessageEvents, taskpool, worker } from '@kit.ArkTS';
16import { sleep } from './Ulits';
17import { fetchData, SendableClass2, Sendablefun } from './ConcurrencyModularImportPromiseTest';
18
19@Concurrent
20function taskFunction(): void {
21  console.info('Task is executing');
22  throw new Error('error from task');
23}
24
25export let task: taskpool.Task = new taskpool.Task(taskFunction);
26
27@Concurrent
28function additionDelay(delay: number): void {
29  let start: number = new Date().getTime();
30  while (new Date().getTime() - start < delay) {
31    continue;
32  }
33}
34
35export let taskadd: taskpool.Task = new taskpool.Task(additionDelay, 100);
36
37@Concurrent
38export async function taskFunction2(input: string) {
39  console.info('Task is executing');
40  try {
41    const data = await fetchData(input);
42    let result: collections.ConcatArray<string> = new collections.Array<string>(data);
43    return result;
44  } catch (error) {
45    console.error('Error in taskFunction:', error);
46    throw new Error('error from task');
47  }
48}
49
50export const task2: taskpool.Task = new taskpool.Task(taskFunction2, '1');
51
52
53@Concurrent
54async function taskFunction3(input: SendableClass2): Promise<SendableClass2> {
55  return new Promise((resolve, reject) => {
56    setTimeout(() => {
57      try {
58        console.info('Task received input (delayed):', input.getValue());
59        const result = new SendableClass2(`Processed: ${input.getValue()}`);
60        console.info('Task will return (delayed):', result.getValue());
61        resolve(result);
62      } catch (error) {
63        console.error('Error in setTimeout:', error);
64        reject(error);
65      }
66    }, 0);
67  });
68}
69
70export const task3: taskpool.Task = new taskpool.Task(taskFunction3, '1');
71
72@Concurrent
73async function taskFunction4(arg: number): Promise<number> {
74  return arg;
75}
76
77export const longTask = new taskpool.Task(taskFunction4, 1);
78
79@Concurrent
80async function taskWorker(arg: number): Promise<Object> {
81  let count: number = 0;
82  let workThread: worker.ThreadWorker;
83  workThread = new worker.ThreadWorker('../components/workers/Worker');
84  workThread.onexit = () => {
85    console.log('onexit');
86  }
87  workThread.postMessage(1);
88  workThread.onmessage = (e: MessageEvents): void => {
89    count = e.data;
90    workThread.terminate();
91  }
92  while (count != 42) {
93    await sleep(300)
94  }
95  return arg;
96}
97
98export const workertask = new taskpool.Task(taskWorker, 1);
99
100const sharedBuffer = new SharedArrayBuffer(16);
101
102export let int8Array: Int8Array = new Int8Array(sharedBuffer);
103
104export let uint8Array: Uint8Array = new Uint8Array(sharedBuffer);
105
106export let int16Array: Int16Array = new Int16Array(sharedBuffer);
107
108export let uint16Array: Uint16Array = new Uint16Array(sharedBuffer);
109
110export let int32Array: Int32Array = new Int32Array(sharedBuffer);
111
112export let uint32Array: Uint32Array = new Uint32Array(sharedBuffer);
113
114export let uint8ClampedArray: Uint8ClampedArray = new Uint8ClampedArray(sharedBuffer);
115
116export let float32Array: Float32Array = new Float32Array(sharedBuffer);
117
118export let bigInt64Array: BigInt64Array = new BigInt64Array(sharedBuffer);
119
120export let bigUint64Array: BigUint64Array = new BigUint64Array(sharedBuffer);
121