Skip to content

Commit 46d99ae

Browse files
committed
Support for ref.once('child_added')
1 parent e7f268c commit 46d99ae

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
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
}]);
@@ -489,7 +497,7 @@ - (id) init
489497
callback:(RCTResponseSenderBlock) callback)
490498
{
491499
FirestackDBReference *ref = [self getDBHandle:path modifiers:modifiers modifiersString:modifiersString];
492-
[ref addSingleEventHandler:callback];
500+
[ref addSingleEventHandler:callback ofType:name];
493501
}
494502

495503
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
@@ -120,7 +120,9 @@ export default class Reference extends ReferenceBase {
120120
const modifiers = this.query.getModifiers();
121121
const modifiersString = this.query.getModifiersString();
122122
return promisify('onOnce', FirestackDatabase)(path, modifiersString, modifiers, eventName)
123-
.then(({ snapshot }) => new Snapshot(this, snapshot))
123+
.then(({ snapshot, path, modifiersString}) => new Snapshot(
124+
new Reference(this.db, path.split('/'), modifiersString.split('|')),
125+
snapshot))
124126
.then((snapshot) => {
125127
if (isFunction(cb)) cb(snapshot);
126128
return snapshot;

0 commit comments

Comments
 (0)