• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 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
16/**
17 * ConfigureStateMgmt keeps track if V2 @ObservedV2 and @Trace are used.
18 * If yes, it enables object deep observation mechanisms need with @ObservedV2.
19 */
20class ConfigureStateMgmt {
21
22    private static readonly HOW_TO_SAY = `Your application uses both state management V1 and V2 features! - It is strongly recommended not to mix V1 and V2. Consult the rules how state management V1 and V2 can be mixed in the same app.`;
23
24    private static instance__?: ConfigureStateMgmt;
25
26    private v2ObservedTrackInUse_: boolean = false;
27    private puObservedTrackInUse_: boolean = false;
28
29    public static get instance(): ConfigureStateMgmt {
30        return ConfigureStateMgmt.instance__
31            ? ConfigureStateMgmt.instance__
32            : (ConfigureStateMgmt.instance__ = new ConfigureStateMgmt());
33    }
34
35    /**
36     * framework code call this function when it sees use of a stateMgmt V2 @ObservedV2 @Trace
37     *
38     * @param feature specify feature separately from context of use, so that in future decision can be made
39     *                for individual features, not use permit either use of V1 or V2.
40     * @param contextOfUse purely for error messages. Give enough info that use is able to local the feature use in source code.
41     * @returns true if no mix of features detected, false if mix is detected
42     */
43    public usingV2ObservedTrack(feature: string, contextOfUse: string = ''): void {
44        this.v2ObservedTrackInUse_ = true;
45        stateMgmtConsole.debug(`ConfigureStateMgmt: Found use of stateMgmt V2 feature ${feature} ${contextOfUse} - enable V2 state observation.`);
46    }
47
48    /**
49 * framework code call this function when it sees use of a stateMgmt PU Observed / @Track
50 *
51 * @param feature specify feature separately from context of use, so that in future decision can be made
52 *                for individual features, not use permit either use of V1 or V2.
53 * @param contextOfUse purely for error messages. Give enough info that use is able to local the feature use in source code.
54 * @returns true if no mix of features detected, false if mix is detected
55 */
56    public usingPUObservedTrack(feature: string, contextOfUse: string = ''): void {
57        this.puObservedTrackInUse_ = true;
58        stateMgmtConsole.debug(`ConfigureStateMgmt: Found use of stateMgmt PU feature ${feature} ${contextOfUse} - enable PU state observation.`);
59    }
60
61    /**
62      * Return true if object deep observation mechanisms need to be enabled
63      * that is when seen V2 @ObservedV2, @Trace, or @Monitor decorator used in at least one class
64      * (we could but we do not check for class object instance creation for performance reasons)
65      * @returns
66      */
67    public needsV2Observe(): boolean {
68        return this.v2ObservedTrackInUse_;
69    }
70} // ConfigureStateMgmt