Overview
Comment: | OFNotificationCenter: Add support for blocks |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | notifications |
Files: | files | file ages | folders |
SHA3-256: |
108d3601163bd5a3a9140c4f5820491d |
User & Date: | js on 2021-10-31 20:14:44 |
Other Links: | branch diff | manifest | tags |
Context
2021-10-31
| ||
20:24 | OFNotificationCenter: Add convenience methods check-in: fed7fa621e user: js tags: notifications | |
20:14 | OFNotificationCenter: Add support for blocks check-in: 108d360116 user: js tags: notifications | |
18:02 | OFNotificationCenter: Move posting out of the lock check-in: 93851ccd0f user: js tags: notifications | |
Changes
Modified src/OFNotificationCenter.h from [7a53480f1b] to [f094627460].
︙ | |||
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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 | + + + + + + + + + + - + | OF_ASSUME_NONNULL_BEGIN @class OFMutableDictionary OF_GENERIC(KeyType, ObjectType); #ifdef OF_HAVE_THREADS @class OFMutex; #endif @class OFNotificationCenterHandle; #ifdef OF_HAVE_BLOCKS /** * @brief A block which is called when a notification has been posted. * * @param notification The notification that has been posted */ typedef void (^OFNotificationCenterBlock)(OFNotification *notification); #endif /** * @class OFNotificationCenter OFNotificationCenter.h \ * ObjFW/OFNotificationCenter.h * * @brief A class to send and register for notifications. */ @interface OFNotificationCenter: OFObject { #ifdef OF_HAVE_THREADS OFMutex *_mutex; #endif |
︙ | |||
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | 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 | + + + + + + + + + + + + + + + + + + + + + + + + + + + | * @param name The name that was specified when adding the observer * @param object The object that was specified when adding the observer */ - (void)removeObserver: (id)observer selector: (SEL)selector name: (OFNotificationName)name object: (nullable id)object; #ifdef OF_HAVE_BLOCKS /** * @brief Adds an observer for the specified notification and object. * * To remove the observer again, use @ref removeObserver:. * * @param name The name of the notification to observe * @param object The object that should be sending the notification, or `nil` * if the object should be ignored to determine what * notifications to deliver * @param block The block to handle notifications * @return An opaque object to remove the observer again */ - (OFNotificationCenterHandle *) addObserverForName: (OFNotificationName)name object: (nullable id)object usingBlock: (OFNotificationCenterBlock)block; /** * @brief Removes an observer. The specified observer must be one returned by * @ref addObserver:selector:name:object:. * * @param observer The object that was returned when adding the observer */ - (void)removeObserver: (OFNotificationCenterHandle *)observer; #endif /** * @brief Posts the specified notification. * * @param notification The notification to post */ - (void)postNotification: (OFNotification *)notification; @end OF_ASSUME_NONNULL_END |
Modified src/OFNotificationCenter.m from [7d4ea0e843] to [ffe3d96692].
︙ | |||
19 20 21 22 23 24 25 26 27 28 29 | 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | + + - + + + + + - - - + + + + + + + + + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + + + + + - + + - + + + + + + - + + + + + + | #import "OFArray.h" #import "OFDictionary.h" #ifdef OF_HAVE_THREADS # import "OFMutex.h" #endif #import "OFSet.h" #import "OFString.h" #import "OFInvalidArgumentException.h" @interface OFDefaultNotificationCenter: OFNotificationCenter @end |
︙ | |||
128 129 130 131 132 133 134 | 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | - + - + + + + + + + + + + + + + + + + + + + + + + + + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - + - - - - - - + + + - - - - - - - + + + + + + + - - - - - - - - - - - + - - - - - + + + + + - - - - + + + + + + + + + + + - + + + + | { self = [super init]; @try { #ifdef OF_HAVE_THREADS _mutex = [[OFMutex alloc] init]; #endif |
︙ |
Modified tests/OFNotificationCenterTests.m from [1ce580811f] to [96bdb8154b].
︙ | |||
102 103 104 105 106 107 108 109 110 111 112 113 114 115 | 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 | + + + + + + + + + + + + + + + + + + + + + + | notification = [OFNotification notificationWithName: notificationName object: @"foo"]; TEST(@"-[postNotification:] #3", R([center postNotification: notification]) && test1->_received == 1 && test2->_received == 3 && test3->_received == 0 && test4->_received == 0) #ifdef OF_HAVE_BLOCKS __block bool received = false; OFNotificationCenterHandle *handle; notification = [OFNotification notificationWithName: notificationName object: self]; TEST(@"-[addObserverForName:object:usingBlock:]", (handle = [center addObserverForName: notificationName object: self usingBlock: ^ (OFNotification *notif) { OFEnsure(notif == notification && !received); received = true; }]) && R([center postNotification: notification]) && received && test1->_received == 2 && test2->_received == 4 && test3->_received == 0 && test4->_received == 0) /* Act like the blocks test didn't happen. */ [center removeObserver: handle]; test1->_received--; test2->_received--; #endif TEST(@"-[removeObserver:selector:name:object:]", R([center removeObserver: test1 selector: @selector(handleNotification:) name: notificationName object: self]) && R([center removeObserver: test2 |
︙ |