File tree Expand file tree Collapse file tree 7 files changed +38
-24
lines changed Expand file tree Collapse file tree 7 files changed +38
-24
lines changed Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ var getName = ""
1818
1919let viewModel = ViewModel ( )
2020
21- viewModel. myName. observer ( viewModel) { name in
21+ viewModel. myName. addObserver ( viewModel) { name in
2222 getName = name
2323}
2424
Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ let package = Package(
2323)
2424
2525package . dependencies = [
26- . package ( url: " https://github.com/heroesofcode/DataLife " , from : " 1.5.0 " )
26+ . package ( url: " https://github.com/heroesofcode/DataLife " , branch : " master " )
2727]
2828package . targets = [
2929 . target( name: " PlaygroundDependencies " ,
@@ -37,4 +37,4 @@ package.platforms = [
3737 . macOS( " 10.13 " ) ,
3838 . tvOS( " 12.0 " ) ,
3939 . watchOS( " 4.0 " )
40- ]
40+ ]
Original file line number Diff line number Diff line change @@ -52,7 +52,7 @@ final class ViewController: UIViewController {
5252 }
5353
5454 private func setupState () {
55- viewModel.myName .observer (viewModel) { [weak self ] name in
55+ viewModel.myName .addObserver (viewModel) { [weak self ] name in
5656 self ? .nameLabel .text = name
5757 }
5858 }
Original file line number Diff line number Diff line change 1- public class DataLife < T> {
2-
3- public typealias CompletionHandler = ( ( T ) -> Void )
1+ import Foundation
42
3+ public class DataLife < T> {
4+
5+ public typealias CompletionHandler = ( T ) -> Void
6+
57 public var value : T ? {
68 didSet {
7- self . notifyObservers ( self . observers )
9+ notifyObservers ( )
810 }
911 }
10-
11- private var observers : [ Int : CompletionHandler ] = [ : ]
12-
12+
13+ private var observers : [ UUID : CompletionHandler ] = [ : ]
14+ private let queue = DispatchQueue ( label: " com.myproject.datalife.queue " , attributes: . concurrent)
15+
1316 public init ( ) { }
14-
15- public func observer( _ observer: ObserverProtocol , completion: @escaping CompletionHandler ) {
16- self . observers [ observer. id] = completion
17+
18+ public func addObserver( _ observer: ObserverProtocol , completion: @escaping CompletionHandler ) {
19+ let id = UUID ( )
20+
21+ queue. async ( flags: . barrier) {
22+ self . observers [ id] = completion
23+ }
1724 }
1825
19- private func notifyObservers( _ observers: [ Int : CompletionHandler ] ) {
20- if value != nil {
21- guard let value = value else { return }
22- observers. forEach ( { $0. value ( value) } )
26+ public func removeObserver( _ observer: ObserverProtocol ) {
27+ queue. async ( flags: . barrier) {
28+ self . observers. removeValue ( forKey: observer. id)
2329 }
2430 }
25-
31+
32+ private func notifyObservers( ) {
33+ guard let value = value else { return }
34+
35+ queue. sync {
36+ self . observers. forEach { $0. value ( value) }
37+ }
38+ }
39+
2640 deinit {
2741 observers. removeAll ( )
2842 }
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import Foundation
22
33open class DataLifeViewModel : ObserverProtocol {
44 public init ( ) { }
5-
6- public var id : Int = 0
5+
6+ public var id : UUID = UUID ( )
77 public func onValueChanged( _ value: Any ? ) { }
88}
Original file line number Diff line number Diff line change 11import Foundation
22
3- public protocol ObserverProtocol {
4- var id : Int { get set }
3+ public protocol ObserverProtocol : AnyObject {
4+ var id : UUID { get set }
55 func onValueChanged( _ value: Any ? )
66}
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ final class DataLifeTests: XCTestCase {
88
99 let viewModel = ViewModel ( )
1010
11- viewModel. myName. observer ( viewModel) { name in
11+ viewModel. myName. addObserver ( viewModel) { name in
1212 getName = name
1313 }
1414
You can’t perform that action at this time.
0 commit comments