-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
afa249c
commit 4d5e36c
Showing
12 changed files
with
131 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...mental/market/src/commonMain/kotlin/org/mobilenativefoundation/market/MemoizedSelector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.mobilenativefoundation.market | ||
|
||
|
||
class MemoizedSelector<S : Market.State, P : Any, R : Any>( | ||
private val selector: (S) -> R, | ||
private val getParams: (S) -> P, | ||
private val areParamsEqual: (P, P) -> Boolean = { p1, p2 -> p1 == p2 }, | ||
) : Market.Selector<S, R> { | ||
|
||
private var lastParams: P? = null | ||
private var lastResult: R? = null | ||
|
||
override fun select(state: S): R { | ||
val params = getParams(state) | ||
|
||
if (lastParams == null || params == Unit || !areParamsEqual(params, lastParams!!)) { | ||
lastParams = params | ||
lastResult = selector(state) | ||
} | ||
|
||
return lastResult!! | ||
} | ||
} | ||
|
||
fun <S : Market.State, P : Any, R : Any> memoize( | ||
selectState: (S) -> R, | ||
getParams: (S) -> P, | ||
areParamsEqual: (P, P) -> Boolean = { p1, p2 -> p1 == p2 } | ||
): Market.Selector<S, R> { | ||
return MemoizedSelector(selectState, getParams, areParamsEqual) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...rg/mobilenativefoundation/sample/octonaut/xplat/common/market/OctonautMemoizedSelector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.mobilenativefoundation.sample.octonaut.xplat.common.market | ||
|
||
import org.mobilenativefoundation.market.memoize | ||
|
||
fun <P : Any, R : Any> memoize( | ||
selectState: (OctonautMarketState) -> R, | ||
getParams: (OctonautMarketState) -> P, | ||
areParamsEqual: (P, P) -> Boolean = { p1, p2 -> p1 == p2 } | ||
) = memoize( | ||
selectState = selectState, | ||
getParams = getParams, | ||
areParamsEqual = areParamsEqual | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters