@@ -374,6 +374,7 @@ extension PropertyProtocol {
374374/// its source outlives it too.
375375public final class Property < Value> : PropertyProtocol {
376376 private let sources : [ AnyObject ]
377+ private let disposable : Disposable ?
377378
378379 private let _value : ( ) -> Value
379380 private let _producer : ( ) -> SignalProducer < Value , NoError >
@@ -403,6 +404,7 @@ public final class Property<Value>: PropertyProtocol {
403404 /// - property: A value of the constant property.
404405 public init ( value: Value ) {
405406 sources = [ ]
407+ disposable = nil
406408 _value = { value }
407409 _producer = { SignalProducer ( value: value) }
408410 _signal = { Signal < Value , NoError > . empty }
@@ -414,6 +416,7 @@ public final class Property<Value>: PropertyProtocol {
414416 /// - property: A property to be wrapped.
415417 public init < P: PropertyProtocol > ( _ property: P ) where P. Value == Value {
416418 sources = Property . capture ( property)
419+ disposable = nil
417420 _value = { property. value }
418421 _producer = { property. producer }
419422 _signal = { property. signal }
@@ -490,29 +493,18 @@ public final class Property<Value>: PropertyProtocol {
490493 // they see a consistent view of the `self.value`.
491494 // https://github.com/ReactiveCocoa/ReactiveCocoa/pull/3042
492495 let producer = unsafeProducer. replayLazily ( upTo: 1 )
493-
496+
497+ let atomic = Atomic < Value ? > ( nil )
498+ disposable = producer. startWithNext { atomic. value = $0 }
499+
494500 // Verify that an initial is sent. This is friendlier than deadlocking
495501 // in the event that one isn't.
496- var value : Value ? = nil
497- let disposable = producer. start { event in
498- switch event {
499- case let . next( newValue) :
500- value = newValue
501-
502- case . completed, . interrupted:
503- break
504-
505- case let . failed( error) :
506- fatalError ( " Receive unexpected error from a producer of `NoError` type: \( error) " )
507- }
508- }
509- guard value != nil else {
502+ guard atomic. value != nil else {
510503 fatalError ( " A producer promised to send at least one value. Received none. " )
511504 }
512- disposable. dispose ( )
513505
514506 self . sources = sources
515- _value = { producer . take ( first : 1 ) . single ( ) ! . value! }
507+ _value = { atomic . value! }
516508 _producer = { producer }
517509 _signal = {
518510 var extractedSignal : Signal < Value , NoError > !
@@ -521,6 +513,10 @@ public final class Property<Value>: PropertyProtocol {
521513 }
522514 }
523515
516+ deinit {
517+ disposable? . dispose ( )
518+ }
519+
524520 /// Inspect if `property` is an `AnyProperty` and has already captured its
525521 /// sources using a closure. Returns that closure if it does. Otherwise,
526522 /// returns a closure which captures `property`.
0 commit comments