From 6a5b918b22318c0f01b9c1c980423b3cb6355353 Mon Sep 17 00:00:00 2001 From: jparez Date: Fri, 14 Jun 2024 11:32:43 +0200 Subject: [PATCH] refacto keyboard start / stop listening --- src/DeviceRenderer.js | 6 ------ src/plugins/FingerPrint.js | 2 +- src/plugins/KeyboardEvents.js | 22 ++++++++-------------- src/store/index.js | 27 +++++++++++++++------------ tests/unit/fingerprint.test.js | 2 ++ 5 files changed, 26 insertions(+), 33 deletions(-) diff --git a/src/DeviceRenderer.js b/src/DeviceRenderer.js index 7fb617e..423f453 100644 --- a/src/DeviceRenderer.js +++ b/src/DeviceRenderer.js @@ -52,8 +52,6 @@ module.exports = class DeviceRenderer { this.initialized = false; this.reconnecting = false; - // Enabled features - this.keyboardEventsEnabled = false; this.touchEventsEnabled = false; this.mouseEventsEnabled = false; this.gamepadEventsEnabled = false; @@ -486,10 +484,6 @@ module.exports = class DeviceRenderer { this.mouseEvents.addMouseCallbacks(); } - if (this.keyboardEventsEnabled) { - this.keyboardEvents.addKeyboardCallbacks(); - } - if (this.gamepadEventsEnabled) { this.gamepadManager.addGamepadCallbacks(); } diff --git a/src/plugins/FingerPrint.js b/src/plugins/FingerPrint.js index cab68af..6fa0441 100644 --- a/src/plugins/FingerPrint.js +++ b/src/plugins/FingerPrint.js @@ -127,7 +127,7 @@ module.exports = class FingerPrint extends OverlayPlugin { // register callback this.instance.registerEventCallback('fingerprint', (message) => this.handleFingerprintEvent(message)); - if (this.instance.store.getState().isWebRTCConnectionReady) { + if (this.instance.store.state.isWebRTCConnectionReady) { this.sendDataToInstance(FINGERPRINT_MESSAGES.toSend.NOTIFY_ALL); } else { const unSubscribe = this.instance.store.subscribe(({isWebRTCConnectionReady}) => { diff --git a/src/plugins/KeyboardEvents.js b/src/plugins/KeyboardEvents.js index fd15c17..68f3465 100644 --- a/src/plugins/KeyboardEvents.js +++ b/src/plugins/KeyboardEvents.js @@ -54,15 +54,20 @@ module.exports = class KeyboardEvents { // Register plugin this.instance.keyboardEvents = this; - this.instance.keyboardEventsEnabled = true; - this.transmitKeys = this.instance.store.getState().isKeyboardEventsEnabled; this.isListenerAdded = false; this.currentlyPressedKeys = new Map(); this.instance.store.subscribe(({isKeyboardEventsEnabled}) => { - this.transmitKeys = isKeyboardEventsEnabled; + if (isKeyboardEventsEnabled) { + this.addKeyboardCallbacks(); + } else { + this.removeKeyboardCallbacks(); + } }); + + // activate the plugin listening + this.instance.store.dispatch({type: 'KEYBOARD_EVENTS_ENABLED', payload: true}); } /** @@ -89,9 +94,6 @@ module.exports = class KeyboardEvents { * @param {Event} event Event. */ onKeyPress(event) { - if (!this.transmitKeys) { - return; - } const key = event.charCode; let text = event.key || String.fromCharCode(key); if (text === 'Spacebar') { @@ -118,10 +120,6 @@ module.exports = class KeyboardEvents { * @return {boolean} Whether or not the event must continue propagation. */ onKeyDown(event) { - if (!this.transmitKeys) { - return true; - } - let key; /** * Convert invisible key or shortcut keys when ctrl/meta are pressed @@ -176,10 +174,6 @@ module.exports = class KeyboardEvents { * @return {boolean} Whether or not the event must continue propagation. */ onKeyUp(event) { - if (!this.transmitKeys) { - return true; - } - let key; /** * Convert invisible key or shortcut keys when ctrl/meta are pressed diff --git a/src/store/index.js b/src/store/index.js index d30c07b..f03303e 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -9,14 +9,12 @@ const initialState = { isOpen: false, widgetOpened: [], }, - isKeyboardEventsEnabled: true, + isKeyboardEventsEnabled: false, }; const createStore = (instance, reducer, state) => { const listeners = []; - const getState = () => instance.store.state; - const getters = { isWidgetOpened: (overlayID) => instance.store.state.overlay.isOpen && instance.store.state.overlay.widgetOpened.includes(overlayID), @@ -33,7 +31,7 @@ const createStore = (instance, reducer, state) => { const uid = generateUID(); listeners.push({ uid, - cb: () => listener(getState()), + cb: () => listener(instance.store.state), }); const unsubscribe = () => { @@ -46,17 +44,18 @@ const createStore = (instance, reducer, state) => { return unsubscribe; }; - instance.store = {state, getState, dispatch, subscribe, getters}; + instance.store = {state, dispatch, subscribe, getters}; }; const reducer = (state, action) => { - log.debug('Store updated', action.type, action.payload); - + let newState = state; switch (action.type) { case 'WEBRTC_CONNECTION_READY': - return {...state, isWebRTCConnectionReady: action.payload}; + newState = {...state, isWebRTCConnectionReady: action.payload}; + break; case 'KEYBOARD_EVENTS_ENABLED': - return {...state, isKeyboardEventsEnabled: action.payload}; + newState = {...state, isKeyboardEventsEnabled: action.payload}; + break; case 'OVERLAY_OPEN': // eslint-disable-next-line no-case-declarations const {overlayID, toOpen} = action.payload; @@ -80,7 +79,7 @@ const reducer = (state, action) => { * to open several widgets at the same time * const widgetOpened = state.overlay.widgetOpened.filter((widgetId) => widgetId !== overlayID); */ - return { + newState = { ...state, overlay: { isOpen: widgetOpened.length > 0, @@ -88,10 +87,14 @@ const reducer = (state, action) => { }, isKeyboardEventsEnabled: true, }; - + break; default: - return state; + break; } + + log.debug('Store updated below type - payload - result', action.type, action.payload, newState); + + return newState; }; const store = (instance) => { diff --git a/tests/unit/fingerprint.test.js b/tests/unit/fingerprint.test.js index 5d32397..d4a2b47 100644 --- a/tests/unit/fingerprint.test.js +++ b/tests/unit/fingerprint.test.js @@ -4,6 +4,7 @@ jest.mock('loglevel'); const FingerPrint = require('../../src/plugins/FingerPrint'); const Instance = require('../mocks/DeviceRenderer'); +const store = require('../../src/store/index'); let instance; @@ -12,6 +13,7 @@ describe('FingerPrint Plugin', () => { instance = new Instance({ giveFeedbackLink: 'https://github.com/orgs/Genymobile/discussions', }); + store(instance); new FingerPrint(instance, {}, true); });