Skip to content

Commit 9acc4aa

Browse files
committed
Bind the composer strictly to the component lifecycle
1 parent 88da445 commit 9acc4aa

File tree

3 files changed

+19
-35
lines changed

3 files changed

+19
-35
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,11 @@ public class FooView extends AbstractChildView<FooViewModel> {
270270
}
271271
```
272272

273-
Composer Assignment. To assign a `Composer` to a `View` and a `Mediator` to a `ViewModel`, use the public method
274-
`AbstractComponentView#setComposer(...)`. There is also a `AbstractComponentView#createComposer()` method, which can
275-
be overridden to automatically create the `Composer` during construction.
273+
Composer Creation and Initialization. The `Composer` is created during the component’s pre-initialization phase via
274+
the protected `AbstractComponentView#createComposer()` method. Creating the composer at this stage ensures that both
275+
the `View` and the `ViewModel` are fully constructed, allowing the composer to immediately access and interact with
276+
their properties and methods. The composer is also initialized during the pre-initialization phase, while its
277+
deinitialization takes place in the component’s post-deinitialization phase.
276278

277279
Advantages of this approach:
278280

mvvm4fx-core/src/main/java/com/techsenger/mvvm4fx/core/AbstractComponentView.java

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public abstract class AbstractComponentView<T extends AbstractComponentViewModel
3333

3434
public AbstractComponentView(T viewModel) {
3535
this.viewModel = viewModel;
36-
setComposer(createComposer());
3736
}
3837

3938
@Override
@@ -95,34 +94,11 @@ public ComponentComposer<?> getComposer() {
9594
return composer;
9695
}
9796

98-
/**
99-
* Sets the {@link ComponentComposer} for this view and updates the view model with the corresponding
100-
* {@link ComposerMediator}.
101-
*
102-
* <p>When a non-{@code null} composer is provided, the view becomes associated with that composer, and the view
103-
* model receives the mediator obtained via {@link ComponentComposer#getMediator()}. This mediator enables
104-
* communication between the view model and the rest of the component through the composer.
105-
*
106-
* <p>If {@code composer} is {@code null}, the view is detached from any composer, and the view model's mediator
107-
* is cleared as well, effectively disconnecting both layers from the component's composition mechanism.
108-
*
109-
* @param composer the composer to attach to this view, or {@code null} to detach
110-
*/
111-
public void setComposer(ComponentComposer<?> composer) {
112-
this.composer = composer;
113-
if (composer == null) {
114-
viewModel.setMediator(null);
115-
} else {
116-
viewModel.setMediator(composer.getMediator());
117-
}
118-
}
119-
12097
/**
12198
* Creates a new {@link ComponentComposer} instance for this component.
12299
*
123-
* <p>This method is invoked during the component's construction phase and allows subclasses to provide a custom
124-
* composer implementation. However, this method should be used only if the component is the owner of the composer.
125-
* Otherwise the component setter should be used.
100+
* <p>This method is invoked during the component's pre-initialization phase and allows subclasses to provide
101+
* a custom composer implementation.
126102
*
127103
* <p>The default implementation returns {@code null}, meaning the component does not create a composer by default.
128104
*
@@ -146,7 +122,11 @@ protected ComponentDescriptor getDescriptor() {
146122
* The first method called in initialization.
147123
*/
148124
protected void preInitialize(T viewModel) {
149-
125+
this.composer = createComposer();
126+
if (this.composer != null) {
127+
viewModel.setMediator(this.composer.getMediator());
128+
this.composer.initialize();
129+
}
150130
}
151131

152132
/**
@@ -230,6 +210,8 @@ protected void unbuild(T viewModel) {
230210
* The last method called in deinitialization.
231211
*/
232212
protected void postDeinitialize(T viewModel) {
233-
213+
if (this.composer != null) {
214+
this.composer.deinitialize();
215+
}
234216
}
235217
}

mvvm4fx-core/src/main/java/com/techsenger/mvvm4fx/core/AbstractComponentViewModel.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,6 @@ public ComponentMediator getMediator() {
117117
return this.mediator;
118118
}
119119

120-
protected void setMediator(ComponentMediator mediator) {
121-
this.mediator = mediator;
122-
}
123-
124120
protected void initialize() {
125121

126122
}
@@ -139,6 +135,10 @@ protected void deinitialize() {
139135

140136
protected abstract ComponentDescriptor createDescriptor();
141137

138+
void setMediator(ComponentMediator mediator) {
139+
this.mediator = mediator;
140+
}
141+
142142
private ComponentHistory getOrRequestHistory() {
143143
if (this.history == null) {
144144
if (this.historyProvider == null) {

0 commit comments

Comments
 (0)