This repository has been archived by the owner on Jun 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
NSObject+BKBlockObservation.h
142 lines (108 loc) · 5.77 KB
/
NSObject+BKBlockObservation.h
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// NSObject+BKBlockObservation.h
// BlocksKit
//
#import "BKDefines.h"
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
/** Blocks wrapper for key-value observation.
In Mac OS X Panther, Apple introduced an API called "key-value
observing." It implements an [observer pattern](http://en.wikipedia.org/wiki/Observer_pattern),
where an object will notify observers of any changes in state.
NSNotification is a rudimentary form of this design style;
KVO, however, allows for the observation of any change in key-value state.
The API for key-value observation, however, is flawed, ugly, and lengthy.
Like most of the other block abilities in BlocksKit, observation saves
and a bunch of code and a bunch of potential bugs.
Includes code by the following:
- [Andy Matuschak](https://github.com/andymatuschak)
- [Jon Sterling](https://github.com/jonsterling)
- [Zach Waldowski](https://github.com/zwaldowski)
- [Jonathan Wight](https://github.com/schwa)
*/
@interface NSObject (BlockObservation)
/** Adds an observer to an object conforming to NSKeyValueObserving.
Adds a block observer that executes a block upon a state change.
@param keyPath The property to observe, relative to the reciever.
@param task A block with no return argument, and a single parameter: the reciever.
@return Returns a globally unique process identifier for removing
observation with removeObserverWithBlockToken:.
@see addObserverForKeyPath:identifier:options:task:
*/
- (NSString *)bk_addObserverForKeyPath:(NSString *)keyPath task:(void (^)(id target))task;
/** Adds an observer to an object conforming to NSKeyValueObserving.
Adds a block observer that executes the same block upon
multiple state changes.
@param keyPaths An array of properties to observe, relative to the reciever.
@param task A block with no return argument and two parameters: the
reciever and the key path of the value change.
@return A unique identifier for removing
observation with removeObserverWithBlockToken:.
@see addObserverForKeyPath:identifier:options:task:
*/
- (NSString *)bk_addObserverForKeyPaths:(NSArray *)keyPaths task:(void (^)(id obj, NSString *keyPath))task;
/** Adds an observer to an object conforming to NSKeyValueObserving.
Adds a block observer that executes a block upon a state change
with specific options.
@param keyPath The property to observe, relative to the reciever.
@param options The NSKeyValueObservingOptions to use.
@param task A block with no return argument and two parameters: the
reciever and the change dictionary.
@return Returns a globally unique process identifier for removing
observation with removeObserverWithBlockToken:.
@see addObserverForKeyPath:identifier:options:task:
*/
- (NSString *)bk_addObserverForKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options task:(void (^)(id obj, NSDictionary *change))task;
/** Adds an observer to an object conforming to NSKeyValueObserving.
Adds a block observer that executes the same block upon
multiple state changes with specific options.
@param keyPaths An array of properties to observe, relative to the reciever.
@param options The NSKeyValueObservingOptions to use.
@param task A block with no return argument and three parameters: the
reciever, the key path of the value change, and the change dictionary.
@return A unique identifier for removing
observation with removeObserverWithBlockToken:.
@see addObserverForKeyPath:identifier:options:task:
*/
- (NSString *)bk_addObserverForKeyPaths:(NSArray *)keyPaths options:(NSKeyValueObservingOptions)options task:(void (^)(id obj, NSString *keyPath, NSDictionary *change))task;
/** Adds an observer to an object conforming to NSKeyValueObserving.
Adds a block observer that executes the block upon a
state change.
@param keyPath The property to observe, relative to the reciever.
@param token An identifier for the observation block.
@param options The NSKeyValueObservingOptions to use.
@param task A block responding to the reciever and the KVO change.
observation with removeObserverWithBlockToken:.
@see addObserverForKeyPath:task:
*/
- (void)bk_addObserverForKeyPath:(NSString *)keyPath identifier:(NSString *)token options:(NSKeyValueObservingOptions)options task:(void (^)(id obj, NSDictionary *change))task;
/** Adds an observer to an object conforming to NSKeyValueObserving.
Adds a block observer that executes the same block upon
multiple state changes.
@param keyPaths An array of properties to observe, relative to the reciever.
@param token An identifier for the observation block.
@param options The NSKeyValueObservingOptions to use.
@param task A block responding to the reciever, the key path, and the KVO change.
observation with removeObserversWithIdentifier:.
@see addObserverForKeyPath:task:
*/
- (void)bk_addObserverForKeyPaths:(NSArray *)keyPaths identifier:(NSString *)token options:(NSKeyValueObservingOptions)options task:(void (^)(id obj, NSString *keyPath, NSDictionary *change))task;
/** Removes a block observer.
@param keyPath The property to stop observing, relative to the reciever.
@param token The unique key returned by addObserverForKeyPath:task:
or the identifier given in addObserverForKeyPath:identifier:task:.
@see removeObserversWithIdentifier:
*/
- (void)bk_removeObserverForKeyPath:(NSString *)keyPath identifier:(NSString *)token;
/** Removes multiple block observers with a certain identifier.
@param token A unique key returned by addObserverForKeyPath:task:
and addObserverForKeyPaths:task: or the identifier given in
addObserverForKeyPath:identifier:task: and
addObserverForKeyPaths:identifier:task:.
@see removeObserverForKeyPath:identifier:
*/
- (void)bk_removeObserversWithIdentifier:(NSString *)token;
/** Remove all registered block observers. */
- (void)bk_removeAllBlockObservers;
@end
NS_ASSUME_NONNULL_END