• 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
16import worker, { ThreadWorkerGlobalScope, MessageEvents, ErrorEvent } from '@ohos.worker';
17import { HiLog } from "../../../../../../common";
18import WorkFactory from "../WorkFactory"
19
20var workerPort: ThreadWorkerGlobalScope = worker.workerPort;
21const TAG = "Worker";
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 e message data
27 */
28workerPort.onmessage = function (e: MessageEvents) {
29    HiLog.w(TAG, "onmessage");
30    if (e.data) {
31        let task = WorkFactory.getTask(workerPort, e)
32        task?.onmessage(e);
33    } else {
34        HiLog.w(TAG, `onmessage ${JSON.stringify(e)} not allow`)
35    }
36}
37
38
39/**
40 * Defines the event handler to be called when the worker receives a message that cannot be deserialized.
41 * The event handler is executed in the worker thread.
42 *
43 * @param e message data
44 */
45workerPort.onmessageerror = function (e: MessageEvents) {
46    HiLog.w(TAG, "onmessageerror" + JSON.stringify(e));
47}
48
49/**
50 * Defines the event handler to be called when an exception occurs during worker execution.
51 * The event handler is executed in the worker thread.
52 *
53 * @param e error message
54 */
55workerPort.onerror = function (e: ErrorEvent) {
56    HiLog.w(TAG, "onerror" + JSON.stringify(e));
57}