[Event Handler] Fix observers for reused classes

Fix class handling so it cleans up observers if the InputNode is reused.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
swiftingly
Christopher Snowhill 2022-06-16 18:16:09 -07:00
parent 789e8e432e
commit 7bc49ccb80
3 changed files with 21 additions and 10 deletions

View File

@ -33,6 +33,8 @@ static void *kInputNodeContext = &kInputNodeContext;
}
- (BOOL)openWithSource:(id<CogSource>)source {
[self removeObservers];
decoder = [AudioDecoder audioDecoderForSource:source];
if(decoder == nil)
@ -63,6 +65,8 @@ static void *kInputNodeContext = &kInputNodeContext;
}
- (BOOL)openWithDecoder:(id<CogDecoder>)d {
[self removeObservers];
DLog(@"Opening with old decoder: %@", d);
decoder = d;

View File

@ -12,6 +12,7 @@
@interface CogDecoderMulti : NSObject <CogDecoder> {
NSArray *theDecoders;
id<CogDecoder> theDecoder;
BOOL observersAdded;
}
- (id)initWithDecoders:(NSArray *)decoders;

View File

@ -113,20 +113,26 @@ static void *kCogDecoderMultiContext = &kCogDecoderMultiContext;
}
- (void)registerObservers {
[theDecoder addObserver:self
forKeyPath:@"properties"
options:(NSKeyValueObservingOptionNew)
context:kCogDecoderMultiContext];
if(!observersAdded) {
[theDecoder addObserver:self
forKeyPath:@"properties"
options:(NSKeyValueObservingOptionNew)
context:kCogDecoderMultiContext];
[theDecoder addObserver:self
forKeyPath:@"metadata"
options:(NSKeyValueObservingOptionNew)
context:kCogDecoderMultiContext];
[theDecoder addObserver:self
forKeyPath:@"metadata"
options:(NSKeyValueObservingOptionNew)
context:kCogDecoderMultiContext];
observersAdded = YES;
}
}
- (void)removeObservers {
[theDecoder removeObserver:self forKeyPath:@"properties" context:kCogDecoderMultiContext];
[theDecoder removeObserver:self forKeyPath:@"metadata" context:kCogDecoderMultiContext];
if(observersAdded) {
observersAdded = NO;
[theDecoder removeObserver:self forKeyPath:@"properties" context:kCogDecoderMultiContext];
[theDecoder removeObserver:self forKeyPath:@"metadata" context:kCogDecoderMultiContext];
}
}
- (BOOL)setTrack:(NSURL *)track {