diff --git a/Example/Example.playground/Contents.swift b/Example/Example.playground/Contents.swift index 55198e1..83b2339 100644 --- a/Example/Example.playground/Contents.swift +++ b/Example/Example.playground/Contents.swift @@ -200,3 +200,24 @@ example("forceCast(to:)") { .disposed(by: disposeBag) } + +/*: + ## Other Extensions + */ + +example("update(_:with:)") { + let disposeBag = DisposeBag() + + let subject = PublishSubject() + + subject + .subscribe(onNext: { dump($0) }) + .disposed(by: disposeBag) + + Observable.of("1", "5", "7", "8") + .update(subject, with: true) + .subscribe(onNext: { dump($0) }) + .disposed(by: disposeBag) + + +} diff --git a/Example/Example.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Example/Example.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/Example/Example.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 59c20d7..f970e0c 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -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) @@ -16,7 +16,7 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: RxCocoa: d88ba0f1f6abf040011a9eb4b539324fc426843a RxSwift: e49536837d9901277638493ea537394d4b55f570 - STDevRxExt: db625d87a2f2f74ce2f9efb869b020e944781851 + STDevRxExt: d8ba812104f9cebe6aee0250536f3cd8d62d040f PODFILE CHECKSUM: 03f0d9ab6a3ddca86ac78ea53d2cfc1bef03c8f8 diff --git a/Example/STDevRxExt.xcodeproj/project.pbxproj b/Example/STDevRxExt.xcodeproj/project.pbxproj index 56f22d3..e9345d8 100644 --- a/Example/STDevRxExt.xcodeproj/project.pbxproj +++ b/Example/STDevRxExt.xcodeproj/project.pbxproj @@ -112,7 +112,7 @@ 607FACE21AFB9204008FA782 /* Frameworks */, 607FACE31AFB9204008FA782 /* Resources */, ABFFA786528145F941134D7B /* [CP] Embed Pods Frameworks */, - 1314E7A7F724A441011AC3F2 /* [CP] Copy Pods Resources */, + 92DAC64742707319431404BA /* [CP] Copy Pods Resources */, ); buildRules = ( ); @@ -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 = ( diff --git a/README.md b/README.md index 82f2cb6..1f82bb2 100644 --- a/README.md +++ b/README.md @@ -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 @@ -92,7 +93,7 @@ true true ``` -#### Map Extensions +### Map Extensions You can map every element in sequence with provided value. @@ -148,7 +149,7 @@ William Jack ``` -#### Cast Extensions +### Cast Extensions You can do downcast elements in sequence using `cast(to:)`. @@ -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 diff --git a/STDevRxExt.podspec b/STDevRxExt.podspec index c74ef11..7a41238 100644 --- a/STDevRxExt.podspec +++ b/STDevRxExt.podspec @@ -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 diff --git a/STDevRxExt/Classes/OtherExtensions.swift b/STDevRxExt/Classes/OtherExtensions.swift new file mode 100644 index 0000000..7baba88 --- /dev/null +++ b/STDevRxExt/Classes/OtherExtensions.swift @@ -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(_ observer: T, with value: T.E) -> Observable { + return self.do(onNext: { [observer] _ in + observer.onNext(value) + }) + } + +} + +public extension SharedSequenceConvertibleType where SharingStrategy == DriverSharingStrategy { + + public func update(_ observer: T, with value: T.E) -> SharedSequence { + return self.do(onNext: { [observer] _ in + observer.onNext(value) + }) + } + +}