Skip to content

Commit

Permalink
Refactor slightly API to work with datafibers
Browse files Browse the repository at this point in the history
  • Loading branch information
PhantomYdn committed Mar 30, 2021
1 parent 18b3391 commit bfcb648
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ChatApp extends VueComponent<Void> {

public ChatApp(String id) {
super(id);
dataFiberBuilder(MESSAGES, "messages").init().build();
dataFiberBuilder(MESSAGES, "messages").init().bind();
}

@VueMethod
Expand Down
22 changes: 21 additions & 1 deletion vuecket/src/main/java/org/orienteer/vuecket/DataFiber.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,25 @@ public static enum DataFiberType {
private final DataFiberType type;
private final String name;
private final IModel<T> model;
private final IModel<T> initPropModel;
private int revisionHash=-1;
private final boolean init;
private final boolean update;
private final boolean observe;

public DataFiber(DataFiberType type, String name, IModel<T> model, boolean init, boolean update, boolean observe) {
public DataFiber(DataFiberType type, String name, IModel<T> model, boolean init , boolean update, boolean observe) {
this(type, name, model, null, init, update, observe);
}

public DataFiber(String name, IModel<T> model, IModel<T> initPropModel, boolean update, boolean observe) {
this(DataFiberType.PROPERTY, name, model, initPropModel, initPropModel!=null, update, observe);
}

DataFiber(DataFiberType type, String name, IModel<T> model, IModel<T> initPropModel, boolean init, boolean update, boolean observe) {
this.type = type;
this.name = name;
this.model = model;
this.initPropModel = initPropModel;
this.revisionHash = Objects.hashCode(model!=null?model.getObject():null);
this.init = init || update; //If fiber updatable: it should be also initialized
this.update = update;
Expand All @@ -55,6 +65,10 @@ public IModel<T> getModel() {
return model;
}

public IModel<T> getInitPropModel() {
return initPropModel;
}

public T getValue() {
T value = getValueInternal();
revisionHash = Objects.hashCode(value);
Expand Down Expand Up @@ -123,10 +137,16 @@ public boolean shouldUpdate() {
public boolean shouldObserve() {
return observe;
}

public DataFiber<T> bind(IVueBehaviorLocator locator) {
locator.getVueBehavior().getDataFibers().registerDataFiber(this);
return this;
}

@Override
public void detach() {
if(model!=null) model.detach();
if(initPropModel!=null) initPropModel.detach();
}

}
19 changes: 13 additions & 6 deletions vuecket/src/main/java/org/orienteer/vuecket/DataFiberBuilder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.orienteer.vuecket;

import org.apache.wicket.model.IModel;
import org.orienteer.vuecket.DataFiber.DataFiberType;

/**
* Builder to simplify association of IModels with different fibers: property, load, observe, refresh
Expand All @@ -13,7 +14,8 @@ public class DataFiberBuilder<M, V extends IVueBehaviorLocator> {
private final String name;
private final IModel<M> model;

private boolean property;
private IModel<M> initPropValue;
private DataFiberType type=DataFiberType.DATA;
private boolean init;
private boolean update;
private boolean observe;
Expand All @@ -25,12 +27,17 @@ public class DataFiberBuilder<M, V extends IVueBehaviorLocator> {
}

public DataFiberBuilder<M, V> bindToProperty() {
this.property = true;
return bindToProperty(null);
}

public DataFiberBuilder<M, V> bindToProperty(IModel<M> initPropValue) {
this.type = DataFiberType.PROPERTY;
this.initPropValue = initPropValue;
return this;
}

public DataFiberBuilder<M, V> bindToData() {
this.property = false;
this.type = DataFiberType.DATA;
return this;
}

Expand All @@ -50,9 +57,9 @@ public DataFiberBuilder<M, V> update() {
}


public V build() {
VueBehavior vueBehavior = locator.getVueBehavior();
vueBehavior.addDataFiber(name, model, property, init, update, observe);
public V bind() {
DataFiber<M> df = new DataFiber<M>(type, name, model, initPropValue, init, update, observe);
df.bind(locator);
return locator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ public default <M> DataFiberBuilder<M, C> dataFiberBuilder(IModel<M> model, Stri
return new DataFiberBuilder<M, C>(getThisComponent(), model, name);
}

public default <M> C addDataFiber(String name, IModel<M> model, boolean prop, boolean load, boolean observe, boolean refresh) {
getVueBehavior().addDataFiber(name, model, prop, load, observe, refresh);
return getThisComponent();
}

public default int getRefreshPeriod() {
return getVueBehavior().getRefreshPeriod();
}
Expand Down
15 changes: 10 additions & 5 deletions vuecket/src/main/java/org/orienteer/vuecket/VueBehavior.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,17 @@ public <M> DataFiberBuilder<M, VueBehavior> dataFiberBuilder(IModel<M> model, St
return new DataFiberBuilder<M, VueBehavior>(this, model, name);
}

public <M> VueBehavior addDataFiber(String name, IModel<M> model, boolean property, boolean init, boolean update, boolean observe) {
DataFibersGroup getDataFibers() {
return dataFibers;
}

/*public <M> VueBehavior addDataFiber(String name, IModel<M> model, boolean property, boolean init, boolean update, boolean observe) {
DataFiber<M> df = new DataFiber<M>(property?DataFiberType.PROPERTY:DataFiberType.DATA,
name, model, init, update, observe);
dataFibers.registerDataFiber(df);
return this;
}
}*/

@Override
protected void onBind() {
Expand All @@ -308,11 +312,12 @@ protected void onBind() {
public void vcInit(IVuecketMethod.Context ctx, Collection<String> toBeLoaded) {
Map<String, DataFiber<?>> initFibers = dataFibers.getDataFibersAsMap(DataFibersGroup.INIT_DATAFIBERS);
if(toBeLoaded!=null && !toBeLoaded.isEmpty()) initFibers.keySet().retainAll(toBeLoaded);
Map<String, Object> loadPatch = new HashMap<String, Object>();
Map<String, Object> dataPatch = new HashMap<String, Object>();
Map<String, Object> propsPatch = new HashMap<String, Object>();
for (DataFiber<?> df : initFibers.values()) {
loadPatch.put(df.getName(), df.getValue());
df.updatePatch(dataPatch, propsPatch);
}
IVuecketMethod.pushPatch(ctx, loadPatch, null);
IVuecketMethod.pushPatch(ctx, dataPatch, propsPatch);
}

@VueMethod
Expand Down
4 changes: 2 additions & 2 deletions vuecket/src/test/java/org/orienteer/vuecket/HomePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public HomePage(final PageParameters parameters) {
super(parameters);
add(new VueComponent<String>("app", Model.of("my attr"))
.setVueDescriptor("{ data: { message : 'Hello Vue'}}")
.dataFiberBuilder("customattr").bindToProperty().build()
.dataFiberBuilder("customattr").bindToProperty().bind()
);

add(new VueComponent<String>("app2")
Expand Down Expand Up @@ -63,7 +63,7 @@ public void countChanged(Integer newCount, Integer oldCount) {
.init()
.observe()
.update()
.build();
.bind();
app6.add(new VueMarkdown("markdown", ""));
add(app6);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class VueCustomComponent extends VueComponent<String> {

public VueCustomComponent(String id, IModel<String> model) {
super(id, model);
dataFiberBuilder("content").bindToProperty().update().build();
dataFiberBuilder("content").bindToProperty().update().bind();
}


Expand Down

0 comments on commit bfcb648

Please sign in to comment.