Skip to content

Commit

Permalink
MetaProperty events fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Sharanda committed May 11, 2019
1 parent 92dda5a commit cf1cb3b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 41 deletions.
8 changes: 4 additions & 4 deletions Demo/Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,12 @@
TargetAttributes = {
1BC89F711F800FB4008560A2 = {
CreatedOnToolsVersion = 9.0;
LastSwiftMigration = 1000;
LastSwiftMigration = 1020;
ProvisioningStyle = Automatic;
};
1BC89F851F800FB5008560A2 = {
CreatedOnToolsVersion = 9.0;
LastSwiftMigration = 1000;
LastSwiftMigration = 1020;
ProvisioningStyle = Automatic;
TestTargetID = 1BC89F711F800FB4008560A2;
};
Expand Down Expand Up @@ -670,7 +670,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = org.sharanda.DemoTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Demo.app/Demo";
};
Expand All @@ -686,7 +686,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = org.sharanda.DemoTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Demo.app/Demo";
};
Expand Down
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.3"
s.version = "4.1.4"
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
24 changes: 8 additions & 16 deletions Sources/MetaProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,28 @@ public protocol ChangeEventProtocol {
}

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

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

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




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

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


extension MetaProperty {
public static func just(_ value: T) -> MetaProperty<T, Event> {
return MetaProperty(observable: Observable.just((value, .resetEvent)), getter: { value })
return MetaProperty(property: Property.just((value, Event.resetEvent)))
}
}
36 changes: 16 additions & 20 deletions Sources/MutableMetaProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,47 @@ open class MutableMetaProperty<T, Event: ChangeEventProtocol>: ObservableProtoco

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

private let subject = PublishSubject<(T, Event)>()
private var _value: T

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

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

public func update(_ newValue: T) {
_value = newValue
subject.update((_value, .resetEvent))
mutableProperty.update((newValue, .resetEvent))
}

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

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

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

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

public func changeWithEvent(_ handler: (inout T)-> Event) {
let event = handler(&_value)
subject.update((_value, event))
var v = value
let event = handler(&v)
mutableProperty.update((v, event))
}
}

0 comments on commit cf1cb3b

Please sign in to comment.