• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 { Log } from '../utils/Log'
17import type { IModeMap } from './IModeMap'
18import type { FunctionId } from './FunctionId'
19
20export class ModeAssembler {
21  private TAG: string = '[ModeAssembler]:'
22  private mNeedAdd: FunctionId[]
23  private mNeedDelete: FunctionId[]
24  private mMode: IModeMap
25  private mFunctionsMap: Map<FunctionId, any>
26
27  constructor(functionsMap: Map<FunctionId, any>, modeMap: IModeMap) {
28    this.mFunctionsMap = functionsMap
29    this.mMode = modeMap
30  }
31  public assembler(preMode: string, currentMode: string): void {
32    Log.info(`${this.TAG} assembler preMode = ${preMode}  currentMode = ${currentMode} E `)
33    this.mNeedAdd = []
34    this.mNeedDelete = []
35    Log.info(`${this.TAG} assembler preMode = ${this.mMode.getFunctions(preMode)}  currentMode = ${this.mMode.getFunctions(currentMode)} E `)
36
37    let preModeFun: FunctionId[] = this.mMode.getFunctions(preMode)
38    let currentModeFun: FunctionId[] = this.mMode.getFunctions(currentMode)
39
40    Log.info(`${this.TAG} assembler preModeFun = ${preModeFun}  currentModeFun = ${currentModeFun}  `)
41    if (!preMode) {
42      this.mNeedAdd = JSON.parse(JSON.stringify(currentModeFun))
43    } else if (preMode != currentMode) {
44      this.mNeedAdd = JSON.parse(JSON.stringify(currentModeFun))
45      for (let fun of preModeFun) {
46        let index = currentModeFun.indexOf(fun)
47          if (index == -1) {
48            this.mNeedDelete.push(fun)
49          } else {
50            this.mNeedAdd.splice(index, 1)
51          }
52      }
53    }
54    Log.info(`${this.TAG} assembler mNeedAdd = ${this.mNeedAdd}  mNeedDelete = ${this.mNeedDelete}`)
55    this.attachFunction(this.mNeedAdd)
56    this.detachFunction(this.mNeedDelete)
57    Log.info(`${this.TAG} assembler X`)
58  }
59
60  private attachFunction(item: FunctionId[]): void {
61    for (let fun of item) {
62      Log.info(`${this.TAG} attachFunction fun: ${fun}`)
63      this.mFunctionsMap.get(fun).load()
64    }
65  }
66
67  private detachFunction(item: FunctionId[]): void {
68    for (let fun of item) {
69      Log.info(`${this.TAG} disattachFunction fun: ${fun}`)
70      this.mFunctionsMap.get(fun).unload()
71    }
72  }
73}