1/* 2 * Copyright (c) 2021-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 16// A mock up 17interface CustomComponent {} 18class ArkComponent { 19 width(value: string): ArkComponent { 20 console.log("setting width 100%") 21 return this 22 } 23 height(value: string): ArkComponent { 24 console.log("setting width 100%") 25 return this 26 } 27 color(value: string): ArkComponent { 28 console.log("setting width 100%") 29 return this 30 } 31} 32 33// A mock up 34function remember<Value>(exec: () => Value): Value { 35 return exec() 36} 37 38// A mock up 39export class ArkStructCommon extends ArkComponent implements CustomComponent { 40} 41 42export abstract class ArkCommonStruct0<T extends ArkCommonStruct0<T>> extends ArkStructCommon { 43 /** @memo */ 44 static _instantiate<S extends ArkCommonStruct0<S>>( 45 builder: ((instance: S) => void) | undefined, 46 factory: () => S, 47 initializers?: S 48 ): S { 49 const receiver = remember(() => { 50 const instance = factory() 51 instance.__initializeStruct(initializers) 52 return instance; 53 }) 54 55 receiver._buildWrapper(builder, initializers) 56 return receiver 57 } 58 protected __initializeStruct(initializers: T | undefined): void {} 59 60 /** @memo */ 61 protected __updateStruct(initializers: T | undefined): void { } 62 63 /** @memo */ 64 _buildWrapper( 65 builder: ((instance: T) => void) | undefined, 66 initializers: T | undefined 67 ): void { 68 this.__updateStruct(initializers) 69 this.build(builder) 70 } 71 72 /** @memo */ 73 build(builder: ((instance: T) => void) | undefined): void { 74 throw new Error("Called common build()") 75 } 76} 77 78// A mock up 79class Column extends ArkCommonStruct0<Column> { 80 build() { 81 console.log("Running Column build()") 82 } 83} 84 85class ArkMyStructComponent extends ArkCommonStruct0<ArkMyStructComponent> { 86 __initializeStruct(initializers: ArkMyStructComponent | undefined): void { 87 console.log("initializing MyStruct") 88 } 89 /** @memo */ 90 build(builder: ((instance: ArkMyStructComponent) => void) | undefined) { 91 console.log("Running MyStruct build") 92 Column._instantiate( 93 instance => instance 94 .width('100%') 95 .height('50%'), 96 () => new Column(), 97 undefined, 98 ) 99 } 100} 101 102ArkMyStructComponent._instantiate( 103 undefined, 104 () => new ArkMyStructComponent(), 105 {} 106) 107