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 { ProcedureLogicWorkerPerf } from './ProcedureLogicWorkerPerf';
17import { ProcedureLogicWorkerNativeMemory } from './ProcedureLogicWorkerNativeNemory';
18import { ProcedureLogicWorkerFileSystem } from './ProcedureLogicWorkerFileSystem';
19import { ProcedureLogicWorkerSPT } from './ProcedureLogicWorkerSPT';
20import { ProcedureLogicWorkerCpuState } from './ProcedureLogicWorkerCpuState';
21import { ProcedureLogicWorkerSchedulingAnalysis } from './ProcedureLogicWorkerSchedulingAnalysis';
22import { DataCache } from './ProcedureLogicWorkerCommon';
23import { ProcedureLogicWorkerJsCpuProfiler } from './ProcedureLogicWorkerJsCpuProfiler';
24
25let logicWorker = {
26  perf: new ProcedureLogicWorkerPerf(),
27  'native-memory': new ProcedureLogicWorkerNativeMemory(),
28  fileSystem: new ProcedureLogicWorkerFileSystem(),
29  CpuState: new ProcedureLogicWorkerCpuState(),
30  spt: new ProcedureLogicWorkerSPT(),
31  scheduling: new ProcedureLogicWorkerSchedulingAnalysis(),
32  jsCpuProfile: new ProcedureLogicWorkerJsCpuProfiler(),
33};
34
35function match(req: { id: string; type: string; params: { dataDict: Map<number, string> } }): void {
36  if (req.type === 'clear') {
37    //@ts-ignore
38    Reflect.ownKeys(logicWorker).forEach((key) => logicWorker[key].clearAll());
39    DataCache.getInstance().clearAll();
40    return;
41  }
42  if (req.type === 'cache-data-dict') {
43    DataCache.getInstance().dataDict = req.params.dataDict;
44    self.postMessage({
45      id: req.id,
46      action: req.type,
47      results: 'ok',
48    });
49    return;
50  }
51  Reflect.ownKeys(logicWorker).filter((it) => {
52    if (req.type && req.type.startsWith(it as string)) {
53      //@ts-ignore
54      logicWorker[it].handle(req);
55    }
56  });
57}
58
59self.onmessage = function (e: unknown): void {
60  //@ts-ignore
61  match(e.data);
62};
63