-
Notifications
You must be signed in to change notification settings - Fork 213
/
ObservableType+Weak.swift
119 lines (102 loc) · 4.63 KB
/
ObservableType+Weak.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// ObservableType+Weak.swift
// RxSwiftExt
//
// Created by Ian Keen on 17/04/2016.
// Copyright © 2016 RxSwift Community. All rights reserved.
//
import Foundation
import RxSwift
extension ObservableType {
/**
Leverages instance method currying to provide a weak wrapper around an instance function
- parameter obj: The object that owns the function
- parameter method: The instance function represented as `InstanceType.instanceFunc`
*/
fileprivate func weakify<Object: AnyObject, Input>(_ obj: Object, method: ((Object) -> (Input) -> Void)?) -> ((Input) -> Void) {
return { [weak obj] value in
guard let obj = obj else { return }
method?(obj)(value)
}
}
fileprivate func weakify<Object: AnyObject>(_ obj: Object, method: ((Object) -> () -> Void)?) -> (() -> Void) {
return { [weak obj] in
guard let obj = obj else { return }
method?(obj)()
}
}
/**
Subscribes an event handler to an observable sequence.
- parameter weak: Weakly referenced object containing the target function.
- parameter on: Function to invoke on `weak` for each event in the observable sequence.
- returns: Subscription object used to unsubscribe from the observable sequence.
*/
public func subscribe<Object: AnyObject>(weak obj: Object, _ on: @escaping (Object) -> (Event<Element>) -> Void) -> Disposable {
return self.subscribe(weakify(obj, method: on))
}
/**
Subscribes an element handler, an error handler, a completion handler and disposed handler to an observable sequence.
- parameter weak: Weakly referenced object containing the target function.
- parameter onNext: Function to invoke on `weak` for each element in the observable sequence.
- parameter onError: Function to invoke on `weak` upon errored termination of the observable sequence.
- parameter onCompleted: Function to invoke on `weak` upon graceful termination of the observable sequence.
- parameter onDisposed: Function to invoke on `weak` upon any type of termination of sequence (if the sequence has
gracefully completed, errored, or if the generation is cancelled by disposing subscription)
- returns: Subscription object used to unsubscribe from the observable sequence.
*/
public func subscribe<Object: AnyObject>(
weak obj: Object,
onNext: ((Object) -> (Element) -> Void)? = nil,
onError: ((Object) -> (Error) -> Void)? = nil,
onCompleted: ((Object) -> () -> Void)? = nil,
onDisposed: ((Object) -> () -> Void)? = nil
) -> Disposable {
let disposable: Disposable
if let disposed = onDisposed {
disposable = Disposables.create(with: weakify(obj, method: disposed))
} else {
disposable = Disposables.create()
}
let observer = AnyObserver { [weak obj] (e: Event<Element>) in
guard let obj = obj else { return }
switch e {
case .next(let value):
onNext?(obj)(value)
case .error(let e):
onError?(obj)(e)
disposable.dispose()
case .completed:
onCompleted?(obj)()
disposable.dispose()
}
}
return Disposables.create(self.asObservable().subscribe(observer), disposable)
}
/**
Subscribes an element handler to an observable sequence.
- parameter weak: Weakly referenced object containing the target function.
- parameter onNext: Function to invoke on `weak` for each element in the observable sequence.
- returns: Subscription object used to unsubscribe from the observable sequence.
*/
public func subscribeNext<Object: AnyObject>(weak obj: Object, _ onNext: @escaping (Object) -> (Element) -> Void) -> Disposable {
return self.subscribe(onNext: weakify(obj, method: onNext))
}
/**
Subscribes an error handler to an observable sequence.
- parameter weak: Weakly referenced object containing the target function.
- parameter onError: Function to invoke on `weak` upon errored termination of the observable sequence.
- returns: Subscription object used to unsubscribe from the observable sequence.
*/
public func subscribeError<Object: AnyObject>(weak obj: Object, _ onError: @escaping (Object) -> (Error) -> Void) -> Disposable {
return self.subscribe(onError: weakify(obj, method: onError))
}
/**
Subscribes a completion handler to an observable sequence.
- parameter weak: Weakly referenced object containing the target function.
- parameter onCompleted: Function to invoke on `weak` graceful termination of the observable sequence.
- returns: Subscription object used to unsubscribe from the observable sequence.
*/
public func subscribeCompleted<Object: AnyObject>(weak obj: Object, _ onCompleted: @escaping (Object) -> () -> Void) -> Disposable {
return self.subscribe(onCompleted: weakify(obj, method: onCompleted))
}
}