Skip to content

Commit 74c4f09

Browse files
authored
Merge pull request #164 from arkivanov/rename-InstanceKeeper-getOrCreate-to-get
Renamed InstanceKeeper getOrCreate to get
2 parents d58ee1b + d8071b7 commit 74c4f09

File tree

8 files changed

+17
-21
lines changed

8 files changed

+17
-21
lines changed

keepers/src/androidTest/kotlin/com/arkivanov/mvikotlin/keepers/instancekeeper/AndroidInstanceKeeperTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class AndroidInstanceKeeperTest {
1515
fun retains_instance_WHEN_recreated() {
1616
val viewModelStore = ViewModelStore()
1717
val instance = Data()
18-
Owner(viewModelStore).keeper.getOrCreate(key = "data") { instance }
18+
Owner(viewModelStore).keeper.get(key = "data") { instance }
1919

20-
val retainedInstance = Owner(viewModelStore).keeper.getOrCreate<Data>(key = "data") { throw IllegalStateException() }
20+
val retainedInstance = Owner(viewModelStore).keeper.get<Data>(key = "data") { throw IllegalStateException() }
2121

2222
assertSame(instance, retainedInstance)
2323
}
@@ -29,7 +29,7 @@ class AndroidInstanceKeeperTest {
2929
val instances = List(10) { Data() }
3030

3131
instances.forEachIndexed { index, data ->
32-
owner.keeper.getOrCreate(index) { data }
32+
owner.keeper.get(index) { data }
3333
}
3434

3535
viewModelStore.clear()

keepers/src/commonMain/kotlin/com/arkivanov/mvikotlin/keepers/instancekeeper/DefaultInstanceKeeper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class DefaultInstanceKeeper : InstanceKeeper {
1515
private val map = HashMap<Any, InstanceKeeper.Instance>()
1616

1717
@Suppress("UNCHECKED_CAST")
18-
override fun <T : InstanceKeeper.Instance> getOrCreate(key: Any, factory: () -> T): T =
18+
override fun <T : InstanceKeeper.Instance> get(key: Any, factory: () -> T): T =
1919
map.getOrPut(key, factory) as T
2020

2121
/**

keepers/src/commonMain/kotlin/com/arkivanov/mvikotlin/keepers/instancekeeper/InstanceKeeper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface InstanceKeeper {
1313
* @param factory a factory function, called when there is no retained `Instance` yet
1414
* @return either a currently retained `Instance` or a new one
1515
*/
16-
fun <T : Instance> getOrCreate(key: Any, factory: () -> T): T
16+
fun <T : Instance> get(key: Any, factory: () -> T): T
1717

1818
/**
1919
* Represents a retained instance
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package com.arkivanov.mvikotlin.keepers.instancekeeper
22

33
/**
4-
* Same as [InstanceKeeper.getOrCreate] but the key is `T::class`
4+
* Same as [InstanceKeeper.get] but the key is `T::class`
55
*/
66
@ExperimentalInstanceKeeperApi
7-
inline fun <reified T : InstanceKeeper.Instance> InstanceKeeper.getOrCreate(noinline factory: () -> T): T =
8-
getOrCreate(T::class, factory)
7+
inline fun <reified T : InstanceKeeper.Instance> InstanceKeeper.get(noinline factory: () -> T): T = get(T::class, factory)

keepers/src/commonTest/kotlin/com/arkivanov/mvikotlin/keepers/instancekeeper/DefaultInstanceKeeperTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class DefaultInstanceKeeperTest {
1212
val container = DefaultInstanceKeeper()
1313
val data = Data()
1414

15-
container.getOrCreate(key = "data") { data }
16-
val retainedData = container.getOrCreate<Data>(key = "data") { throw IllegalStateException() }
15+
container.get(key = "data") { data }
16+
val retainedData = container.get<Data>(key = "data") { throw IllegalStateException() }
1717

1818
assertSame(data, retainedData)
1919
}
@@ -24,7 +24,7 @@ class DefaultInstanceKeeperTest {
2424
val dataList = List(10) { Data() }
2525

2626
dataList.forEachIndexed { index, data ->
27-
container.getOrCreate(index) { data }
27+
container.get(index) { data }
2828
}
2929

3030
container.destroy()

mvikotlin/src/commonMain/kotlin/com/arkivanov/mvikotlin/core/instancekeeper/InstanceKeeperExt.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,13 @@ fun <T : Store<*, *, *>> OldInstanceKeeper<T>.getOrCreateStore(factory: () -> T)
4040
}
4141

4242
@com.arkivanov.mvikotlin.keepers.instancekeeper.ExperimentalInstanceKeeperApi
43-
fun <T : Store<*, *, *>> InstanceKeeper.getOrCreateStore(key: Any, factory: () -> T): T =
44-
getOrCreate(key) {
43+
fun <T : Store<*, *, *>> InstanceKeeper.getStore(key: Any, factory: () -> T): T =
44+
get(key) {
4545
StoreInstance(factory())
4646
}.store
4747

4848
@com.arkivanov.mvikotlin.keepers.instancekeeper.ExperimentalInstanceKeeperApi
49-
inline fun <reified T : Store<*, *, *>> InstanceKeeper.getOrCreateStore(
50-
noinline factory: () -> T
51-
): T =
52-
getOrCreateStore(T::class, factory)
49+
inline fun <reified T : Store<*, *, *>> InstanceKeeper.getStore(noinline factory: () -> T): T = getStore(T::class, factory)
5350

5451
@com.arkivanov.mvikotlin.keepers.instancekeeper.ExperimentalInstanceKeeperApi
5552
private class StoreInstance<out T : Store<*, *, *>>(

sample/todo-coroutines/src/commonMain/kotlin/com/arkivanov/mvikotlin/sample/todo/coroutines/controller/TodoListCoroutinesController.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.arkivanov.mvikotlin.sample.todo.coroutines.controller
22

33
import com.arkivanov.mvikotlin.core.binder.BinderLifecycleMode
4-
import com.arkivanov.mvikotlin.core.instancekeeper.getOrCreateStore
4+
import com.arkivanov.mvikotlin.core.instancekeeper.getStore
55
import com.arkivanov.mvikotlin.core.lifecycle.Lifecycle
66
import com.arkivanov.mvikotlin.core.lifecycle.doOnDestroy
77
import com.arkivanov.mvikotlin.extensions.coroutines.bind
@@ -47,7 +47,7 @@ class TodoListCoroutinesController internal constructor(
4747
)
4848

4949
private val todoListStore =
50-
dependencies.instanceKeeper.getOrCreateStore {
50+
dependencies.instanceKeeper.getStore {
5151
TodoListStoreFactory(
5252
storeFactory = dependencies.storeFactory,
5353
database = dependencies.database,

sample/todo-reaktive/src/commonMain/kotlin/com/arkivanov/mvikotlin/sample/todo/reaktive/controller/TodoListReaktiveController.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.arkivanov.mvikotlin.sample.todo.reaktive.controller
22

33
import com.arkivanov.mvikotlin.core.binder.BinderLifecycleMode
4-
import com.arkivanov.mvikotlin.core.instancekeeper.getOrCreateStore
4+
import com.arkivanov.mvikotlin.core.instancekeeper.getStore
55
import com.arkivanov.mvikotlin.core.lifecycle.Lifecycle
66
import com.arkivanov.mvikotlin.core.lifecycle.doOnDestroy
77
import com.arkivanov.mvikotlin.extensions.reaktive.bind
@@ -31,7 +31,7 @@ class TodoListReaktiveController(
3131
) : TodoListController {
3232

3333
private val todoListStore =
34-
dependencies.instanceKeeper.getOrCreateStore {
34+
dependencies.instanceKeeper.getStore {
3535
TodoListStoreFactory(
3636
storeFactory = dependencies.storeFactory,
3737
database = dependencies.database

0 commit comments

Comments
 (0)