Skip to content

Commit ee00012

Browse files
committed
Support for ref.once('child_added')
1 parent 9ec08b8 commit ee00012

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

ios/Firestack/FirestackDatabase.m

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ - (id) initWithPathAndModifiers:(RCTEventEmitter *) emitter
4242
return self;
4343
}
4444

45+
- (NSString *) absPath:(FIRDatabaseReference *) ref {
46+
NSString *url = ref.URL;
47+
NSString *rooturl = ref.root.URL;
48+
return [[url substringFromIndex:rooturl.length] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
49+
}
50+
4551
- (void) addEventHandler:(NSString *) eventName
4652
{
4753
if (![self isListeningTo:eventName]) {
@@ -51,7 +57,7 @@ - (void) addEventHandler:(NSString *) eventName
5157
title:eventName
5258
props: @{
5359
@"eventName": eventName,
54-
@"path": _path,
60+
@"path": [self absPath:[snapshot ref]],
5561
@"modifiersString": _modifiersString,
5662
@"snapshot": props
5763
}];
@@ -71,13 +77,15 @@ - (void) addEventHandler:(NSString *) eventName
7177
}
7278

7379
- (void) addSingleEventHandler:(RCTResponseSenderBlock) callback
80+
ofType:(NSString *) type
7481
{
75-
[_query observeSingleEventOfType:FIRDataEventTypeValue
82+
int eventType = [self eventTypeFromName:type];
83+
[_query observeSingleEventOfType:eventType
7684
withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
77-
NSDictionary *props = [self snapshotToDict:snapshot];
85+
NSDictionary *props = [FirestackDBReference snapshotToDict:snapshot];
7886
callback(@[[NSNull null], @{
79-
@"eventName": @"value",
80-
@"path": _path,
87+
@"eventName": type,
88+
@"path": [self absPath:[snapshot ref]],
8189
@"modifiersString": _modifiersString,
8290
@"snapshot": props
8391
}]);
@@ -478,7 +486,7 @@ - (id) init
478486
callback:(RCTResponseSenderBlock) callback)
479487
{
480488
FirestackDBReference *ref = [self getDBHandle:path modifiers:modifiers modifiersString:modifiersString];
481-
[ref addSingleEventHandler:callback];
489+
[ref addSingleEventHandler:callback ofType:name];
482490
}
483491

484492
RCT_EXPORT_METHOD(off:(NSString *)path

lib/modules/database/reference.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ export default class Reference extends ReferenceBase {
109109
const modifiers = this.query.getModifiers();
110110
const modifiersString = this.query.getModifiersString();
111111
return promisify('onOnce', FirestackDatabase)(path, modifiersString, modifiers, eventName)
112-
.then(({ snapshot }) => new Snapshot(this, snapshot))
112+
.then(({ snapshot, path, modifiersString}) => new Snapshot(
113+
new Reference(this.db, path.split('/'), modifiersString.split('|')),
114+
snapshot))
113115
.then((snapshot) => {
114116
if (isFunction(cb)) cb(snapshot);
115117
return snapshot;

lib/utils/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function deepGet(object, path, joiner = '/') {
2828
const len = keys.length;
2929

3030
while (i < len) {
31-
const key = keys[i += 1];
31+
const key = keys[i++];
3232
if (!tmp || !hasOwnProperty.call(tmp, key)) return null;
3333
tmp = tmp[key];
3434
}

0 commit comments

Comments
 (0)