Skip to content

Commit bfd2686

Browse files
author
Ludwig DUBOS
committed
add connect event and retry connection behaviour
fix #6
1 parent ecf1f7e commit bfd2686

File tree

3 files changed

+70
-9
lines changed

3 files changed

+70
-9
lines changed

.npmignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
node_modules/
2+
lib/
3+
.editorconfig
4+
.eslintrc.json
5+
.gitattributes
6+
.gitignore
7+
.prettierrc
8+
.tsconfig.json
9+
*.lock

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ const vlc = new VLC({
2828
tickLengthMs,
2929
// checks that browse, status and playlist have changed since the last update of one of its elements,
3030
// if it the case fire browsechange, statuschange or playlistchange event. default true.
31-
changeEvents
31+
changeEvents,
32+
// max tries at the first connection before throwing an error set it to -1 for infinite try, default -1
33+
maxTries,
34+
// interval between each try in ms, default 1000
35+
triesInterval
3236
});
3337

3438
// update status and playlist at the same time
@@ -84,9 +88,13 @@ vlc.on(
8488
}
8589
);
8690

87-
vlc.on('error', listener: (err: Error) => {
91+
vlc.on('error', (err: Error) => {
8892
// do stuff
8993
});
94+
95+
vlc.on('connect', () => {
96+
// do stuff
97+
})
9098
```
9199

92100
### Actions

lib/index.ts

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface Audiofilters {
2525
}
2626

2727
export interface Meta {
28+
// eslint-disable-next-line camelcase
2829
encoded_by: string;
2930
filename: string;
3031
}
@@ -106,8 +107,10 @@ type BrowseElement = {
106107
path: string;
107108
name: string;
108109
uid: number;
110+
// eslint-disable-next-line camelcase
109111
creation_time: number;
110112
gid: number;
113+
// eslint-disable-next-line camelcase
111114
modification_time: number;
112115
mode: number;
113116
uri: string;
@@ -139,20 +142,27 @@ type PlaylistNode = PlaylistBase & {
139142

140143
type Playlist = PlaylistNode | PlaylistLeaf;
141144

145+
const waitFor = (ms: number): Promise<void> =>
146+
new Promise(resolve => setTimeout(resolve, ms));
147+
142148
export type VLCOptions = {
143149
host?: string;
144150
port?: number;
145151
username: string;
146152
password: string;
147153
/** update automatically status and playlist of VLC, default true. */
148154
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). */
150156
tickLengthMs?: number;
151157
/**
152158
* checks that browse, status and playlist have changed since the last update of one of its elements,
153159
* if it the case fire browsechange, statuschange or playlistchange event. default true.
154160
*/
155161
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;
156166
};
157167

158168
export declare interface VLC {
@@ -164,6 +174,8 @@ export declare interface VLC {
164174
): this;
165175
on(event: 'playlistchange', listener: (prev: any, next: any) => void): this;
166176
on(event: 'error', listener: (err: Error) => void): this;
177+
/** fired when connected */
178+
on(event: 'connect', listener: () => void): this;
167179
on(event: string | symbol, listener: (...args: any[]) => void): this;
168180
}
169181

@@ -210,6 +222,8 @@ export class VLC extends EventEmitter {
210222
private _target: number;
211223
private _status: Status = null as any;
212224
private _playlist: Playlist = null as any;
225+
private _maxTries: number = -1;
226+
private _triesInterval: number = 1000;
213227

214228
constructor(options: VLCOptions) {
215229
super();
@@ -232,6 +246,9 @@ export class VLC extends EventEmitter {
232246
this._tickLengthMs = 16;
233247
}
234248

249+
this._maxTries = options.maxTries || -1;
250+
this._triesInterval = options.triesInterval || 1000;
251+
235252
this._tickLengthNano = this._tickLengthMs * ms2nano;
236253
this._longWaitMs = Math.floor(this._tickLengthMs - 1);
237254
this._longWaitNano = this._longWaitMs * ms2nano;
@@ -244,14 +261,41 @@ export class VLC extends EventEmitter {
244261
).toString('base64')}`;
245262

246263
// check if VLC is up
247-
this._sendCommand(CommandScope.STATUS)
248-
.catch(this.emit.bind(this, 'error'))
264+
this._connect()
249265
.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;
253281
}
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+
}
255299
}
256300

257301
private _doTick(): void {

0 commit comments

Comments
 (0)