1
- import invariant from 'invariant' ;
2
1
import { Base , ReferenceBase } from './base' ;
3
2
4
3
class PresenceRef extends ReferenceBase {
5
4
constructor ( presence , ref , pathParts ) {
6
5
super ( presence . firestack ) ;
7
-
8
6
this . ref = ref ;
7
+ this . _onConnect = [ ] ;
9
8
this . presence = presence ;
10
- const db = this . firestack . database ( ) ;
11
- this . lastOnlineRef = this . ref . child ( 'lastOnline' ) ;
12
-
13
- this . _connectedRef = db . ref ( '.info/connected' ) ;
14
9
this . _pathParts = pathParts ;
15
-
16
- this . _onConnect = [ ] ;
10
+ this . lastOnlineRef = this . ref . child ( 'lastOnline' ) ;
11
+ this . _connectedRef = this . firestack . database ( ) . ref ( '.info/connected' ) ;
17
12
}
18
13
19
14
setOnline ( ) {
20
- this . ref . setAt ( { online : true } ) ;
15
+ this . ref . set ( { online : true } ) ;
16
+
17
+ // todo cleanup - creating a ref every time?
21
18
this . _connectedRef . on ( 'value' , ( snapshot ) => {
22
19
const val = snapshot . val ( ) ;
23
20
if ( val ) {
24
21
// add self to connection list
25
22
// this.ref.push()
26
- this . ref . setAt ( {
23
+ this . ref . set ( {
27
24
online : true ,
28
25
} )
29
26
. then ( ( ) => {
30
27
this . _disconnect ( ) ;
31
28
29
+ // todo switch to event emitter
30
+ // todo this will leak
32
31
this . _onConnect . forEach ( ( fn ) => {
33
32
if ( fn && typeof fn === 'function' ) {
34
33
fn . bind ( this ) ( this . ref ) ;
@@ -42,27 +41,26 @@ class PresenceRef extends ReferenceBase {
42
41
43
42
setOffline ( ) {
44
43
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' ) ) ;
47
45
this . presence . off ( this . _pathParts ) ;
48
46
}
49
47
return this ;
50
48
}
51
49
52
50
_disconnect ( ) {
53
51
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 ) ;
59
55
}
60
56
}
61
57
62
58
_pathKey ( ) {
63
59
return this . _pathParts . join ( '/' ) ;
64
60
}
65
61
62
+ // todo switch to event emitter
63
+ // todo this will leak
66
64
onConnect ( cb ) {
67
65
this . _onConnect . push ( cb ) ;
68
66
return this ;
@@ -79,15 +77,13 @@ export default class Presence extends Base {
79
77
}
80
78
81
79
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' ) ;
83
81
const path = this . path . concat ( key ) ;
84
82
const pathKey = this . _presenceKey ( path ) ;
85
83
if ( ! this . instances [ pathKey ] ) {
86
84
const _ref = this . firestack . database ( ) . ref ( pathKey ) ;
87
85
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 ) ;
91
87
}
92
88
93
89
return this . instances [ pathKey ] ;
0 commit comments