Skip to content

Commit 6aceb3b

Browse files
committed
Updated presence api
1 parent a1bcd22 commit 6aceb3b

File tree

2 files changed

+36
-34
lines changed

2 files changed

+36
-34
lines changed

ios/Firestack/FirestackDatabase.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,10 @@ @implementation FirestackDatabase
384384
@"handle": @(handle)
385385
}]);
386386
} else {
387-
callback(@{
387+
callback(@[@{
388388
@"result": @"exists",
389389
@"msg": @"Listener already exists"
390-
});
390+
}]);
391391
}
392392
}
393393

lib/modules/presence.js

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,33 @@ class PresenceRef extends ReferenceBase {
66
constructor(presence, ref, pathParts) {
77
super(presence.firestack);
88

9+
this.presence = presence;
910
const db = this.firestack.database;
1011
this.ref = ref;
12+
this.lastOnlineRef = this.ref.child('lastOnline');
13+
1114
this._connectedRef = db.ref('.info/connected');
1215
this._pathParts = pathParts;
1316

1417
this._onConnect = [];
1518
}
1619

17-
setOnline(path) {
18-
let connectedDeviceRef = this.ref;
19-
if (path) {
20-
this._pathParts.push(path);
21-
connectedDeviceRef = this.ref.child(path);
22-
}
23-
24-
this._lastOnlineRef = connectedDeviceRef.child('lastOnline');
25-
26-
this._connectedDeviceRef = connectedDeviceRef;
20+
setOnline() {
21+
this.ref.setAt({online: true})
2722
this._connectedRef.on('value', (snapshot) => {
2823
const val = snapshot.val();
2924
if (val) {
3025
// add self to connection list
3126
// this.ref.push()
32-
connectedDeviceRef.setAt({
27+
this.ref.setAt({
3328
online: true
3429
})
3530
.then(() => {
3631
this._disconnect();
3732

3833
this._onConnect.forEach(fn => {
3934
if (fn && typeof fn === 'function') {
40-
fn.bind(this)(this._connectedDeviceRef);
35+
fn.bind(this)(this.ref);
4136
}
4237
})
4338
})
@@ -47,19 +42,20 @@ class PresenceRef extends ReferenceBase {
4742
}
4843

4944
setOffline() {
50-
if (this._connectedDeviceRef) {
51-
this._connectedDeviceRef.setAt({online: false})
52-
.then(() => this._connectedDeviceRef.off('value'))
45+
if (this.ref) {
46+
this.ref.setAt({online: false})
47+
.then(() => this.ref.off('value'))
48+
this.presence.off(this._pathParts);
5349
}
5450
return this;
5551
}
5652

5753
_disconnect() {
58-
if (this._connectedDeviceRef) {
59-
this._connectedDeviceRef.onDisconnect()
54+
if (this.ref) {
55+
this.ref.onDisconnect()
6056
.setValue({online: false});
6157
// set last online time
62-
this._lastOnlineRef.onDisconnect()
58+
this.lastOnlineRef.onDisconnect()
6359
.setValue(this.firestack.ServerValue.TIMESTAMP)
6460
}
6561
}
@@ -85,26 +81,32 @@ export class Presence extends Base {
8581

8682
on(key) {
8783
invariant(key, 'You must supply a key for presence');
88-
this.path = ['/presence/connections'].concat(key);
89-
return this._presence();
90-
}
84+
const path = this.path.concat(key);
85+
const pathKey = this._presenceKey(path);
86+
if (!this.instances[pathKey]) {
87+
const _ref = this.firestack.database.ref(pathKey);
88+
this.log.debug('Created new presence object for ', pathKey)
89+
const inst = new PresenceRef(this, _ref, path);
90+
91+
this.instances[pathKey] = inst;
92+
}
9193

92-
ref(dbRef, path) {
93-
return new PresenceRef(this, dbRef, path);
94+
return this.instances[pathKey];
9495
}
9596

96-
_presence() {
97-
const key = this._presenceKey();
98-
if (!this.instances[key]) {
99-
const _ref = this.firestack.database.ref(key);
100-
this.log.debug('Created new presence object for ', key)
101-
this.instances[key] = new PresenceRef(this, _ref, this.path);
97+
off(path) {
98+
const pathKey = this._presenceKey(path);
99+
if (this.instances[pathKey]) {
100+
delete this.instances[pathKey];
102101
}
103-
return this.instances[key];
104102
}
105103

106-
_presenceKey() {
107-
return this.path.join('/');
104+
ref(dbRef, path) {
105+
return new PresenceRef(this, dbRef, path);
106+
}
107+
108+
_presenceKey(path) {
109+
return (path || this.path).join('/');
108110
}
109111

110112
get namespace() {

0 commit comments

Comments
 (0)