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 // Function overloading. 53 public makeBinding<T>(getter: () => T): Binding<T>; 54 public makeBinding<T>(getter: () => T, setter: (newValue: T) => void): MutableBinding<T>; 55 public makeBinding<T>(getter: () => T, setter?: 56 (newValue: T) => void): Binding<T> | MutableBinding<T> { 57 return setter ? new MutableBinding(getter, setter) : new Binding(getter); 58 } 59 60 public addMonitor(target: object, path: string | string[], monitorFunc: MonitorCallback, options?: MonitorOptions): void { 61 if (!target || typeof target !== 'object' || !(ObserveV2.IsObservedObjectV2(target) || target instanceof ViewV2)) { 62 const message = `addMonitor failed because the target is illegal. The target must be an instance of @ObservedV2 (with at least one @Trace inside) or @ComponentV2.`; 63 stateMgmtConsole.applicationError(message); 64 throw new BusinessError(ADD_MONITOR_FAIL_TARGET_ILLEGAL, message); 65 } 66 if (!(typeof path === 'string' || Array.isArray(path))) { 67 const message = `addMonitor failed because path must be string or Array of string.`; 68 stateMgmtConsole.applicationError(message); 69 throw new BusinessError(ADD_MONITOR_FAIL_PATH_ILLEGAL, message); 70 } 71 72 if (typeof monitorFunc !== 'function' || !monitorFunc.name) { 73 const message = `addMonitor failed because the monitorFunc is illegal, monitorFunc must be function or but cannot be an anonymous function.`; 74 stateMgmtConsole.applicationError(message); 75 throw new BusinessError(ADD_MONITOR_FAIL_FUNC_ILLEGAL, message); 76 } 77 ObserveV2.getObserve().AddMonitorPath(target, path, monitorFunc, options); 78 } 79 80 public clearMonitor(target: object, path: string | string[], monitorFunc: MonitorCallback): void { 81 if (!target || typeof target !== 'object' || !(ObserveV2.IsObservedObjectV2(target) || target instanceof ViewV2)) { 82 const message = `clearMonitor failed because the target is illegal. The target must be an instance of @ObservedV2 (with at least one @Trace inside) or @ComponentV2.`; 83 stateMgmtConsole.applicationError(message); 84 throw new BusinessError(ADD_MONITOR_FAIL_TARGET_ILLEGAL, message); 85 } 86 if (!(typeof path === 'string' || Array.isArray(path))) { 87 const message = `clearMonitor failed because path must be string or Array of string.`; 88 stateMgmtConsole.applicationError(message); 89 throw new BusinessError(ADD_MONITOR_FAIL_PATH_ILLEGAL, message); 90 } 91 92 if (monitorFunc && (typeof monitorFunc !== 'function' || !monitorFunc.name)) { 93 const message = `clearMonitor failed because the monitorFunc is illegal, monitorFunc must be function or but cannot be an anonymous function.`; 94 stateMgmtConsole.applicationError(message); 95 throw new BusinessError(ADD_MONITOR_FAIL_FUNC_ILLEGAL, message); 96 } 97 ObserveV2.getObserve().clearMonitorPath(target, path, monitorFunc); 98 } 99 100 public static instance(): UIUtilsImpl { 101 if (UIUtilsImpl.instance_) { 102 return UIUtilsImpl.instance_; 103 } 104 UIUtilsImpl.instance_ = new UIUtilsImpl(); 105 return UIUtilsImpl.instance_; 106 } 107}