Skip to content

Commit

Permalink
Merge branch 'data_source_patch' of https://github.com/sgl0v/RxSwift
Browse files Browse the repository at this point in the history
…into sgl0v-data_source_patch
  • Loading branch information
kzaher committed Dec 17, 2016
2 parents 9d7ab84 + a691bcd commit 51c6e06
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions RxCocoa/Common/DelegateProxyType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@
// Copyright © 2015 Krunoslav Zaher. All rights reserved.
//

#if !os(Linux)
#if os(iOS) || os(tvOS) || os(macOS)

import Foundation
#if !RX_NO_MODULE
import RxSwift
#endif

#if os(macOS)
import Cocoa
typealias View = NSView
#else
import UIKit
typealias View = UIView
#endif

/**
`DelegateProxyType` protocol enables using both normal delegates and Rx observable sequences with
views that can have only one delegate/datasource registered.
Expand Down Expand Up @@ -221,8 +229,10 @@ extension DelegateProxyType {
}

extension ObservableType {
func subscribeProxyDataSource<P: DelegateProxyType>(ofObject object: AnyObject, dataSource: AnyObject, retainDataSource: Bool, binding: @escaping (P, Event<E>) -> Void)
func subscribeProxyDataSource<P: DelegateProxyType>(ofObject object: View, dataSource: AnyObject, retainDataSource: Bool, binding: @escaping (P, Event<E>) -> Void)
-> Disposable {
// this is needed to flush any delayed old state (https://github.com/RxSwiftCommunity/RxDataSources/pull/75)
object.layoutIfNeeded()
let proxy = P.proxyForObject(object)
let disposable = P.installForwardDelegate(dataSource, retainDelegate: retainDataSource, onProxyForObject: object)

Expand All @@ -233,7 +243,7 @@ extension ObservableType {
}
// source can never end, otherwise it would release the subscriber, and deallocate the data source
.concat(Observable.never())
.takeUntil((object as! NSObject).rx.deallocated)
.takeUntil(object.rx.deallocated)
.subscribe { [weak object] (event: Event<E>) in
MainScheduler.ensureExecutingOnScheduler()

Expand All @@ -254,7 +264,10 @@ extension ObservableType {
}
}

return Disposables.create(subscription, disposable)
let flushStateDisposable = Disposables.create { [weak object] in
object?.layoutIfNeeded()
}
return Disposables.create(subscription, disposable, flushStateDisposable)
}
}

Expand Down

0 comments on commit 51c6e06

Please sign in to comment.