Skip to content

Commit

Permalink
[SIW-777,SIW-775] Add check to start proximity manager only once and …
Browse files Browse the repository at this point in the history
…removeListeners method (#17)

* Remove key parser
* Remove console log

Co-authored-by: Mario Perrotta <mario.perrotta@pagopa.it>

* chore: add isStarted check
* chore;: update version
* chore: add new event

---------

Co-authored-by: grausof <grausof@gmail.com>
  • Loading branch information
hevelius and grausof authored Dec 21, 2023
1 parent 0c9a788 commit 6cb526f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 29 deletions.
4 changes: 4 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export default function App() {
case 'ON_BLE_STOP':
setIsStarted(false);
break;
case 'ON_DOCUMENT_PRESENTATION_COMPLETED':
stopProximityManager();
break;
default:
break;
}
Expand Down Expand Up @@ -94,6 +97,7 @@ export default function App() {
ProximityManager.stop().then(() => {
setQrCodeUri(undefined);
setIsStarted(false);
ProximityManager.removeListeners();
});
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pagopa/io-react-native-proximity",
"version": "0.1.6",
"version": "0.1.7",
"description": "A RN library to get proximity flow based on ISO-18013",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
74 changes: 47 additions & 27 deletions src/proximity-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,37 +96,44 @@ const ProximityManager = () => {
state: string;
};

// This flag is used to understand if the BLE manager is started
// and avoid starting it multiple times causing multiple
// connection to the same peripheral
let isStarted = false;

const start = () => {
return new Promise<void>(async (resolve, reject) => {
await session.start();
randomVerifierUUID = uuid.v4().toString();
if (!isStarted) {
BleManager.start({ showAlert: false })
.then(() => {
bleManagerEmitter.addListener(
'BleManagerDiscoverPeripheral',
handleDiscoverPeripheral
);
bleManagerEmitter.addListener('BleManagerStopScan', handleStopScan);

BleManager.start({ showAlert: false })
.then(() => {
bleManagerEmitter.addListener(
'BleManagerDiscoverPeripheral',
handleDiscoverPeripheral
);
bleManagerEmitter.addListener('BleManagerStopScan', handleStopScan);

bleManagerEmitter.addListener(
'BleManagerDisconnectPeripheral',
handleDisconnectedPeripheral
);

bleManagerEmitter.addListener(
'BleManagerDidUpdateValueForCharacteristic',
handleUpdateValueForCharacteristic
);
eventManager.emit('onEvent', {
type: 'ON_BLE_START',
message: 'ble manager is started.',
bleManagerEmitter.addListener(
'BleManagerDisconnectPeripheral',
handleDisconnectedPeripheral
);

bleManagerEmitter.addListener(
'BleManagerDidUpdateValueForCharacteristic',
handleUpdateValueForCharacteristic
);
isStarted = true;
resolve();
})
.catch((error) => {
reject(error);
});
randomVerifierUUID = uuid.v4().toString();
resolve();
})
.catch((error) => {
reject(error);
});
}
eventManager.emit('onEvent', {
type: 'ON_BLE_START',
message: 'ble manager is started.',
});
});
};

Expand All @@ -141,6 +148,10 @@ const ProximityManager = () => {

const messageToSend = encode(responseData);
await sendMdocResponseChunks(messageToSend);
eventManager.emit('onEvent', {
type: 'ON_DOCUMENT_PRESENTATION_COMPLETED',
message: 'Document presentation completed.',
});
};

const sendMdocResponseChunks = async (messageToSend: Buffer) => {
Expand Down Expand Up @@ -236,7 +247,6 @@ const ProximityManager = () => {
console.log('Unable to disconnect from pheripheral', error);
}
}

resolve();
eventManager.emit('onEvent', {
type: 'ON_BLE_STOP',
Expand Down Expand Up @@ -466,6 +476,15 @@ const ProximityManager = () => {
});
};

/**
* This function is to remove all the listeners
* @example
* ProximityManager.removeListeners();
*/
const removeListeners = () => {
eventManager.removeAllListeners();
};

const setOnDocumentRequestHandler = (
handler: typeof handleDocumentsRequest
) => {
Expand Down Expand Up @@ -511,6 +530,7 @@ const ProximityManager = () => {
setListeners,
setOnDocumentRequestHandler,
dataPresentation,
removeListeners,
stop,
};
};
Expand Down
14 changes: 13 additions & 1 deletion src/utils/EventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type EventType =
| 'ON_CHARACTERISTIC_CHANGED'
| 'ON_BLE_ERROR'
| 'ON_SESSION_ESTABLISHMENT'
| 'ON_DOCUMENT_REQUESTS_RECEIVED';
| 'ON_DOCUMENT_REQUESTS_RECEIVED'
| 'ON_DOCUMENT_PRESENTATION_COMPLETED';

export type EventData = {
message: string;
Expand Down Expand Up @@ -42,6 +43,7 @@ export type EventData = {
const createEventManager = () => {
const listeners: { [key: string]: Array<(eventData: EventData) => void> } =
{};

const addListener = (
eventName: string,
callback: (eventData: EventData) => void
Expand All @@ -51,6 +53,7 @@ const createEventManager = () => {
}
listeners[eventName]?.push(callback);
};

const removeListener = (
eventName: string,
callback: (eventData: EventData) => void
Expand All @@ -61,14 +64,23 @@ const createEventManager = () => {
);
}
};

const removeAllListeners = () => {
Object.keys(listeners).forEach((eventName) => {
listeners[eventName] = [];
});
};

const emit = (eventName: string, data: EventData) => {
if (listeners[eventName]) {
listeners[eventName]?.forEach((listener) => listener(data));
}
};

return {
addListener,
removeListener,
removeAllListeners,
emit,
};
};
Expand Down

0 comments on commit 6cb526f

Please sign in to comment.