• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 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
16async function concurrentFunc(startup, asyncCallback, context, startupName): void {
17  'use concurrent';
18  console.log('concurrentFunc start.');
19  let startupResult;
20  await startup.init(context).then((result: Object) => {
21    startupResult = result;
22    let taskPool = requireNapi('taskpool');
23    taskPool.Task.sendData(asyncCallback, startupName, startupResult);
24  });
25  console.log('concurrentFunc end.');
26}
27
28function receiveResult(asyncCallback, startupName, startupResult): void {
29  console.log('receiveResult called.');
30  asyncCallback.onAsyncTaskCompleted(startupName, startupResult);
31  console.log('receiveResult end.');
32}
33
34function pushTask(startup, asyncCallback, context, startupName) {
35  console.log('pushTask start.');
36  let taskPool = requireNapi('taskpool');
37  try {
38    let task = new taskPool.Task(concurrentFunc, startup, asyncCallback, context, startupName);
39    task.onReceiveData(receiveResult);
40    taskPool.execute(task);
41  } catch (error) {
42    console.log('new taskPool failed message:' + error);
43  }
44  console.log('pushTask end.');
45}
46
47class AsyncTaskExcutor {
48  public asyncPushTask(startup, asyncCallback, context, startupName) {
49    console.log('asyncPushTask AsyncPushTask start.');
50    pushTask(startup, asyncCallback, context, startupName);
51    console.log('asyncPushTask AsyncPushTask end.');
52  }
53}
54
55export default AsyncTaskExcutor;