Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define Middleware, Reducer, and Effect #617

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.mobilenativefoundation.paging.core

/**
* Represents an effect that can be triggered after reducing a specific [PagingAction] and [PagingState] combination.
*
* Effects are side effects or additional actions that need to be performed after the state has been reduced based on a dispatched action.
* They can be used for tasks such as loading more data, updating the UI, triggering network requests, or any other side effects that depend on the current state and action.
*
* @param Id The type of the unique identifier for each item in the paged data.
* @param K The type of the key used for paging.
* @param P The type of the parameters associated with each page of data.
* @param D The type of the data items.
* @param E The type of errors that can occur during the paging process.
* @param A The type of custom actions that can be dispatched to modify the paging state.
* @param PA The specific type of [PagingAction] that triggers this effect.
* @param S The specific type of [PagingState] that triggers this effect.
*/
interface Effect<Id : Comparable<Id>, K : Any, P : Any, D : Any, E : Any, A : Any, PA : PagingAction<Id, K, P, D, E, A>, S : PagingState<Id, K, P, D, E>> {
operator fun invoke(action: PA, state: S, dispatch: (PagingAction<Id, K, P, D, E, A>) -> Unit)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.mobilenativefoundation.paging.core

/**
* Represents a middleware that intercepts and modifies paging actions before they reach the reducer.
*
* [Middleware] allows for pre-processing, logging, or any other custom logic to be applied to actions before they are handled by the [Reducer].
* It can also modify or replace the action before passing it to the next [Middleware] or [Reducer].
*
* @param Id The type of the unique identifier for each item in the paged data.
* @param K The type of the key used for paging.
* @param P The type of the parameters associated with each page of data.
* @param D The type of the data items.
* @param E The type of errors that can occur during the paging process.
* @param A The type of custom actions that can be dispatched to modify the paging state.
*/
interface Middleware<Id : Comparable<Id>, K : Any, P : Any, D : Any, E : Any, A : Any> {

/**
* Applies the middleware logic to the given [action].
*
* The middleware can perform any necessary pre-processing, logging, or modification of the action
* before invoking the [next] function to pass the action to the next middleware or the reducer.
*
* @param action The paging action to be processed by the middleware.
* @param next A suspending function that should be invoked with the processed action to pass it to the next middleware or the reducer.
* If the middleware does not want to pass the action further, it can choose not to invoke this function.
*/
suspend fun apply(action: PagingAction<Id, K, P, D, E, A>, next: suspend (PagingAction<Id, K, P, D, E, A>) -> Unit)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.mobilenativefoundation.paging.core

/**
* The [Reducer] is responsible for taking the current [PagingState] and a dispatched [PagingAction],
* and producing a new [PagingState] based on the action and the current state.
*
* @param Id The type of the unique identifier for each item in the paged data.
* @param K The type of the key used for paging.
* @param P The type of the parameters associated with each page of data.
* @param D The type of the data items.
* @param E The type of errors that can occur during the paging process.
* @param A The type of custom actions that can be dispatched to modify the paging state.
*/
interface Reducer<Id : Comparable<Id>, K : Any, P : Any, D : Any, E : Any, A : Any> {
/**
* Reduces the current [PagingState] based on the dispatched [PagingAction] and returns a new [PagingState].
*
* This function is called whenever a [PagingAction] is dispatched to update the paging state.
* It should handle the action and produce a new state based on the current state and the action.
*
* @param action The dispatched [PagingAction] to be reduced.
* @param state The current [PagingState] before applying the action.
* @return The new [PagingState] after applying the action to the current state.
*/
suspend fun reduce(
action: PagingAction<Id, K, P, D, E, A>,
state: PagingState<Id, K, P, D, E>
): PagingState<Id, K, P, D, E>
}
Loading