Skip to content

Commit 51dc8b6

Browse files
committed
Initial checkin for presence
1 parent f878bf3 commit 51dc8b6

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

lib/firestack.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const firebase = require('firebase');
88

99
const app = require('firebase/app');
1010
const storage = require('firebase/storage');
11+
const db = require('firebase/database');
1112

1213
import {NativeModules, NativeEventEmitter, AsyncStorage} from 'react-native';
1314
// TODO: Break out modules into component pieces
@@ -23,6 +24,7 @@ import {Authentication} from './modules/authentication'
2324
import {Database} from './modules/database'
2425
import {Analytics} from './modules/analytics'
2526
import {Storage} from './modules/storage'
27+
import {Presence} from './modules/presence'
2628

2729
let log;
2830
export class Firestack extends Singleton {
@@ -66,7 +68,7 @@ export class Firestack extends Singleton {
6668
log.info('Native configureWithOptions success', configuredProperties);
6769
this.configured = true;
6870
this.firestackOptions = configuredProperties;
69-
return args;
71+
return configuredProperties;
7072
}).catch((err) => {
7173
log.error('Native error occurred while calling configure', err);
7274
})
@@ -108,6 +110,12 @@ export class Firestack extends Singleton {
108110
return this._storage;
109111
}
110112

113+
// presence
114+
get presence() {
115+
if (!this._presence) { this._presence = new Presence(this); }
116+
return this._presence;
117+
}
118+
111119
// Storage
112120
//
113121
// /**

lib/modules/database.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ class DatabaseRef extends ReferenceBase {
234234
return this.query.setFilter('startAt', value, key);
235235
}
236236

237+
presence(path) {
238+
const presence = this.firestack.presence;
239+
const ref = path ? this.child(path) : this;
240+
return presence.ref(ref, this.dbPath());
241+
}
242+
237243
// onDisconnect
238244
onDisconnect() {
239245
return new DatabaseOnDisconnect(this);

lib/modules/presence.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import promisify from '../promisify'
2+
import { Base, ReferenceBase } from './base'
3+
4+
class PresenceRef extends ReferenceBase {
5+
constructor(presence, ref) {
6+
super(presence.firestack);
7+
8+
const db = this.firestack.database;
9+
this.ref = ref;
10+
this._connectedRef = db.ref('.info/connected');
11+
12+
this._onConnect = [];
13+
}
14+
15+
setOnlineFor(path) {
16+
const connectedDeviceRef = this.ref.child(path)
17+
this._lastOnlineRef = connectedDeviceRef.child('lastOnline');
18+
19+
this._connectedRef.on('value', (snapshot) => {
20+
const val = snapshot.val();
21+
if (val) {
22+
// add self to connection list
23+
// this.ref.push()
24+
connectedDeviceRef.setAt({
25+
online: true
26+
})
27+
.then(() => {
28+
connectedDeviceRef.onDisconnect()
29+
.setValue({
30+
online: false
31+
});
32+
33+
this._lastOnlineRef.onDisconnect()
34+
.setValue(this.firestack.ServerValue.TIMESTAMP)
35+
36+
this._onConnect.forEach(fn => {
37+
if (fn && typeof fn === 'function') {
38+
fn.bind(this)(connectedDeviceRef);
39+
}
40+
})
41+
})
42+
}
43+
})
44+
}
45+
46+
}
47+
48+
export class Presence extends Base {
49+
constructor(firestack, options={}) {
50+
super(firestack, options);
51+
}
52+
53+
on(path='presence/connections') {
54+
const _ref = this.firestack.database.ref(path);
55+
return new PresenceRef(this, _ref);
56+
}
57+
58+
ref(dbRef, path) {
59+
return new PresenceRef(this, dbRef, path);
60+
}
61+
62+
_pathKey(...path) {
63+
return path.join('-');
64+
}
65+
66+
get namespace() {
67+
return 'firestack:presence'
68+
}
69+
}
70+
71+
export default Presence;

0 commit comments

Comments
 (0)