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

Replace Variable with BehaviorRelay. #65

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions Reactant.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
22F48FE71FAB0EE900E736C8 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22F48FE61FAB0EE900E736C8 /* AppDelegate.swift */; };
22F48FEE1FAB0EE900E736C8 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 22F48FED1FAB0EE900E736C8 /* Assets.xcassets */; };
677138D6038E38CF1E2AB7C6 /* Pods_ReactantPrototyping.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7B0CE169634834E74544F2A0 /* Pods_ReactantPrototyping.framework */; };
687B48BD20F88597002BB4A4 /* BehaviorRelay+mutate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 687B48BC20F88597002BB4A4 /* BehaviorRelay+mutate.swift */; };
71E49563208F37DE00CA3044 /* SubConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71E49562208F37DE00CA3044 /* SubConfiguration.swift */; };
71E49567208F38FA00CA3044 /* BaseSubConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71E49566208F38FA00CA3044 /* BaseSubConfiguration.swift */; };
71E49569208F398600CA3044 /* Configuration+TableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71E49568208F398600CA3044 /* Configuration+TableView.swift */; };
Expand Down Expand Up @@ -283,6 +284,7 @@
22F48FF41FAB0EE900E736C8 /* TVPrototypingTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TVPrototypingTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
50895ED3801A06D4D45EE033 /* Pods_Reactant.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Reactant.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5A648916F85EF94ED93EB6F6 /* Pods-Reactant.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Reactant.release.xcconfig"; path = "Pods/Target Support Files/Pods-Reactant/Pods-Reactant.release.xcconfig"; sourceTree = "<group>"; };
687B48BC20F88597002BB4A4 /* BehaviorRelay+mutate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BehaviorRelay+mutate.swift"; sourceTree = "<group>"; };
711603479E5441514316A7F3 /* Pods_ReactantTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ReactantTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
71E49562208F37DE00CA3044 /* SubConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubConfiguration.swift; sourceTree = "<group>"; };
71E49566208F38FA00CA3044 /* BaseSubConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseSubConfiguration.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -530,6 +532,7 @@
183E0CC21EB88E9C006CF52A /* UILabel+Init.swift */,
183E0CC31EB88E9C006CF52A /* UIOffset+Init.swift */,
183E0CC41EB88E9C006CF52A /* UITableView+Init.swift */,
687B48BC20F88597002BB4A4 /* BehaviorRelay+mutate.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -1613,6 +1616,7 @@
183E0CCF1EB88E9C006CF52A /* UILabel+Init.swift in Sources */,
183457891EB16B8B00C972A4 /* ReactantUI.swift in Sources */,
DC69407F1E51AE9A00C99380 /* ReactantCollectionView+Delegates.swift in Sources */,
687B48BD20F88597002BB4A4 /* BehaviorRelay+mutate.swift in Sources */,
DC1C67F21E5C39F30081E7BD /* Sequence+Utils.swift in Sources */,
DC1C68011E5F574B0081E7BD /* ActivityIndicator.swift in Sources */,
DC6940851E51FC8400C99380 /* FlowCollectionViewBase+Delegates.swift in Sources */,
Expand Down
25 changes: 15 additions & 10 deletions Source/ActivityIndicator/ActivityIndicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public final class ActivityIndicator<T>: ObservableConvertibleType {

public let disposeBag = DisposeBag()

private let variable: Variable<[(id: UUID, associatedValue: T?)]> = Variable([])
private let behaviorRelay: BehaviorRelay<[(id: UUID, associatedValue: T?)]> = BehaviorRelay(value: [])
private let driver: Driver<E>
private let equalFunction: (T?, T?) -> Bool
fileprivate let defaultAssociatedValue: T?
Expand All @@ -32,10 +32,9 @@ public final class ActivityIndicator<T>: ObservableConvertibleType {

// `self` cannot be captured before all fields are initialized.
let equal = self.equalFunction
driver = variable.asDriver()
driver = behaviorRelay.asDriver()
.map { (loading: !$0.isEmpty, associatedValue: $0.compactMap { $0.associatedValue }.first) }
.distinctUntilChanged { lhs, rhs in lhs.loading == rhs.loading &&
equal(lhs.associatedValue, rhs.associatedValue) }
.distinctUntilChanged { lhs, rhs in lhs.loading == rhs.loading && equal(lhs.associatedValue, rhs.associatedValue) }
}

public func trackActivity<O: ObservableConvertibleType>(of source: O, initialAssociatedValue: T?,
Expand All @@ -50,20 +49,26 @@ public final class ActivityIndicator<T>: ObservableConvertibleType {
let messageChangeDisposable = observable.map(associatedValueProvider)
.startWith(initialAssociatedValue)
.distinctUntilChanged(self.equalFunction)
.subscribe(onNext: {
if let index = self.variable.value.index(where: { $0.id == id }) {
self.variable.value[index].associatedValue = $0
.subscribe(onNext: { associatedValue in
if let index = self.behaviorRelay.value.index(where: { $0.id == id }) {
self.behaviorRelay.mutate(using: {
$0[index].associatedValue = associatedValue
})
} else {
self.variable.value.append((id: id, associatedValue: $0))
self.behaviorRelay.mutate(using: {
$0.append((id: id, associatedValue: associatedValue))
})
}
})

return Disposables.create {
subscriptionDisposable.dispose()
messageChangeDisposable.dispose()

if let index = self.variable.value.index(where: { $0.id == id }) {
self.variable.value.remove(at: index)
if let index = self.behaviorRelay.value.index(where: { $0.id == id }) {
self.behaviorRelay.mutate(using: {
$0.remove(at: index)
})
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions Source/Core/Styling/Extensions/BehaviorRelay+mutate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// BehaviorRelay+mutate.swift
// Reactant
//
// Created by Matyáš Kříž on 13/07/2018.
// Copyright © 2018 Brightify. All rights reserved.
//

import RxCocoa

extension BehaviorRelay {
internal func mutate(using mutator: (inout Element) -> Void) {
var mutableValue = value
mutator(&mutableValue)
accept(mutableValue)
}
}