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 from '@ohos.worker'; 17const parentPort = worker.workerPort; 18 19console.info("worker:: new version"); 20 21parentPort.onmessage = function(e) { 22 let data = e.data; 23 console.info("worker:: worker thread worker data is " + data.data); 24 switch(data.type) { 25 case "normal": 26 console.info("worker:: worker thread receive data " + data.data); 27 parentPort.postMessage(data); 28 console.info("worker:: worker thread post back"); 29 break; 30 case "error": 31 throw new Error("123"); 32 case "buffer": 33 console.info("worker:: worker.js receive buffer length is " + data.data.byteLength); 34 parentPort.postMessage(data, [data.data]); 35 console.info("worker:: worker.js post buffer length is " + data.data.byteLength); 36 break; 37 default: 38 console.info("worker:: worker.js receive unknow type"); 39 break 40 } 41} 42 43// Deserialization error 44parentPort.onmessageerror = function() { 45 console.info("worker:: worker.js onmessageerror"); 46} 47 48// js execution error 49parentPort.onerror = function(data) { 50 console.info("worker:: worker.js onerror " + data.lineno + ", msg = " + data.message + ", filename = " + data.filename + ", colno = " + data.colno); 51}