Skip to content

Commit

Permalink
Merge pull request #140 from arkivanov/remove-SimpleInstance
Browse files Browse the repository at this point in the history
Don't use reified T in getOrCreate
  • Loading branch information
arkivanov authored Jan 22, 2024
2 parents 7aa555d + 4e5c276 commit b433410
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions instance-keeper/api/android/instance-keeper.api
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public final class com/arkivanov/essenty/instancekeeper/InstanceKeeperDispatcher
public static final fun InstanceKeeperDispatcher ()Lcom/arkivanov/essenty/instancekeeper/InstanceKeeperDispatcher;
}

public final class com/arkivanov/essenty/instancekeeper/InstanceKeeperExtKt {
public static final fun getOrCreate (Lcom/arkivanov/essenty/instancekeeper/InstanceKeeper;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)Lcom/arkivanov/essenty/instancekeeper/InstanceKeeper$Instance;
public static final fun getOrCreateSimple (Lcom/arkivanov/essenty/instancekeeper/InstanceKeeper;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
}

public abstract interface class com/arkivanov/essenty/instancekeeper/InstanceKeeperOwner {
public abstract fun getInstanceKeeper ()Lcom/arkivanov/essenty/instancekeeper/InstanceKeeper;
}
Expand Down
5 changes: 5 additions & 0 deletions instance-keeper/api/jvm/instance-keeper.api
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public final class com/arkivanov/essenty/instancekeeper/InstanceKeeperDispatcher
public static final fun InstanceKeeperDispatcher ()Lcom/arkivanov/essenty/instancekeeper/InstanceKeeperDispatcher;
}

public final class com/arkivanov/essenty/instancekeeper/InstanceKeeperExtKt {
public static final fun getOrCreate (Lcom/arkivanov/essenty/instancekeeper/InstanceKeeper;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)Lcom/arkivanov/essenty/instancekeeper/InstanceKeeper$Instance;
public static final fun getOrCreateSimple (Lcom/arkivanov/essenty/instancekeeper/InstanceKeeper;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
}

public abstract interface class com/arkivanov/essenty/instancekeeper/InstanceKeeperOwner {
public abstract fun getInstanceKeeper ()Lcom/arkivanov/essenty/instancekeeper/InstanceKeeper;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import com.arkivanov.essenty.instancekeeper.InstanceKeeper.SimpleInstance
* Returns a previously stored [InstanceKeeper.Instance] of type [T] with the given key,
* or creates and stores a new one if it doesn't exist.
*/
inline fun <reified T : InstanceKeeper.Instance> InstanceKeeper.getOrCreate(key: Any, factory: () -> T): T {
inline fun <T : InstanceKeeper.Instance> InstanceKeeper.getOrCreate(key: Any, factory: () -> T): T {
@Suppress("UNCHECKED_CAST") // Assuming the type per key is always the same
var instance: T? = get(key) as T?
if (instance == null) {
instance = factory()
Expand All @@ -29,7 +30,7 @@ inline fun <reified T : InstanceKeeper.Instance> InstanceKeeper.getOrCreate(fact
*
* This overload is for simple cases when instance destroying is not required.
*/
inline fun <reified T> InstanceKeeper.getOrCreateSimple(key: Any, factory: () -> T): T =
inline fun <T> InstanceKeeper.getOrCreateSimple(key: Any, factory: () -> T): T =
getOrCreate(key = key) { SimpleInstance(factory()) }
.instance

Expand Down

0 comments on commit b433410

Please sign in to comment.