Created
April 7, 2020 17:07
-
-
Save patjackson52/020737717f101719c770ba1559f3eaf6 to your computer and use it in GitHub Desktop.
ProfileingStore - provides a wrapper around a ReduxKotlin store that profiles all calls to function. For testing purposes only, do not use in production.
This file contains hidden or 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
/** | |
* Wraps all calls to Redux store with a profile function that reports the | |
* milliseconds to complete operation. getCurrentSystemTime & logger are passed as params | |
* so can work in multiplatform (provide your own implementation) | |
* | |
* @param store Redux store from [https://ReduxKotlin.org] | |
* @param getCurrentSystemTime function that returns current sys time in ms | |
* @param logger function that logs | |
*/ | |
class ProfilingStore<T>( | |
val store: Store<T>, | |
val getCurrentSystemTime: () -> Long, | |
val logger: (String) -> Unit | |
) : Store<T> { | |
override var dispatch: Dispatcher = { action -> | |
profile("dispatch") { | |
store.dispatch(action) | |
} | |
} | |
override val getState: GetState<T> = { | |
profile("getState") { store.getState() } | |
} | |
override val replaceReducer: (Reducer<T>) -> Unit = { reducer -> | |
profile("replaceReducer") { store.replaceReducer(reducer) } | |
} | |
override val subscribe: (StoreSubscriber) -> StoreSubscription = { storeSubscriber: StoreSubscriber -> | |
profile("storeSubscriber") { store.subscribe(storeSubscriber) } | |
} | |
fun <T> profile(msg: String, block: () -> T): T { | |
val start = getCurrentSystemTime() | |
val value = block() | |
logger(msg + " completed in ${currentSystemTimeInMs() - start}ms") | |
return value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment