@@ -21,18 +21,20 @@ let dataSourceAssociatedTag: UnsafeRawPointer = UnsafeRawPointer(UnsafeMutablePo
21
21
/// Base class for `DelegateProxyType` protocol.
22
22
///
23
23
/// This implementation is not thread safe and can be used only from one thread (Main thread).
24
- open class DelegateProxy : _RXDelegateProxy {
25
-
24
+ open class DelegateProxy < P: AnyObject , D: NSObjectProtocol > : _RXDelegateProxy {
25
+ public typealias ParentObject = P
26
+ public typealias Delegate = D
27
+
26
28
private var sentMessageForSelector = [ Selector: MessageDispatcher] ( )
27
29
private var methodInvokedForSelector = [ Selector: MessageDispatcher] ( )
28
30
29
31
/// Parent object associated with delegate proxy.
30
- weak private( set) var parentObject : AnyObject ?
32
+ weak private( set) var parentObject : ParentObject ?
31
33
32
34
/// Initializes new instance.
33
35
///
34
36
/// - parameter parentObject: Optional parent object that owns `DelegateProxy` as associated object.
35
- public required init ( parentObject: AnyObject ) {
37
+ public required init ( parentObject: ParentObject ) {
36
38
self . parentObject = parentObject
37
39
38
40
MainScheduler . ensureExecutingOnScheduler ( )
@@ -73,7 +75,7 @@ open class DelegateProxy : _RXDelegateProxy {
73
75
74
76
// reactive property implementation in a real class (`UIScrollView`)
75
77
public var property: Observable<CGPoint> {
76
- let proxy = RxScrollViewDelegateProxy.proxyForObject( base)
78
+ let proxy = RxScrollViewDelegateProxy.proxy(for: base)
77
79
return proxy.internalSubject.asObservable()
78
80
}
79
81
@@ -131,7 +133,7 @@ open class DelegateProxy : _RXDelegateProxy {
131
133
132
134
// reactive property implementation in a real class (`UIScrollView`)
133
135
public var property: Observable<CGPoint> {
134
- let proxy = RxScrollViewDelegateProxy.proxyForObject( base)
136
+ let proxy = RxScrollViewDelegateProxy.proxy(for: base)
135
137
return proxy.internalSubject.asObservable()
136
138
}
137
139
@@ -192,7 +194,7 @@ open class DelegateProxy : _RXDelegateProxy {
192
194
///
193
195
/// - parameter object: Object that can have assigned delegate proxy.
194
196
/// - returns: Assigned delegate proxy or `nil` if no delegate proxy is assigned.
195
- open class func assignedProxyFor ( _ object: AnyObject ) -> AnyObject ? {
197
+ open class func assignedProxy ( for object: ParentObject ) -> Delegate ? {
196
198
let maybeDelegate = objc_getAssociatedObject ( object, self . delegateAssociatedObjectTag ( ) )
197
199
return castOptionalOrFatalError ( maybeDelegate. map { $0 as AnyObject } )
198
200
}
@@ -201,18 +203,45 @@ open class DelegateProxy : _RXDelegateProxy {
201
203
///
202
204
/// - parameter object: Object that can have assigned delegate proxy.
203
205
/// - parameter proxy: Delegate proxy object to assign to `object`.
204
- open class func assignProxy( _ proxy: AnyObject , toObject object: AnyObject ) {
205
- precondition ( proxy. isKind ( of: self . classForCoder ( ) ) )
206
-
206
+ open class func assignProxy( _ proxy: Delegate , toObject object: ParentObject ) {
207
207
objc_setAssociatedObject ( object, self . delegateAssociatedObjectTag ( ) , proxy, . OBJC_ASSOCIATION_RETAIN)
208
208
}
209
209
210
+
211
+ /// Returns designated delegate property for object.
212
+ ///
213
+ /// Objects can have multiple delegate properties.
214
+ ///
215
+ /// Each delegate property needs to have it's own type implementing `DelegateProxyType`.
216
+ ///
217
+ /// It's abstract method.
218
+ ///
219
+ /// - parameter object: Object that has delegate property.
220
+ /// - returns: Value of delegate property.
221
+ open class func currentDelegate( for object: ParentObject ) -> Delegate ? {
222
+ rxAbstractMethod ( )
223
+ }
224
+
225
+ /// Sets designated delegate property for object.
226
+ ///
227
+ /// Objects can have multiple delegate properties.
228
+ ///
229
+ /// Each delegate property needs to have it's own type implementing `DelegateProxyType`.
230
+ ///
231
+ /// It's abstract method.
232
+ ///
233
+ /// - parameter toObject: Object that has delegate property.
234
+ /// - parameter delegate: Delegate value.
235
+ open class func setCurrentDelegate( _ delegate: Delegate ? , toObject: ParentObject ) {
236
+ rxAbstractMethod ( )
237
+ }
238
+
210
239
/// Sets reference of normal delegate that receives all forwarded messages
211
240
/// through `self`.
212
241
///
213
242
/// - parameter forwardToDelegate: Reference of delegate that receives all messages through `self`.
214
243
/// - parameter retainDelegate: Should `self` retain `forwardToDelegate`.
215
- open func setForwardToDelegate( _ delegate: AnyObject ? , retainDelegate: Bool ) {
244
+ open func setForwardToDelegate( _ delegate: Delegate ? , retainDelegate: Bool ) {
216
245
#if DEBUG // 4.0 all configurations
217
246
MainScheduler . ensureExecutingOnScheduler ( )
218
247
#endif
@@ -224,8 +253,8 @@ open class DelegateProxy : _RXDelegateProxy {
224
253
/// through `self`.
225
254
///
226
255
/// - returns: Value of reference if set or nil.
227
- open func forwardToDelegate( ) -> AnyObject ? {
228
- return self . _forwardToDelegate
256
+ open func forwardToDelegate( ) -> Delegate ? {
257
+ return castOptionalOrFatalError ( self . _forwardToDelegate)
229
258
}
230
259
231
260
private func hasObservers( selector: Selector ) -> Bool {
@@ -240,20 +269,15 @@ open class DelegateProxy : _RXDelegateProxy {
240
269
}
241
270
242
271
internal func reset( ) {
243
- guard let delegateProxySelf = self as? DelegateProxyType else {
244
- rxFatalErrorInDebug ( " \( self ) doesn't implement delegate proxy type. " )
245
- return
246
- }
247
-
248
272
guard let parentObject = self . parentObject else { return }
249
273
250
- let selfType = type ( of: delegateProxySelf )
274
+ let selfType = type ( of: self )
251
275
252
- let maybeCurrentDelegate = selfType. currentDelegateFor ( parentObject)
276
+ let maybeCurrentDelegate = selfType. currentDelegate ( for : parentObject)
253
277
254
278
if maybeCurrentDelegate === self {
255
279
selfType. setCurrentDelegate ( nil , toObject: parentObject)
256
- selfType. setCurrentDelegate ( self , toObject: parentObject)
280
+ selfType. setCurrentDelegate ( castOrFatalError ( self ) , toObject: parentObject)
257
281
}
258
282
}
259
283
@@ -276,7 +300,7 @@ fileprivate final class MessageDispatcher {
276
300
private let dispatcher : PublishSubject < [ Any ] >
277
301
private let result : Observable < [ Any ] >
278
302
279
- init ( delegateProxy _delegateProxy: DelegateProxy ) {
303
+ init < P , D > ( delegateProxy _delegateProxy: DelegateProxy < P , D > ) {
280
304
weak var weakDelegateProxy = _delegateProxy
281
305
282
306
let dispatcher = PublishSubject < [ Any ] > ( )
0 commit comments