• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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
16/**
17 * JS cross-thread task executor.
18 * @since 9
19 * @syscap SystemCapability.Utils.Lang
20 */
21 declare namespace taskpool {
22  /**
23   * The Priority defines the task priority.
24   * @since 9
25   * @syscap SystemCapability.Utils.Lang
26   */
27  enum Priority {
28    HIGH,
29    MEDIUM,
30    LOW
31  }
32
33  /**
34   * The Task class provides an interface to create a task.
35   * @since 9
36   * @syscap SystemCapability.Utils.Lang
37   */
38  class Task {
39    /**
40     * Create a Task instance.
41     * @param func Concurrent function to execute in taskpool.
42     * @param args The concurrent function arguments.
43     * @throws {BusinessError} 401 - if the input parameters are invalid.
44     * @throws {BusinessError} 10200014 - if the function is not mark as concurrent.
45     * @since 9
46     * @syscap SystemCapability.Utils.Lang
47     */
48    constructor(func: Function, ...args: unknown[]);
49
50    /**
51     * Concurrent function to execute in taskpool.
52     */
53    function: Function;
54
55    /**
56     * The concurrent function arguments.
57     */
58    arguments?: unknown[];
59  }
60
61  /**
62   * Execute a concurrent function.
63   * @param func Concurrent function want to execute.
64   * @param args The concurrent function arguments.
65   * @throws {BusinessError} 401 - if the input parameters are invalid.
66   * @throws {BusinessError} 10200003 - Worker initialization failure.
67   * @throws {BusinessError} 10200006 - Serializing an uncaught exception failed.
68   * @throws {BusinessError} 10200014 - if the function is not mark as concurrent.
69   * @since 9
70   * @syscap SystemCapability.Utils.Lang
71   */
72  function execute(func: Function, ...args: unknown[]): Promise<unknown>;
73
74  /**
75   * Execute a concurrent task.
76   * @param task The task want to execute.
77   * @param priority Task priority, MEDIUM is default.
78   * @throws {BusinessError} 401 - if the input parameters are invalid.
79   * @throws {BusinessError} 10200003 - Worker initialization failure.
80   * @throws {BusinessError} 10200006 - Serializing an uncaught exception failed.
81   * @throws {BusinessError} 10200014 - if the function in task is not mark as concurrent.
82   * @since 9
83   * @syscap SystemCapability.Utils.Lang
84   */
85  function execute(task: Task, priority?: Priority): Promise<unknown>;
86
87  /**
88   * Cancel a concurrent task.
89   * @param task The task want to cancel.
90   * @throws {BusinessError} 401 - if the input parameters are invalid.
91   * @throws {BusinessError} 10200015 - if the task is not exist.
92   * @throws {BusinessError} 10200016 - if the task is running.
93   * @since 9
94   * @syscap SystemCapability.Utils.Lang
95   */
96  function cancel(task: Task): void;
97}
98
99export default taskpool;
100