Skip to content

Commit

Permalink
- added retry to file listener after error
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyusung4698 committed Nov 7, 2020
1 parent 5532056 commit 0c3ce8b
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/app/core/odk/ow-file-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,47 @@ export interface OWFileListenerDelegate {
onError(error?: string): void;
}

const RESTART_DELAY = 1000 * 10;

interface OWFileListenerResult extends overwolf.Result {
content: string;
info: string;
}

export class OWFileListener {
private restartHandle: any;
private path: string;
private skipToEnd: boolean;

constructor(
private readonly id: string,
private readonly delegate: OWFileListenerDelegate) { }

public start(path: string, skipToEnd = true): void {
overwolf.io.listenOnFile(this.id, path, { skipToEnd }, this.onListenOnFile);
this.path = path;
this.skipToEnd = skipToEnd;
this.listen();
}

public stop(): void {
overwolf.io.stopFileListener(this.id);
}

private restart(): void {
this.stop();
clearTimeout(this.restartHandle);
this.restartHandle = setTimeout(() => this.listen(), RESTART_DELAY);
}

private listen(): void {
overwolf.io.listenOnFile(this.id, this.path, { skipToEnd: this.skipToEnd }, this.onListenOnFile);
}

private onListenOnFile = (event: OWFileListenerResult): void => {
if (!event.success || event.error) {
return this.delegate.onError(event.error);
this.delegate.onError(event.error)
this.restart();
return;
}

if (!event.info?.length) {
Expand All @@ -38,7 +58,9 @@ export class OWFileListener {
try {
info = JSON.parse(event.info);
} catch (error) {
return this.delegate.onError(error);
this.delegate.onError(error)
this.restart();
return;
}

if (info.isNew) {
Expand Down

0 comments on commit 0c3ce8b

Please sign in to comment.