1- import invariant from 'invariant' ;
21import { Base , ReferenceBase } from './base' ;
32
43class PresenceRef extends ReferenceBase {
54 constructor ( presence , ref , pathParts ) {
65 super ( presence . firestack ) ;
7-
86 this . ref = ref ;
7+ this . _onConnect = [ ] ;
98 this . presence = presence ;
10- const db = this . firestack . database ( ) ;
11- this . lastOnlineRef = this . ref . child ( 'lastOnline' ) ;
12-
13- this . _connectedRef = db . ref ( '.info/connected' ) ;
149 this . _pathParts = pathParts ;
15-
16- this . _onConnect = [ ] ;
10+ this . lastOnlineRef = this . ref . child ( 'lastOnline' ) ;
11+ this . _connectedRef = this . firestack . database ( ) . ref ( '.info/connected' ) ;
1712 }
1813
1914 setOnline ( ) {
20- this . ref . setAt ( { online : true } ) ;
15+ this . ref . set ( { online : true } ) ;
16+
17+ // todo cleanup - creating a ref every time?
2118 this . _connectedRef . on ( 'value' , ( snapshot ) => {
2219 const val = snapshot . val ( ) ;
2320 if ( val ) {
2421 // add self to connection list
2522 // this.ref.push()
26- this . ref . setAt ( {
23+ this . ref . set ( {
2724 online : true ,
2825 } )
2926 . then ( ( ) => {
3027 this . _disconnect ( ) ;
3128
29+ // todo switch to event emitter
30+ // todo this will leak
3231 this . _onConnect . forEach ( ( fn ) => {
3332 if ( fn && typeof fn === 'function' ) {
3433 fn . bind ( this ) ( this . ref ) ;
@@ -42,27 +41,26 @@ class PresenceRef extends ReferenceBase {
4241
4342 setOffline ( ) {
4443 if ( this . ref ) {
45- this . ref . setAt ( { online : false } )
46- . then ( ( ) => this . ref . off ( 'value' ) ) ;
44+ this . ref . set ( { online : false } ) . then ( ( ) => this . ref . off ( 'value' ) ) ;
4745 this . presence . off ( this . _pathParts ) ;
4846 }
4947 return this ;
5048 }
5149
5250 _disconnect ( ) {
5351 if ( this . ref ) {
54- this . ref . onDisconnect ( )
55- . setValue ( { online : false } ) ;
56- // set last online time
57- this . lastOnlineRef . onDisconnect ( )
58- . setValue ( this . firestack . ServerValue . TIMESTAMP ) ;
52+ this . ref . onDisconnect ( ) . setValue ( { online : false } ) ;
53+ // todo ServerValue is a promise? so this should be broken..?
54+ this . lastOnlineRef . onDisconnect ( ) . setValue ( this . firestack . ServerValue . TIMESTAMP ) ;
5955 }
6056 }
6157
6258 _pathKey ( ) {
6359 return this . _pathParts . join ( '/' ) ;
6460 }
6561
62+ // todo switch to event emitter
63+ // todo this will leak
6664 onConnect ( cb ) {
6765 this . _onConnect . push ( cb ) ;
6866 return this ;
@@ -79,15 +77,13 @@ export default class Presence extends Base {
7977 }
8078
8179 on ( key ) {
82- invariant ( key , 'You must supply a key for presence' ) ;
80+ if ( ! key || ! key . length ) throw new Error ( 'You must supply a key for presence' ) ;
8381 const path = this . path . concat ( key ) ;
8482 const pathKey = this . _presenceKey ( path ) ;
8583 if ( ! this . instances [ pathKey ] ) {
8684 const _ref = this . firestack . database ( ) . ref ( pathKey ) ;
8785 this . log . debug ( 'Created new presence object for ' , pathKey ) ;
88- const inst = new PresenceRef ( this , _ref , path ) ;
89-
90- this . instances [ pathKey ] = inst ;
86+ this . instances [ pathKey ] = new PresenceRef ( this , _ref , path ) ;
9187 }
9288
9389 return this . instances [ pathKey ] ;
0 commit comments