@@ -3,14 +3,14 @@ import { createDiv, createImg, createLabel } from "../utils/domUtil";
3
3
import { getCallOption } from "../utils/util" ;
4
4
import CallButtons from "./CallButtons" ;
5
5
import { classes } from "../css/styles.js" ;
6
+ import PeriodicJob from "../modules/PeriodicJob" ;
6
7
7
8
export default class CallView extends BaseElement {
8
9
constructor ( { call, state, args } ) {
9
10
super ( { id : 'call_view' , className : `${ classes [ 'column' ] } ${ classes [ 'center' ] } ${ classes [ 'view' ] } ${ classes [ 'viewCall' ] } ` , args } ) ;
10
11
11
12
this . call = call ;
12
13
this . state = state ;
13
- this . timer = null ;
14
14
this . connected = false ;
15
15
this . addCallListeners ( call ) ;
16
16
}
@@ -25,7 +25,7 @@ export default class CallView extends BaseElement {
25
25
const peerProfile = createImg ( { src : remoteUser . profileUrl , className : classes [ 'remoteProfile' ] } ) ;
26
26
const peerName = createLabel ( { id : 'peer_name' , innerText : remoteUser . userId , className : `${ classes [ 'peerName' ] } ${ classes [ 'fontBig' ] } ` } ) ;
27
27
28
- const connectionInfo = createLabel ( { id : 'conn_info_label' , className : `${ classes [ 'connectionInfo' ] } ${ classes [ 'fontNormal' ] } ` } ) ;
28
+ this . connectionInfo = createLabel ( { id : 'conn_info_label' , className : `${ classes [ 'connectionInfo' ] } ${ classes [ 'fontNormal' ] } ` } ) ;
29
29
30
30
const peerStateDiv = createDiv ( { id : 'peer_state' , className : `${ classes [ 'column' ] } ${ classes [ 'peerStateDiv' ] } ${ classes [ 'invisible' ] } ` } ) ;
31
31
const peerMuteIcon = createDiv ( { id : 'peer_mute_icon' , className : classes [ 'peerMuteIcon' ] } ) ;
@@ -37,41 +37,37 @@ export default class CallView extends BaseElement {
37
37
38
38
this . element . appendChild ( peerProfile ) ;
39
39
this . element . appendChild ( peerName ) ;
40
- this . element . appendChild ( connectionInfo ) ;
40
+ this . element . appendChild ( this . connectionInfo ) ;
41
41
this . element . appendChild ( peerStateDiv ) ;
42
42
buttons . appendToBaseElement ( this ) ;
43
43
44
- let tick = 0 ;
45
- this . timer = setInterval ( ( ) => {
46
- const callingStr = 'Calling' ;
47
- if ( this . call && this . call . endResult ) {
48
- connectionInfo . innerText = this . call . endResult ;
49
- clearInterval ( this . timer ) ;
50
- } else if ( ! this . connected && this . call . caller . userId === localUser . userId ) {
51
- connectionInfo . innerText = callingStr + '.' . repeat ( ( tick % 3 ) + 1 ) ;
52
- } else if ( this . connected ) {
53
- const durationInSec = Math . floor ( this . call . getDuration ( ) / 1000 ) ;
54
- const minutes = `0${ Math . floor ( durationInSec / 60 ) } ` . slice ( - 2 ) ;
55
- const seconds = `0${ durationInSec % 60 } ` . slice ( - 2 ) ;
56
- connectionInfo . innerText = `${ minutes } :${ seconds } ` ;
57
- }
58
-
59
- tick += 1 ;
60
- } , 1000 ) ;
44
+ if ( this . call . caller . userId === localUser . userId ) {
45
+ this . drawCallingText ( ) ;
46
+ }
61
47
62
48
this . sendToChildren ( this . state ) ;
63
49
}
64
50
65
51
addCallListeners ( call ) {
66
52
call . onConnected = ( ) => {
67
53
this . connected = true ;
54
+ this . drawCurrentTime ( ) ;
68
55
this . sendToChildren ( 'connected' ) ;
69
56
} ;
70
57
71
58
call . onEnded = ( ) => {
59
+ this . drawEndResult ( ) ;
72
60
this . sendToChildren ( 'ended' ) ;
73
61
} ;
74
62
63
+ call . onReconnected = ( ) => {
64
+ this . drawCurrentTime ( ) ;
65
+ } ;
66
+
67
+ call . onReconnecting = ( ) => {
68
+ this . drawReconnectingText ( ) ;
69
+ } ;
70
+
75
71
call . onRemoteAudioSettingsChanged = ( call ) => {
76
72
this . onRemoteMuted ( call . isRemoteAudioEnabled ) ;
77
73
} ;
@@ -108,7 +104,7 @@ export default class CallView extends BaseElement {
108
104
accept ( ) {
109
105
const callOption = getCallOption ( ) ;
110
106
111
- this . call . accept ( callOption ) . catch ( ( e ) => {
107
+ this . call . accept ( { callOption : callOption } ) . catch ( ( e ) => {
112
108
alert ( e ) ;
113
109
} ) ;
114
110
}
@@ -127,8 +123,51 @@ export default class CallView extends BaseElement {
127
123
128
124
close ( ) {
129
125
this . end ( ) ;
130
- clearInterval ( this . timer ) ;
126
+ this . removeConnectionInfoDrawer ( ) ;
131
127
132
128
this . sendToParent ( 'close' , this . call ) ;
133
129
}
130
+
131
+ drawCallingText ( ) {
132
+ this . removeConnectionInfoDrawer ( ) ;
133
+ const rotatingTexts = [ 'Calling' , 'Calling.' , 'Calling..' ] ;
134
+ let frame = 0 ;
135
+ this . connectionInfoDrawer = new PeriodicJob ( ( ) => {
136
+ this . connectionInfo . innerText = rotatingTexts [ frame ] ;
137
+ frame = ( frame + 1 ) % rotatingTexts . length ;
138
+ } ) . start ( ) ;
139
+ }
140
+
141
+ drawReconnectingText ( ) {
142
+ this . removeConnectionInfoDrawer ( ) ;
143
+ const rotatingTexts = [ 'Reconnecting' , 'Reconnecting.' , 'Reconnecting..' ] ;
144
+ let frame = 0 ;
145
+ this . connectionInfoDrawer = new PeriodicJob ( ( ) => {
146
+ this . connectionInfo . innerText = rotatingTexts [ frame ] ;
147
+ frame = ( frame + 1 ) % rotatingTexts . length ;
148
+ } ) . start ( ) ;
149
+ }
150
+
151
+ drawCurrentTime ( ) {
152
+ this . removeConnectionInfoDrawer ( ) ;
153
+ this . connectionInfoDrawer = new PeriodicJob ( ( ) => {
154
+ const durationInSec = Math . floor ( this . call . getDuration ( ) / 1000 ) ;
155
+ const minutes = `0${ Math . floor ( durationInSec / 60 ) } ` . slice ( - 2 ) ;
156
+ const seconds = `0${ durationInSec % 60 } ` . slice ( - 2 ) ;
157
+ this . connectionInfo . innerText = `${ minutes } :${ seconds } ` ;
158
+ } ) . start ( ) ;
159
+ }
160
+
161
+ drawEndResult ( ) {
162
+ this . removeConnectionInfoDrawer ( ) ;
163
+ this . connectionInfo . innerText = this . call . endResult ;
164
+ }
165
+
166
+ removeConnectionInfoDrawer ( ) {
167
+ if ( this . connectionInfoDrawer ) {
168
+ this . connectionInfoDrawer . stop ( ) ;
169
+ this . connectionInfoDrawer = null ;
170
+ }
171
+ this . connectionInfo . innerText = '' ;
172
+ }
134
173
}
0 commit comments