Skip to content

Commit

Permalink
Merge pull request #3 from tigran-stdev/master
Browse files Browse the repository at this point in the history
Adding update method
  • Loading branch information
Tigran Hamabrdzumyan authored May 17, 2018
2 parents 4f2af71 + ee10562 commit 98cbdab
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 7 deletions.
21 changes: 21 additions & 0 deletions Example/Example.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,24 @@ example("forceCast(to:)") {
.disposed(by: disposeBag)

}

/*:
## Other Extensions
*/

example("update(_:with:)") {
let disposeBag = DisposeBag()

let subject = PublishSubject<Bool>()

subject
.subscribe(onNext: { dump($0) })
.disposed(by: disposeBag)

Observable<String>.of("1", "5", "7", "8")
.update(subject, with: true)
.subscribe(onNext: { dump($0) })
.disposed(by: disposeBag)


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ PODS:
- RxCocoa (4.1.2):
- RxSwift (~> 4.0)
- RxSwift (4.1.2)
- STDevRxExt (0.1.1):
- STDevRxExt (0.1.2):
- RxCocoa (~> 4.1.2)
- RxSwift (~> 4.1.2)

Expand All @@ -16,7 +16,7 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
RxCocoa: d88ba0f1f6abf040011a9eb4b539324fc426843a
RxSwift: e49536837d9901277638493ea537394d4b55f570
STDevRxExt: db625d87a2f2f74ce2f9efb869b020e944781851
STDevRxExt: d8ba812104f9cebe6aee0250536f3cd8d62d040f

PODFILE CHECKSUM: 03f0d9ab6a3ddca86ac78ea53d2cfc1bef03c8f8

Expand Down
4 changes: 2 additions & 2 deletions Example/STDevRxExt.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
607FACE21AFB9204008FA782 /* Frameworks */,
607FACE31AFB9204008FA782 /* Resources */,
ABFFA786528145F941134D7B /* [CP] Embed Pods Frameworks */,
1314E7A7F724A441011AC3F2 /* [CP] Copy Pods Resources */,
92DAC64742707319431404BA /* [CP] Copy Pods Resources */,
);
buildRules = (
);
Expand Down Expand Up @@ -169,7 +169,7 @@
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
1314E7A7F724A441011AC3F2 /* [CP] Copy Pods Resources */ = {
92DAC64742707319431404BA /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
Expand Down
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pod 'STDevRxExt'
* [Filter Extensions](#filter-extensions)
* [Map Extensions](#map-extensions)
* [Cast Extensions](#cast_extensions)
* [Other Extensions](#other_extensions)
* _more coming soon_

### Filter Extensions
Expand Down Expand Up @@ -92,7 +93,7 @@ true
true
```

#### Map Extensions
### Map Extensions

You can map every element in sequence with provided value.

Expand Down Expand Up @@ -148,7 +149,7 @@ William
Jack
```

#### Cast Extensions
### Cast Extensions

You can do downcast elements in sequence using `cast(to:)`.

Expand Down Expand Up @@ -194,6 +195,42 @@ In case of downcast exception it will return `Observable.error(RxCastError.castF

Allow extension functions can be used on `Driver` as well, except `forceCast(to:)`.

### Other Extensions

Sometimes we need to update some subject or observer on each `next` event of `Observable` or `Driver`. For example:

```swift
request
.do(onNext: { [weak self] _ in
self?.inProgress.onNext(true)
})
.flatMap {
service.doRequest($0)
}
.do(onNext: { [weak self] _ in
self?.inProgress.onNext(false)
})
.subscribe(onNext: { response in
dump(response)
})
.disposed(by: disposeBag)
```

You can use `update(_:with:)` method for shorting code like this:

```swift
request
.update(inProgress, with: true)
.flatMap {
service.doRequest($0)
}
.update(inProgress, with: false)
.subscribe(onNext: { response in
dump(response)
})
.disposed(by: disposeBag)
```

## Author

Tigran Hambardzumyan, tigran@stdevmail.com
Expand Down
2 changes: 1 addition & 1 deletion STDevRxExt.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'STDevRxExt'
s.version = '0.1.2'
s.version = '0.1.3'
s.summary = 'STDevRxExt contains some extension functions for RxSwift and RxCoca which makes our live easy.'

s.description = <<-DESC
Expand Down
30 changes: 30 additions & 0 deletions STDevRxExt/Classes/OtherExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// OtherExtensions.swift
// STDevRxExt
//
// Created by Tigran Hambardzumyan on 5/15/18.
//

import Foundation
import RxSwift
import RxCocoa

public extension ObservableType {

public func update<T: ObserverType>(_ observer: T, with value: T.E) -> Observable<Self.E> {
return self.do(onNext: { [observer] _ in
observer.onNext(value)
})
}

}

public extension SharedSequenceConvertibleType where SharingStrategy == DriverSharingStrategy {

public func update<T: ObserverType>(_ observer: T, with value: T.E) -> SharedSequence<DriverSharingStrategy, Self.E> {
return self.do(onNext: { [observer] _ in
observer.onNext(value)
})
}

}

0 comments on commit 98cbdab

Please sign in to comment.