diff --git a/paging/core/src/commonMain/kotlin/org/mobilenativefoundation/paging/core/Effect.kt b/paging/core/src/commonMain/kotlin/org/mobilenativefoundation/paging/core/Effect.kt new file mode 100644 index 000000000..da2db0ed0 --- /dev/null +++ b/paging/core/src/commonMain/kotlin/org/mobilenativefoundation/paging/core/Effect.kt @@ -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, K : Any, P : Any, D : Any, E : Any, A : Any, PA : PagingAction, S : PagingState> { + operator fun invoke(action: PA, state: S, dispatch: (PagingAction) -> Unit) +} \ No newline at end of file diff --git a/paging/core/src/commonMain/kotlin/org/mobilenativefoundation/paging/core/Middleware.kt b/paging/core/src/commonMain/kotlin/org/mobilenativefoundation/paging/core/Middleware.kt new file mode 100644 index 000000000..bd7299432 --- /dev/null +++ b/paging/core/src/commonMain/kotlin/org/mobilenativefoundation/paging/core/Middleware.kt @@ -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, 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, next: suspend (PagingAction) -> Unit) +} \ No newline at end of file diff --git a/paging/core/src/commonMain/kotlin/org/mobilenativefoundation/paging/core/Reducer.kt b/paging/core/src/commonMain/kotlin/org/mobilenativefoundation/paging/core/Reducer.kt new file mode 100644 index 000000000..414205f05 --- /dev/null +++ b/paging/core/src/commonMain/kotlin/org/mobilenativefoundation/paging/core/Reducer.kt @@ -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, 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, + state: PagingState + ): PagingState +} \ No newline at end of file