@@ -16,11 +16,6 @@ import { NgtTweakDebounce } from './debounce';
1616import { NgtTweakFolder } from './folder' ;
1717import { NgtTweakLabel } from './label' ;
1818
19- export type NgtTweakBindingParams < TParams extends BindingParams > = Omit <
20- TParams ,
21- 'tag' | 'label' | 'disabled' | 'hidden'
22- > ;
23-
2419export const NGT_TWEAK_BINDING_AS_HOST = new InjectionToken <
2520 true | { in : ( value : unknown ) => unknown ; out : ( value : unknown ) => unknown } | null
2621> ( 'hostDirective NgtTweakBinding' , { factory : ( ) => null } ) ;
@@ -47,13 +42,13 @@ export class NgtTweakBinding<TValue> {
4742 private injector = inject ( Injector ) ;
4843 private asHostDirective = inject ( NGT_TWEAK_BINDING_AS_HOST ) ;
4944
50- binding = signal < BindingApi < unknown , TValue > | null > ( null ) ;
5145 private bindingBaseParams = computed ( ( ) => ( {
5246 label : this . label . snapshot . label ,
5347 tag : this . label . snapshot . tag ,
5448 disabled : this . blade . disabled ( ) ,
5549 hidden : this . blade . hidden ( ) ,
5650 } ) ) ;
51+ private bindingParams = signal < Record < string , unknown > > ( { } ) ;
5752
5853 private get bindableObject ( ) {
5954 let value = untracked ( this . value ) ;
@@ -65,43 +60,64 @@ export class NgtTweakBinding<TValue> {
6560 return { value } ;
6661 }
6762
63+ private bindingApi = computed ( ( ) => {
64+ const parent = this . parent . folder ( ) ;
65+ if ( ! parent ) return null ;
66+
67+ const bindingParams = { ...this . bindingBaseParams ( ) , ...this . bindingParams ( ) } ;
68+ return parent . addBinding ( this . bindableObject , 'value' , bindingParams ) as BindingApi < unknown , TValue > ;
69+ } ) ;
70+
6871 constructor ( ) {
69- this . blade . startChangeEffect ( this . binding ) ;
70- this . label . startChangeEffect ( this . binding ) ;
71- this . debounce . startDebounceEffect ( this . binding , ( ev ) => {
72+ this . blade . startChangeEffect ( this . bindingApi ) ;
73+ this . label . startChangeEffect ( this . bindingApi ) ;
74+ this . debounce . startDebounceEffect ( this . bindingApi , ( ev ) => {
7275 if ( this . asHostDirective && typeof this . asHostDirective === 'object' ) {
7376 this . value . set ( this . asHostDirective . out ( ev . value ) as TValue ) ;
7477 } else {
7578 this . value . set ( ev . value ) ;
7679 }
7780 } ) ;
7881
79- if ( ! this . asHostDirective ) {
80- this . createBindingEffect ( this . bindingBaseParams ) ;
81- }
82+ effect ( ( onCleanup ) => {
83+ const bindingApi = this . bindingApi ( ) ;
84+ if ( ! bindingApi ) return ;
85+ onCleanup ( ( ) => {
86+ bindingApi . dispose ( ) ;
87+ } ) ;
88+ } ) ;
8289
8390 inject ( DestroyRef ) . onDestroy ( ( ) => {
84- this . binding ( ) ?. dispose ( ) ;
91+ this . bindingApi ( ) ?. dispose ( ) ;
8592 } ) ;
8693 }
8794
88- createBindingEffect ( params : ( ) => BindingParams ) {
95+ syncBindingParams ( params : ( ) => BindingParams ) {
8996 return effect (
90- ( onCleanup ) => {
91- const parent = this . parent . folder ( ) ;
92- if ( ! parent ) return ;
93-
94- const bindingParams = { ...this . bindingBaseParams ( ) , ...params ( ) } ;
95- const binding = parent . addBinding ( this . bindableObject , 'value' , bindingParams ) ;
96-
97- this . binding . set ( binding ) ;
98-
99- onCleanup ( ( ) => {
100- binding . dispose ( ) ;
101- this . binding . set ( null ) ;
102- } ) ;
97+ ( ) => {
98+ this . bindingParams . set ( params ( ) ) ;
10399 } ,
104100 { injector : this . injector } ,
105101 ) ;
106102 }
103+
104+ // createBindingEffect(params: () => BindingParams) {
105+ // return effect(
106+ // (onCleanup) => {
107+ // const parent = this.parent.folder();
108+ // if (!parent) return;
109+ //
110+ // const bindingParams = { ...this.bindingBaseParams(), ...params() };
111+ // const binding = parent.addBinding(this.bindableObject, 'value', bindingParams);
112+ //
113+ // this.bindingApi.set(binding);
114+ //
115+ // onCleanup(() => {
116+ // binding.dispose();
117+ // this.bindingApi.set(null);
118+ // });
119+ // },
120+ // { injector: this.injector },
121+ // );
122+ // }
107123}
0 commit comments