Skip to content

Commit 98cbdab

Browse files
author
Tigran Hamabrdzumyan
authored
Merge pull request #3 from tigran-stdev/master
Adding update method
2 parents 4f2af71 + ee10562 commit 98cbdab

7 files changed

Lines changed: 103 additions & 7 deletions

File tree

Example/Example.playground/Contents.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,24 @@ example("forceCast(to:)") {
200200
.disposed(by: disposeBag)
201201

202202
}
203+
204+
/*:
205+
## Other Extensions
206+
*/
207+
208+
example("update(_:with:)") {
209+
let disposeBag = DisposeBag()
210+
211+
let subject = PublishSubject<Bool>()
212+
213+
subject
214+
.subscribe(onNext: { dump($0) })
215+
.disposed(by: disposeBag)
216+
217+
Observable<String>.of("1", "5", "7", "8")
218+
.update(subject, with: true)
219+
.subscribe(onNext: { dump($0) })
220+
.disposed(by: disposeBag)
221+
222+
223+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

Example/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ PODS:
22
- RxCocoa (4.1.2):
33
- RxSwift (~> 4.0)
44
- RxSwift (4.1.2)
5-
- STDevRxExt (0.1.1):
5+
- STDevRxExt (0.1.2):
66
- RxCocoa (~> 4.1.2)
77
- RxSwift (~> 4.1.2)
88

@@ -16,7 +16,7 @@ EXTERNAL SOURCES:
1616
SPEC CHECKSUMS:
1717
RxCocoa: d88ba0f1f6abf040011a9eb4b539324fc426843a
1818
RxSwift: e49536837d9901277638493ea537394d4b55f570
19-
STDevRxExt: db625d87a2f2f74ce2f9efb869b020e944781851
19+
STDevRxExt: d8ba812104f9cebe6aee0250536f3cd8d62d040f
2020

2121
PODFILE CHECKSUM: 03f0d9ab6a3ddca86ac78ea53d2cfc1bef03c8f8
2222

Example/STDevRxExt.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
607FACE21AFB9204008FA782 /* Frameworks */,
113113
607FACE31AFB9204008FA782 /* Resources */,
114114
ABFFA786528145F941134D7B /* [CP] Embed Pods Frameworks */,
115-
1314E7A7F724A441011AC3F2 /* [CP] Copy Pods Resources */,
115+
92DAC64742707319431404BA /* [CP] Copy Pods Resources */,
116116
);
117117
buildRules = (
118118
);
@@ -169,7 +169,7 @@
169169
/* End PBXResourcesBuildPhase section */
170170

171171
/* Begin PBXShellScriptBuildPhase section */
172-
1314E7A7F724A441011AC3F2 /* [CP] Copy Pods Resources */ = {
172+
92DAC64742707319431404BA /* [CP] Copy Pods Resources */ = {
173173
isa = PBXShellScriptBuildPhase;
174174
buildActionMask = 2147483647;
175175
files = (

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pod 'STDevRxExt'
2929
* [Filter Extensions](#filter-extensions)
3030
* [Map Extensions](#map-extensions)
3131
* [Cast Extensions](#cast_extensions)
32+
* [Other Extensions](#other_extensions)
3233
* _more coming soon_
3334

3435
### Filter Extensions
@@ -92,7 +93,7 @@ true
9293
true
9394
```
9495

95-
#### Map Extensions
96+
### Map Extensions
9697

9798
You can map every element in sequence with provided value.
9899

@@ -148,7 +149,7 @@ William
148149
Jack
149150
```
150151

151-
#### Cast Extensions
152+
### Cast Extensions
152153

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

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

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

198+
### Other Extensions
199+
200+
Sometimes we need to update some subject or observer on each `next` event of `Observable` or `Driver`. For example:
201+
202+
```swift
203+
request
204+
.do(onNext: { [weak self] _ in
205+
self?.inProgress.onNext(true)
206+
})
207+
.flatMap {
208+
service.doRequest($0)
209+
}
210+
.do(onNext: { [weak self] _ in
211+
self?.inProgress.onNext(false)
212+
})
213+
.subscribe(onNext: { response in
214+
dump(response)
215+
})
216+
.disposed(by: disposeBag)
217+
```
218+
219+
You can use `update(_:with:)` method for shorting code like this:
220+
221+
```swift
222+
request
223+
.update(inProgress, with: true)
224+
.flatMap {
225+
service.doRequest($0)
226+
}
227+
.update(inProgress, with: false)
228+
.subscribe(onNext: { response in
229+
dump(response)
230+
})
231+
.disposed(by: disposeBag)
232+
```
233+
197234
## Author
198235

199236
Tigran Hambardzumyan, tigran@stdevmail.com

STDevRxExt.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'STDevRxExt'
3-
s.version = '0.1.2'
3+
s.version = '0.1.3'
44
s.summary = 'STDevRxExt contains some extension functions for RxSwift and RxCoca which makes our live easy.'
55

66
s.description = <<-DESC
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// OtherExtensions.swift
3+
// STDevRxExt
4+
//
5+
// Created by Tigran Hambardzumyan on 5/15/18.
6+
//
7+
8+
import Foundation
9+
import RxSwift
10+
import RxCocoa
11+
12+
public extension ObservableType {
13+
14+
public func update<T: ObserverType>(_ observer: T, with value: T.E) -> Observable<Self.E> {
15+
return self.do(onNext: { [observer] _ in
16+
observer.onNext(value)
17+
})
18+
}
19+
20+
}
21+
22+
public extension SharedSequenceConvertibleType where SharingStrategy == DriverSharingStrategy {
23+
24+
public func update<T: ObserverType>(_ observer: T, with value: T.E) -> SharedSequence<DriverSharingStrategy, Self.E> {
25+
return self.do(onNext: { [observer] _ in
26+
observer.onNext(value)
27+
})
28+
}
29+
30+
}

0 commit comments

Comments
 (0)