Skip to content

Commit

Permalink
Merge 5bb2a1c into 60070a0
Browse files Browse the repository at this point in the history
  • Loading branch information
theimo1221 committed Jan 1, 2021
2 parents 60070a0 + 5bb2a1c commit 4311f97
Show file tree
Hide file tree
Showing 4 changed files with 906 additions and 63 deletions.
70 changes: 65 additions & 5 deletions src/models/notificationQueue.ts
@@ -1,4 +1,3 @@
import { EventEmitter } from 'events';
import { PlayNotificationOptions } from './requests';

export interface NotificationQueueItem {
Expand All @@ -10,17 +9,78 @@ export interface NotificationQueueItem {
options: PlayNotificationOptions;

/**
* The name of the event fired when finished
* The Resolve Promise we have to resolve once finished successfully
*
* @type {Promise<boolean>}
* @type {(reject: boolean | PromiseLike<boolean>) => void}
*/
eventName: string
resolve: (resolve: boolean | PromiseLike<boolean>) => void;

/**
* The Reject Promise we have to resolve once finished with failure
*
* @type {(reject: boolean | PromiseLike<boolean>) => void}
*/
reject: (reject: boolean | PromiseLike<boolean>) => void;

/**
* Whether we should only resolve the promise when we reverted correctly
*
* @type {boolean}
*/
resolveAfterRevert: boolean;

/**
* Object with details regarding the timeout for this queue item
*
* @type {NotificationQueueTimeoutItem}
*/
generalTimeout?: NotificationQueueTimeoutItem;
}

export class NotificationQueueTimeoutItem {
public constructor(
/**
* The timeout reference to clear if anything is okay
*
* @type {NotificationQueueTimeoutItem}
*/
public timeout: NodeJS.Timeout,

/**
* The timestamp when the timeout will fire
*
* @type {NotificationQueueTimeoutItem}
*/
public fireTime: number,
) { }

public timeLeft(): number {
return this.fireTime - (new Date()).getTime();
}
}

export class NotificationQueue {
public queue: NotificationQueueItem[] = [];

public events = new EventEmitter();
public promisesToResolve: Array<{
promise: (resolve: boolean | PromiseLike<boolean>) => void,
value: boolean,
timeout?: NotificationQueueTimeoutItem,
}> = [];

public playing = false;

/**
* Whether any item in the Queue changed the volume
*
* @type {boolean}
*/
public volumeChanged = false;

/**
* Whether the Queue played any item at all
*
* @type {boolean}
*/
public anythingPlayed = false;
}
2 changes: 2 additions & 0 deletions src/models/requests.ts
Expand Up @@ -17,6 +17,8 @@ export interface PlayNotificationOptionsBase {
*/
onlyWhenPlaying?: boolean;

resolveAfterRevert?: boolean;

/**
* If listening for events doesn't work you can set a timeout after which playback is reverted to the state before the notification.
*
Expand Down

0 comments on commit 4311f97

Please sign in to comment.