Skip to content

Commit

Permalink
Using XxxOptions via the XxxRef interface
Browse files Browse the repository at this point in the history
With the implementation of the Strategy Interface (#84), we believe there will be cases in the future where players will
want to refer to the options, so we have made the change so that they can be referenced.

refs: #84
  • Loading branch information
ogaclejapan committed Aug 31, 2024
1 parent 0609434 commit df98250
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ class MutationPreviewClient(
marker: Marker
): MutationRef<T, S> {
val state = previewData[key.id] as? MutationState<T> ?: MutationState.initial()
return SnapshotMutation(key, marker, MutableStateFlow(state))
val options = key.onConfigureOptions()?.invoke(defaultMutationOptions) ?: defaultMutationOptions
return SnapshotMutation(key, options, marker, MutableStateFlow(state))
}

private class SnapshotMutation<T, S>(
override val key: MutationKey<T, S>,
override val options: MutationOptions,
override val marker: Marker,
override val state: StateFlow<MutationState<T>>
) : MutationRef<T, S> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class QueryPreviewClient(
marker: Marker
): QueryRef<T> {
val state = previewData[key.id] as? QueryState<T> ?: QueryState.initial()
return SnapshotQuery(key, marker, MutableStateFlow(state))
val options = key.onConfigureOptions()?.invoke(defaultQueryOptions) ?: defaultQueryOptions
return SnapshotQuery(key, options, marker, MutableStateFlow(state))
}

@Suppress("UNCHECKED_CAST")
Expand All @@ -53,7 +54,8 @@ class QueryPreviewClient(
marker: Marker
): InfiniteQueryRef<T, S> {
val state = previewData[key.id] as? QueryState<QueryChunks<T, S>> ?: QueryState.initial()
return SnapshotInfiniteQuery(key, marker, MutableStateFlow(state))
val options = key.onConfigureOptions()?.invoke(defaultQueryOptions) ?: defaultQueryOptions
return SnapshotInfiniteQuery(key, options, marker, MutableStateFlow(state))
}

override fun <T> prefetchQuery(
Expand All @@ -68,6 +70,7 @@ class QueryPreviewClient(

private class SnapshotQuery<T>(
override val key: QueryKey<T>,
override val options: QueryOptions,
override val marker: Marker,
override val state: StateFlow<QueryState<T>>
) : QueryRef<T> {
Expand All @@ -79,6 +82,7 @@ class QueryPreviewClient(

private class SnapshotInfiniteQuery<T, S>(
override val key: InfiniteQueryKey<T, S>,
override val options: QueryOptions,
override val marker: Marker,
override val state: StateFlow<QueryState<QueryChunks<T, S>>>
) : InfiniteQueryRef<T, S> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,24 @@ import soil.query.core.awaitOrNull
*/
interface InfiniteQueryRef<T, S> : Actor {

/**
* The [InfiniteQueryKey] for the Query.
*/
val key: InfiniteQueryKey<T, S>

/**
* The QueryOptions configured for the query.
*/
val options: QueryOptions

/**
* The Marker specified in [QueryClient.getInfiniteQuery].
*/
val marker: Marker

/**
* [State Flow][StateFlow] to receive the current state of the query.
*/
val state: StateFlow<QueryState<QueryChunks<T, S>>>

/**
Expand Down
5 changes: 5 additions & 0 deletions soil-query-core/src/commonMain/kotlin/soil/query/Mutation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import soil.query.core.Actor
*/
internal interface Mutation<T> : Actor {

/**
* The MutationOptions configured for the mutation.
*/
val options: MutationOptions

/**
* [State Flow][StateFlow] to receive the current state of the mutation.
*/
Expand Down
16 changes: 16 additions & 0 deletions soil-query-core/src/commonMain/kotlin/soil/query/MutationRef.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,24 @@ import soil.query.core.Marker
*/
interface MutationRef<T, S> : Actor {

/**
* The [MutationKey] for the Mutation.
*/
val key: MutationKey<T, S>

/**
* The MutationOptions configured for the mutation.
*/
val options: MutationOptions

/**
* The Marker specified in [MutationClient.getMutation].
*/
val marker: Marker

/**
* [State Flow][StateFlow] to receive the current state of the mutation.
*/
val state: StateFlow<MutationState<T>>

/**
Expand Down
7 changes: 6 additions & 1 deletion soil-query-core/src/commonMain/kotlin/soil/query/Query.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import soil.query.core.Actor
*
* @param T Type of the return value from the query.
*/
internal interface Query<T>: Actor {
internal interface Query<T> : Actor {

/**
* The QueryOptions configured for the query.
*/
val options: QueryOptions

/**
* [Shared Flow][SharedFlow] to receive query [events][QueryEvent].
Expand Down
16 changes: 16 additions & 0 deletions soil-query-core/src/commonMain/kotlin/soil/query/QueryRef.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,24 @@ import soil.query.core.awaitOrNull
*/
interface QueryRef<T> : Actor {

/**
* The [QueryKey] for the Query.
*/
val key: QueryKey<T>

/**
* The QueryOptions configured for the query.
*/
val options: QueryOptions

/**
* The Marker specified in [QueryClient.getQuery].
*/
val marker: Marker

/**
* [State Flow][StateFlow] to receive the current state of the query.
*/
val state: StateFlow<QueryState<T>>

/**
Expand Down
38 changes: 18 additions & 20 deletions soil-query-core/src/commonMain/kotlin/soil/query/SwrCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,12 @@ class SwrCache(private val policy: SwrCachePolicy) : SwrClient, QueryMutableClie
}
}
return ManagedMutation(
scope = scope,
id = id,
options = options,
scope = scope,
actor = actor,
state = state,
command = command
command = command,
actor = actor
)
}

Expand Down Expand Up @@ -318,14 +318,14 @@ class SwrCache(private val policy: SwrCachePolicy) : SwrClient, QueryMutableClie
}
}
return ManagedQuery(
scope = scope,
id = id,
options = options,
scope = scope,
dispatch = dispatch,
actor = actor,
event = event,
state = state,
command = command
command = command,
actor = actor,
dispatch = dispatch
)
}

Expand Down Expand Up @@ -395,8 +395,7 @@ class SwrCache(private val policy: SwrCachePolicy) : SwrClient, QueryMutableClie
val query = getQuery(key, marker).also { it.launchIn(scope) }
return coroutineScope.launch {
try {
val options = key.configureOptions(defaultQueryOptions)
withTimeoutOrNull(options.prefetchWindowTime) {
withTimeoutOrNull(query.options.prefetchWindowTime) {
query.resume()
}
} finally {
Expand All @@ -413,8 +412,7 @@ class SwrCache(private val policy: SwrCachePolicy) : SwrClient, QueryMutableClie
val query = getInfiniteQuery(key, marker).also { it.launchIn(scope) }
return coroutineScope.launch {
try {
val options = key.configureOptions(defaultQueryOptions)
withTimeoutOrNull(options.prefetchWindowTime) {
withTimeoutOrNull(query.options.prefetchWindowTime) {
query.resume()
}
} finally {
Expand Down Expand Up @@ -595,12 +593,12 @@ class SwrCache(private val policy: SwrCachePolicy) : SwrClient, QueryMutableClie
}

internal class ManagedMutation<T>(
val id: UniqueId,
val options: MutationOptions,
val scope: CoroutineScope,
internal val actor: ActorBlockRunner,
val id: UniqueId,
override val options: MutationOptions,
override val state: StateFlow<MutationState<T>>,
override val command: SendChannel<MutationCommand<T>>
override val command: SendChannel<MutationCommand<T>>,
internal val actor: ActorBlockRunner,
) : Mutation<T> {

override fun launchIn(scope: CoroutineScope): Job {
Expand All @@ -623,14 +621,14 @@ class SwrCache(private val policy: SwrCachePolicy) : SwrClient, QueryMutableClie
) : MutationCommand.Context<T>

internal class ManagedQuery<T>(
val id: UniqueId,
val options: QueryOptions,
val scope: CoroutineScope,
val dispatch: QueryDispatch<T>,
internal val actor: ActorBlockRunner,
val id: UniqueId,
override val options: QueryOptions,
override val event: MutableSharedFlow<QueryEvent>,
override val state: StateFlow<QueryState<T>>,
override val command: SendChannel<QueryCommand<T>>
override val command: SendChannel<QueryCommand<T>>,
internal val actor: ActorBlockRunner,
private val dispatch: QueryDispatch<T>
) : Query<T> {

override fun launchIn(scope: CoroutineScope): Job {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ internal class SwrInfiniteQuery<T, S>(
private val query: Query<QueryChunks<T, S>>
) : InfiniteQueryRef<T, S> {

override val options: QueryOptions
get() = query.options

override val state: StateFlow<QueryState<QueryChunks<T, S>>>
get() = query.state

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ internal class SwrMutation<T, S>(
private val mutation: Mutation<T>
) : MutationRef<T, S> {

override val options: MutationOptions
get() = mutation.options

override val state: StateFlow<MutationState<T>>
get() = mutation.state

Expand Down
3 changes: 3 additions & 0 deletions soil-query-core/src/commonMain/kotlin/soil/query/SwrQuery.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ internal class SwrQuery<T>(
private val query: Query<T>
) : QueryRef<T> {

override val options: QueryOptions
get() = query.options

override val state: StateFlow<QueryState<T>>
get() = query.state

Expand Down

0 comments on commit df98250

Please sign in to comment.