-
Notifications
You must be signed in to change notification settings - Fork 212
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
Swiftlint #128
Swiftlint #128
Changes from 3 commits
f4d2235
79166c0
f9c6ff4
a3c7a48
6bf998b
4d67ba3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
included: | ||
- Source | ||
- Tests | ||
disabled_rules: | ||
- line_length | ||
identifier_name: | ||
min_length: 1 | ||
type_name: | ||
excluded: T | ||
function_body_length: 100 | ||
type_body_length: 500 | ||
cyclomatic_complexity: | ||
warning: 15 | ||
error: 25 | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,9 @@ import Foundation | |
import RxSwift | ||
|
||
extension Observable where Element : ObservableType { | ||
|
||
public typealias T = Element.E | ||
|
||
/** | ||
Cascade through a sequence of observables: every observable that sends a `next` value becomes the "current" | ||
observable (like in `switchLatest`), and the subscription to all previous observables in the sequence is disposed. | ||
|
@@ -23,96 +23,88 @@ extension Observable where Element : ObservableType { | |
- parameter observables: a sequence of observables which will all be immediately subscribed to | ||
- returns: An observable sequence that contains elements from the latest observable sequence that emitted elements | ||
*/ | ||
|
||
|
||
public static func cascade<S : Sequence>(_ observables : S) -> Observable<T> where S.Iterator.Element == Element { | ||
let flow = Array(observables) | ||
if flow.isEmpty { | ||
return Observable<T>.empty() | ||
} | ||
|
||
return Observable<T>.create { observer in | ||
var current = 0, initialized = false | ||
var subscriptions: [SerialDisposable?] = flow.map { _ in SerialDisposable() } | ||
|
||
let lock = NSRecursiveLock() | ||
lock.lock() | ||
defer { lock.unlock() } | ||
|
||
for i in 0 ..< flow.count { | ||
let index = i | ||
let disposable = flow[index].subscribe { event in | ||
lock.lock() | ||
defer { lock.unlock() } | ||
|
||
switch event { | ||
case .next(let element): | ||
while current < index { | ||
subscriptions[current]?.dispose() | ||
subscriptions[current] = nil | ||
current += 1 | ||
} | ||
if index == current { | ||
assert(subscriptions[index] != nil) | ||
observer.onNext(element) | ||
} | ||
|
||
case .completed: | ||
if index >= current { | ||
if (initialized) { | ||
subscriptions[index]?.dispose() | ||
subscriptions[index] = nil | ||
for next in current ..< subscriptions.count { | ||
if subscriptions[next] != nil { | ||
return | ||
} | ||
} | ||
observer.onCompleted() | ||
} | ||
} | ||
|
||
case .error(let error): | ||
observer.onError(error) | ||
} | ||
} | ||
if let serialDisposable = subscriptions[index] { | ||
serialDisposable.disposable = disposable | ||
} else { | ||
disposable.dispose() | ||
} | ||
} | ||
|
||
initialized = true | ||
|
||
for i in 0 ..< flow.count { | ||
if subscriptions[i] != nil { | ||
return Disposables.create { | ||
subscriptions.forEach { $0?.dispose() } | ||
} | ||
} | ||
} | ||
|
||
observer.onCompleted() | ||
return Disposables.create() | ||
} | ||
public static func cascade<S: Sequence>(_ observables: S) -> Observable<T> where S.Iterator.Element == Element { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The big changes here is just spacing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The old tabs-vs-spaces war eh? Always been a tabs guy, never understood the purpose of spaces, as:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, it's not a war or anything. I just think people mostly use spaces, because it ensures the files look the same wherever you go. I don't mind going one way or the other but then we need to see if we go the same everywhere. Otherwise, if you use tabs and someone who uses spaces edits your files, it will look very strange (like happened for me). Good either way :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I remember that editors like Emacs and VIM support a comment indicator that tells whether a file uses spaces or tabs, and adjust accordingly. AppCode and all IntelliJ-derived IDEs have a setting to automatically detect what's being use. Is there anything we could do for Xcode ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pray? :( 🙏 I also think the default setting is spaces so that could cause even more issues potentially. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm trying to think if there's any good way to support either method but I don't have enough experience here TBH There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think this defeats the purpose of avoiding such major diffs out of simple re-indent, correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I'm saying "a way to support either" - i mean without messing up the code on each commit 😛 |
||
let flow = Array(observables) | ||
if flow.isEmpty { | ||
return Observable<T>.empty() | ||
} | ||
|
||
return Observable<T>.create { observer in | ||
var current = 0, initialized = false | ||
var subscriptions: [SerialDisposable?] = flow.map { _ in SerialDisposable() } | ||
|
||
let lock = NSRecursiveLock() | ||
lock.lock() | ||
defer { lock.unlock() } | ||
|
||
for i in 0 ..< flow.count { | ||
let index = i | ||
let disposable = flow[index].subscribe { event in | ||
lock.lock() | ||
defer { lock.unlock() } | ||
|
||
switch event { | ||
case .next(let element): | ||
while current < index { | ||
subscriptions[current]?.dispose() | ||
subscriptions[current] = nil | ||
current += 1 | ||
} | ||
if index == current { | ||
assert(subscriptions[index] != nil) | ||
observer.onNext(element) | ||
} | ||
|
||
case .completed: | ||
if index >= current { | ||
if initialized { | ||
subscriptions[index]?.dispose() | ||
subscriptions[index] = nil | ||
for next in current ..< subscriptions.count where subscriptions[next] != nil { | ||
return | ||
} | ||
observer.onCompleted() | ||
} | ||
} | ||
|
||
case .error(let error): | ||
observer.onError(error) | ||
} | ||
} | ||
if let serialDisposable = subscriptions[index] { | ||
serialDisposable.disposable = disposable | ||
} else { | ||
disposable.dispose() | ||
} | ||
} | ||
|
||
initialized = true | ||
|
||
for i in 0 ..< flow.count where subscriptions[i] != nil { | ||
return Disposables.create { | ||
subscriptions.forEach { $0?.dispose() } | ||
} | ||
} | ||
|
||
observer.onCompleted() | ||
return Disposables.create() | ||
} | ||
} | ||
} | ||
|
||
extension ObservableType { | ||
|
||
/** | ||
Cascade through a sequence of observables: every observable that sends a `next` value becomes the "current" | ||
observable (like in `switchLatest`), and the subscription to all previous observables in the sequence is disposed. | ||
|
||
This allows subscribing to multiple observable sequences while irrevocably switching to the next when it starts emitting. If any of the | ||
currently subscribed-to sequences errors, the error is propagated to the observer and the sequence terminates. | ||
|
||
- parameter observables: a sequence of observables which will all be immediately subscribed to | ||
- returns: An observable sequence that contains elements from the latest observable sequence that emitted elements | ||
*/ | ||
|
||
public func cascade<S : Sequence>(_ next : S) -> Observable<E> where S.Iterator.Element == Self { | ||
return Observable.cascade([self.asObservable()] + Array(next).map { $0.asObservable() }) | ||
} | ||
|
||
|
||
/** | ||
Cascade through a sequence of observables: every observable that sends a `next` value becomes the "current" | ||
observable (like in `switchLatest`), and the subscription to all previous observables in the sequence is disposed. | ||
|
||
This allows subscribing to multiple observable sequences while irrevocably switching to the next when it starts emitting. If any of the | ||
currently subscribed-to sequences errors, the error is propagated to the observer and the sequence terminates. | ||
|
||
- parameter observables: a sequence of observables which will all be immediately subscribed to | ||
- returns: An observable sequence that contains elements from the latest observable sequence that emitted elements | ||
*/ | ||
public func cascade<S: Sequence>(_ next: S) -> Observable<E> where S.Iterator.Element == Self { | ||
return Observable.cascade([self.asObservable()] + Array(next).map { $0.asObservable() }) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ | |
|
||
import RxSwift | ||
|
||
|
||
extension ObservableType { | ||
/** | ||
Dismiss errors and complete the sequence instead | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we need a trailing newline here? I'm surprised that swiftlint doesn't complain
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newline after what? the last line here?
Neh its YAML, no need :)