Skip to content

Commit

Permalink
Change replay function type
Browse files Browse the repository at this point in the history
  • Loading branch information
Qata committed Jul 29, 2021
1 parent 52a90c1 commit d1b8dc4
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions Sources/RecombinePackage/Store/BaseStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ public class BaseStore<State: Equatable, RawAction, RefinedAction>: StoreProtoco
)
}

open func _replay<S: Sequence>(_ values: S) where S.Element == (offset: Double, actions: [RefinedAction]) {
open func replay<S: Sequence>(_ values: S) where S.Element == (offset: Double, actions: [RefinedAction]) {
dispatchEnabled = false
values
.publisher
.flatMap { offset, actions in
Expand All @@ -197,30 +198,15 @@ public class BaseStore<State: Equatable, RawAction, RefinedAction>: StoreProtoco
scheduler: DispatchQueue.global()
)
}

values.dropLast()
.publisher
}

open func replay<S: Sequence>(_ values: S) -> AnyPublisher<[RefinedAction], Never>
where S.Element == (offset: Double, actions: [RefinedAction])
{
values
.publisher
.flatMap { offset, actions in
Just(actions).delay(
for: .seconds(max(0, offset)),
scheduler: DispatchQueue.global()
)
}
.handleEvents(
receiveSubscription: { _ in self.dispatchEnabled = false },
receiveOutput: _postMiddlewareRefinedActions.send,
receiveCompletion: { _ in self.dispatchEnabled = true },
receiveCancel: { self.dispatchEnabled = true }
)
// Cancel if dispatch is manually reenabled.
.prefix(untilOutputFrom: $dispatchEnabled.filter { $0 })
.eraseToAnyPublisher()
.print("REPLAY::")
.sink(
receiveCompletion: { _ in
self.dispatchEnabled = true
},
receiveValue: _postMiddlewareRefinedActions.send
)
.store(in: &cancellables)
}
}

0 comments on commit d1b8dc4

Please sign in to comment.