@@ -25,6 +25,7 @@ export interface Audiofilters {
25
25
}
26
26
27
27
export interface Meta {
28
+ // eslint-disable-next-line camelcase
28
29
encoded_by : string ;
29
30
filename : string ;
30
31
}
@@ -106,8 +107,10 @@ type BrowseElement = {
106
107
path : string ;
107
108
name : string ;
108
109
uid : number ;
110
+ // eslint-disable-next-line camelcase
109
111
creation_time : number ;
110
112
gid : number ;
113
+ // eslint-disable-next-line camelcase
111
114
modification_time : number ;
112
115
mode : number ;
113
116
uri : string ;
@@ -139,20 +142,27 @@ type PlaylistNode = PlaylistBase & {
139
142
140
143
type Playlist = PlaylistNode | PlaylistLeaf ;
141
144
145
+ const waitFor = ( ms : number ) : Promise < void > =>
146
+ new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
147
+
142
148
export type VLCOptions = {
143
149
host ?: string ;
144
150
port ?: number ;
145
151
username : string ;
146
152
password : string ;
147
153
/** update automatically status and playlist of VLC, default true. */
148
154
autoUpdate ?: boolean ;
149
- /** how many times per seconds (in ms) node-vlc-http will update the status of VLC, default 1000/30 ~ 33ms (30fps).*/
155
+ /** how many times per seconds (in ms) node-vlc-http will update the status of VLC, default 1000/30 ~ 33ms (30fps). */
150
156
tickLengthMs ?: number ;
151
157
/**
152
158
* checks that browse, status and playlist have changed since the last update of one of its elements,
153
159
* if it the case fire browsechange, statuschange or playlistchange event. default true.
154
160
*/
155
161
changeEvents ?: boolean ;
162
+ /** max tries at the first connection before throwing an error set it to -1 for infinite try, default -1 */
163
+ maxTries ?: number ;
164
+ /** interval between two try in ms, default 1000 */
165
+ triesInterval ?: number ;
156
166
} ;
157
167
158
168
export declare interface VLC {
@@ -164,6 +174,8 @@ export declare interface VLC {
164
174
) : this;
165
175
on ( event : 'playlistchange' , listener : ( prev : any , next : any ) => void ) : this;
166
176
on ( event : 'error' , listener : ( err : Error ) => void ) : this;
177
+ /** fired when connected */
178
+ on ( event : 'connect' , listener : ( ) => void ) : this;
167
179
on ( event : string | symbol , listener : ( ...args : any [ ] ) => void ) : this;
168
180
}
169
181
@@ -210,6 +222,8 @@ export class VLC extends EventEmitter {
210
222
private _target : number ;
211
223
private _status : Status = null as any ;
212
224
private _playlist : Playlist = null as any ;
225
+ private _maxTries : number = - 1 ;
226
+ private _triesInterval : number = 1000 ;
213
227
214
228
constructor ( options : VLCOptions ) {
215
229
super ( ) ;
@@ -232,6 +246,9 @@ export class VLC extends EventEmitter {
232
246
this . _tickLengthMs = 16 ;
233
247
}
234
248
249
+ this . _maxTries = options . maxTries || - 1 ;
250
+ this . _triesInterval = options . triesInterval || 1000 ;
251
+
235
252
this . _tickLengthNano = this . _tickLengthMs * ms2nano ;
236
253
this . _longWaitMs = Math . floor ( this . _tickLengthMs - 1 ) ;
237
254
this . _longWaitNano = this . _longWaitMs * ms2nano ;
@@ -244,14 +261,41 @@ export class VLC extends EventEmitter {
244
261
) . toString ( 'base64' ) } `;
245
262
246
263
// check if VLC is up
247
- this . _sendCommand ( CommandScope . STATUS )
248
- . catch ( this . emit . bind ( this , 'error' ) )
264
+ this . _connect ( )
249
265
. then ( ( ) => {
250
- if ( this . _autoUpdate ) {
251
- // start loop
252
- this . _doTick ( ) ;
266
+ this . emit ( 'connect' ) ;
267
+ if ( this . _autoUpdate ) this . _doTick ( ) ;
268
+ } )
269
+ . catch ( this . emit . bind ( this , 'error' ) ) ;
270
+ }
271
+
272
+ private async _connect ( ) : Promise < void > {
273
+ if ( this . _maxTries === 0 ) await this . _sendCommand ( CommandScope . STATUS ) ;
274
+ else if ( this . _maxTries === - 1 ) {
275
+ while ( true ) {
276
+ try {
277
+ await this . _sendCommand ( CommandScope . STATUS ) ;
278
+ } catch ( _ ) {
279
+ await waitFor ( this . _triesInterval ) ;
280
+ continue ;
253
281
}
254
- } ) ;
282
+
283
+ break ;
284
+ }
285
+ } else {
286
+ for ( let i = 1 ; i < this . _maxTries ; i ++ ) {
287
+ try {
288
+ await this . _sendCommand ( CommandScope . STATUS ) ;
289
+ } catch ( _ ) {
290
+ await waitFor ( this . _triesInterval ) ;
291
+ continue ;
292
+ }
293
+
294
+ return ;
295
+ }
296
+
297
+ await this . _sendCommand ( CommandScope . STATUS ) ;
298
+ }
255
299
}
256
300
257
301
private _doTick ( ) : void {
0 commit comments