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

Swiftlint #128

Merged
merged 6 commits into from
Dec 12, 2017
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
14 changes: 14 additions & 0 deletions .swiftlint.yml
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
Copy link
Member

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 :trollface:

Copy link
Member Author

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 :)

2 changes: 1 addition & 1 deletion Source/RxCocoa/mapTo+RxCocoa.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension SharedSequenceConvertibleType {
public func mapTo<R>(_ value: R) -> SharedSequence<SharingStrategy, R> {
return map { _ in value }
}

public func map<R>(to value: R) -> SharedSequence<SharingStrategy, R> {
return map { _ in value }
}
Expand Down
38 changes: 16 additions & 22 deletions Source/RxSwift/ObservableType+Weak.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,25 @@ extension ObservableType {
method?(obj)(value)
}
}

fileprivate func weakify<A: AnyObject>(_ obj: A, method: ((A) -> () -> Void)?) -> (() -> Void) {
return { [weak obj] in
guard let obj = obj else { return }
method?(obj)()
}
}

/**
Subscribes an event handler to an observable sequence.

- parameter weak: Weakly referenced object containing the target function.
- parameter on: Function to invoke on `weak` for each event in the observable sequence.
- returns: Subscription object used to unsubscribe from the observable sequence.
*/

public func subscribe<A: AnyObject>(weak obj: A, _ on: @escaping (A) -> (RxSwift.Event<Self.E>) -> Void) -> Disposable {
return self.subscribe(weakify(obj, method: on))
}

/**
Subscribes an element handler, an error handler, a completion handler and disposed handler to an observable sequence.

Expand All @@ -53,23 +52,21 @@ extension ObservableType {
gracefully completed, errored, or if the generation is cancelled by disposing subscription)
- returns: Subscription object used to unsubscribe from the observable sequence.
*/

public func subscribe<A: AnyObject>(
weak obj: A,
onNext: ((A) -> (Self.E) -> Void)? = nil,
onError: ((A) -> (Error) -> Void)? = nil,
onCompleted: ((A) -> () -> Void)? = nil,
onDisposed: ((A) -> () -> Void)? = nil) -> Disposable {
weak obj: A,
onNext: ((A) -> (Self.E) -> Void)? = nil,
onError: ((A) -> (Error) -> Void)? = nil,
onCompleted: ((A) -> () -> Void)? = nil,
onDisposed: ((A) -> () -> Void)? = nil)
-> Disposable {
let disposable: Disposable

if let disposed = onDisposed {
disposable = Disposables.create(with: weakify(obj, method: disposed))
}
else {
} else {
disposable = Disposables.create()
}

let observer = AnyObserver { [weak obj] (e: RxSwift.Event<Self.E>) in
guard let obj = obj else { return }
switch e {
Expand All @@ -83,42 +80,39 @@ extension ObservableType {
disposable.dispose()
}
}

return Disposables.create(self.asObservable().subscribe(observer), disposable)
}

/**
Subscribes an element handler to an observable sequence.

- parameter weak: Weakly referenced object containing the target function.
- parameter onNext: Function to invoke on `weak` for each element in the observable sequence.
- returns: Subscription object used to unsubscribe from the observable sequence.
*/

public func subscribeNext<A: AnyObject>(weak obj: A, _ onNext: @escaping (A) -> (Self.E) -> Void) -> Disposable {
return self.subscribe(onNext: weakify(obj, method: onNext))
}

/**
Subscribes an error handler to an observable sequence.

- parameter weak: Weakly referenced object containing the target function.
- parameter onError: Function to invoke on `weak` upon errored termination of the observable sequence.
- returns: Subscription object used to unsubscribe from the observable sequence.
*/

public func subscribeError<A: AnyObject>(weak obj: A, _ onError: @escaping (A) -> (Error) -> Void) -> Disposable {
return self.subscribe(onError: weakify(obj, method: onError))
}

/**
Subscribes a completion handler to an observable sequence.

- parameter weak: Weakly referenced object containing the target function.
- parameter onCompleted: Function to invoke on `weak` graceful termination of the observable sequence.
- returns: Subscription object used to unsubscribe from the observable sequence.
*/

public func subscribeCompleted<A: AnyObject>(weak obj: A, _ onCompleted: @escaping (A) -> () -> Void) -> Disposable {
return self.subscribe(onCompleted: weakify(obj, method: onCompleted))
}
Expand Down
172 changes: 82 additions & 90 deletions Source/RxSwift/cascade.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The big changes here is just spacing.
This seems to be the only file to use Tabs instead of Spaces, so changing one portion of the code made everything look strange, so I just re-indented it.

Copy link
Member

Choose a reason for hiding this comment

The 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:

  • some developers like to have 2, 3, 4 or 8 spaces indentation. Using a tab makes it flexible
  • using tabs makes smaller files (yeah I know in this day and age, not important)
    :)

Copy link
Member Author

Choose a reason for hiding this comment

The 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 :)

Copy link
Member

Choose a reason for hiding this comment

The 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 ?

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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

Copy link

@mosamer mosamer Dec 12, 2017

Choose a reason for hiding this comment

The 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

I think this defeats the purpose of avoiding such major diffs out of simple re-indent, correct?

Copy link
Member Author

Choose a reason for hiding this comment

The 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() })
}
}
1 change: 0 additions & 1 deletion Source/RxSwift/catchErrorJustComplete.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import RxSwift


extension ObservableType {
/**
Dismiss errors and complete the sequence instead
Expand Down
Loading