MobileNativeFoundation / Store

A Kotlin Multiplatform library for building network-resilient applications
https://mobilenativefoundation.github.io/Store/
Apache License 2.0
3.16k stars 197 forks source link

Paging #630

Open matt-ramotar opened 6 months ago

matt-ramotar commented 6 months ago

Context:

Description

Introducing a solution for paging in KMP projects. Motivations:

  1. Seamless integration with Store and Mutable Store
  2. Support for local mutations and streaming of child items within the list of paging items
  3. Support for custom reducer, middleware, and post-reducer effects

Type of Change

Test Plan

Unit tests

Checklist:

Before submitting your PR, please review and check all of the following:

Additional Notes:

Paging Technical Design Doc

1. Overview

Modular and flexible architecture. Using builder, reducer, middleware, and post-reducer effect patterns. Unidirectional data flow. The Pager is the main component. Actions are dispatched through the Pager. The PagerBuilder creates the Pager. It allows configuration of the paging behavior. The PagingSource defines the data loading logic. The FetchingStrategy determines when to fetch the next page. The AggregatingStrategy combines loaded pages into a single list. The Reducer handles state changes based on actions. When an action is dispatched, it goes through the middleware pipeline. The middleware can modify the action. The reducer then updates the state based on the action. After the reducer, we invoke post-reducer effects associated with the action and new state. The updated state is sent back the Pager and emitted to the UI.

2. Key Components

3. Customizations

Providing many extension points and customization options to tailor behavior. Main customization points:

4. Data Flow

Unidirectional data flow. Main steps:

  1. Pager is configured using PagerBuilder and provided an initial key, flow of anchor position, and paging config.
  2. Pager subscribes to the PagingSource to receive paging data updates.
  3. When a PagingAction is dispatched, it goes through the configured Middleware chain. This enables interception and modification of the action.
  4. The modified action reaches the Reducer, which reduces the current PagingState based on the action and returns a new PagingState.
  5. After reduction, any configured Effect instances are launched, enabling side effects to be performed based on the new PagingState.
  6. Pager updates StateManager with the new PagingState.
  7. FetchingStrategy determines when to fetch the next page of data based on the PagingConfig and current PagingState.
  8. When a new page needs to be fetched, QueueManager enqueues the page key, and the JobCoordinator coordinates the execution of the paging job.
  9. PagingSource loads the requested page and emits the loaded data through the PagingSourceStreamProvider.
  10. The loaded page is stored in the MutablePagingBuffer for efficient retrieval and aggregation.
  11. The AggregatingStrategy aggregates the loaded pages into a single list, which is then emitted through the Pager for consumption by the UI.

5. Sample Code

See https://github.com/MobileNativeFoundation/Store/tree/paging/paging

OliverRhyme commented 2 weeks ago

Any updates on this?