From a123e2859db5704ee5f92c0cbbb9c3ce484da37a Mon Sep 17 00:00:00 2001 From: vadimdolgachev Date: Wed, 3 Dec 2025 18:58:32 +0700 Subject: [PATCH] Added @State, @ObjectLink, @Watch, @Provide/@Consume, @PropRef --- .../src/component/extendableComponent.ets | 2 +- .../stateManagement/base/mutableStateMeta.ts | 2 +- .../base/observeWrappedDate.ts | 137 ++++---- .../stateManagement/base/stateUpdateLoop.ts | 19 +- .../arkui/src/stateManagement/decorator.ts | 14 +- .../decoratorImpl/decoratorProp.ts | 19 +- .../pages/watch/WatchProp.ets | 2 +- ui2abc/ui-plugins-ng/src/class-transformer.ts | 2 +- .../src/component-transformer.ts | 74 ++-- .../src/property-transformers.ts | 322 +++++++++++++----- 10 files changed, 392 insertions(+), 201 deletions(-) diff --git a/arkoala-arkts/arkui/src/component/extendableComponent.ets b/arkoala-arkts/arkui/src/component/extendableComponent.ets index 488206713..272dd532d 100644 --- a/arkoala-arkts/arkui/src/component/extendableComponent.ets +++ b/arkoala-arkts/arkui/src/component/extendableComponent.ets @@ -148,7 +148,7 @@ export abstract class ExtendableComponent extends ComponentBase implements LifeC addProvidedVar(provideAliasName: string, provider: IProvideDecoratedVariable, allowOverride: boolean): void { if (!allowOverride && (this.parent?.findProvide(provideAliasName) || this.providers?.has(provideAliasName))) { - throw new Error(`Use @Provide({allowOverride: "${provideAliasName}"}) for explicit override or choose another variable name`) + throw new Error(`Provide key "${provideAliasName}" was already used in the component. Use @Provide({allowOverride: true}) to explicitly override.`) } if (this.providers == undefined) { this.providers = new Map>() diff --git a/arkoala-arkts/arkui/src/stateManagement/base/mutableStateMeta.ts b/arkoala-arkts/arkui/src/stateManagement/base/mutableStateMeta.ts index 9c091ea95..406fdf193 100644 --- a/arkoala-arkts/arkui/src/stateManagement/base/mutableStateMeta.ts +++ b/arkoala-arkts/arkui/src/stateManagement/base/mutableStateMeta.ts @@ -66,7 +66,7 @@ export class MutableStateMeta extends MutableStateMetaBase implements IMutableSt this.bindingRefs_.add(ObserveSingleton.instance.renderingComponentRef!.weakThis); ObserveSingleton.instance.renderingComponentRef!.reverseBindings.add(this.weakThis); } else { - this.getMetaDependency()!.value; + this.getMetaDependency().value; } } diff --git a/arkoala-arkts/arkui/src/stateManagement/base/observeWrappedDate.ts b/arkoala-arkts/arkui/src/stateManagement/base/observeWrappedDate.ts index 6a16ee3fc..9e17f479a 100644 --- a/arkoala-arkts/arkui/src/stateManagement/base/observeWrappedDate.ts +++ b/arkoala-arkts/arkui/src/stateManagement/base/observeWrappedDate.ts @@ -20,7 +20,6 @@ import { FactoryInternal } from './iFactoryInternal'; import { ObserveWrappedBase } from './observeWrappedBase'; export class WrappedDate extends Date implements IObservedObject, ObserveWrappedBase, ISubscribedWatches { - private store_: Date; private meta_: IMutableStateMeta; // support for @Watch // each IObservedObject manages a set of @Wtch subscribers @@ -36,9 +35,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ constructor(date: Date) { // Create without parameters to avoid call back to WrappedMap before "this" is fully constructed! - super(); - - this.store_ = date; + super(date); this.meta_ = FactoryInternal.mkMutableStateMeta(''); } @@ -55,7 +52,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped } public getRaw(): Object { - return this.store_; + return this; } public setV1RenderId(renderId: RenderIdType): void { @@ -74,7 +71,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override isDateValid(): boolean { this.conditionalAddRef(); - return this.store_.isDateValid(); + return super.isDateValid(); } /** @@ -86,7 +83,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override valueOf(): long { this.conditionalAddRef(); - return this.store_.valueOf(); + return super.valueOf(); } /** @@ -96,7 +93,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override toLocaleTimeString(): string { this.conditionalAddRef(); - return this.store_.toLocaleTimeString(); + return super.toLocaleTimeString(); } /** @@ -108,7 +105,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override toLocaleDateString(): string { this.conditionalAddRef(); - return this.store_.toLocaleDateString(); + return super.toLocaleDateString(); } /** @@ -119,7 +116,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getDate(): int { this.conditionalAddRef(); - return this.store_.getDate(); + return super.getDate(); } /** @@ -128,7 +125,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new day. */ public override setDate(value: int): long { - const result = this.store_.setDate(value); + const result = super.setDate(value); this.meta_.fireChange(); this.executeOnSubscribingWatches('setDate'); return result; @@ -141,7 +138,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getUTCDate(): int { this.conditionalAddRef(); - return this.store_.getUTCDate(); + return super.getUTCDate(); } /** @@ -150,7 +147,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new day. */ public override setUTCDate(value: int): long { - const result = this.store_.setUTCDate(value); + const result = super.setUTCDate(value); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCDate'); return result; @@ -162,7 +159,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new day. */ public override setUTCDay(value: int): long { - const result = this.store_.setUTCDay(value); + const result = super.setUTCDay(value); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCDay'); return result; @@ -178,7 +175,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getDay(): int { this.conditionalAddRef(); - return this.store_.getDay(); + return super.getDay(); } /** @@ -189,7 +186,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getUTCDay(): int { this.conditionalAddRef(); - return this.store_.getUTCDay(); + return super.getUTCDay(); } /** @@ -201,7 +198,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getYear(): int { this.conditionalAddRef(); - return this.store_.getYear(); + return super.getYear(); } /** @@ -215,7 +212,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getUTCFullYear(): int { this.conditionalAddRef(); - return this.store_.getUTCFullYear(); + return super.getUTCFullYear(); } /** @@ -234,7 +231,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getFullYear(): int { this.conditionalAddRef(); - return this.store_.getFullYear(); + return super.getFullYear(); } /** @@ -243,7 +240,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new year */ public override setUTCFullYear(value: int): long { - const result = this.store_.setUTCFullYear(value); + const result = super.setUTCFullYear(value); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCFullYear'); return result; @@ -255,7 +252,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new year */ public override setUTCFullYear(value: int, month: int): long { - const result = this.store_.setUTCFullYear(value, month); + const result = super.setUTCFullYear(value, month); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCFullYear'); return result; @@ -267,7 +264,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new year */ public override setUTCFullYear(value: int, month: int, date: int): long { - const result = this.store_.setUTCFullYear(value, month, date); + const result = super.setUTCFullYear(value, month, date); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCFullYear'); return result; @@ -279,7 +276,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new year */ public override setYear(value: int): void { - this.store_.setYear(value); + super.setYear(value); this.meta_.fireChange(); this.executeOnSubscribingWatches('setYear'); } @@ -290,7 +287,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new year */ public override setFullYear(value: int): long { - const result = this.store_.setFullYear(value); + const result = super.setFullYear(value); this.meta_.fireChange(); this.executeOnSubscribingWatches('setFullYear'); return result; @@ -302,7 +299,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new year */ public override setFullYear(value: int, month: int): long { - const result = this.store_.setFullYear(value, month); + const result = super.setFullYear(value, month); this.meta_.fireChange(); this.executeOnSubscribingWatches('setFullYear'); return result; @@ -314,7 +311,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new year */ public override setFullYear(value: int, month: int, date: int): long { - const result = this.store_.setFullYear(value, month, date); + const result = super.setFullYear(value, month, date); this.meta_.fireChange(); this.executeOnSubscribingWatches('setFullYear'); return result; @@ -333,7 +330,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getHours(): int { this.conditionalAddRef(); - return this.store_.getHours(); + return super.getHours(); } /** @@ -343,7 +340,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getUTCHours(): int { this.conditionalAddRef(); - return this.store_.getUTCHours(); + return super.getUTCHours(); } /** @@ -352,7 +349,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new hours */ public override setHours(value: int): long { - const result = this.store_.setHours(value); + const result = super.setHours(value); this.meta_.fireChange(); this.executeOnSubscribingWatches('setHours'); return result; @@ -364,7 +361,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new hours */ public override setHours(value: int, min: int): long { - const result = this.store_.setHours(value, min); + const result = super.setHours(value, min); this.meta_.fireChange(); this.executeOnSubscribingWatches('setHours'); return result; @@ -376,7 +373,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new hours */ public override setHours(value: int, min: int, sec: int): long { - const result = this.store_.setHours(value, min, sec); + const result = super.setHours(value, min, sec); this.meta_.fireChange(); this.executeOnSubscribingWatches('setHours'); return result; @@ -388,7 +385,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new hours */ public override setHours(value: int, min: int, sec: int, ms: int): long { - const result = this.store_.setHours(value, min, sec, ms); + const result = super.setHours(value, min, sec, ms); this.meta_.fireChange(); this.executeOnSubscribingWatches('setHours'); return result; @@ -400,7 +397,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new hours */ public override setUTCHours(value: int): long { - const result = this.store_.setUTCHours(value); + const result = super.setUTCHours(value); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCHours'); return result; @@ -412,7 +409,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new hours */ public override setUTCHours(value: int, min: int): long { - const result = this.store_.setUTCHours(value, min); + const result = super.setUTCHours(value, min); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCHours'); return result; @@ -424,7 +421,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new hours */ public override setUTCHours(value: int, min: int, sec: int): long { - const result = this.store_.setUTCHours(value, min, sec); + const result = super.setUTCHours(value, min, sec); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCHours'); return result; @@ -436,7 +433,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new hours */ public override setUTCHours(value: int, min: int, sec: int, ms: int): long { - const result = this.store_.setUTCHours(value, min, sec, ms); + const result = super.setUTCHours(value, min, sec, ms); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCHours'); return result; @@ -455,7 +452,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getMilliseconds(): int { this.conditionalAddRef(); - return this.store_.getMilliseconds(); + return super.getMilliseconds(); } /** @@ -465,7 +462,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getUTCMilliseconds(): int { this.conditionalAddRef(); - return this.store_.getUTCMilliseconds(); + return super.getUTCMilliseconds(); } /** @@ -474,7 +471,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new ms */ public override setMilliseconds(value: int): long { - const result = this.store_.setMilliseconds(value); + const result = super.setMilliseconds(value); this.meta_.fireChange(); this.executeOnSubscribingWatches('setMilliseconds'); return result; @@ -486,7 +483,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new ms */ public override setUTCMilliseconds(value: int): long { - const result = this.store_.setUTCMilliseconds(value); + const result = super.setUTCMilliseconds(value); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCMilliseconds'); return result; @@ -505,7 +502,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getSeconds(): int { this.conditionalAddRef(); - return this.store_.getSeconds(); + return super.getSeconds(); } /** @@ -515,7 +512,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getUTCSeconds(): int { this.conditionalAddRef(); - return this.store_.getUTCSeconds(); + return super.getUTCSeconds(); } /** @@ -524,7 +521,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new seconds */ public override setSeconds(value: int): long { - const result = this.store_.setSeconds(value); + const result = super.setSeconds(value); this.meta_.fireChange(); this.executeOnSubscribingWatches('setSeconds'); return result; @@ -536,7 +533,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new seconds */ public override setSeconds(value: int, ms: int): long { - const result = this.store_.setSeconds(value, ms); + const result = super.setSeconds(value, ms); this.meta_.fireChange(); this.executeOnSubscribingWatches('setSeconds'); return result; @@ -548,7 +545,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new seconds */ public override setUTCSeconds(value: int): long { - const result = this.store_.setUTCSeconds(value); + const result = super.setUTCSeconds(value); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCSeconds'); return result; @@ -560,7 +557,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new seconds */ public override setUTCSeconds(value: int, ms: int): long { - const result = this.store_.setUTCSeconds(value, ms); + const result = super.setUTCSeconds(value, ms); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCSeconds'); return result; @@ -579,7 +576,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getMinutes(): int { this.conditionalAddRef(); - return this.store_.getMinutes(); + return super.getMinutes(); } /** @@ -588,7 +585,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new minutes */ public override setUTCMinutes(value: int): long { - const result = this.store_.setUTCMinutes(value); + const result = super.setUTCMinutes(value); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCMinutes'); return result; @@ -600,7 +597,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new minutes */ public override setUTCMinutes(value: int, sec: int): long { - const result = this.store_.setUTCMinutes(value, sec); + const result = super.setUTCMinutes(value, sec); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCMinutes'); return result; @@ -612,7 +609,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new minutes */ public override setUTCMinutes(value: int, sec: int, ms: int): long { - const result = this.store_.setUTCMinutes(value, sec, ms); + const result = super.setUTCMinutes(value, sec, ms); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCMinutes'); return result; @@ -625,7 +622,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getUTCMinutes(): int { this.conditionalAddRef(); - return this.store_.getUTCMinutes(); + return super.getUTCMinutes(); } /** @@ -634,7 +631,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new minutes */ public override setMinutes(value: int): long { - const result = this.store_.setMinutes(value); + const result = super.setMinutes(value); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCMinutes'); return result; @@ -646,7 +643,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new minutes */ public override setMinutes(value: int, sec: int): long { - const result = this.store_.setMinutes(value, sec); + const result = super.setMinutes(value, sec); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCMinutes'); return result; @@ -658,7 +655,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new minutes */ public override setMinutes(value: int, sec: int, ms: int): long { - const result = this.store_.setMinutes(value, sec, ms); + const result = super.setMinutes(value, sec, ms); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCMinutes'); return result; @@ -679,7 +676,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getMonth(): int { this.conditionalAddRef(); - return this.store_.getMonth(); + return super.getMonth(); } /** @@ -690,7 +687,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getUTCMonth(): int { this.conditionalAddRef(); - return this.store_.getUTCMonth(); + return super.getUTCMonth(); } /** @@ -699,7 +696,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param month new month */ public override setMonth(month: int): long { - const result = this.store_.setMonth(month); + const result = super.setMonth(month); this.meta_.fireChange(); this.executeOnSubscribingWatches('setMonth'); return result; @@ -711,7 +708,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param month new month */ public override setMonth(month: int, date: int): long { - const result = this.store_.setMonth(month, date); + const result = super.setMonth(month, date); this.meta_.fireChange(); this.executeOnSubscribingWatches('setMonth'); return result; @@ -723,7 +720,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param month new month */ public override setUTCMonth(month: int): long { - const result = this.store_.setUTCMonth(month); + const result = super.setUTCMonth(month); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCMonth'); return result; @@ -735,7 +732,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param month new month */ public override setUTCMonth(month: int, date: int): long { - const result = this.store_.setUTCMonth(month, date); + const result = super.setUTCMonth(month, date); this.meta_.fireChange(); this.executeOnSubscribingWatches('setUTCMonth'); return result; @@ -750,7 +747,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getTime(): long { this.conditionalAddRef(); - return this.store_.getTime(); + return super.getTime(); } /** @@ -762,7 +759,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @returns A number representing the milliseconds elapsed between 1 January 1970 00:00:00 UTC and the given date. */ public override setTime(value: long): long { - const result = this.store_.setTime(value); + const result = super.setTime(value); this.meta_.fireChange(); this.executeOnSubscribingWatches('setTime'); return result; @@ -777,7 +774,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override getTimezoneOffset(): long { this.conditionalAddRef(); - return this.store_.getTimezoneOffset(); + return super.getTimezoneOffset(); } /** @@ -787,7 +784,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped * @param value new timezone offset */ public override setTimezoneOffset(value: int): long { - const result = this.store_.setTimezoneOffset(value); + const result = super.setTimezoneOffset(value); this.meta_.fireChange(); this.executeOnSubscribingWatches('setTime'); return result; @@ -810,7 +807,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override toISOString(): string { this.conditionalAddRef(); - return this.store_.toISOString(); + return super.toISOString(); } /** @@ -820,7 +817,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override toJSON(): string { this.conditionalAddRef(); - return this.store_.toJSON() ?? ''; + return super.toJSON() ?? ''; } /** @@ -836,7 +833,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override toTimeString(): string { this.conditionalAddRef(); - return this.store_.toTimeString(); + return super.toTimeString(); } /** @@ -852,7 +849,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override toDateString(): string { this.conditionalAddRef(); - return this.store_.toDateString(); + return super.toDateString(); } /** @@ -868,7 +865,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override toString(): string { this.conditionalAddRef(); - return this.store_.toString(); + return super.toString(); } /** @@ -884,7 +881,7 @@ export class WrappedDate extends Date implements IObservedObject, ObserveWrapped */ public override toUTCString(): string { this.conditionalAddRef(); - return this.store_.toUTCString(); + return super.toUTCString(); } // shorthand function because diff --git a/arkoala-arkts/arkui/src/stateManagement/base/stateUpdateLoop.ts b/arkoala-arkts/arkui/src/stateManagement/base/stateUpdateLoop.ts index 8cc4665e1..b8c093832 100644 --- a/arkoala-arkts/arkui/src/stateManagement/base/stateUpdateLoop.ts +++ b/arkoala-arkts/arkui/src/stateManagement/base/stateUpdateLoop.ts @@ -13,17 +13,22 @@ * limitations under the License. */ export class StateUpdateLoop { - private static callbacks: Array<() => void> = new Array<() => void>(); + private static callbacks: Array<() => void> | undefined = undefined; public static add(callback: () => void): void { - StateUpdateLoop.callbacks.push(callback); + if (StateUpdateLoop.callbacks == undefined) { + StateUpdateLoop.callbacks = new Array<() => void>(); + } + StateUpdateLoop.callbacks!.push(callback); } public static consume(): void { - StateUpdateLoop.callbacks.forEach((callback: () => void) => { - callback(); - }); - StateUpdateLoop.callbacks.length = 0; + if (StateUpdateLoop.callbacks) { + StateUpdateLoop.callbacks!.forEach((callback: () => void) => { + callback(); + }); + StateUpdateLoop.callbacks!.length = 0; + } } public static get len(): number { - return StateUpdateLoop.callbacks.length; + return StateUpdateLoop.callbacks?.length ?? 0; } } diff --git a/arkoala-arkts/arkui/src/stateManagement/decorator.ts b/arkoala-arkts/arkui/src/stateManagement/decorator.ts index 99cd98c96..a5b4a8e5b 100644 --- a/arkoala-arkts/arkui/src/stateManagement/decorator.ts +++ b/arkoala-arkts/arkui/src/stateManagement/decorator.ts @@ -539,7 +539,7 @@ final class NgStateMgmtFactory implements IStateMgmtFactory { initValue: T, watchFunc?: WatchFuncType ): IStateDecoratedVariable { - throw new Error('Method not implemented') + return STATE_MGMT_FACTORY.makeState(owner, varName, initValue, watchFunc) } makeProp( @@ -548,7 +548,7 @@ final class NgStateMgmtFactory implements IStateMgmtFactory { initValue: T, watchFunc?: WatchFuncType ): IPropDecoratedVariable { - throw new Error('Method not implemented') + return STATE_MGMT_FACTORY.makeProp(owner, varName, initValue, watchFunc) } makePropRef( @@ -557,7 +557,7 @@ final class NgStateMgmtFactory implements IStateMgmtFactory { initValue: T, watchFunc?: WatchFuncType ): IPropRefDecoratedVariable { - throw new Error('Method not implemented') + return STATE_MGMT_FACTORY.makePropRef(owner, varName, initValue, watchFunc) } makeLink( @@ -566,7 +566,7 @@ final class NgStateMgmtFactory implements IStateMgmtFactory { source: LinkSourceType, watchFunc?: WatchFuncType ): ILinkDecoratedVariable { - throw new Error('Method not implemented') + return STATE_MGMT_FACTORY.makeLink(owner, varName, source, watchFunc) } makeObjectLink( @@ -575,7 +575,7 @@ final class NgStateMgmtFactory implements IStateMgmtFactory { initValue: T, watchFunc?: WatchFuncType ): IObjectLinkDecoratedVariable { - throw new Error('Method not implemented') + return STATE_MGMT_FACTORY.makeObjectLink(owner, varName, initValue, watchFunc) } makeProvide( @@ -586,7 +586,7 @@ final class NgStateMgmtFactory implements IStateMgmtFactory { allowOverride: boolean, watchFunc?: WatchFuncType ): IProvideDecoratedVariable { - throw new Error('Method not implemented') + return STATE_MGMT_FACTORY.makeProvide(owner, varName, provideAlias, initValue, allowOverride, watchFunc) } makeConsume( @@ -595,7 +595,7 @@ final class NgStateMgmtFactory implements IStateMgmtFactory { provideAlias: string, watchFunc?: WatchFuncType ): IConsumeDecoratedVariable { - throw new Error('Method not implemented') + return STATE_MGMT_FACTORY.makeConsume(owner, varName, provideAlias, watchFunc) } makeStorageLink( diff --git a/arkoala-arkts/arkui/src/stateManagement/decoratorImpl/decoratorProp.ts b/arkoala-arkts/arkui/src/stateManagement/decoratorImpl/decoratorProp.ts index 988825c25..26a016307 100644 --- a/arkoala-arkts/arkui/src/stateManagement/decoratorImpl/decoratorProp.ts +++ b/arkoala-arkts/arkui/src/stateManagement/decoratorImpl/decoratorProp.ts @@ -23,11 +23,12 @@ import { IBackingValue } from '../base/iBackingValue'; import { DecoratorBackingValue } from '../base/backingValue'; import { ObserveSingleton } from '../base/observeSingleton'; import { NullableObject } from '../base/types'; -import { StateMgmtConsole } from '../tools/stateMgmtDFX'; import { UIUtils } from '../utils'; import { CompatibleStateChangeCallback, getObservedObject, isDynamicObject } from '../../component/interop'; import { StateMgmtTool } from '../tools/arkts/stateMgmtTool'; import { WatchFunc } from './decoratorWatch'; +import { scheduleCallback,GlobalStateManager } from "#incrementalAndStateManagementReexport" + /** * implementation of V1 @Prop * @@ -66,8 +67,7 @@ export class PropDecoratedVariable extends DecoratedV1VariableBase impleme if (isDynamicObject(initValue)) { initValue = getObservedObject(initValue, this); } - const deepCopyValue = deepCopy(initValue); - this.__localValue = new DecoratorBackingValue(varName, deepCopyValue); + this.__localValue = new DecoratorBackingValue(varName, UIUtils.makeObserved(deepCopy(initValue)) as T); // if initValue not from parent, this __sourceValue should never changed this.__soruceValue = new DecoratorBackingValue(varName, initValue); this.registerWatchForObservedObjectChanges(initValue); @@ -111,17 +111,24 @@ export class PropDecoratedVariable extends DecoratedV1VariableBase impleme // @Prop updates from parent, value from parent needs to be copied public update(newValue: T): void { const sourceValue = this.__soruceValue.get(false); - if (sourceValue !== newValue) { + const scope = GlobalStateManager.instance.scope(0, 1) + let param = scope.param(0, newValue) + if (scope.unchanged) { + scope.cached + } + if (param.modified) { this.unregisterWatchFromObservedObjectChanges(sourceValue); this.registerWatchForObservedObjectChanges(newValue); - - this.__soruceValue.setSilently(newValue); + if (StateUpdateLoop.len == 0) { + scheduleCallback(StateUpdateLoop.consume) + } StateUpdateLoop.add(() => { if (this.__localValue.set(UIUtils.makeObserved(deepCopy(newValue)) as T)) { this.execWatchFuncs(); } }); } + scope.recache(); } public updateForStorage(newValue: T): void { diff --git a/ui2abc/ets-tests/ets/environment-tests/pages/watch/WatchProp.ets b/ui2abc/ets-tests/ets/environment-tests/pages/watch/WatchProp.ets index e74abc58c..fd5b2ad0d 100644 --- a/ui2abc/ets-tests/ets/environment-tests/pages/watch/WatchProp.ets +++ b/ui2abc/ets-tests/ets/environment-tests/pages/watch/WatchProp.ets @@ -22,7 +22,7 @@ let watchPropCounter = 0 @Component struct WatchPropChild { - @Prop @Watch('valueUpdate') propValue: number + @Prop @Watch('valueUpdate') propValue: number = 0 @State result: string = '' build() { diff --git a/ui2abc/ui-plugins-ng/src/class-transformer.ts b/ui2abc/ui-plugins-ng/src/class-transformer.ts index 16b3a5463..5c85a0572 100644 --- a/ui2abc/ui-plugins-ng/src/class-transformer.ts +++ b/ui2abc/ui-plugins-ng/src/class-transformer.ts @@ -120,7 +120,7 @@ export class ClassTransformer extends arkts.AbstractVisitor { const className = clazz.definition!.ident!.name // For @ObservedV1 classes, one meta is used to monitor all fields - if (!observeByProperty) { + if (!observeByProperty && trackedPropNames.length > 0) { result.push( createStateMetaProperty( getClassScopeStateMetaFieldName(), diff --git a/ui2abc/ui-plugins-ng/src/component-transformer.ts b/ui2abc/ui-plugins-ng/src/component-transformer.ts index 4a77b67e6..192797c4c 100644 --- a/ui2abc/ui-plugins-ng/src/component-transformer.ts +++ b/ui2abc/ui-plugins-ng/src/component-transformer.ts @@ -36,15 +36,15 @@ import { } from "./utils"; import { BuilderParamTransformer, - ConsumeTransformer, - LinkTransformer, + ConsumeTransformerStateMgmtFactory, + LinkTransformerStateMgmtFactory, LocalStorageLinkTransformer, LocalStoragePropTransformer, ObjectLinkTransformer, PropertyTransformer, - PropTransformer, - ProvideTransformer, - StateTransformer, + PropTransformerStateMgmtFactory, + ProvideTransformerStateMgmtFactory, + StateTransformerStateMgmtFactory, StorageLinkTransformer, StoragePropTransformer, PlainPropertyTransformer, @@ -55,7 +55,13 @@ import { EventPropertyTransformer, RequirePropertyTransformer, ParamPropertyTransformer, - ParamOncePropertyTransformer + ParamOncePropertyTransformer, + ObjectLinkTransformerStateMgmtFactory, + StateTransformer, + LinkTransformer, + PropTransformer, + ProvideTransformer, + ConsumeTransformer } from "./property-transformers"; import { ComputedMethodTransformer, @@ -84,8 +90,42 @@ function computeOptionsName(clazz: arkts.ClassDeclaration): string { } export class ComponentTransformer extends arkts.AbstractVisitor { + private readonly useStateMgmtFactoryInterfaceV1 = false constructor(private imports: Importer, options?: ComponentTransformerOptions) { super() + const propertyTransformers = this.useStateMgmtFactoryInterfaceV1 + ? [ + new StateTransformerStateMgmtFactory(), + new LinkTransformerStateMgmtFactory(), + new PropTransformerStateMgmtFactory(), + new ObjectLinkTransformerStateMgmtFactory(), + new ProvideTransformerStateMgmtFactory(), + new ConsumeTransformerStateMgmtFactory(), + ] + : [ + new StateTransformer(), + new LinkTransformer(), + new PropTransformer(), + new ObjectLinkTransformer(), + new ProvideTransformer(), + new ConsumeTransformer(), + ] + this.propertyTransformers = + [...propertyTransformers, + new PlainPropertyTransformer(), + new StorageLinkTransformer(), + new StoragePropTransformer(), + new LocalStorageLinkTransformer(), + new LocalStoragePropTransformer(), + new ProviderTransformer(), + new ConsumerTransformer(), + new BuilderParamTransformer(), + new LocalPropertyTransformer(), + new ParamPropertyTransformer(), + new ParamOncePropertyTransformer(), + new EventPropertyTransformer(), + new RequirePropertyTransformer() + ] } private addMemoImports(): void { @@ -578,27 +618,7 @@ export class ComponentTransformer extends arkts.AbstractVisitor { } } - propertyTransformers: PropertyTransformer[] = [ - new StateTransformer(), - new PlainPropertyTransformer(), - new LinkTransformer(), - new PropTransformer(), - new StorageLinkTransformer(), - new StoragePropTransformer(), - new LocalStorageLinkTransformer(), - new LocalStoragePropTransformer(), - new ObjectLinkTransformer(), - new ProvideTransformer(), - new ProviderTransformer(), - new ConsumeTransformer(), - new ConsumerTransformer(), - new BuilderParamTransformer(), - new LocalPropertyTransformer(), - new ParamPropertyTransformer(), - new ParamOncePropertyTransformer(), - new EventPropertyTransformer(), - new RequirePropertyTransformer() - ] + private readonly propertyTransformers: readonly PropertyTransformer[] private methodTransformers: MethodTransformer[] = [ new MonitorMethodTransformer(), diff --git a/ui2abc/ui-plugins-ng/src/property-transformers.ts b/ui2abc/ui-plugins-ng/src/property-transformers.ts index c4d92be1d..726c77814 100644 --- a/ui2abc/ui-plugins-ng/src/property-transformers.ts +++ b/ui2abc/ui-plugins-ng/src/property-transformers.ts @@ -733,7 +733,7 @@ export class BuilderParamTransformer implements PropertyTransformer { } } -abstract class StateMgmtFactoryTransformerBase extends PropertyTransformerBase { +abstract class PropertyTransformerStateMgmtFactoryBase extends PropertyTransformerBase { collectImports(importer: Importer) { super.collectImports(importer); importer.add(STATE_MGMT_FACTORY_NAME, getDecoratorPackage()) @@ -752,28 +752,6 @@ abstract class StateMgmtFactoryTransformerBase extends PropertyTransformerBase { if (!valueType) throw new Error(`@${propertyTypeName}: type is not specified for ${property.id?.name}`) const propertyName = property.id!.name - const propertyArgs: arkts.Expression[] = [arkts.factory.createStringLiteral(propertyName)] - const watches = property.annotations.filter(isWatchDecorator).map(getWatchParameter) - if (watches.length > 0) { - propertyArgs.push( - arkts.factory.createArrowFunctionExpression( - arkts.factory.createScriptFunction( - arkts.factory.createBlockStatement( - watches.map(watch => createWatchCall(clazz, watch, propertyName)) - ), - undefined, - [], - undefined, - false, - arkts.Es2pandaScriptFunctionFlags.SCRIPT_FUNCTION_FLAGS_ARROW, - arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_NONE, - undefined, - undefined - ) - ) - ) - } - const backingField = arkts.factory.createClassProperty( arkts.factory.createIdentifier(backingFieldNameOfProperty(property)), arkts.factory.createUndefinedLiteral(), @@ -861,26 +839,185 @@ abstract class StateMgmtFactoryTransformerBase extends PropertyTransformerBase { abstract createCallFactoryMethod(property: arkts.ClassProperty): arkts.Expression } -export class LocalPropertyTransformer extends StateMgmtFactoryTransformerBase { +export class StateTransformerStateMgmtFactory extends PropertyTransformerStateMgmtFactoryBase { constructor() { - super(DecoratorNames.LOCAL, "ILocalDecoratedVariable") + super(DecoratorNames.STATE, "IStateDecoratedVariable") + } + applyOptions(property: arkts.ClassProperty, result: arkts.ClassElement[]): void { + result.push(createOptionalClassProperty(property.id!.name, property)) + } + applyReuseRecord(property: arkts.ClassProperty, result: arkts.Expression[]): void { + addPropertyRecordTo(result, property) } createCallFactoryMethod(property: arkts.ClassProperty): arkts.Expression { - return arkts.factory.createCallExpression( - fieldOf(arkts.factory.createIdentifier(STATE_MGMT_FACTORY_NAME), "makeLocal"), - [ - arkts.factory.createThisExpression(), - arkts.factory.createStringLiteral(property.id!.name), - property.value!.clone() - ], - arkts.factory.createTSTypeParameterInstantiation([property.typeAnnotation!.clone()]), - false, + return createStateMgmtFactory(property, "makeState", [ + arkts.factory.createThisExpression(), + arkts.factory.createStringLiteral(property.id!.name), + ifHasInitializer(property, asNonNull(initializerOf(property)), property.value!.clone()), + createWatchField(property) + ]) + } +} + +export class LinkTransformerStateMgmtFactory extends PropertyTransformerStateMgmtFactoryBase { + constructor() { + super(DecoratorNames.LINK, "ILinkDecoratedVariable") + } + applyOptions(property: arkts.ClassProperty, result: arkts.ClassElement[]): void { + const backing = arkts.factory.createClassProperty( + arkts.factory.createIdentifier(property.id?.name!), + undefined, + createWrapperType("LinkSourceType", property.typeAnnotation!, true), + arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_OPTIONAL, false ) + backing.setAnnotations([annotation(InternalAnnotations.PASS_REFERENCE)]) + result.push(backing) + } + collectImports(imports: Importer): void { + super.collectImports(imports) + imports.add("LinkSourceType", getDecoratorPackage()) + } + applyReuseRecord(property: arkts.ClassProperty, result: arkts.Expression[]): void { + addPropertyRecordTo(result, property) + } + createCallFactoryMethod(property: arkts.ClassProperty): arkts.Expression { + return createStateMgmtFactory(property, "makeLink", [ + arkts.factory.createThisExpression(), + arkts.factory.createStringLiteral(property.id!.name), + arkts.factory.createTSAsExpression( + initializerOf(property), + createWrapperType("LinkSourceType", property.typeAnnotation!.clone()), + false, + ), + createWatchField(property) + ]) + } +} + +export class PropTransformerStateMgmtFactory extends PropertyTransformerStateMgmtFactoryBase { + constructor() { + super(DecoratorNames.PROP, "IPropDecoratedVariable") + } + check(property: arkts.ClassProperty): boolean { + checkRequireDecoratorUsage(property) + checkOnceDecoratorUsage(property) + return hasDecorator(property, DecoratorNames.PROP) || hasDecorator(property, DecoratorNames.PROP_REF) + } + applyOptions(property: arkts.ClassProperty, result: arkts.ClassElement[]): void { + result.push(createOptionalClassProperty(property.id!.name, property)) + } + applyReuseRecord(property: arkts.ClassProperty, result: arkts.Expression[]): void { + addPropertyRecordTo(result, property) + } + applyBuild(property: arkts.ClassProperty, result: arkts.Statement[]): void { + result.push( + ifHasInitializerStatement(property, + thisPropertyMethodCallExpr(property, "update", [asNonNull(initializerOf(property))], true) + ) + ) + } + createCallFactoryMethod(property: arkts.ClassProperty): arkts.Expression { + const methodName = hasDecorator(property, DecoratorNames.PROP) ? "makeProp" : "makePropRef" + return createStateMgmtFactory(property, methodName, [ + arkts.factory.createThisExpression(), + arkts.factory.createStringLiteral(property.id!.name), + ifHasInitializer(property, asNonNull(initializerOf(property)), property.value!.clone()), + createWatchField(property) + ]) + } +} + +export class ProvideTransformerStateMgmtFactory extends PropertyTransformerStateMgmtFactoryBase { + constructor() { + super(DecoratorNames.PROVIDE, "IProvideDecoratedVariable") + } + applyOptions(property: arkts.ClassProperty, result: arkts.ClassElement[]): void { + result.push(createOptionalClassProperty(property.id!.name, property)) + } + applyReuseRecord(property: arkts.ClassProperty, result: arkts.Expression[]): void { + addPropertyRecordTo(result, property) + } + createCallFactoryMethod(property: arkts.ClassProperty): arkts.Expression { + const alias = parseAlias(property, this.decoratorName) + const allowOverride = parseAllowOverride(property, this.decoratorName) + return createStateMgmtFactory(property, "makeProvide", [ + arkts.factory.createThisExpression(), + arkts.factory.createStringLiteral(property.id!.name), + arkts.factory.createStringLiteral(alias ?? property.id!.name), + ifHasInitializer(property, asNonNull(initializerOf(property)), property.value!.clone()), + arkts.factory.createBooleanLiteral(allowOverride ?? false), + createWatchField(property) + ]) + } +} + +export class ConsumeTransformerStateMgmtFactory extends PropertyTransformerStateMgmtFactoryBase { + constructor() { + super(DecoratorNames.CONSUME, "IConsumeDecoratedVariable") + } + applyInitializeStruct(localStorage: arkts.Expression | undefined, property: arkts.ClassProperty, result: arkts.Statement[]): void { + if (property.value) throw new Error("@Consume decorator does not expect property initializer") + super.applyInitializeStruct(localStorage, property, result) + } + createCallFactoryMethod(property: arkts.ClassProperty): arkts.Expression { + const alias = withStorageKey([], property, this.decoratorName) + if (alias.length == 0) { + alias.push(arkts.factory.createStringLiteral(property.id!.name)) + } + return createStateMgmtFactory(property, "makeConsume", [ + arkts.factory.createThisExpression(), + arkts.factory.createStringLiteral(property.id!.name), + ...alias, + createWatchField(property) + ]) + } +} + +export class ObjectLinkTransformerStateMgmtFactory extends PropertyTransformerStateMgmtFactoryBase { + constructor() { + super(DecoratorNames.OBJECT_LINK, "IObjectLinkDecoratedVariable") + } + applyOptions(property: arkts.ClassProperty, result: arkts.ClassElement[]): void { + result.push(createOptionalClassProperty(property.id!.name, property)) + } + applyReuseRecord(property: arkts.ClassProperty, result: arkts.Expression[]): void { + addPropertyRecordTo(result, property) + } + applyBuild(property: arkts.ClassProperty, result: arkts.Statement[]): void { + result.push( + ifHasInitializerStatement(property, + thisPropertyMethodCallExpr(property, "update", [asNonNull(initializerOf(property))], true) + ) + ) + } + createCallFactoryMethod(property: arkts.ClassProperty): arkts.Expression { + return createStateMgmtFactory(property, "makeObjectLink", [ + arkts.factory.createThisExpression(), + arkts.factory.createStringLiteral(property.id!.name), + asNonNull(initializerOf(property)), + createWatchField(property) + ]) + } + protected isReadOnlyProperty(property: arkts.ClassProperty): boolean { + return true } } -abstract class ParamPropertyTransformerBase extends StateMgmtFactoryTransformerBase { +export class LocalPropertyTransformer extends PropertyTransformerStateMgmtFactoryBase { + constructor() { + super(DecoratorNames.LOCAL, "ILocalDecoratedVariable") + } + createCallFactoryMethod(property: arkts.ClassProperty): arkts.Expression { + return createStateMgmtFactory(property, "makeLocal", [ + arkts.factory.createThisExpression(), + arkts.factory.createStringLiteral(property.id!.name), + property.value!.clone() + ]) + } +} + +abstract class ParamPropertyTransformerBase extends PropertyTransformerStateMgmtFactoryBase { readonly optionFieldTypeName = "IDecoratedReadableVariable" collectImports(importer: Importer) { @@ -985,17 +1122,11 @@ abstract class ParamPropertyTransformerBase extends StateMgmtFactoryTransformerB ? "makeParamOnce" : "makeParam" - return arkts.factory.createCallExpression( - fieldOf(arkts.factory.createIdentifier(STATE_MGMT_FACTORY_NAME), factoryName), - [ - arkts.factory.createThisExpression(), - arkts.factory.createStringLiteral(property.id!.name), - asNonNull(arkts.factory.createIdentifier(this.getInitVarName(property))) - ], - arkts.factory.createTSTypeParameterInstantiation([property.typeAnnotation!.clone()]), - false, - false - ) + return createStateMgmtFactory(property, factoryName, [ + arkts.factory.createThisExpression(), + arkts.factory.createStringLiteral(property.id!.name), + asNonNull(arkts.factory.createIdentifier(this.getInitVarName(property))) + ]) } getInitVarName(property: arkts.ClassProperty): string { return `${backingFieldNameOfProperty(property)}_init` @@ -1103,14 +1234,7 @@ export class EventPropertyTransformer implements PropertyTransformer { arkts.factory.createExpressionStatement( arkts.factory.createAssignmentExpression( fieldOf(arkts.factory.createThisExpression(), backingFieldNameOfProperty(property)), - arkts.factory.createConditionalExpression( - arkts.factory.createBinaryExpression( - initializerOf(property), - arkts.factory.createUndefinedLiteral(), - arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_NOT_EQUAL), - initializerOf(property), - initialValue - ), + ifHasInitializer(property, initializerOf(property), initialValue), arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_SUBSTITUTION ) ) @@ -1130,7 +1254,7 @@ export class EventPropertyTransformer implements PropertyTransformer { } } -export class ProviderTransformer extends StateMgmtFactoryTransformerBase { +export class ProviderTransformer extends PropertyTransformerStateMgmtFactoryBase { constructor() { super(DecoratorNames.PROVIDER, "IProviderDecoratedVariable") } @@ -1145,22 +1269,16 @@ export class ProviderTransformer extends StateMgmtFactoryTransformerBase { if (alias.length == 0) { alias.push(arkts.factory.createStringLiteral("")) } - return arkts.factory.createCallExpression( - fieldOf(arkts.factory.createIdentifier(STATE_MGMT_FACTORY_NAME), "makeProvider"), - [ - arkts.factory.createThisExpression(), - arkts.factory.createStringLiteral(property.id!.name), - ...alias, - property.value!.clone() - ], - arkts.factory.createTSTypeParameterInstantiation([property.typeAnnotation!.clone()]), - false, - false - ) + return createStateMgmtFactory(property, "makeProvider", [ + arkts.factory.createThisExpression(), + arkts.factory.createStringLiteral(property.id!.name), + ...alias, + property.value!.clone() + ]) } } -export class ConsumerTransformer extends StateMgmtFactoryTransformerBase { +export class ConsumerTransformer extends PropertyTransformerStateMgmtFactoryBase { constructor() { super(DecoratorNames.CONSUMER, "IConsumerDecoratedVariable") } @@ -1175,18 +1293,12 @@ export class ConsumerTransformer extends StateMgmtFactoryTransformerBase { if (alias.length == 0) { alias.push(arkts.factory.createStringLiteral("")) } - return arkts.factory.createCallExpression( - fieldOf(arkts.factory.createIdentifier(STATE_MGMT_FACTORY_NAME), "makeConsumer"), - [ - arkts.factory.createThisExpression(), - arkts.factory.createStringLiteral(property.id!.name), - ...alias, - property.value!.clone() - ], - arkts.factory.createTSTypeParameterInstantiation([property.typeAnnotation!.clone()]), - false, - false - ) + return createStateMgmtFactory(property, "makeConsumer", [ + arkts.factory.createThisExpression(), + arkts.factory.createStringLiteral(property.id!.name), + ...alias, + property.value!.clone() + ]) } } @@ -1235,6 +1347,33 @@ function applyInitStatement(property: arkts.ClassProperty): arkts.Statement { return arkts.factory.createBlockStatement([initDeclaration, initBlock]) } +function ifHasInitializerStatement(property: arkts.ClassProperty, + consequent: arkts.Expression): arkts.Statement { + return arkts.factory.createIfStatement( + arkts.factory.createBinaryExpression( + initializerOf(property), + arkts.factory.createUndefinedLiteral(), + arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_NOT_EQUAL + ), + arkts.factory.createBlockStatement([ + arkts.factory.createExpressionStatement(consequent) + ]) + ) +} + +function ifHasInitializer(property: arkts.ClassProperty, + consequent: arkts.Expression, + alternate: arkts.Expression): arkts.Expression { + return arkts.factory.createConditionalExpression( + arkts.factory.createBinaryExpression( + initializerOf(property), + arkts.factory.createUndefinedLiteral(), + arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_NOT_EQUAL), + consequent, + alternate + ) +} + export function isOptionBackedByProperty(property: arkts.ClassProperty): boolean { return hasDecorator(property, DecoratorNames.LINK) } @@ -1294,4 +1433,27 @@ function checkOnceDecoratorUsage(property: arkts.ClassProperty) { throw new Error(`@${getAnnotationName(unsuitable[0])} cannot be used with @Once decorator`) } } +} + +function createWatchField(property: arkts.ClassProperty): arkts.Expression { + const watchDecorator = property.annotations.find(isWatchDecorator); + + if (watchDecorator) { + const param = getWatchParameter(watchDecorator); + return fieldOf(arkts.factory.createThisExpression(), param); + } + + return arkts.factory.createUndefinedLiteral(); +} + +function createStateMgmtFactory(property: arkts.ClassProperty, + methodName: string, + args: arkts.Expression[]): arkts.Expression { + return arkts.factory.createCallExpression( + fieldOf(arkts.factory.createIdentifier(STATE_MGMT_FACTORY_NAME), methodName), + args, + arkts.factory.createTSTypeParameterInstantiation([property.typeAnnotation!.clone()]), + false, + false + ) } \ No newline at end of file -- Gitee