Skip to content

Commit

Permalink
Migrates Scheduler API to DispatchTimeInterval and deprecate vers…
Browse files Browse the repository at this point in the history
…ions using `Foundation.TimeInterval`.
  • Loading branch information
kzaher committed Apr 14, 2019
1 parent 2d62dda commit a4ea8e5
Show file tree
Hide file tree
Showing 29 changed files with 568 additions and 135 deletions.
4 changes: 4 additions & 0 deletions Rx.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
1E3079AC21FB52330072A7E6 /* AtomicTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E3079AB21FB52330072A7E6 /* AtomicTests.swift */; };
1E3079AD21FB52330072A7E6 /* AtomicTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E3079AB21FB52330072A7E6 /* AtomicTests.swift */; };
1E3079AE21FB52330072A7E6 /* AtomicTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E3079AB21FB52330072A7E6 /* AtomicTests.swift */; };
1E3EDF65226356A000B631B9 /* Date+Dispatch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E3EDF64226356A000B631B9 /* Date+Dispatch.swift */; };
1E9DA0C522006858000EB80A /* Synchronized.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E9DA0C422006858000EB80A /* Synchronized.swift */; };
1E9DA0C622006858000EB80A /* Synchronized.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E9DA0C422006858000EB80A /* Synchronized.swift */; };
1E9DA0C722006858000EB80A /* Synchronized.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E9DA0C422006858000EB80A /* Synchronized.swift */; };
Expand Down Expand Up @@ -927,6 +928,7 @@
1AF67DA11CED420A00C310FA /* PublishSubjectTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PublishSubjectTest.swift; sourceTree = "<group>"; };
1AF67DA51CED430100C310FA /* ReplaySubjectTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReplaySubjectTest.swift; sourceTree = "<group>"; };
1E3079AB21FB52330072A7E6 /* AtomicTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AtomicTests.swift; sourceTree = "<group>"; };
1E3EDF64226356A000B631B9 /* Date+Dispatch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+Dispatch.swift"; sourceTree = "<group>"; };
1E9DA0C422006858000EB80A /* Synchronized.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Synchronized.swift; sourceTree = "<group>"; };
25F6ECBB1F48C366008552FA /* Maybe.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Maybe.swift; sourceTree = "<group>"; };
25F6ECBD1F48C373008552FA /* Completable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Completable.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1569,6 +1571,7 @@
C81A09851E6C701700900B3B /* Traits */,
C8093C661B8A72BE0088E94D /* Info.plist */,
C81A09801E6C6B2400900B3B /* Deprecated.swift */,
1E3EDF64226356A000B631B9 /* Date+Dispatch.swift */,
);
path = RxSwift;
sourceTree = "<group>";
Expand Down Expand Up @@ -3678,6 +3681,7 @@
C820A87C1EB4DA5A00D431BC /* TakeWhile.swift in Sources */,
C8093D6B1B8A72BE0088E94D /* AnonymousObserver.swift in Sources */,
C8093DA11B8A72BE0088E94D /* PublishSubject.swift in Sources */,
1E3EDF65226356A000B631B9 /* Date+Dispatch.swift in Sources */,
C83D73C81C1DBAEE003DC470 /* ScheduledItemType.swift in Sources */,
C820A8C41EB4DA5A00D431BC /* CombineLatest+Collection.swift in Sources */,
C820A9301EB4DA5A00D431BC /* Producer.swift in Sources */,
Expand Down
3 changes: 2 additions & 1 deletion RxBlocking/BlockingObservable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright © 2015 Krunoslav Zaher. All rights reserved.
//

import Foundation
import RxSwift

/**
Expand All @@ -17,6 +18,6 @@ If you think you need to use a `BlockingObservable` this is usually a sign that
design.
*/
public struct BlockingObservable<E> {
let timeout: RxTimeInterval?
let timeout: TimeInterval?
let source: Observable<E>
}
3 changes: 2 additions & 1 deletion RxBlocking/ObservableConvertibleType+Blocking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
//

import RxSwift
import Foundation

extension ObservableConvertibleType {
/// Converts an Observable into a `BlockingObservable` (an Observable with blocking operators).
///
/// - parameter timeout: Maximal time interval BlockingObservable can block without throwing `RxError.timeout`.
/// - returns: `BlockingObservable` version of `self`
public func toBlocking(timeout: RxTimeInterval? = nil) -> BlockingObservable<E> {
public func toBlocking(timeout: TimeInterval? = nil) -> BlockingObservable<E> {
return BlockingObservable(timeout: timeout, source: self.asObservable())
}
}
6 changes: 3 additions & 3 deletions RxBlocking/RunLoopLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import CoreFoundation

import Foundation
import RxSwift

#if os(Linux)
Expand All @@ -29,9 +29,9 @@ final class RunLoopLock {

let _calledRun = AtomicInt(0)
let _calledStop = AtomicInt(0)
var _timeout: RxTimeInterval?
var _timeout: TimeInterval?

init(timeout: RxTimeInterval?) {
init(timeout: TimeInterval?) {
self._timeout = timeout
self._currentRunLoop = CFRunLoopGetCurrent()
}
Expand Down
85 changes: 85 additions & 0 deletions RxCocoa/Deprecated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,89 @@ extension ObservableType {
}
}

// MARK: throttle
extension SharedSequenceConvertibleType {
/**
Returns an Observable that emits the first and the latest item emitted by the source Observable during sequential time windows of a specified duration.

This operator makes sure that no two elements are emitted in less then dueTime.

- seealso: [debounce operator on reactivex.io](http://reactivex.io/documentation/operators/debounce.html)

- parameter dueTime: Throttling duration for each element.
- parameter latest: Should latest element received in a dueTime wide time window since last element emission be emitted.
- returns: The throttled sequence.
*/
@available(*, deprecated, message: "Use DispatchTimeInterval overload instead.", renamed: "timeout(_:latest:)")
public func throttle(_ dueTime: Foundation.TimeInterval, latest: Bool = true)
-> SharedSequence<SharingStrategy, E> {
return throttle(.milliseconds(Int(dueTime * 1000.0)), latest: latest)
}

/**
Ignores elements from an observable sequence which are followed by another element within a specified relative time duration, using the specified scheduler to run throttling timers.

- parameter dueTime: Throttling duration for each element.
- returns: The throttled sequence.
*/
@available(*, deprecated, message: "Use DispatchTimeInterval overload instead.", renamed: "debounce(_:)")
public func debounce(_ dueTime: Foundation.TimeInterval)
-> SharedSequence<SharingStrategy, E> {
return debounce(.milliseconds(Int(dueTime * 1000.0)))
}
}

// MARK: delay
extension SharedSequenceConvertibleType {

/**
Returns an observable sequence by the source observable sequence shifted forward in time by a specified delay. Error events from the source observable sequence are not delayed.

- seealso: [delay operator on reactivex.io](http://reactivex.io/documentation/operators/delay.html)

- parameter dueTime: Relative time shift of the source by.
- parameter scheduler: Scheduler to run the subscription delay timer on.
- returns: the source Observable shifted in time by the specified delay.
*/
@available(*, deprecated, message: "Use DispatchTimeInterval overload instead.", renamed: "delay(_:)")
public func delay(_ dueTime: Foundation.TimeInterval)
-> SharedSequence<SharingStrategy, E> {
return delay(.milliseconds(Int(dueTime * 1000.0)))
}
}

extension SharedSequence where Element : RxAbstractInteger {
/**
Returns an observable sequence that produces a value after each period, using the specified scheduler to run timers and to send out observer messages.

- seealso: [interval operator on reactivex.io](http://reactivex.io/documentation/operators/interval.html)

- parameter period: Period for producing the values in the resulting sequence.
- returns: An observable sequence that produces a value after each period.
*/
@available(*, deprecated, message: "Use DispatchTimeInterval overload instead.", renamed: "interval(_:)")
public static func interval(_ period: Foundation.TimeInterval)
-> SharedSequence<S, E> {
return interval(.milliseconds(Int(period * 1000.0)))
}
}

// MARK: timer

extension SharedSequence where Element: RxAbstractInteger {
/**
Returns an observable sequence that periodically produces a value after the specified initial relative due time has elapsed, using the specified scheduler to run timers.

- seealso: [timer operator on reactivex.io](http://reactivex.io/documentation/operators/timer.html)

- parameter dueTime: Relative time at which to produce the first value.
- parameter period: Period to produce subsequent values.
- returns: An observable sequence that produces a value after due time has elapsed and then each period.
*/
@available(*, deprecated, message: "Use DispatchTimeInterval overload instead.", renamed: "timer(_:)")
public static func timer(_ dueTime: Foundation.TimeInterval, period: Foundation.TimeInterval)
-> SharedSequence<S, E> {
return timer(.milliseconds(Int(dueTime * 1000.0)), period: .milliseconds(Int(period * 1000.0)))
}
}

65 changes: 65 additions & 0 deletions RxSwift/Date+Dispatch.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// Date+Dispatch.swift
// RxSwift
//
// Created by Krunoslav Zaher on 4/14/19.
// Copyright © 2019 Krunoslav Zaher. All rights reserved.
//

import struct Foundation.Date
import struct Foundation.TimeInterval
import enum Dispatch.DispatchTimeInterval

extension DispatchTimeInterval {
var convertToSecondsFactor: Double {
switch self {
case .nanoseconds: return 1_000_000_000.0
case .microseconds: return 1_000_000.0
case .milliseconds: return 1_000.0
case .seconds: return 1.0
case .never: fatalError()
@unknown default: fatalError()
}
}

func map(_ transform: (Int, Double) -> Int) -> DispatchTimeInterval {
switch self {
case .nanoseconds(let value): return .nanoseconds(transform(value, 1_000_000_000.0))
case .microseconds(let value): return .microseconds(transform(value, 1_000_000.0))
case .milliseconds(let value): return .milliseconds(transform(value, 1_000.0))
case .seconds(let value): return .seconds(transform(value, 1.0))
case .never: return .never
@unknown default: fatalError()
}
}

var isNow: Bool {
switch self {
case .nanoseconds(let value), .microseconds(let value), .milliseconds(let value), .seconds(let value): return value == 0
case .never: return false
@unknown default: fatalError()
}
}

internal func reduceWithSpanBetween(earlierDate: Date, laterDate: Date) -> DispatchTimeInterval {
return self.map { value, factor in
let interval = laterDate.timeIntervalSince(earlierDate)
let remainder = Double(value) - interval * factor
guard remainder > 0 else { return 0 }
return Int(remainder)
}
}
}

extension Date {

internal func addingDispatchInterval(_ dispatchInterval: DispatchTimeInterval) -> Date {
switch dispatchInterval {
case .nanoseconds(let value), .microseconds(let value), .milliseconds(let value), .seconds(let value):
return self.addingTimeInterval(TimeInterval(value) / dispatchInterval.convertToSecondsFactor)
case .never: return Date.distantFuture
@unknown default: fatalError()
}
}

}
Loading

0 comments on commit a4ea8e5

Please sign in to comment.