1- import { type InputFieldMDRC } from '../../renderChildren/InputFieldMDRC' ;
21import { InputFieldComponent } from './InputFieldComponent' ;
32import { type SvelteComponent } from 'svelte' ;
4- import { ComputedSignal , type Listener , Notifier } from '../../utils/Signal' ;
3+ import { ComputedSignal , Signal } from '../../utils/Signal' ;
54
65import { InputFieldArgumentType } from '../../config/FieldConfigs' ;
6+ import { type IInputFieldBase } from './IInputFieldBase' ;
7+ import { type MetadataSubscription } from '../../metadata/MetadataSubscription' ;
78
8- export abstract class AbstractInputField < MetadataValueType , ComponentValueType > extends Notifier <
9- MetadataValueType ,
10- Listener < MetadataValueType >
11- > {
12- readonly renderChild : InputFieldMDRC ;
9+ export abstract class AbstractInputField < MetadataValueType , ComponentValueType > {
10+ readonly renderChild : IInputFieldBase ;
1311 readonly inputFieldComponent : InputFieldComponent < ComponentValueType > ;
12+ readonly inputSignal : Signal < unknown > ;
1413 readonly signal : ComputedSignal < unknown , MetadataValueType > ;
1514
16- protected constructor ( renderChild : InputFieldMDRC ) {
17- super ( ) ;
15+ private metadataSubscription ?: MetadataSubscription ;
1816
17+ protected constructor ( renderChild : IInputFieldBase ) {
1918 this . renderChild = renderChild ;
19+ this . inputSignal = new Signal < unknown > ( undefined ) ;
2020 this . inputFieldComponent = new InputFieldComponent < ComponentValueType > ( this . getSvelteComponent ( ) ) ;
2121
2222 this . signal = new ComputedSignal < unknown , MetadataValueType > (
23- this . renderChild . inputSignal ,
23+ this . inputSignal ,
2424 ( value : unknown ) : MetadataValueType => {
2525 const filteredValue = this . filterValue ( value ) ;
2626 return filteredValue ?? this . getDefaultValue ( ) ;
@@ -31,12 +31,33 @@ export abstract class AbstractInputField<MetadataValueType, ComponentValueType>
3131 callback : value => this . inputFieldComponent . setValue ( this . reverseMapValue ( value ) ) ,
3232 } ) ;
3333
34- this . inputFieldComponent . registerListener ( {
35- callback : value => {
36- // console.log('input field component change', value);
37- this . notifyListeners ( this . mapValue ( value ) ) ;
38- } ,
39- } ) ;
34+ const fullBindTarget = this . renderChild . getFullBindTarget ( ) ;
35+
36+ if ( fullBindTarget ) {
37+ this . inputFieldComponent . registerListener ( {
38+ callback : value => {
39+ // console.log('input field component change', value);
40+ this . notifySubscription ( this . mapValue ( value ) ) ;
41+ } ,
42+ } ) ;
43+
44+ this . metadataSubscription = this . renderChild . plugin . metadataManager . subscribe (
45+ this . renderChild . getUuid ( ) ,
46+ this . inputSignal ,
47+ fullBindTarget ,
48+ ( ) => this . renderChild . unload ( ) ,
49+ ) ;
50+ }
51+ }
52+
53+ public destroy ( ) : void {
54+ // we don't need to unregister the listener because the component will destroy all listeners on unmount
55+
56+ if ( this . inputFieldComponent . isMounted ( ) ) {
57+ this . unmount ( ) ;
58+ }
59+
60+ this . metadataSubscription ?. unsubscribe ( ) ;
4061 }
4162
4263 protected abstract getSvelteComponent ( ) : typeof SvelteComponent ;
@@ -96,7 +117,7 @@ export abstract class AbstractInputField<MetadataValueType, ComponentValueType>
96117 */
97118 public setValue ( value : MetadataValueType ) : void {
98119 this . signal . set ( value ) ;
99- this . notifyListeners ( value ) ;
120+ this . notifySubscription ( value ) ;
100121 }
101122
102123 /**
@@ -108,6 +129,10 @@ export abstract class AbstractInputField<MetadataValueType, ComponentValueType>
108129 this . setValue ( this . mapValue ( value ) ) ;
109130 }
110131
132+ private notifySubscription ( value : MetadataValueType ) : void {
133+ this . metadataSubscription ?. update ( value ) ;
134+ }
135+
111136 private getDefaultValue ( ) : MetadataValueType {
112137 const defaultValueArgument = this . renderChild . getArgument ( InputFieldArgumentType . DEFAULT_VALUE ) ;
113138 if ( ! defaultValueArgument ) {
@@ -128,4 +153,8 @@ export abstract class AbstractInputField<MetadataValueType, ComponentValueType>
128153 public unmount ( ) : void {
129154 this . inputFieldComponent . unmount ( ) ;
130155 }
156+
157+ protected onmount ( ) : void { }
158+
159+ protected onunmount ( ) : void { }
131160}
0 commit comments