Skip to content

Commit

Permalink
Merge pull request #8 from jakehawken/no_more_equatable_requirement
Browse files Browse the repository at this point in the history
Removed the requirement for T on StreamState to be equatable.
  • Loading branch information
jakehawken committed Oct 26, 2017
2 parents 7232356 + 45ede39 commit 610de86
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
3 changes: 1 addition & 2 deletions Concurrency.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Concurrency'
s.version = '1.1.0'
s.version = '1.1.1'
s.summary = 'A small toolkit for handling concurrency in Swift.'

s.description = <<-DESC
Expand All @@ -17,7 +17,6 @@ Pod::Spec.new do |s|

s.platform = :ios, "10.0"
s.ios.deployment_target = '10.0'
# s.compiler_flags = 'SWIFT_VERSION', '3.2'

s.source_files = 'Source/*'
s.dependency 'RxSwift' #, '~> 3.5'
Expand Down
2 changes: 1 addition & 1 deletion Concurrency/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.1.0</string>
<string>1.1.1</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
Expand Down
10 changes: 5 additions & 5 deletions ConcurrencyTests/PeriodicFetcherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class PeriodicFetcherTests: QuickSpec {
it("should begin in the non-fetching state") {
expect(subject.isFetching).to(equal(false))
expect(streamStates.count).to(equal(1))
expect(streamStates.first).to(equal(.noData))
expect(streamStates.first == .noData).to(beTrue())
}

describe("fetching once (without periodic fetching)") {
Expand All @@ -82,8 +82,8 @@ class PeriodicFetcherTests: QuickSpec {

it("should only fetch once (plus initial .noData)") {
expect(streamStates.count).toEventually(equal(2))
expect(streamStates.first).to(equal(StreamState.noData))
expect(streamStates.last).to(equal(StreamState.newData(0)))
expect(streamStates.first == .noData).to(beTrue())
expect(streamStates.last == .newData(0)).to(beTrue())
expect(showsIsFetching).to(equal(false))
}
}
Expand Down Expand Up @@ -128,7 +128,7 @@ class PeriodicFetcherTests: QuickSpec {
it("should stop emitting data after being told to stop fetching") {
expect(streamStates.count).toEventually(equal(1))
expect(streamStates.count).toNotEventually(equal(2))
expect(streamStates.last).to(equal(StreamState.noData))
expect(streamStates.last == .noData).to(beTrue())
expect(subject.isFetching).to(equal(false))
}
}
Expand All @@ -155,7 +155,7 @@ class PeriodicFetcherTests: QuickSpec {
it("should stop emitting data after being told to stop fetching") {
expect(streamStates.count).toEventually(equal(3))
expect(streamStates.count).toNotEventually(equal(4))
expect(streamStates.last).toEventually(equal(StreamState.newData(2)))
expect(streamStates.last == .newData(2)).toEventually(beTrue())
expect(subject.isFetching).toEventually(equal(false))
}
}
Expand Down
42 changes: 29 additions & 13 deletions Source/PeriodicFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,11 @@ import Foundation
import RxSwift


public enum StreamState<T:Equatable>: Equatable {
public enum StreamState<T> {
case noData
case newData(T)
case error(Error)

public static func ==(lhs: StreamState, rhs: StreamState) -> Bool {
switch (lhs, rhs) {
case (.newData(let data1), .newData(let data2)):
return data1 == data2
case (.error(_), .error(_)), (.noData, .noData):
return true
default:
return false
}
}

@discardableResult public func onError(_ errorBlock: (Error)->()) -> StreamState<T> {
switch self {
case .error(let error):
Expand All @@ -45,7 +34,34 @@ public enum StreamState<T:Equatable>: Equatable {

}

public class PeriodicFetcher<T:Equatable> {
public extension StreamState where T: Equatable {
public static func ==(lhs: StreamState, rhs: StreamState) -> Bool {
switch (lhs, rhs) {
case (.noData, .noData), (.error(_), .error(_)):
return true
case (.newData(let value1), .newData(let value2)):
return value1 == value2
default:
return false
}
}

public static func ==(lhs: StreamState<T>?, rhs: StreamState<T>) -> Bool {
guard let lhs = lhs else {
return false
}
return lhs == rhs
}

public static func ==(lhs: StreamState<T>, rhs: StreamState<T>?) -> Bool {
guard let rhs = rhs else {
return false
}
return lhs == rhs
}
}

public class PeriodicFetcher<T> {

public typealias FutureGenerator = ()->(Future<T>)
public typealias TimeIntervalGenerator = ()->(Double)
Expand Down

0 comments on commit 610de86

Please sign in to comment.