1/* 2 * Copyright (c) 2024 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 16class UIUtilsImpl { 17 private static instance_: UIUtilsImpl = undefined; 18 19 public getTarget<T extends Object>(source: T): T { 20 if (!source || typeof source !== 'object') { 21 return source; 22 } 23 if (ObservedObject.IsObservedObject(source)) { 24 // V1 Proxy object 25 return ObservedObject.GetRawObject(source); 26 } else if (source[ObserveV2.SYMBOL_PROXY_GET_TARGET]) { 27 // V2 Proxy object 28 return source[ObserveV2.SYMBOL_PROXY_GET_TARGET]; 29 } else { 30 // other situation, not handle 31 return source; 32 } 33 } 34 35 public makeObserved<T extends object>(target: T): T { 36 // mark makeObserved using V2 feature 37 ConfigureStateMgmt.instance.usingV2ObservedTrack('makeObserved', 'use'); 38 return RefInfo.get(target)[RefInfo.MAKE_OBSERVED_PROXY] as T; 39 } 40 41 public makeV1Observed<T extends object>(target: T): T { 42 // Make non-observed data into observed V1 data 43 return ObservedObject.makeV1Observed(target); 44 } 45 46 public enableV2Compatibility<T extends object>(target: T): T { 47 // Enables V2 compatibility for the given object and all its nested objects 48 ObservedObject.enableV2Compatible(target); 49 return target; 50 } 51 52 public static instance(): UIUtilsImpl { 53 if (UIUtilsImpl.instance_) { 54 return UIUtilsImpl.instance_; 55 } 56 UIUtilsImpl.instance_ = new UIUtilsImpl(); 57 return UIUtilsImpl.instance_; 58 } 59}