Skip to content

Commit

Permalink
allow to stop random randomActivateAudio
Browse files Browse the repository at this point in the history
  • Loading branch information
vpalmisano committed Mar 29, 2024
1 parent b428081 commit 4ecbccf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import {
import {
checkChromiumExecutable,
logger,
randomActivateAudio,
registerExitHandler,
resolvePackagePath,
sleep,
startRandomActivateAudio,
stopRandomActivateAudio,
stopUpdateSystemStats,
} from './utils'
import { calculateVmafScore } from './vmaf'
Expand Down Expand Up @@ -99,7 +100,7 @@ export async function setupApplication(
// Start the local sessions.
if ((config.url || config.customUrlHandler) && config.sessions) {
if (config.randomAudioPeriod) {
await randomActivateAudio(
startRandomActivateAudio(
stats.sessions,
config.randomAudioPeriod,
config.randomAudioProbability,
Expand Down Expand Up @@ -131,6 +132,8 @@ export async function setupApplication(
return async (): Promise<void> => {
log.debug('Stopping')

stopRandomActivateAudio()

if (server) {
server.stop()
}
Expand Down
28 changes: 25 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,28 @@ declare global {
}

let randomActivateAudioTimeoutId: NodeJS.Timeout | null = null
let randomActivateAudioRunning = false

export function startRandomActivateAudio(
sessions: Map<number, Session>,
randomAudioPeriod: number,
randomAudioProbability: number,
randomAudioRange: number,
): void {
if (randomActivateAudioRunning) return
randomActivateAudioRunning = true
void randomActivateAudio(
sessions,
randomAudioPeriod,
randomAudioProbability,
randomAudioRange,
)
}

export function stopRandomActivateAudio(): void {
randomActivateAudioRunning = false
randomActivateAudioTimeoutId && clearTimeout(randomActivateAudioTimeoutId)
}

/**
* Randomly activate audio from one tab at time.
Expand All @@ -237,7 +259,7 @@ export async function randomActivateAudio(
randomAudioProbability: number,
randomAudioRange: number,
): Promise<void> {
if (!randomAudioPeriod) {
if (!randomAudioPeriod || !randomActivateAudioRunning) {
return
}
try {
Expand Down Expand Up @@ -620,8 +642,8 @@ export async function resolveIP(
return ip
}
})
.catch(err => {
log.error(`resolveIP error: ${(err as Error).stack}`)
.catch(_err => {
// log.error(`resolveIP error: ${(err as Error).stack}`)
ipCache.set(ip, { host: ip, timestamp })
}),
])
Expand Down

0 comments on commit 4ecbccf

Please sign in to comment.