Skip to content

Commit 7b99820

Browse files
author
Patrick Jackson
committed
[WIP] conversion of classes & interfaces to typealiases
1 parent c865abc commit 7b99820

25 files changed

+83
-429
lines changed

common/src/commonMain/kotlin/com/beyondeye/reduks/DispatcherFn.kt

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
package com.beyondeye.reduks
22

33
/**
4-
* single method interface, mainly used because kotlin does not support yet type alias for function types
54
* see also https://github.com/reactjs/redux/blob/master/docs/Glossary.md#middleware
65
*/
76

87
typealias Middleware<State> = (store: Store<State>, nextDispatcher: (Any) -> Any, action: Any) -> Any
9-
10-
//interface Middleware<S> {
11-
// fun dispatch(store: Store<S>, nextDispatcher: (Any)->Any, action: Any): Any
12-
//}

common/src/commonMain/kotlin/com/beyondeye/reduks/Reducer.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.beyondeye.reduks
22

33
/**
4-
* single method interface, mainly used because kotlin does not support yet type alias for function types
54
* see also https://github.com/reactjs/redux/blob/master/docs/Glossary.md#reducer
65
*/
76

common/src/commonMain/kotlin/com/beyondeye/reduks/Reduks.kt

Lines changed: 0 additions & 54 deletions
This file was deleted.

common/src/commonMain/kotlin/com/beyondeye/reduks/ReduksInternalLogUtils.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package com.beyondeye.reduks
22

33
object ReduksInternalLogUtils {
44
fun reportErrorInReducer(s:Store<*>, e:Throwable) {
5-
s.errorLogFn?.invoke("REDUKS: exception while running reducer: ${e}")
5+
s.errorLogFn?.invoke("REDUKS: exception while running reducer: $e")
66
}
77

88
fun reportErrorInSubscriber(s:Store<*>, e:Throwable) {
9-
s.errorLogFn?.invoke("REDUKS: exception while notifying subscriber: ${e}")
9+
s.errorLogFn?.invoke("REDUKS: exception while notifying subscriber: $e")
1010
}
1111
}

common/src/commonMain/kotlin/com/beyondeye/reduks/SimpleStore.kt

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,9 @@ class SimpleStore<S>(initialState: S, private var reducer: Reducer<S>) : Store<S
99
dispatch(INIT())
1010
}
1111

12-
class Creator<S>(val withStandardMiddlewares: Boolean = true) : StoreCreator<S> {
13-
override fun create(reducer: Reducer<S>, initialState: S): Store<S> {
14-
val res = SimpleStore<S>(initialState, reducer)
15-
return if (!withStandardMiddlewares)
16-
res
17-
else
18-
res.applyMiddleware(::thunkMiddleware)
19-
}
20-
}
21-
2212
override var errorLogFn: ((String) -> Unit)? = null
2313
override var state: S = initialState
24-
private val subscribers = mutableListOf<StoreSubscriber<S>>()
14+
private val subscribers = mutableListOf<StoreSubscriber>()
2515
private fun mainDispatcher(store: Store<S>, nextDispatcher: (Any) -> Any, action: Any): Any {
2616
//Todo consider how accessing from multiple threads affects this.
2717
try {
@@ -32,7 +22,7 @@ class SimpleStore<S>(initialState: S, private var reducer: Reducer<S>) : Store<S
3222

3323
subscribers.forEach {
3424
try {
35-
it.onStateChange()
25+
it()
3626
} catch (e: Throwable) {
3727
ReduksInternalLogUtils.reportErrorInSubscriber(this@SimpleStore, e)
3828
}
@@ -55,17 +45,21 @@ class SimpleStore<S>(initialState: S, private var reducer: Reducer<S>) : Store<S
5545
action)
5646
}
5747

58-
override fun subscribe(storeSubscriber: StoreSubscriber<S>): StoreSubscription {
48+
override fun subscribe(storeSubscriber: StoreSubscriber): StoreSubscription {
5949
this.subscribers.add(storeSubscriber)
60-
return object : StoreSubscription {
61-
override fun unsubscribe() {
62-
subscribers.remove(storeSubscriber)
63-
}
64-
}
50+
return { subscribers.remove(storeSubscriber) }
6551
}
6652

6753
companion object {
6854
val redukstag = "rdks"
55+
56+
fun <S> create(withStandardMiddlewares: Boolean = true): StoreCreator<S> = { reducer, initialState ->
57+
val res = SimpleStore(initialState, reducer)
58+
if (!withStandardMiddlewares)
59+
res
60+
else
61+
res.applyMiddleware(::thunkMiddleware)
62+
}
6963
}
7064
}
7165

common/src/commonMain/kotlin/com/beyondeye/reduks/Store.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface Store<S> {
1616
/**
1717
* return a subscription
1818
*/
19-
fun subscribe(storeSubscriber: StoreSubscriber<S>): StoreSubscription
19+
fun subscribe(storeSubscriber: StoreSubscriber): StoreSubscription
2020

2121
/**
2222
* replace current reducer with new one
@@ -44,12 +44,12 @@ operator fun ((action:Any) -> Any)?.invoke(action:Any):Any? =
4444
/**
4545
* extension method for directly provide a lambda as argument for store subscribe
4646
*/
47-
fun <S> Store<S>.subscribe(lambda: () -> Unit) = this.subscribe(StoreSubscriberFn<S> { lambda() })
47+
fun <S> Store<S>.subscribe(lambda: () -> Unit) = this.subscribe { lambda() }
4848

4949
/**
5050
* extension method for directly subscribing using a store subscriber builder
5151
*/
52-
fun <S> Store<S>.subscribe(sb: StoreSubscriberBuilder<S>?) =if(sb!=null) this.subscribe(sb.build(this)) else null
52+
fun <S> Store<S>.subscribe(sb: StoreSubscriberBuilder<S>?) =if(sb!=null) this.subscribe(sb(this)) else null
5353

5454
/**
5555
* extension method for checking at compile time that we only dispatch objects derived from
@@ -64,7 +64,3 @@ fun <S> Store<S>.dispatch_a(action: Action) = dispatch(action)
6464
fun <S> Store<S>.dispatch_sa(action: StandardAction) = dispatch(action)
6565

6666

67-
/**
68-
* extension method for obtained encapsulated [DispatcherFn] reference to the store [dispatch] method
69-
*/
70-
fun <S> Store<S>.getDispatcherFn() =DispatcherFn(this.dispatch)

common/src/commonMain/kotlin/com/beyondeye/reduks/StoreCreator.kt

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,29 @@ import com.beyondeye.reduks.middlewares.applyMiddleware
66
* Factory for some specific Store type
77
* Created by daely on 7/31/2016.
88
*/
9-
interface StoreCreator<S> {
10-
/**
11-
* create a new store associated to this specific factory type
12-
*/
13-
fun create(reducer: Reducer<S>, initialState: S):Store<S>
14-
}
15-
fun<S> StoreCreator<S>.enhancedWith(vararg enhancers: StoreEnhancer<S>):StoreCreator<S> {
16-
return combineEnhancers(*enhancers).enhance(this)
9+
typealias StoreCreator<S> = (reducer: Reducer<S>, initialState: S) -> Store<S>
10+
11+
fun <S> StoreCreator<S>.enhancedWith(vararg enhancers: StoreEnhancer<S>): StoreCreator<S> {
12+
return combineEnhancers(*enhancers)(this)
1713
}
1814

19-
fun <S> StoreCreator<S>.withMiddlewares(vararg middlewares: Middleware<S>):StoreCreator<S> = StoreCreatorWithMiddlewares(this,*middlewares)
15+
fun <S> StoreCreator<S>.withMiddlewares(vararg middlewares: Middleware<S>): StoreCreator<S> = StoreCreatorWithMiddlewares(this, *middlewares)
2016

21-
class StoreCreatorWithMiddlewares<S>(val creator:StoreCreator<S>,vararg middlewares_: Middleware<S>):StoreCreator<S> {
22-
val middlewares=middlewares_
23-
override fun create(reducer: Reducer<S>, initialState: S): Store<S> {
24-
val res=creator.create(reducer,initialState)
25-
return res.applyMiddleware(*middlewares)
17+
fun <S> StoreCreatorWithMiddlewares(creator: StoreCreator<S>, vararg middlewares: Middleware<S>): StoreCreator<S> {
18+
return { reducer: Reducer<S>, initialState: S ->
19+
val res = creator(reducer, initialState)
20+
val store = res.applyMiddleware(*middlewares)
21+
store
2622
}
2723
}
2824

2925
/**
3026
* create an enhanced store
3127
* extension method, so we save on method count
3228
*/
33-
fun<S> StoreCreator<S>.create(
29+
fun <S> StoreCreator<S>.create(
3430
reducer: Reducer<S>,
3531
initialState: S,
3632
enhancer: StoreEnhancer<S>): Store<S> {
37-
return enhancer.enhance(this).create(reducer, initialState)
33+
return enhancer(this)(reducer, initialState)
3834
}

common/src/commonMain/kotlin/com/beyondeye/reduks/StoreEnhancer.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ package com.beyondeye.reduks
33
/**
44
* get a store creator and return a new enhanced one
55
* see https://github.com/reactjs/redux/blob/master/docs/Glossary.md#store-enhancer
6-
7-
* single method interface, mainly used because kotlin does not support yet type alias for function types
8-
* Created by daely on 8/23/2016.
96
*/
10-
interface StoreEnhancer<S> {
11-
fun enhance(next: StoreCreator<S>): StoreCreator<S>
12-
}
7+
typealias StoreEnhancer<S> = (next: StoreCreator<S>) -> StoreCreator<S>
8+
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
package com.beyondeye.reduks
22

3-
/**
4-
* single method interface, mainly used because kotlin does not support yet type alias for function types
5-
*/
6-
interface StoreSubscriber<S> {
7-
fun onStateChange()
8-
}
3+
typealias StoreSubscriber = ()-> Unit

0 commit comments

Comments
 (0)