Reworked loki_p2p_api to not use the window object, which means it needs to be instantiated after the storage is ready so that your pubkey can be passed in to the constructor. This makes it more modular and allows for easier testing

pull/175/head
Beaudan 6 years ago
parent 432c336048
commit 69ebf017ff

@ -219,6 +219,13 @@
return;
}
first = false;
window.lokiP2pAPI = new window.LokiP2pAPI(
textsecure.storage.user.getNumber()
);
window.lokiP2pAPI.on(
'pingContact',
window.libloki.api.sendOnlineBroadcastMessage
);
// These make key operations available to IPC handlers created in preload.js
window.Events = {

@ -1,16 +1,23 @@
/* global setTimeout, clearTimeout, window */
/* global setTimeout, clearTimeout */
const EventEmitter = require('events');
class LokiP2pAPI extends EventEmitter {
constructor() {
constructor(ourKey) {
super();
this.contactP2pDetails = {};
this.ourKey = ourKey;
}
updateContactP2pDetails(pubKey, address, port, fromP2p = false) {
reset() {
Object.keys(this.contactP2pDetails).forEach(key => {
clearTimeout(this.contactP2pDetails[key].pingTimer);
delete this.contactP2pDetails[key];
});
}
updateContactP2pDetails(pubKey, address, port, isOnline = false) {
// Stagger the timers so the friends don't ping each other at the same time
this.ourKey = this.ourKey || window.textsecure.storage.user.getNumber();
const timerDuration =
pubKey < this.ourKey
? 60 * 1000 // 1 minute
@ -28,7 +35,7 @@ class LokiP2pAPI extends EventEmitter {
pingTimer: null,
};
if (fromP2p) {
if (isOnline) {
this.setContactOnline(pubKey);
return;
}
@ -73,7 +80,7 @@ class LokiP2pAPI extends EventEmitter {
if (!this.contactP2pDetails[pubKey]) {
return;
}
window.libloki.api.sendOnlineBroadcastMessage(pubKey, true);
this.emit('pingContact', pubKey, true);
}
}

@ -276,9 +276,7 @@ window.LokiSnodeAPI = new LokiSnodeAPI({
swarmServerPort: config.swarmServerPort,
});
const LokiP2pAPI = require('./js/modules/loki_p2p_api');
window.lokiP2pAPI = new LokiP2pAPI();
window.LokiP2pAPI = require('./js/modules/loki_p2p_api');
const LokiMessageAPI = require('./js/modules/loki_message_api');

Loading…
Cancel
Save