Files
firebase-ios-sdk/Firebase/Database/Core/View/FChildEventRegistration.m
T

112 lines
3.9 KiB
Objective-C
Raw Normal View History

2017-05-15 12:27:07 -07:00
/*
* Copyright 2017 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "FChildEventRegistration.h"
2019-08-05 13:27:47 -07:00
#import "FCancelEvent.h"
#import "FDataEvent.h"
#import "FIRDataSnapshot_Private.h"
2017-05-15 12:27:07 -07:00
#import "FIRDatabaseQuery_Private.h"
#import "FQueryParams.h"
#import "FQuerySpec.h"
2019-08-05 13:27:47 -07:00
#import <FirebaseCore/FIRLogger.h>
2017-05-15 12:27:07 -07:00
@interface FChildEventRegistration ()
2019-08-05 13:27:47 -07:00
@property(nonatomic, strong) FRepo *repo;
@property(nonatomic, copy, readwrite) NSDictionary *callbacks;
@property(nonatomic, copy, readwrite) fbt_void_nserror cancelCallback;
@property(nonatomic, readwrite) FIRDatabaseHandle handle;
2017-05-15 12:27:07 -07:00
@end
@implementation FChildEventRegistration
2019-08-05 13:27:47 -07:00
- (id)initWithRepo:(id)repo
handle:(FIRDatabaseHandle)fHandle
callbacks:(NSDictionary *)callbackBlocks
cancelCallback:(fbt_void_nserror)cancelCallbackBlock {
2017-05-15 12:27:07 -07:00
self = [super init];
if (self) {
self.repo = repo;
self.handle = fHandle;
self.callbacks = callbackBlocks;
self.cancelCallback = cancelCallbackBlock;
}
return self;
}
2019-08-05 13:27:47 -07:00
- (BOOL)responseTo:(FIRDataEventType)eventType {
return self.callbacks != nil &&
[self.callbacks
objectForKey:[NSNumber numberWithInteger:eventType]] != nil;
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (FDataEvent *)createEventFrom:(FChange *)change query:(FQuerySpec *)query {
FIRDatabaseReference *ref = [[FIRDatabaseReference alloc]
initWithRepo:self.repo
path:[query.path childFromString:change.childKey]];
FIRDataSnapshot *snapshot =
[[FIRDataSnapshot alloc] initWithRef:ref
indexedNode:change.indexedNode];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
FDataEvent *eventData =
[[FDataEvent alloc] initWithEventType:change.type
eventRegistration:self
dataSnapshot:snapshot
prevName:change.prevKey];
2017-05-15 12:27:07 -07:00
return eventData;
}
2019-08-05 13:27:47 -07:00
- (void)fireEvent:(id<FEvent>)event queue:(dispatch_queue_t)queue {
2017-05-15 12:27:07 -07:00
if ([event isCancelEvent]) {
FCancelEvent *cancelEvent = event;
FFLog(@"I-RDB061001", @"Raising cancel value event on %@", event.path);
2019-08-05 13:27:47 -07:00
NSAssert(
self.cancelCallback != nil,
@"Raising a cancel event on a listener with no cancel callback");
2017-05-15 12:27:07 -07:00
dispatch_async(queue, ^{
2019-08-05 13:27:47 -07:00
self.cancelCallback(cancelEvent.error);
2017-05-15 12:27:07 -07:00
});
} else if (self.callbacks != nil) {
FDataEvent *dataEvent = event;
2019-08-05 13:27:47 -07:00
FFLog(@"I-RDB061002", @"Raising event callback (%ld) on %@",
(long)dataEvent.eventType, dataEvent.path);
fbt_void_datasnapshot_nsstring callback = [self.callbacks
objectForKey:[NSNumber numberWithInteger:dataEvent.eventType]];
2017-05-15 12:27:07 -07:00
if (callback != nil) {
dispatch_async(queue, ^{
2019-08-05 13:27:47 -07:00
callback(dataEvent.snapshot, dataEvent.prevName);
2017-05-15 12:27:07 -07:00
});
}
}
}
2019-08-05 13:27:47 -07:00
- (FCancelEvent *)createCancelEventFromError:(NSError *)error
path:(FPath *)path {
2017-05-15 12:27:07 -07:00
if (self.cancelCallback != nil) {
2019-08-05 13:27:47 -07:00
return [[FCancelEvent alloc] initWithEventRegistration:self
error:error
path:path];
2017-05-15 12:27:07 -07:00
} else {
return nil;
}
}
2019-08-05 13:27:47 -07:00
- (BOOL)matches:(id<FEventRegistration>)other {
return self.handle == NSNotFound || other.handle == NSNotFound ||
self.handle == other.handle;
2017-05-15 12:27:07 -07:00
}
@end