Skip to content

Commit

Permalink
refacto keyboard start / stop listening
Browse files Browse the repository at this point in the history
  • Loading branch information
jparez committed Jun 14, 2024
1 parent e67547a commit 6a5b918
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 33 deletions.
6 changes: 0 additions & 6 deletions src/DeviceRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -486,10 +484,6 @@ module.exports = class DeviceRenderer {
this.mouseEvents.addMouseCallbacks();
}

if (this.keyboardEventsEnabled) {
this.keyboardEvents.addKeyboardCallbacks();
}

if (this.gamepadEventsEnabled) {
this.gamepadManager.addGamepadCallbacks();
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/FingerPrint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}) => {
Expand Down
22 changes: 8 additions & 14 deletions src/plugins/KeyboardEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}

/**
Expand All @@ -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') {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
27 changes: 15 additions & 12 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 = () => {
Expand All @@ -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;
Expand All @@ -80,18 +79,22 @@ 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,
widgetOpened,
},
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) => {
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/fingerprint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -12,6 +13,7 @@ describe('FingerPrint Plugin', () => {
instance = new Instance({
giveFeedbackLink: 'https://github.com/orgs/Genymobile/discussions',
});
store(instance);
new FingerPrint(instance, {}, true);
});

Expand Down

0 comments on commit 6a5b918

Please sign in to comment.