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

Prepare isExpired to pass a margin for slower scenarios #20

Merged
merged 8 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
84 changes: 45 additions & 39 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ GEM
base64
nkf
rexml
activesupport (7.1.3.4)
activesupport (7.2.1.2)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
concurrent-ruby (~> 1.0, >= 1.3.1)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
logger (>= 1.4.2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
algoliasearch (1.27.5)
Expand All @@ -23,25 +24,26 @@ GEM
artifactory (3.0.17)
atomos (0.1.3)
aws-eventstream (1.3.0)
aws-partitions (1.959.0)
aws-sdk-core (3.201.3)
aws-partitions (1.994.0)
aws-sdk-core (3.211.0)
aws-eventstream (~> 1, >= 1.3.0)
aws-partitions (~> 1, >= 1.651.0)
aws-sigv4 (~> 1.8)
aws-partitions (~> 1, >= 1.992.0)
aws-sigv4 (~> 1.9)
jmespath (~> 1, >= 1.6.1)
aws-sdk-kms (1.88.0)
aws-sdk-core (~> 3, >= 3.201.0)
aws-sdk-kms (1.95.0)
aws-sdk-core (~> 3, >= 3.210.0)
aws-sigv4 (~> 1.5)
aws-sdk-s3 (1.156.0)
aws-sdk-core (~> 3, >= 3.201.0)
aws-sdk-s3 (1.169.0)
aws-sdk-core (~> 3, >= 3.210.0)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.5)
aws-sigv4 (1.9.1)
aws-sigv4 (1.10.1)
aws-eventstream (~> 1, >= 1.0.2)
babosa (1.0.4)
base64 (0.2.0)
bigdecimal (3.1.8)
childprocess (5.0.0)
childprocess (5.1.0)
logger (~> 1.5)
claide (1.1.0)
claide-plugins (0.9.2)
cork
Expand Down Expand Up @@ -88,11 +90,12 @@ GEM
colored2 (3.1.2)
commander (4.6.0)
highline (~> 2.0.0)
concurrent-ruby (1.3.3)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
cork (0.3.0)
colored2 (~> 3.1)
danger (9.4.3)
danger (9.5.1)
base64 (~> 0.2)
claide (~> 1.0)
claide-plugins (>= 0.9.2)
colored2 (~> 3.1)
Expand All @@ -102,21 +105,21 @@ GEM
git (~> 1.13)
kramdown (~> 2.3)
kramdown-parser-gfm (~> 1.0)
no_proxy_fix
octokit (>= 4.0)
pstore (~> 0.1)
terminal-table (>= 1, < 4)
danger-junit (1.0.2)
danger (> 2.0)
ox (~> 2.0)
danger-plugin-api (1.0.0)
danger (> 2.0)
danger-swiftlint (0.36.1)
danger-swiftlint (0.37.0)
danger
rake (> 10)
thor (~> 1.0.0)
danger-xcode_summary (1.3.0)
danger-xcode_summary (1.3.1)
danger-plugin-api (~> 1.0)
xcresult (~> 0.2)
xcresult (~> 0.2.2)
danger-xcodebuild (0.0.6)
danger-plugin-api (~> 1.0)
danger-xcov (0.5.0)
Expand All @@ -132,8 +135,8 @@ GEM
escape (0.0.4)
ethon (0.16.0)
ffi (>= 1.15.0)
excon (0.111.0)
faraday (1.10.3)
excon (0.112.0)
faraday (1.10.4)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1)
Expand Down Expand Up @@ -161,10 +164,10 @@ GEM
faraday-patron (1.0.0)
faraday-rack (1.0.0)
faraday-retry (1.0.3)
faraday_middleware (1.2.0)
faraday_middleware (1.2.1)
faraday (~> 1.0)
fastimage (2.3.1)
fastlane (2.222.0)
fastlane (2.225.0)
CFPropertyList (>= 2.3, < 4.0.0)
addressable (>= 2.8, < 3.0.0)
artifactory (~> 3.0)
Expand All @@ -180,6 +183,7 @@ GEM
faraday-cookie_jar (~> 0.0.6)
faraday_middleware (~> 1.0)
fastimage (>= 2.1.0, < 3.0.0)
fastlane-sirp (>= 1.0.0)
gh_inspector (>= 1.1.2, < 2.0.0)
google-apis-androidpublisher_v3 (~> 0.3)
google-apis-playcustomapp_v1 (~> 0.1)
Expand All @@ -205,6 +209,8 @@ GEM
xcodeproj (>= 1.13.0, < 2.0.0)
xcpretty (~> 0.3.0)
xcpretty-travis-formatter (>= 0.0.3, < 2.0.0)
fastlane-sirp (1.0.0)
sysrandom (~> 1.0)
ffi (1.17.0)
fourflusher (2.3.1)
fuzzy_match (2.0.4)
Expand All @@ -228,7 +234,7 @@ GEM
google-apis-core (>= 0.11.0, < 2.a)
google-apis-storage_v1 (0.31.0)
google-apis-core (>= 0.11.0, < 2.a)
google-cloud-core (1.7.0)
google-cloud-core (1.7.1)
google-cloud-env (>= 1.0, < 3.a)
google-cloud-errors (~> 1.0)
google-cloud-env (1.6.0)
Expand All @@ -249,34 +255,33 @@ GEM
os (>= 0.9, < 2.0)
signet (>= 0.16, < 2.a)
highline (2.0.3)
http-cookie (1.0.6)
http-cookie (1.0.7)
domain_name (~> 0.5)
httpclient (2.8.3)
i18n (1.14.5)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
iniparse (1.5.0)
jmespath (1.6.2)
json (2.7.2)
jwt (2.8.2)
jwt (2.9.3)
base64
kramdown (2.4.0)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
logger (1.6.1)
mini_magick (4.13.2)
mini_mime (1.1.5)
minitest (5.24.1)
minitest (5.25.1)
molinillo (0.8.0)
multi_json (1.15.0)
multipart-post (2.4.1)
mutex_m (0.2.0)
nanaimo (0.3.0)
nap (1.1.0)
naturally (2.2.1)
netrc (0.11.0)
nkf (0.2.0)
no_proxy_fix (0.1.2)
octokit (9.1.0)
octokit (9.2.0)
faraday (>= 1, < 3)
sawyer (~> 0.9)
open4 (1.3.4)
Expand All @@ -288,6 +293,7 @@ GEM
rexml (~> 3.2)
ox (2.14.18)
plist (3.7.1)
pstore (0.1.3)
public_suffix (4.0.7)
rake (13.2.1)
rchardet (1.8.0)
Expand All @@ -296,15 +302,15 @@ GEM
trailblazer-option (>= 0.1.1, < 0.2.0)
uber (< 0.2.0)
retriable (3.1.2)
rexml (3.2.9)
strscan
rexml (3.3.9)
rouge (2.0.7)
ruby-macho (2.5.1)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
sawyer (0.9.2)
addressable (>= 2.3.5)
faraday (>= 0.17.3, < 3)
securerandom (0.3.1)
security (0.1.5)
signet (0.19.0)
addressable (~> 2.8)
Expand All @@ -315,7 +321,7 @@ GEM
CFPropertyList
naturally
slack-notifier (2.4.0)
strscan (3.1.0)
sysrandom (1.0.5)
terminal-notifier (2.0.0)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
Expand All @@ -330,15 +336,15 @@ GEM
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
uber (0.1.0)
unicode-display_width (2.5.0)
unicode-display_width (2.6.0)
word_wrap (1.0.0)
xcodeproj (1.24.0)
xcodeproj (1.25.1)
CFPropertyList (>= 2.3.3, < 4.0)
atomos (~> 0.1.3)
claide (>= 1.0.2, < 2.0)
colored2 (~> 3.1)
nanaimo (~> 0.3.0)
rexml (~> 3.2.4)
rexml (>= 3.3.6, < 4.0)
xcov (1.8.1)
fastlane (>= 2.141.0, < 3.0.0)
multipart-post
Expand All @@ -352,7 +358,7 @@ GEM
xcpretty (~> 0.2, >= 0.0.7)
xcpretty-travis-formatter (1.0.1)
xcpretty (~> 0.2, >= 0.0.7)
xcresult (0.2.1)
xcresult (0.2.2)

PLATFORMS
ruby
Expand Down
16 changes: 10 additions & 6 deletions Sources/Publishers/Publishers.RemoveExpired.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Combine

public extension Publisher {
func removeExpired() -> Publishers.RemoveExpired<Self>
func removeExpired(margin: TimeInterval = taskDefaultMargin) -> Publishers.RemoveExpired<Self>
where Output: Taskable {
Publishers.RemoveExpired(upstream: self)
Publishers.RemoveExpired(upstream: self, margin: margin)
}
}

Expand All @@ -15,13 +15,15 @@ public extension Publishers {
public typealias Failure = Upstream.Failure

public let upstream: Upstream
private let margin: TimeInterval

public init(upstream: Upstream) {
public init(upstream: Upstream, margin: TimeInterval) {
self.upstream = upstream
self.margin = margin
}

public func receive<S: Subscriber>(subscriber: S) where Upstream.Failure == S.Failure, Output == S.Input {
upstream.subscribe(Inner(downstream: subscriber))
upstream.subscribe(Inner(downstream: subscriber, margin: margin))
}
}
}
Expand All @@ -31,17 +33,19 @@ extension Publishers.RemoveExpired {
where Downstream.Input == Output, Downstream.Failure == Upstream.Failure, Output: Taskable {
let combineIdentifier = CombineIdentifier()
private let downstream: Downstream
private let margin: TimeInterval

fileprivate init(downstream: Downstream) {
fileprivate init(downstream: Downstream, margin: TimeInterval) {
self.downstream = downstream
self.margin = margin
}

func receive(subscription: Subscription) {
downstream.receive(subscription: subscription)
}

func receive(_ input: Upstream.Output) -> Subscribers.Demand {
if input.isExpired {
if input.isExpired(margin: margin) {
return .none
}
return downstream.receive(input)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Publishers/Publishers.Scope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import Foundation

public extension Publisher {
/// From a publisher, we can focus on a task and filter all expired and duplicated task. This publisher don't send value if at suscription moment there is a expired task.
func scope<T: Taskable & Equatable>(_ transform: @escaping (Self.Output) -> T) -> AnyPublisher<T, Failure> {
func scope<T: Taskable & Equatable>(_ transform: @escaping (Self.Output) -> T, margin: TimeInterval = taskDefaultMargin) -> AnyPublisher<T, Failure> {
map(transform)
.removeExpired()
.removeExpired(margin: margin)
.removeDuplicates()
.eraseToAnyPublisher()
}
Expand Down
7 changes: 4 additions & 3 deletions Sources/Task/Task.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Foundation

public let taskDefaultMargin: TimeInterval = 0.250

public class Task<T: Equatable, E: Error & Equatable>: Taskable, Equatable, CustomDebugStringConvertible {
public typealias Payload = T
public typealias Failure = E
Expand Down Expand Up @@ -50,9 +52,8 @@ public class Task<T: Equatable, E: Error & Equatable>: Taskable, Equatable, Cust
status == .running
}

public var isExpired: Bool {
let margin: TimeInterval = 0.1 // 100ms for suscriptions propagations
return started.timeIntervalSinceNow + expiration.value + margin < 0
public func isExpired(margin: TimeInterval) -> Bool {
started.timeIntervalSinceNow + expiration.value + margin < 0
}

public var isRecentlySucceeded: Bool {
Expand Down
8 changes: 7 additions & 1 deletion Sources/Task/Taskable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public protocol Taskable {

var isIdle: Bool { get }
var isRunning: Bool { get }
var isExpired: Bool { get }
func isExpired(margin: TimeInterval) -> Bool
var isRecentlySucceeded: Bool { get }
var isTerminal: Bool { get }
var isSuccessful: Bool { get }
Expand All @@ -23,6 +23,12 @@ public protocol Taskable {
static func success(_ payload: Payload, started: Date, expiration: TaskExpiration, tag: String?, progress: Decimal?) -> Self
}

public extension Taskable {
var isExpired: Bool {
self.isExpired(margin: taskDefaultMargin)
}
}

public extension Taskable {
static func idle(started: Date = Date(),
tag: String? = nil,
Expand Down
Loading