nitro-react/src/api/utils/PlaySound.ts

26 lines
759 B
TypeScript
Raw Normal View History

2022-03-15 22:27:44 +01:00
import { MouseEventType } from '@nitrots/nitro-renderer';
2021-11-26 04:32:51 +01:00
import { NitroSoundEvent } from '@nitrots/nitro-renderer/src/nitro/events/NitroSoundEvent';
2022-03-03 10:11:31 +01:00
import { DispatchMainEvent } from '../../hooks';
2021-11-26 04:32:51 +01:00
let canPlaySound = false;
export const PlaySound = (sampleCode: string) =>
2021-11-26 04:32:51 +01:00
{
if(!canPlaySound) return;
2022-03-03 10:11:31 +01:00
DispatchMainEvent(new NitroSoundEvent(NitroSoundEvent.PLAY_SOUND, sampleCode));
2021-11-26 04:32:51 +01:00
}
2022-03-15 22:27:44 +01:00
const eventTypes = [ MouseEventType.MOUSE_CLICK ];
const startListening = () =>
{
const stopListening = () => eventTypes.forEach(type => window.removeEventListener(type, onEvent));
const onEvent = (event: Event) => ((canPlaySound = true) && stopListening());
eventTypes.forEach(type => window.addEventListener(type, onEvent));
}
startListening();