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