1/* 2 * Copyright (c) 2022 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 * * ViewPU - View for Partial Update 16 * 17* all definitions in this file are framework internal 18*/ 19 20/** 21 given parameters for calling a @Builder function 22 this function wraps the Object of type T inside a ES6 Proxy. 23 Each param, i.e. Object property is either a function or a value. 24 If it is a function the function can either return a value of expected 25 parameter type or an ObservedPropertyabstract<T> where T is the exected 26 parameter type. The latter is the case when passing a state variable by 27 reference. 28 29 Two purposes: 30 1 - @Builder function boxy accesses params a '$$.paramA' 31 However paramA can be a function, so to obtain the value the 32 access would need to be '$$.param()' The proxy executes 33 the function and return s the result 34 2 - said function returns to ObservedPropertyAbstract backing store of 35 a calling @Component state variable (whenever the state var is 36 provided to the @Builder function). For this case the proxy can provide 37 - the value by executing paramA() to return the ObservedPropertyAbstract 38 and further (monitored!) get() to read its value 39 - when requested to return '__param1' it returns the ObservedPropertyAbstract 40 object. The scenario is to use to init a @Link source. 41 */ 42function makeBuilderParameterProxy(builderName: string, source: Object): Object { 43 return new Proxy(source, { 44 set(target, prop, val) { 45 throw Error(`@Builder '${builderName}': Invalid attempt to set(write to) parameter '${prop.toString()}' error!`); 46 }, 47 get(target, prop) { 48 const prop1 = prop.toString().trim().startsWith("__") 49 ? prop.toString().trim().substring(2) 50 : prop.toString().trim(); 51 stateMgmtConsole.debug(`get - prop ${prop.toString()} prop1 ${prop1}`); 52 if (!(typeof target === "object") && (prop1 in target)) { 53 throw Error(`@Builder '${builderName}': '${prop1}' used but not a function parameter error!`); 54 } 55 const value = target[prop1]; 56 if (typeof value !== "function") { 57 stateMgmtConsole.debug(` - no fun`); 58 return value; 59 } 60 const funcRet = value(); 61 if ((typeof funcRet === "object") && ('get' in funcRet)) { 62 if (prop1 !== prop) { 63 stateMgmtConsole.debug(` - func - is ObservedPropertybstract - ret ObservedPropertyObject`); 64 return funcRet; 65 } else { 66 stateMgmtConsole.debug(` - func - is ObservedPropertybstract - ret get()`); 67 const result = funcRet.get(); 68 stateMgmtConsole.debug(` - returns ${result}`); 69 return result; 70 } 71 } 72 73 stateMgmtConsole.debug(` - func - no ObservedPropertybstract - ret value ${funcRet}`); 74 return funcRet; 75 } // get 76 }); // new Proxy 77} 78