Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface AutoStore {
String value() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,11 @@
@Getter
public class ReflectionDuxSliceBuilder<T extends State> {

private String storeName;
private String basePackage;
private List<Consumer<T>> subscribers = new ArrayList<>();
private Middleware<T> middleware;
private T initialState;

public ReflectionDuxSliceBuilder<T> setStoreName(String storeName) {
this.storeName = storeName;
return this;
}

public ReflectionDuxSliceBuilder<T> setBasePackage(String basePackage) {
this.basePackage = basePackage;
return this;
Expand Down Expand Up @@ -57,8 +51,7 @@ private Map<String, Reducer<T>> discoverReducers() {
Map<String, Reducer<T>> reducers = new HashMap<>();
for (Class<?> clazz : annotated) {
AutoStore annotation = clazz.getAnnotation(AutoStore.class);
if (annotation.value().equals(storeName) && ReducerBlock.class.isAssignableFrom(clazz)) {
System.out.println("Found reducer for : " + this.storeName + " " + clazz.getName());
if (annotation != null && ReducerBlock.class.isAssignableFrom(clazz)) {
ReducerBlock<T> instance = getInstance(clazz);
reducers.put(instance.getType(), instance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public void reflectionSliceBuildPerformance() {
for (int i = 0; i < BUILDS; i++) {
Slice<UserProfile> slice = new ReflectionDuxSliceBuilder<UserProfile>()
.setInitialState(new UserProfile("Karan", "karan@hello.com"))
.setStoreName("CleanStore")
.setBasePackage("org.flux.store.tests.reflection.clean")
.build();
assertTrue(slice.getState().getName().contains("Karan"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.flux.store.api.v3.ReducerBlock;
import org.flux.store.tests.domain.UserProfile;

@AutoStore("MyStore")
@AutoStore
public class DuplicateNameReducer implements ReducerBlock<UserProfile> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.flux.store.api.v3.AutoStore;
import org.flux.store.tests.domain.UserProfile;

@AutoStore("MyStore")
@AutoStore
public class SetEmailReducer implements ReducerBlock<UserProfile> {
@Override
public String getType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.flux.store.api.v3.AutoStore;
import org.flux.store.tests.domain.UserProfile;

@AutoStore("MyStore")
@AutoStore
public class SetNameReducer implements ReducerBlock<UserProfile> {
@Override
public String getType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.flux.store.api.v3.ReducerBlock;
import org.flux.store.tests.domain.UserProfile;

@AutoStore("CleanStore")
@AutoStore
public class CleanReducer implements ReducerBlock<UserProfile> {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.flux.store.tests.reflection;
package org.flux.store.tests.reflection.other;

import org.flux.store.api.v1.Action;
import org.flux.store.api.v3.AutoStore;
import org.flux.store.api.v3.ReducerBlock;
import org.flux.store.tests.domain.UserProfile;

@AutoStore("DifferentStore")
@AutoStore
public class DifferentStoreReducer implements ReducerBlock<UserProfile> {

@Override
Expand All @@ -18,4 +18,4 @@ public UserProfile reduce(Action action, UserProfile state) {
state.setName("different:" + action.getPayload().toString());
return state;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.flux.store.tests.reflection;
package org.flux.store.tests.reflection.other;

import org.flux.store.api.v1.Action;
import org.flux.store.api.v3.AutoStore;
import org.flux.store.api.v3.ReducerBlock;
import org.flux.store.tests.domain.UserProfile;

@AutoStore("OtherStore")
@AutoStore
public class OtherStoreReducer implements ReducerBlock<UserProfile> {

@Override
Expand All @@ -18,4 +18,4 @@ public UserProfile reduce(Action action, UserProfile state) {
state.setName("other:" + action.getPayload().toString());
return state;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class ReflectionReliabilityTest {
public void reducersForOtherStoreAreIgnored() {
Slice<UserProfile> slice = new ReflectionDuxSliceBuilder<UserProfile>()
.setInitialState(new UserProfile("Karan", "karan@hello.com"))
.setStoreName("MyStore")
.setBasePackage(BASE_PACKAGE)
.build();

Expand All @@ -27,40 +26,12 @@ public void reducersForOtherStoreAreIgnored() {
assertDoesNotThrow(() -> slice.getAction("setName"));
}

@Test
public void missingStoreNameMatchesNothing() {
Slice<UserProfile> slice = new ReflectionDuxSliceBuilder<UserProfile>()
.setInitialState(new UserProfile("Karan", "karan@hello.com"))
.setBasePackage(BASE_PACKAGE)
.build();

assertThrows(InvalidActionException.class, () -> slice.getAction("setName"));
}

@Test
public void missingBasePackageThrows() {
assertThrows(RuntimeException.class, () ->
new ReflectionDuxSliceBuilder<UserProfile>()
.setInitialState(new UserProfile("Karan", "karan@hello.com"))
.setStoreName("MyStore")
.build()
);
}

@Test
public void duplicateReducerTypesAreResolved() throws InvalidActionException {
// Both SetNameReducer and DuplicateNameReducer are annotated with MyStore and handle "setName".
// The slice must still be usable; whichever reducer wins, the action should be dispatchable.
Slice<UserProfile> slice = new ReflectionDuxSliceBuilder<UserProfile>()
.setInitialState(new UserProfile("Karan", "karan@hello.com"))
.setStoreName("MyStore")
.setBasePackage(BASE_PACKAGE)
.build();

Consumer setName = slice.getAction("setName");
setName.accept("Alice");
String result = slice.getState().getName();
assertTrue("Alice".equals(result) || "ALICE".equals(result),
"One of the duplicate reducers should have won");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class ReflectionSliceBuilderTest {
public void init() {
this.slice = new ReflectionDuxSliceBuilder<UserProfile>()
.setInitialState(new UserProfile("Karan Gupta", "karan@hello.com"))
.setStoreName("MyStore")
.setBasePackage("org.flux.store.tests.reflection")
.build();
}
Expand Down
Loading