forked from millenomi/muikit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
L0KVODispatcher.m
438 lines (332 loc) · 15 KB
/
L0KVODispatcher.m
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//
// L0MicroBindings.m
// MuiKit
//
// Created by ∞ on 17/07/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "L0KVODispatcher.h"
#import "L0KVODictionaryAdditions.h"
#if kL0KVODispatcherAllowVerboseLogging
#define L0KVOLog L0Log
#else
#define L0KVOLog(...)
#endif
L0UniquePointerConstant(kL0KVODispatcherObservingContext);
@interface L0KVODispatcher ()
- (void) observe:(NSString *)keyPath ofObject:(id)object usingSelectorStringOrBlock:(id)selectorStringOrBlock options:(NSKeyValueObservingOptions)options;
@end
void L0KVODispatcherNoteEndReentry(id object, NSString* keyPath)
{
L0LogAlways(@"Owch. This shouldn't have happened: endObserving:'%@' ofObject:%@ was called during an observe:... call. It can happen if you use NSKeyValueObservingOptionInitial. Fixing this is nontrivial and you should really really REALLY refactor your code to check the initial condition instead, or remove KVO dispatcher state changes in this callback.\nAs a convenient workaround, I have enqueued an end-observing call on the run loop which should work, but bizarreness is always around the corner. PS: break on L0KVODispatcherNoteEndReentry() to debug.", keyPath, object);
}
@implementation L0KVODispatcher
- (id) initWithTarget:(id) t;
{
if (self = [super init]) {
target = t;
selectorsByKeyPathsAndObjects = [NSMutableDictionary new];
}
return self;
}
- (void) dealloc;
{
for (NSValue* ptr in selectorsByKeyPathsAndObjects) {
id object = [ptr nonretainedObjectValue];
for (NSString* keyPath in [selectorsByKeyPathsAndObjects objectForKey:ptr])
[object removeObserver:self forKeyPath:keyPath];
}
[selectorsByKeyPathsAndObjects release];
[super dealloc];
}
- description;
{
return [NSString stringWithFormat:@"%@ { target = %@ }", [super description], target];
}
#pragma mark -
#pragma mark Observation
- (void) observe:(NSString*) keyPath ofObject:(id) object usingSelector:(SEL) selector options:(NSKeyValueObservingOptions) options;
{
[self observe:keyPath ofObject:object usingSelectorStringOrBlock:NSStringFromSelector(selector) options:options];
}
- (void) observe:(NSString *)keyPath ofObject:(id)object usingSelectorStringOrBlock:(id)selectorStringOrBlock options:(NSKeyValueObservingOptions)options;
{
NSValue* ptr = [NSValue valueWithNonretainedObject:object];
NSMutableDictionary* selectorsByKeyPath = [selectorsByKeyPathsAndObjects objectForKey:ptr];
NSString* previousSelector = [selectorsByKeyPath objectForKey:keyPath];
if (!selectorsByKeyPath) {
selectorsByKeyPath = [NSMutableDictionary dictionary];
[selectorsByKeyPathsAndObjects setObject:selectorsByKeyPath forKey:ptr];
}
BOOL alreadyRegistered = (previousSelector != nil) && ![previousSelector isEqual:selectorStringOrBlock];
NSAssert(!alreadyRegistered, @"Should not be observing with a different selector string");
[selectorsByKeyPath setObject:[[selectorStringOrBlock copy] autorelease] forKey:keyPath];
addingReentryCount++;
[object addObserver:self forKeyPath:keyPath options:options context:(void*) kL0KVODispatcherObservingContext];
addingReentryCount--;
L0KVOLog(@"watching (a %@).%@ using %@", [object class], keyPath, selectorStringOrBlock);
#if DEBUG
[self willObserveKeyPath:keyPath ofObject:object];
#endif
}
#if __BLOCKS__
- (void) observe:(NSString*) keyPath ofObject:(id) object options:(NSKeyValueObservingOptions) options usingBlock:(L0KVODispatcherChangeBlock) block;
{
[self observe:keyPath ofObject:object usingSelectorStringOrBlock:[[block copy] autorelease] options:options];
}
#endif
- (void) observeValueForKeyPath:(NSString*) keyPath ofObject:(id) object change:(NSDictionary*) change context:(void*) context;
{
if (context != kL0KVODispatcherObservingContext) return;
NSValue* ptr = [NSValue valueWithNonretainedObject:object];
NSMutableDictionary* selectorsByKeyPath = [selectorsByKeyPathsAndObjects objectForKey:ptr];
id selectorStringOrBlock = [selectorsByKeyPath objectForKey:keyPath];
if (selectorStringOrBlock) {
// Prevent the object from going out of scope soon.
[[object retain] autorelease];
#if __BLOCKS__
if (![selectorStringOrBlock isKindOfClass:[NSString class]]) {
((L0KVODispatcherChangeBlock)selectorStringOrBlock)(object, change);
} else
#endif
{
[target performSelector:NSSelectorFromString(selectorStringOrBlock) withObject:object withObject:change];
}
}
}
- (void) endObservingObjectAndKeyPath:(NSArray*) objectAndKeyPath;
{
[self endObserving:[objectAndKeyPath objectAtIndex:0] ofObject:[objectAndKeyPath objectAtIndex:1]];
}
- (void) endObserving:(NSString*) keyPath ofObject:(id) object;
{
if (addingReentryCount > 0) {
[self performSelector:@selector(endObservingObjectAndKeyPath:) withObject:[NSArray arrayWithObjects:object, keyPath, nil] afterDelay:0.01];
L0KVODispatcherNoteEndReentry(object, keyPath);
return;
}
NSValue* ptr = [NSValue valueWithNonretainedObject:object];
NSMutableDictionary* selectorsByKeyPath = [selectorsByKeyPathsAndObjects objectForKey:ptr];
if ([selectorsByKeyPath objectForKey:keyPath]) {
L0KVOLog(@"stopping observation of (a %@).%@", [object class], keyPath);
[selectorsByKeyPath removeObjectForKey:keyPath];
if ([selectorsByKeyPath count] == 0)
[selectorsByKeyPathsAndObjects removeObjectForKey:ptr];
[object removeObserver:self forKeyPath:keyPath];
#if DEBUG
[self didEndObservingKeyPath:keyPath ofObject:object];
#endif
}
}
#pragma mark -
#pragma mark To-many dispatch.
// insertion => - (void) inArrayOfObject:(id) o inserted:(id) i atIndex:(NSUInteger) idx;
// removal => - (void) inArrayOfObject:(id) o removed:(id) i atIndex:(NSUInteger) idx;
// replacement => - (void) inArrayOfObject:(id) o replaced:(id) oldObject with:(id) newObject atIndex:(NSUInteger) idx;
- (void) forEachArrayChange:(NSDictionary*) change forObject:(id) o invokeSelectorForInsertion:(SEL) insertion removal:(SEL) removal replacement:(SEL) replacement;
{
NSKeyValueChange changeKind = L0KVOChangeKind(change);
NSInvocation* insertionInv = nil, * removalInv = nil, * replacementInv = nil;
// Set up the invocation stuff.
if (changeKind == NSKeyValueChangeInsertion || (changeKind == NSKeyValueChangeReplacement && !replacement)) {
NSMethodSignature* insertSig = [target methodSignatureForSelector:insertion];
insertionInv = [NSInvocation invocationWithMethodSignature:insertSig];
[insertionInv setTarget:target];
[insertionInv setSelector:insertion];
[insertionInv setArgument:&o atIndex:2];
}
if (changeKind == NSKeyValueChangeRemoval || (changeKind == NSKeyValueChangeReplacement && !replacement)) {
NSMethodSignature* removeSig = [target methodSignatureForSelector:removal];
removalInv = [NSInvocation invocationWithMethodSignature:removeSig];
[removalInv setTarget:target];
[removalInv setSelector:removal];
[removalInv setArgument:&o atIndex:2];
}
if (changeKind == NSKeyValueChangeReplacement && replacement) {
NSMethodSignature* replacementSig = [target methodSignatureForSelector:replacement];
replacementInv = [NSInvocation invocationWithMethodSignature:replacementSig];
[replacementInv setTarget:target];
[replacementInv setSelector:replacement];
[replacementInv setArgument:&o atIndex:2];
}
NSIndexSet* indexes = L0KVOChangedIndexes(change);
NSUInteger arrayIndex = [indexes firstIndex], changeIndex = 0;
NSArray* insertions = (changeKind == NSKeyValueChangeRemoval)? nil : L0KVOChangedValue(change);
NSArray* removals = (changeKind == NSKeyValueChangeInsertion)? nil : L0KVOPreviousValue(change);
while (arrayIndex != NSNotFound) {
id insertedObject = [insertions objectAtIndex:changeIndex];
id removedObject = [removals objectAtIndex:changeIndex];
if (changeKind == NSKeyValueChangeRemoval || (changeKind == NSKeyValueChangeReplacement && !replacement)) {
[removalInv setArgument:&removedObject atIndex:3];
[removalInv setArgument:&arrayIndex atIndex:4];
[removalInv invoke];
}
if (changeKind == NSKeyValueChangeInsertion || (changeKind == NSKeyValueChangeReplacement && !replacement)) {
[insertionInv setArgument:&insertedObject atIndex:3];
[insertionInv setArgument:&arrayIndex atIndex:4];
[insertionInv invoke];
}
if (changeKind == NSKeyValueChangeReplacement && replacement) {
[replacementInv setArgument:&removedObject atIndex:3];
[replacementInv setArgument:&insertedObject atIndex:4];
[replacementInv setArgument:&arrayIndex atIndex:5];
[replacementInv invoke];
}
arrayIndex = [indexes indexGreaterThanIndex:arrayIndex];
changeIndex++;
}
}
- (void) forEachSetChange:(NSDictionary*) change forObject:(id) o invokeSelectorForInsertion:(SEL) insertion removal:(SEL) removal;
{
NSKeyValueChange changeKind = L0KVOChangeKind(change);
NSInvocation* insertionInv = nil, * removalInv = nil;
// Set up the invocation stuff.
if (changeKind == NSKeyValueChangeInsertion || changeKind == NSKeyValueChangeReplacement) {
NSMethodSignature* insertSig = [target methodSignatureForSelector:insertion];
insertionInv = [NSInvocation invocationWithMethodSignature:insertSig];
[insertionInv setTarget:target];
[insertionInv setSelector:insertion];
[insertionInv setArgument:&o atIndex:2];
}
if (changeKind == NSKeyValueChangeRemoval || changeKind == NSKeyValueChangeReplacement) {
NSMethodSignature* removeSig = [target methodSignatureForSelector:removal];
removalInv = [NSInvocation invocationWithMethodSignature:removeSig];
[removalInv setTarget:target];
[removalInv setSelector:removal];
[removalInv setArgument:&o atIndex:2];
}
NSSet* insertions = (changeKind == NSKeyValueChangeRemoval)? nil : L0KVOChangedValue(change);
NSSet* removals = (changeKind == NSKeyValueChangeInsertion)? nil : L0KVOPreviousValue(change);
for (id removedObject in removals) {
[removalInv setArgument:&removedObject atIndex:3];
[removalInv invoke];
}
for (id insertedObject in insertions) {
[insertionInv setArgument:&insertedObject atIndex:3];
[insertionInv invoke];
}
}
#if __BLOCKS__
- (void) forEachArrayChange:(NSDictionary*) change invokeBlockForInsertion:(L0KVODispatcherArrayChangeBlock) insertion removal:(L0KVODispatcherArrayChangeBlock) removal replacement:(L0KVODispatcherArrayReplacementBlock) replacement;
{
NSKeyValueChange changeKind = L0KVOChangeKind(change);
NSIndexSet* indexes = L0KVOChangedIndexes(change);
NSUInteger arrayIndex = [indexes firstIndex], changeIndex = 0;
NSArray* insertions = (changeKind == NSKeyValueChangeRemoval)? nil : L0KVOChangedValue(change);
NSArray* removals = (changeKind == NSKeyValueChangeInsertion)? nil : L0KVOPreviousValue(change);
while (arrayIndex != NSNotFound) {
id insertedObject = [insertions objectAtIndex:changeIndex];
id removedObject = [removals objectAtIndex:changeIndex];
if (changeKind == NSKeyValueChangeRemoval || (changeKind == NSKeyValueChangeReplacement && !replacement))
removal(removedObject, arrayIndex);
if (changeKind == NSKeyValueChangeInsertion || (changeKind == NSKeyValueChangeReplacement && !replacement))
insertion(removedObject, arrayIndex);
if (changeKind == NSKeyValueChangeReplacement && replacement)
replacement(removedObject, insertedObject, arrayIndex);
arrayIndex = [indexes indexGreaterThanIndex:arrayIndex];
changeIndex++;
}
}
- (void) forEachSetChange:(NSDictionary*) change invokeBlockForInsertion:(L0KVODispatcherSetChangeBlock) insertion removal:(L0KVODispatcherSetChangeBlock) removal;
{
NSKeyValueChange changeKind = L0KVOChangeKind(change);
NSSet* insertions = (changeKind == NSKeyValueChangeRemoval)? nil : L0KVOChangedValue(change);
NSSet* removals = (changeKind == NSKeyValueChangeInsertion)? nil : L0KVOPreviousValue(change);
if ([removals conformsToProtocol:@protocol(NSFastEnumeration)]) {
for (id removedObject in removals) {
removal(removedObject);
}
} else if (removals && ![removals isKindOfClass:[NSNull class]])
removal(removals);
if ([insertions conformsToProtocol:@protocol(NSFastEnumeration)]) {
for (id insertedObject in insertions) {
insertion(insertedObject);
}
} else if (insertions && ![insertions isKindOfClass:[NSNull class]])
insertion(insertions);
}
#endif
@end
#if DEBUG
/* -- - -- DEBUG FACILITIES -- - -- */
static NSMutableDictionary* nonretainedObjectsToObservers = nil;
@implementation L0KVODispatcher (L0KVODispatcherDebugTools)
+ (BOOL) shouldKeepTrackOfObservers;
{
static BOOL foundOut = NO, shouldKeepTrack;
if (!foundOut) {
shouldKeepTrack = NO;
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"L0KVODispatcherShouldKeepTrackOfObservers"])
shouldKeepTrack = YES;
else {
char* envVar = getenv("L0KVODispatcherShouldKeepTrackOfObservers");
if (envVar && strcmp(envVar, "YES") == 0)
shouldKeepTrack = YES;
}
if (shouldKeepTrack) {
L0LogAlways(@"\n\n!! WARNING !!\nL0KVODispatcher is now tracking all observations, which you can query using [L0KVODispatcher observersOfObject:...] and [L0KVODispatcher observersOfObject:... keyPath:...]. Make sure you disable this when running normally.\n\n");
}
foundOut = YES;
}
return shouldKeepTrack;
}
+ nonretainedObjectsToObservers;
{
if (!nonretainedObjectsToObservers)
nonretainedObjectsToObservers = [NSMutableDictionary new];
return nonretainedObjectsToObservers;
}
+ (void) addObserver:(L0KVODispatcher*) d ofKeyPath:(NSString*) path ofObject:(id) o;
{
if (![self shouldKeepTrackOfObservers]) return;
NSValue* v = [NSValue valueWithNonretainedObject:o];
NSMutableDictionary* n2o = [self nonretainedObjectsToObservers];
NSMutableDictionary* keysToDispatchers = [n2o objectForKey:v];
if (!keysToDispatchers) {
keysToDispatchers = [NSMutableDictionary dictionary];
[n2o setObject:keysToDispatchers forKey:v];
}
NSMutableArray* dispatchers = [keysToDispatchers objectForKey:path];
if (!dispatchers) {
dispatchers = [NSMutableArray array];
[keysToDispatchers setObject:dispatchers forKey:path];
}
[dispatchers addObject:d];
}
+ (void) removeObserver:(L0KVODispatcher*) d ofKeyPath:(NSString*) path ofObject:(id) o;
{
if (![self shouldKeepTrackOfObservers]) return;
NSValue* v = [NSValue valueWithNonretainedObject:o];
NSMutableDictionary* n2o = [self nonretainedObjectsToObservers];
NSMutableDictionary* keysToDispatchers = [n2o objectForKey:v];
if (!keysToDispatchers)
return;
NSMutableArray* dispatchers = [keysToDispatchers objectForKey:path];
if (!dispatchers)
return;
[dispatchers removeObject:d];
if ([dispatchers count] == 0)
[keysToDispatchers removeObjectForKey:path];
if ([keysToDispatchers count] == 0)
[n2o removeObjectForKey:v];
}
- (void) willObserveKeyPath:(NSString*) kp ofObject:(id) o;
{
[[self class] addObserver:self ofKeyPath:kp ofObject:o];
}
- (void) didEndObservingKeyPath:(NSString*) kp ofObject:(id) o;
{
[[self class] removeObserver:self ofKeyPath:kp ofObject:o];
}
+ (NSDictionary*) observersOfObject:(id) o;
{
if (![self shouldKeepTrackOfObservers]) return nil;
return [[self nonretainedObjectsToObservers] objectForKey:[NSValue valueWithNonretainedObject:o]];
}
+ (NSArray*) observersOfObject:(id) o keyPath:(NSString*) path;
{
if (![self shouldKeepTrackOfObservers]) return nil;
return [[self observersOfObject:o] objectForKey:path];
}
@end
#endif