@ -10,7 +10,7 @@ import SessionUtilitiesKit
public protocol ObservableTableSource : AnyObject , SectionedTableData {
typealias TargetObservation = TableObservation < [ SectionModel ] >
typealias TargetPublisher = AnyPublisher < (( [SectionModel ] , StagedChangeset < [ SectionModel ] > ) ) , Error >
typealias TargetPublisher = AnyPublisher < [SectionModel ] , Error >
var dependencies : Dependencies { get }
var state : TableDataState < Section , TableItem > { get }
@ -23,11 +23,11 @@ public protocol ObservableTableSource: AnyObject, SectionedTableData {
}
extension ObservableTableSource {
public var pendingTableDataSubject : CurrentValueSubject < ( [SectionModel ] , StagedChangeset < [ SectionModel ] > ) , Never > {
public var pendingTableDataSubject : CurrentValueSubject < [SectionModel ] , Never > {
self . observableState . pendingTableDataSubject
}
public var observation : TargetObservation {
ObservationBuilder . change setS ubject( self . observableState . pendingTableDataSubject )
ObservationBuilder . subject( self . observableState . pendingTableDataSubject )
}
public var tableDataPublisher : TargetPublisher { self . observation . finalPublisher ( self , using : dependencies ) }
@ -40,7 +40,7 @@ extension ObservableTableSource {
public class ObservableTableSourceState < Section : SessionTableSection , TableItem : Hashable & Differentiable > : SectionedTableData {
public let forcedRefresh : AnyPublisher < Void , Never >
public let pendingTableDataSubject : CurrentValueSubject < ( [SectionModel ] , StagedChangeset < [ SectionModel ] > ) , Never >
public let pendingTableDataSubject : CurrentValueSubject < [SectionModel ] , Never >
// MARK: - I n t e r n a l V a r i a b l e s
@ -52,7 +52,7 @@ public class ObservableTableSourceState<Section: SessionTableSection, TableItem:
init ( ) {
self . hasEmittedInitialData = false
self . forcedRefresh = _forcedRefresh . shareReplay ( 0 )
self . pendingTableDataSubject = CurrentValueSubject ( ( [] , StagedChangeset ( ) ) )
self . pendingTableDataSubject = CurrentValueSubject ( [] )
}
}
@ -78,7 +78,7 @@ public struct TableObservation<T> {
_ source : S ,
using dependencies : Dependencies
) -> S . TargetPublisher {
typealias TargetData = (( [S . SectionModel ] , StagedChangeset < [ S . SectionModel ] > ) )
typealias TargetData = [S . SectionModel ]
switch ( self , self . generatePublisherWithChangeset ) {
case ( _ , . some ( let generatePublisherWithChangeset ) ) :
@ -177,29 +177,6 @@ public enum ObservationBuilder {
. manualRefreshFrom ( source . observableState . forcedRefresh )
}
}
// / T h e ` c h a n g e s e t S u b j e c t ` w i l l e m i t i m m e d i a t e l y w h e n t h e r e i s a s u b s c r i b e r a n d s t o r e t h e m o s t r e c e n t v a l u e t o b e e m i t t e d w h e n e v e r a n e w s u b s c r i b e r i s
// / a d d e d
static func changesetSubject < T > (
_ subject : CurrentValueSubject < ( [ T ] , StagedChangeset < [ T ] > ) , Never >
) -> TableObservation < [ T ] > {
return TableObservation { viewModel , dependencies in
subject
. withPrevious ( ( [ ] , StagedChangeset ( ) ) )
. handleEvents (
receiveCancel : {
// / W h e n w e u n s u b s c r i b e w e s e n d t h r o u g h t h e e x i s t i n g d a t a b u t c l e a r o u t t h e ` S t a g e d C h a n g e s e t ` v a l u e
// / s o t h a t r e s u b s c r i b i n g d o e s n ' t r e s u l t i n t h e U I t r y i n g t o r e a p p l y t h e s a m e c h a n g e s e t ( w h i c h w o u l d c a u s e a
// / c r a s h d u e t o i n v a l i d t a b l e v i e w c h a n g e s )
subject . send ( ( subject . value . 0 , StagedChangeset ( ) ) )
}
)
. map { _ , current -> ( [ T ] , StagedChangeset < [ T ] > ) in current }
. setFailureType ( to : Error . self )
. shareReplay ( 1 )
. eraseToAnyPublisher ( )
}
}
}
// MARK: - C o n v e n i e n c e T r a n s f o r m s
@ -249,27 +226,11 @@ public extension Array {
public extension Publisher {
func mapToSessionTableViewData < S : ObservableTableSource > (
for source : S
) -> AnyPublisher < ( Output , StagedChangeset < Output > ) , Failure > where Output = = [ ArraySection < S . Section , SessionCell . Info < S . TableItem > > ] {
) -> AnyPublisher < Output , Failure > where Output = = [ ArraySection < S . Section , SessionCell . Info < S . TableItem > > ] {
return self
. map { [ weak source ] updatedData -> ( Output , StagedChangeset < Output > ) in
let updatedDataWithPositions : Output = updatedData
. mapToSessionTableViewData ( for : source )
// G e n e r a t e a n u p d a t e d c h a n g e s e t
let changeset = StagedChangeset (
source : ( source ? . state . tableData ? ? [ ] ) ,
target : updatedDataWithPositions
)
return ( updatedDataWithPositions , changeset )
}
. filter { [ weak source ] _ , changeset in
source ? . observableState . hasEmittedInitialData = = false || // A l w a y s e m i t a t l e a s t o n c e
! changeset . isEmpty // D o n o t h i n g i f t h e r e w e r e n o c h a n g e s
. map { [ weak source ] updatedData -> Output in
updatedData . mapToSessionTableViewData ( for : source )
}
. handleEvents ( receiveOutput : { [ weak source ] _ in
source ? . observableState . hasEmittedInitialData = true
} )
. eraseToAnyPublisher ( )
}
}