forked from invertase/react-native-firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRNFBDatabaseQueryModule.m
More file actions
255 lines (219 loc) · 10.1 KB
/
Copy pathRNFBDatabaseQueryModule.m
File metadata and controls
255 lines (219 loc) · 10.1 KB
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
/**
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library 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 <React/RCTBridgeModule.h>
#import <React/RCTUtils.h>
#import "RNFBDatabaseCommon.h"
#import "RNFBDatabaseQuery.h"
#import "RNFBDatabaseQueryModule.h"
#import "RNFBRCTEventEmitter.h"
static __strong NSMutableDictionary *queryDictionary;
static NSString *const RNFB_DATABASE_SYNC = @"database_sync_event";
@implementation RNFBDatabaseQueryModule
#pragma mark -
#pragma mark Module Setup
RCT_EXPORT_MODULE();
- (dispatch_queue_t)methodQueue {
return [RNFBDatabaseCommon getDispatchQueue];
}
- (id)init {
self = [super init];
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
queryDictionary = [[NSMutableDictionary alloc] init];
});
return self;
}
- (void)dealloc {
[self invalidate];
}
- (void)invalidate {
NSArray *queryKeys = [queryDictionary allKeys];
for (NSString *key in queryKeys) {
RNFBDatabaseQuery *query = queryDictionary[key];
[query removeAllEventListeners];
[queryDictionary removeObjectForKey:key];
}
}
- (RNFBDatabaseQuery *)getDatabaseQueryInstance:(FIRDatabaseReference *)reference
modifiers:(NSArray *)modifiers {
return [[RNFBDatabaseQuery alloc] initWithReferenceAndModifiers:reference modifiers:modifiers];
}
- (RNFBDatabaseQuery *)getDatabaseQueryInstance:(NSString *)key
reference:(FIRDatabaseReference *)reference
modifiers:(NSArray *)modifiers {
RNFBDatabaseQuery *cachedQuery = queryDictionary[key];
if (cachedQuery != nil) {
return cachedQuery;
}
RNFBDatabaseQuery *query = [[RNFBDatabaseQuery alloc] initWithReferenceAndModifiers:reference
modifiers:modifiers];
queryDictionary[key] = query;
return query;
}
- (void)addOnceEventListener:(RNFBDatabaseQuery *)databaseQuery
eventType:(NSString *)eventType
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject {
FIRDataEventType firDataEventType =
(FIRDataEventType)[RNFBDatabaseCommon getEventTypeFromName:eventType];
// On success
id andPreviousSiblingKeyWithBlock =
^(FIRDataSnapshot *_Nonnull dataSnapshot, NSString *_Nullable previousChildName) {
NSDictionary *data;
if ([eventType isEqualToString:@"value"]) {
data = [RNFBDatabaseCommon snapshotToDictionary:dataSnapshot];
} else {
data = [RNFBDatabaseCommon snapshotWithPreviousChildToDictionary:dataSnapshot
previousChildName:previousChildName];
}
resolve(data);
};
// On error
id errorBlock = ^(NSError *_Nonnull error) {
[RNFBDatabaseCommon promiseRejectDatabaseException:reject error:error];
};
[databaseQuery.query observeSingleEventOfType:firDataEventType
andPreviousSiblingKeyWithBlock:andPreviousSiblingKeyWithBlock
withCancelBlock:errorBlock];
}
- (void)addEventListener:(RNFBDatabaseQuery *)databaseQuery
eventType:(NSString *)eventType
registration:(NSDictionary *)registration {
NSString *eventRegistrationKey = registration[@"eventRegistrationKey"];
if (![databaseQuery hasEventListener:eventRegistrationKey]) {
id andPreviousSiblingKeyWithBlock =
^(FIRDataSnapshot *_Nonnull dataSnapshot, NSString *_Nullable previousChildName) {
[self handleDatabaseEvent:eventRegistrationKey
eventType:eventType
registration:registration
snapshot:dataSnapshot
previousChildName:previousChildName];
};
id errorBlock = ^(NSError *_Nonnull error) {
[databaseQuery removeEventListener:eventRegistrationKey];
[self handleDatabaseEventError:eventRegistrationKey registration:registration error:error];
};
FIRDataEventType firDataEventType =
(FIRDataEventType)[RNFBDatabaseCommon getEventTypeFromName:eventType];
FIRDatabaseHandle handle = [databaseQuery.query observeEventType:firDataEventType
andPreviousSiblingKeyWithBlock:andPreviousSiblingKeyWithBlock
withCancelBlock:errorBlock];
[databaseQuery addEventListener:eventRegistrationKey handle:handle];
}
}
- (void)handleDatabaseEvent:(NSString *)key
eventType:(NSString *)eventType
registration:(NSDictionary *)registration
snapshot:(FIRDataSnapshot *)dataSnapshot
previousChildName:(NSString *)previousChildName {
NSDictionary *data;
if ([eventType isEqualToString:@"value"]) {
data = [RNFBDatabaseCommon snapshotToDictionary:dataSnapshot];
} else {
data = [RNFBDatabaseCommon snapshotWithPreviousChildToDictionary:dataSnapshot
previousChildName:previousChildName];
}
[[RNFBRCTEventEmitter shared] sendEventWithName:RNFB_DATABASE_SYNC
body:@{
@"body" : @{
@"data" : data,
@"key" : key,
@"eventType" : eventType,
@"registration" : registration,
}
}];
}
- (void)handleDatabaseEventError:(NSString *)key
registration:(NSDictionary *)registration
error:(NSError *)error {
NSArray *codeAndMessage = [RNFBDatabaseCommon getCodeAndMessage:error];
[[RNFBRCTEventEmitter shared] sendEventWithName:RNFB_DATABASE_SYNC
body:@{
@"body" : @{
@"key" : key,
@"error" : @{
@"code" : codeAndMessage[0],
@"message" : codeAndMessage[1],
},
@"registration" : registration,
}
}];
}
#pragma mark -
#pragma mark Firebase Database
RCT_EXPORT_METHOD(once
: (FIRApp *)firebaseApp
: (NSString *)dbURL
: (NSString *)path
: (NSArray *)modifiers
: (NSString *)eventType
: (RCTPromiseResolveBlock)resolve
: (RCTPromiseRejectBlock)reject) {
FIRDatabase *firDatabase = [RNFBDatabaseCommon getDatabaseForApp:firebaseApp dbURL:dbURL];
FIRDatabaseReference *firDatabaseReference =
[RNFBDatabaseCommon getReferenceForDatabase:firDatabase path:path];
RNFBDatabaseQuery *databaseQuery = [self getDatabaseQueryInstance:firDatabaseReference
modifiers:modifiers];
[self addOnceEventListener:databaseQuery eventType:eventType resolve:resolve reject:reject];
}
RCT_EXPORT_METHOD(on : (FIRApp *)firebaseApp : (NSString *)dbURL : (NSDictionary *)props) {
NSString *key = [props valueForKey:@"key"];
NSString *path = [props valueForKey:@"path"];
NSString *eventType = [props valueForKey:@"eventType"];
NSArray *modifiers = [props valueForKey:@"modifiers"];
NSDictionary *registration = [props valueForKey:@"registration"];
FIRDatabase *firDatabase = [RNFBDatabaseCommon getDatabaseForApp:firebaseApp dbURL:dbURL];
FIRDatabaseReference *firDatabaseReference =
[RNFBDatabaseCommon getReferenceForDatabase:key firebaseDatabase:firDatabase path:path];
RNFBDatabaseQuery *databaseQuery = [self getDatabaseQueryInstance:key
reference:firDatabaseReference
modifiers:modifiers];
[self addEventListener:databaseQuery eventType:eventType registration:registration];
}
RCT_EXPORT_METHOD(off
: (NSString *)queryKey eventRegistrationKey
: (NSString *)eventRegistrationKey) {
RNFBDatabaseQuery *databaseQuery = queryDictionary[queryKey];
if (databaseQuery != nil) {
[databaseQuery removeEventListener:eventRegistrationKey];
if (![databaseQuery hasListeners]) {
[queryDictionary removeObjectForKey:queryKey];
[RNFBDatabaseCommon removeReferenceByKey:queryKey];
}
}
}
RCT_EXPORT_METHOD(keepSynced
: (FIRApp *)firebaseApp
: (NSString *)dbURL
: (NSString *)key
: (NSString *)path
: (NSArray *)modifiers
: (BOOL)value
: (RCTPromiseResolveBlock)resolve
: (RCTPromiseRejectBlock)reject) {
FIRDatabase *firDatabase = [RNFBDatabaseCommon getDatabaseForApp:firebaseApp dbURL:dbURL];
FIRDatabaseReference *firDatabaseReference =
[RNFBDatabaseCommon getReferenceForDatabase:firDatabase path:path];
RNFBDatabaseQuery *databaseQuery = [self getDatabaseQueryInstance:firDatabaseReference
modifiers:modifiers];
[databaseQuery.query keepSynced:value];
resolve([NSNull null]);
}
+ (BOOL)requiresMainQueueSetup {
return YES;
}
@end