Skip to content

Commit

Permalink
MetaProperty fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Sharanda committed Jun 27, 2019
1 parent cf1cb3b commit 0039f75
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Jetpack.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Jetpack"
s.version = "4.1.4"
s.version = "4.1.5"
s.summary = "Light and bright functional reactive framework"
s.description = <<-DESC
Minimalistic implementation of rx primitives. Basically observer pattern implementations which helps to easily organize interaction of different components.
Expand Down
25 changes: 15 additions & 10 deletions Sources/MetaProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,33 @@ public protocol ChangeEventProtocol {
}

public final class MetaProperty<T, Event: ChangeEventProtocol>: ObservableProtocol, GetValueProtocol {
private let property: Property<(T, Event)>

private let observable: Observable<(T, Event)>
private let getter: ()->T

public var value: T {
return property.value.0
return getter()
}

init(property: Property<(T, Event)>) {
self.property = property

init(observable: Observable<(T, Event)>, getter: @escaping ()->T) {
self.getter = getter
self.observable = observable
}

@discardableResult
public func subscribe(_ observer: @escaping ((T, Event)) -> Void) -> Disposable {
return property.subscribe(observer)
observer((value, .resetEvent))
return observable.subscribe(observer)
}

public var asProperty: Property<T> {
return property.map { $0.0 }
return Property(observable: observable.map { $0.0 }) {
return self.value
}
}
}

extension MetaProperty {
public static func just(_ value: T) -> MetaProperty<T, Event> {
return MetaProperty(property: Property.just((value, Event.resetEvent)))
return MetaProperty(observable: Observable.just((value, .resetEvent)), getter: { value })
}
}
51 changes: 30 additions & 21 deletions Sources/MutableMetaProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,59 @@
import Foundation

open class MutableMetaProperty<T, Event: ChangeEventProtocol>: ObservableProtocol, UpdateValueProtocol, GetValueProtocol {

public var value: T {
get {
return mutableProperty.value.0
return property.value
}
set {
update(newValue)
}
}

private let mutableProperty: MutableProperty<(T, Event)>

private let property: MetaProperty<T, Event>
private let setter: (T, Event)->Void

public init(_ value: T) {
mutableProperty = MutableProperty((value, .resetEvent))

let subject = PublishSubject<(T, Event)>()
var v = value

property = MetaProperty(observable: subject.asObservable) {
return v
}
setter = {
v = $0
subject.update((v, $1))
}
}

public func update(_ newValue: T) {
mutableProperty.update((newValue, .resetEvent))
setter(newValue, .resetEvent)
}

@discardableResult
public func subscribe(_ observer: @escaping ((T, Event)) -> Void) -> Disposable {
return mutableProperty.subscribe(observer)
return property.subscribe(observer)
}

public var asProperty: Property<T> {
return mutableProperty.asProperty.map { $0.0 }
return property.asProperty
}

public var asMutableProperty: MutableProperty<T> {
return mutableProperty.map(transform: {
$0.0
}, reduce: { lhs, _ in
(lhs.0, .resetEvent)
})
return MutableProperty(property: asProperty) {
self.update($0)
}
}

public var asMetaProperty: MetaProperty<T, Event> {
return MetaProperty(property: mutableProperty.asProperty)
return property
}

public func changeWithEvent(_ handler: (inout T)-> Event) {
var v = value
let event = handler(&v)
mutableProperty.update((v, event))
setter(v, event)
}
}
2 changes: 1 addition & 1 deletion Sources/MutableProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public final class MutableProperty<T>: ObservableProtocol, GetValueProtocol, Upd
return property.value
}
set {
setter(newValue)
update(newValue)
}
}

Expand Down
15 changes: 12 additions & 3 deletions Tests/JetpackTests/JetpackTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -884,11 +884,15 @@ class JetpackTests: XCTestCase {
let a = MutableArrayProperty<Int>([1,2,3])

var s = [Int]()


a.append(1)

var numberOfSets = 0
a.subscribe {
switch $0.1 {
case .set:
s = $0.0
numberOfSets += 1
case .remove(let idx):
s.remove(at: idx)
case .insert(let idx):
Expand All @@ -905,9 +909,14 @@ class JetpackTests: XCTestCase {
a.update(at: 2, with: 599)
a.remove(at: 0)

XCTAssertEqual(numberOfSets, 1)
XCTAssertEqual(a.value, s)

XCTAssertEqual(s, [10, 599, 3])
XCTAssertEqual(s, [10, 599, 3, 1])

a.value = [10]

XCTAssertEqual(numberOfSets, 2)
XCTAssertEqual(s, [10])
}

func testArray2DProperty() {
Expand Down

0 comments on commit 0039f75

Please sign in to comment.