@@ -6,38 +6,33 @@ class PresenceRef extends ReferenceBase {
6
6
constructor ( presence , ref , pathParts ) {
7
7
super ( presence . firestack ) ;
8
8
9
+ this . presence = presence ;
9
10
const db = this . firestack . database ;
10
11
this . ref = ref ;
12
+ this . lastOnlineRef = this . ref . child ( 'lastOnline' ) ;
13
+
11
14
this . _connectedRef = db . ref ( '.info/connected' ) ;
12
15
this . _pathParts = pathParts ;
13
16
14
17
this . _onConnect = [ ] ;
15
18
}
16
19
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 } )
27
22
this . _connectedRef . on ( 'value' , ( snapshot ) => {
28
23
const val = snapshot . val ( ) ;
29
24
if ( val ) {
30
25
// add self to connection list
31
26
// this.ref.push()
32
- connectedDeviceRef . setAt ( {
27
+ this . ref . setAt ( {
33
28
online : true
34
29
} )
35
30
. then ( ( ) => {
36
31
this . _disconnect ( ) ;
37
32
38
33
this . _onConnect . forEach ( fn => {
39
34
if ( fn && typeof fn === 'function' ) {
40
- fn . bind ( this ) ( this . _connectedDeviceRef ) ;
35
+ fn . bind ( this ) ( this . ref ) ;
41
36
}
42
37
} )
43
38
} )
@@ -47,19 +42,20 @@ class PresenceRef extends ReferenceBase {
47
42
}
48
43
49
44
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 ) ;
53
49
}
54
50
return this ;
55
51
}
56
52
57
53
_disconnect ( ) {
58
- if ( this . _connectedDeviceRef ) {
59
- this . _connectedDeviceRef . onDisconnect ( )
54
+ if ( this . ref ) {
55
+ this . ref . onDisconnect ( )
60
56
. setValue ( { online : false } ) ;
61
57
// set last online time
62
- this . _lastOnlineRef . onDisconnect ( )
58
+ this . lastOnlineRef . onDisconnect ( )
63
59
. setValue ( this . firestack . ServerValue . TIMESTAMP )
64
60
}
65
61
}
@@ -85,26 +81,32 @@ export class Presence extends Base {
85
81
86
82
on ( key ) {
87
83
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
+ }
91
93
92
- ref ( dbRef , path ) {
93
- return new PresenceRef ( this , dbRef , path ) ;
94
+ return this . instances [ pathKey ] ;
94
95
}
95
96
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 ] ;
102
101
}
103
- return this . instances [ key ] ;
104
102
}
105
103
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 ( '/' ) ;
108
110
}
109
111
110
112
get namespace ( ) {
0 commit comments