UNPKG

@applicaster/zapp-react-native-utils

Version:

Applicaster Zapp React Native utilities package

69 lines (55 loc) 1.61 kB
export type SeekKeyHandlerListener = { onRewindPress: () => boolean; onForwardPress: () => boolean; onRewindPressOut: () => void; onForwardPressOut: () => void; }; export class KeyInputHandler { private static instance: KeyInputHandler; public static getInstance() { if (!KeyInputHandler.instance) { KeyInputHandler.instance = new KeyInputHandler(); } return KeyInputHandler.instance; } constructor() { this.listeners = []; } private listeners: SeekKeyHandlerListener[] = []; addListener = (listener: SeekKeyHandlerListener) => { this.listeners.splice(0, 0, listener); return () => this.removeListener(listener); }; removeListener = (listener: SeekKeyHandlerListener) => { this.listeners = this.listeners.filter((l) => l !== listener); }; onRewindPress = () => { for (const element of this.listeners) { if (element.onRewindPress()) { break; } } }; onForwardPress = () => { for (const element of this.listeners) { if (element.onForwardPress()) { break; } } }; // TODO: Event should be send to listeners who handled down event onRewindPressOut = () => { this.listeners.forEach((listener) => listener.onRewindPressOut()); }; onForwardPressOut = () => { this.listeners.forEach((listener) => listener.onForwardPressOut()); }; getSeekMethods = () => { return { onForwardPress: this.onForwardPress, onRewindPress: this.onRewindPress, onForwardPressOut: this.onForwardPressOut, onRewindPressOut: this.onRewindPressOut, }; }; }