Skip to content

Commit

Permalink
Change middlewares and reducers lists to maps (#52)
Browse files Browse the repository at this point in the history
* Changed middlewares and reducers lists to maps in Redukt
* Udpated travis configuration to android 27
  • Loading branch information
flaviokreis authored Jun 5, 2018
1 parent 532245d commit 46eb7ca
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 76 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ env:
android:
components:
- tools
- build-tools-26.0.2 # TODO: build gradle without build-tools
- android-26
- build-tools-27.0.3 # TODO: build gradle without build-tools
- android-27
- extra-android-m2repository

jdk:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ allprojects {
Step 2. Add the dependency
```gradle
dependencies {
compile 'com.github.raulccabreu.redukt:core:0.1.1'
compile 'com.github.raulccabreu.redukt:ui:0.1.1'
compile 'com.github.raulccabreu.redukt:core:0.1.3'
compile 'com.github.raulccabreu.redukt:ui:0.1.3'
}
```

Expand All @@ -36,7 +36,7 @@ class CounterReducer : Reducer<Integer> {
}

val redukt = Redukt<Int>(0)
redukt.reducers.add(CounterReducer())
redukt.reducers["counterReducer"] = CounterReducer()
redukt.listeners.add(object: StateListener<String> {
override fun hasChanged(newState: Int, oldState: Int) = newState != oldState
override fun onChanged(state: Int) { println("count: $state") }
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.2.10'
ext.kotlin_version = '1.2.41'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:3.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'

Expand All @@ -23,7 +23,7 @@ allprojects {
}
}

project.ext.versionName = '0.1.1'
project.ext.versionName = '0.1.3'

task clean(type: Delete) {
delete rootProject.buildDir
Expand Down
11 changes: 6 additions & 5 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ tasks.withType(Test) {
}

android {
compileSdkVersion 26
compileSdkVersion 27
defaultConfig {
minSdkVersion 16
targetSdkVersion 26
targetSdkVersion 27
versionCode 1
versionName "0.1.1"
versionName "0.1.3"
}
buildTypes {
release {
Expand All @@ -29,7 +29,8 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:1.4.2"

testCompile 'junit:junit:4.12'
testImplementation 'junit:junit:4.12'
}
28 changes: 16 additions & 12 deletions core/src/main/java/com/github/raulccabreu/redukt/Redukt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,20 @@ import com.github.raulccabreu.redukt.middlewares.DebugMiddleware
import com.github.raulccabreu.redukt.middlewares.Middleware
import com.github.raulccabreu.redukt.reducers.Reducer
import com.github.raulccabreu.redukt.states.StateListener
import com.github.raulccabreu.redukt.utils.createLinkedMap
import java.util.concurrent.ConcurrentLinkedQueue
import kotlin.system.measureTimeMillis

class Redukt<T>(state: T, debug: Boolean = false) {
class Redukt<T>(state: T, val debug: Boolean = false) {
var state = state
private set
val reducers = ConcurrentLinkedQueue<Reducer<T>>()
val middlewares = ConcurrentLinkedQueue<Middleware<T>>()
val reducers = createLinkedMap<Reducer<T>>()
val middlewares = createLinkedMap<Middleware<T>>()
val listeners = ConcurrentLinkedQueue<StateListener<T>>()
val debug = debug
private val dispatcher = Dispatcher { reduce(it) }

init {
if (debug) {
val debugMiddleware = DebugMiddleware<T>()
middlewares.add(debugMiddleware)
listeners.add(debugMiddleware)
}
addDebugMiddleware()
start()
}

Expand All @@ -43,11 +39,11 @@ class Redukt<T>(state: T, debug: Boolean = false) {
val elapsed = measureTimeMillis {
val oldState = state
var tempState = state
middlewares.parallelFor { it.before(tempState, action) }
reducers.forEach { tempState = it.reduce(tempState, action) }
middlewares.ascendingMap().values.parallelFor { it.before(tempState, action) }
reducers.ascendingMap().values.forEach { tempState = it.reduce(tempState, action) }
state = tempState
listeners.parallelFor { notifyListeners(it, oldState) }
middlewares.parallelFor { it.after(tempState, action) }
middlewares.ascendingMap().values.parallelFor { it.after(tempState, action) }
}
log("<Redukt> has spent [$elapsed ms] with [${action.name}]")
}
Expand All @@ -59,4 +55,12 @@ class Redukt<T>(state: T, debug: Boolean = false) {
private fun notifyListeners(it: StateListener<T>, oldState: T) {
if (it.hasChanged(state, oldState)) it.onChanged(state)
}

private fun addDebugMiddleware() {
if (!debug) return

val debugMiddleware = DebugMiddleware<T>()
middlewares["com.github.raulccabreu.redukt.debugMiddleware"] = debugMiddleware
listeners.add(debugMiddleware)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.raulccabreu.redukt.utils

import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap

fun <T> createLinkedMap(): ConcurrentLinkedHashMap<String, T> {
return ConcurrentLinkedHashMap
.Builder<String, T>()
.maximumWeightedCapacity(1000)
.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class BaseAnnotatedMiddlewareTest {
signal.countDown()
}

redukt.middlewares.add(middleware)
redukt.middlewares["middleware"] = middleware

redukt.dispatch(Action("valid", "new state"), false)

Expand All @@ -58,7 +58,7 @@ class BaseAnnotatedMiddlewareTest {
signal.countDown()
})

redukt.middlewares.add(middleware)
redukt.middlewares["middleware"] = middleware

redukt.dispatch(Action("valid", "new state"), false)

Expand All @@ -82,7 +82,7 @@ class BaseAnnotatedMiddlewareTest {
signal.countDown()
})

redukt.middlewares.add(middleware)
redukt.middlewares["middleware"] = middleware

redukt.dispatch(Action("valid", "new state"), false)

Expand Down Expand Up @@ -112,8 +112,8 @@ class BaseAnnotatedMiddlewareTest {
signal.countDown()
})

redukt.middlewares.add(middleware)
redukt.reducers.add(changerReducer)
redukt.middlewares["middleware"] = middleware
redukt.reducers["changerReducer"] = changerReducer

redukt.dispatch(Action("valid", "new state"), false)

Expand All @@ -128,7 +128,7 @@ class BaseAnnotatedMiddlewareTest {
@Test
fun invalidBeforeActionException() {
try {
redukt.middlewares.add(InvalidBeforeAction())
redukt.middlewares["invalidBeforeAction"] = InvalidBeforeAction()
junit.framework.Assert.assertTrue(false)
} catch (ex: Exception) {
System.out.println("${ex.message}")
Expand All @@ -139,7 +139,7 @@ class BaseAnnotatedMiddlewareTest {
@Test
fun invalidBeforeActionMethodsException() {
try {
redukt.middlewares.add(InvalidBeforeActionMethods())
redukt.middlewares["invalidBeforeActionMethods"] = InvalidBeforeActionMethods()
junit.framework.Assert.assertTrue(false)
} catch (ex: Exception) {
System.out.println("${ex.message}")
Expand All @@ -150,7 +150,7 @@ class BaseAnnotatedMiddlewareTest {
@Test
fun invalidAfterActionException() {
try {
redukt.middlewares.add(InvalidAfterAction())
redukt.middlewares["invalidAfterAction"] = InvalidAfterAction()
junit.framework.Assert.assertTrue(false)
} catch (ex: Exception) {
System.out.println("${ex.message}")
Expand All @@ -161,7 +161,7 @@ class BaseAnnotatedMiddlewareTest {
@Test
fun invalidAfterActionMethodsException() {
try {
redukt.middlewares.add(InvalidAfterActionMethods())
redukt.middlewares["invalidAfterActionMethods"] = InvalidAfterActionMethods()
junit.framework.Assert.assertTrue(false)
} catch (ex: Exception) {
System.out.println("${ex.message}")
Expand All @@ -179,7 +179,7 @@ class BaseAnnotatedMiddlewareTest {
signal.countDown()
}

redukt.middlewares.add(middleware)
redukt.middlewares["middleware"] = middleware

redukt.dispatch(Action("valid", "new state"), false)

Expand All @@ -199,7 +199,7 @@ class BaseAnnotatedMiddlewareTest {
signal.countDown()
})

redukt.middlewares.add(middleware)
redukt.middlewares["middleware"] = middleware

redukt.dispatch(Action("valid", "new state"), false)

Expand All @@ -223,7 +223,7 @@ class BaseAnnotatedMiddlewareTest {
signal.countDown()
})

redukt.middlewares.add(middleware)
redukt.middlewares["middleware"] = middleware

redukt.dispatch(Action("valid", "new state"), false)

Expand All @@ -249,7 +249,7 @@ class BaseAnnotatedMiddlewareTest {
signal.countDown()
})

redukt.middlewares.add(middleware)
redukt.middlewares["middleware"] = middleware

redukt.dispatch(Action("action_a", "new state"), false)

Expand Down Expand Up @@ -288,7 +288,7 @@ class BaseAnnotatedMiddlewareTest {
signal.countDown()
})

redukt.middlewares.add(middleware)
redukt.middlewares["middleware"] = middleware

redukt.dispatch(Action("valid", "new state"), false)

Expand Down Expand Up @@ -329,8 +329,8 @@ class BaseAnnotatedMiddlewareTest {
signal.countDown()
})

redukt.middlewares.add(middleware)
redukt.reducers.add(reducer)
redukt.middlewares["middleware"] = middleware
redukt.reducers["reducer"] = reducer

redukt.dispatch(Action("valid", "new state"), false)

Expand Down Expand Up @@ -360,8 +360,8 @@ class BaseAnnotatedMiddlewareTest {
signal.countDown()
})

redukt.middlewares.add(middleware)
redukt.reducers.add(reducer)
redukt.middlewares["middleware"] = middleware
redukt.reducers["reducer"] = reducer

redukt.dispatch(Action("action_a", "new state"), false)

Expand Down Expand Up @@ -389,7 +389,7 @@ class BaseAnnotatedMiddlewareTest {
@Test
fun invalidAfterActionsMethodsException() {
try {
redukt.middlewares.add(InvalidAfterActionsMethods())
redukt.middlewares["invalidAfterActionsMethods"] = InvalidAfterActionsMethods()
junit.framework.Assert.assertTrue(false)
} catch (ex: Exception) {
System.out.println("${ex.message}")
Expand All @@ -400,7 +400,7 @@ class BaseAnnotatedMiddlewareTest {
@Test
fun invalidBeforeActionsMethodsException() {
try {
redukt.middlewares.add(InvalidBeforeActionsMethods())
redukt.middlewares["invalidBeforeActionsMethods"] = InvalidBeforeActionsMethods()
junit.framework.Assert.assertTrue(false)
} catch (ex: Exception) {
System.out.println("${ex.message}")
Expand Down Expand Up @@ -434,8 +434,8 @@ class BaseAnnotatedMiddlewareTest {
signal.countDown()
})

redukt.middlewares.add(middleware)
redukt.reducers.add(changerReducer)
redukt.middlewares["middleware"] = middleware
redukt.reducers["changerReducer"] = changerReducer

redukt.dispatch(Action("valid", "new state"), false)

Expand Down
Loading

0 comments on commit 46eb7ca

Please sign in to comment.