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).proxy as T; 39 } 40 41 public static instance(): UIUtilsImpl { 42 if (UIUtilsImpl.instance_) { 43 return UIUtilsImpl.instance_; 44 } 45 UIUtilsImpl.instance_ = new UIUtilsImpl(); 46 return UIUtilsImpl.instance_; 47 } 48}