Skip to content

Commit

Permalink
working on getting close and disconnect events propagating
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveMieskoski committed Mar 12, 2021
1 parent 85dbb76 commit 4b39ccd
Show file tree
Hide file tree
Showing 17 changed files with 169 additions and 24 deletions.
5 changes: 4 additions & 1 deletion example/app/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<button @click="onClick">CONNECT</button>
<h3>{{ userAddress }}</h3>
<button @click="ecrecover">ecrecover</button>
<button @click="showThing">CHECK</button>
<button @click="doThing">CHECK</button>
<div v-show="userAddress === ''">
<button @click="selectNetwork(1)">Mainnet</button>
<button @click="selectNetwork(3)">Ropsten</button>
Expand Down Expand Up @@ -468,6 +468,9 @@ export default {
// })
},
methods: {
doThing(){
},
showThing() {
console.log('CONSOLE CHECK'); // todo remove dev item
Expand Down
6 changes: 3 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module.exports = {
collectCoverage: true,
setTimeout: 10000,
coverageDirectory: 'coverage',*/
// testEnvironment: 'jsdom',
testMatch: ['**/__tests__/**/*.js?(x)', '**/test/**/?(*.)+(spec|test).js?(x)'],
testEnvironment: 'jsdom',
testMatch: ['**/__tests__/**/*.js?(x)', '**/tests/**/?(*.)+(spec|test).js?(x)', '**/test/**/?(*.)+(spec|test).js?(x)'],
testPathIgnorePatterns: ['/node_modules/'],
"testEnvironment": "node",
// "testEnvironment": "node",
/* transform: {
"^.+\\.(js) ? $": "babel-jest"
},*/
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@myetherwallet/mewconnect-web-client",
"homepage": "https://github.com/myetherwallet/MEWconnect-web-client",
"version": "2.1.23-beta.5",
"version": "2.1.23-beta.7",
"main": "./dist/index.js",
"module": "./src/index.js",
"scripts": {
Expand All @@ -17,6 +17,7 @@
"lint": "npx eslint --fix 'src/**.js'",
"update:lists": "node src/connectProvider/fetchLists/index.js",
"prepublishOnly": "npm install && npm run update:lists && npm run build",
"prepack": "npm run build",
"ci": "npm install && npm run update:lists && npm run build"
},
"gitHooks": {
Expand Down
7 changes: 7 additions & 0 deletions src/connectClient/WebRtcCommunication.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,10 @@ export default class WebRtcCommunication extends MewConnectCommon {
}

onClose(peerID, data) {
console.log('1'); // todo remove dev item
debugStages('WRTC onClose event');
debug('peerID', peerID);
console.log('onClose', this.connected); // todo remove dev item
if (!this.isAlive()) {
debugStages('WRTC CLOSE', data);
if (this.connected) {
Expand All @@ -377,6 +379,11 @@ export default class WebRtcCommunication extends MewConnectCommon {
this.connected = false;
}
}
if (this.connected) {
this.emit(this.lifeCycle.RtcClosedEvent);
this.uiCommunicator(this.lifeCycle.disconnected);
this.connected = false;
}
}

onError(peerID, err) {
Expand Down
3 changes: 2 additions & 1 deletion src/connectClient/constants/signals.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ const signalV2 = {
initiated: 'initiated',
socketTimeout: 'socketTimeout',
receivedSignal: 'receivedSignal',
error: 'error'
error: 'error',
disconnected: 'disconnected'
};

export { signalV1, signalV2 };
5 changes: 2 additions & 3 deletions src/connectClient/initiator/MewConnectInitiator.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export default class MewConnectInitiator extends MewConnectCommon {
}
} else {
this.uiCommunicator(this.lifeCycle.codeDisplay, qrCodeString);
this.uiCommunicator(this.lifeCycle.checkNumber, privateKey);
// this.uiCommunicator(this.lifeCycle.checkNumber, privateKey);
this.uiCommunicator(this.lifeCycle.ConnectionId, this.connId);
}

Expand Down Expand Up @@ -394,9 +394,8 @@ Keys
this.webRtcCommunication.once(
this.jsonDetails.lifeCycle.RtcConnectedEvent,
(peerid) => {
console.log(peerid); // todo remove dev item
this.webRtcCommunication.removeAllListeners(this.jsonDetails.lifeCycle.RtcConnectedEvent)
console.log('RTC CONNECTED ENVIRONMENT SETUP'); // todo remove dev item
debug('RTC CONNECTED ENVIRONMENT SETUP'); // todo remove dev item
this.emit(this.lifeCycle.RtcConnectedEvent);
this.webRtcCommunication.on('appData', this.dataReceived.bind(this));
this.connected = true;
Expand Down
14 changes: 13 additions & 1 deletion src/connectClient/initiator/MewConnectInitiatorV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,22 @@ export default class MewConnectInitiatorV2 extends MewConnectCommon {
this.socketConnected = true;
});

this.socket.on('onClose', () => {
debugStages('SOCKET DISCONNECTED');
this.socketConnected = false;
if (!this.connected) {
// show new QRcode
}
});

this.socketOn(this.signals.initiated, this.initiated.bind(this)); // response
this.socketOn(this.signals.disconnected, data => {
// this.beginRtcSequence(stunServers);
}); // response

this.socketOn(this.signals.confirmation, data => {
this.beginRtcSequence(stunServers);
}); // response
});
// this.signals.answer
this.socketOn('answer', this.recieveAnswer.bind(this));
this.socketOn(
Expand Down
4 changes: 3 additions & 1 deletion src/connectClient/websocketWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ export default class WebsocketConnection {
}

if(parsedMessage.signal === 'ping' || parsedMessage.signal === 'pong') {
console.log(parsedMessage); // todo remove dev item
return;
}
const signal = parsedMessage.signal;
Expand All @@ -156,6 +155,9 @@ export default class WebsocketConnection {

onClose() {
debug(`websocket onClose = ${this.getSocketState()}`);
if(this.listeners['onClose']){
this.listeners['onClose'].call(this);
}
}

/**
Expand Down
22 changes: 20 additions & 2 deletions src/connectProvider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export default class Integration extends EventEmitter {
web3Provider.close = this.disconnect.bind(this);
web3Provider.disconnect = this.disconnect.bind(this);
state.web3Provider = web3Provider;

console.log(state.web3Provider); // todo remove dev item
state.web3 = new Web3(web3Provider);
if (!this.runningInApp) {
state.web3.currentProvider.sendAsync = state.web3.currentProvider.send;
Expand Down Expand Up @@ -324,12 +324,19 @@ export default class Integration extends EventEmitter {
() => {
this.popUpHandler.showNotice(messageConstants.disconnect);
MEWconnectWallet.setConnectionState(connection.lifeCycle.disconnected);
console.log('DISCONNECT NOTIFIER', state.web3Provider); // todo remove dev item
if (state.wallet !== null && state.web3Provider.disconnectCallback) {
state.web3Provider.disconnectCallback();
this.emit('disconnect')
// this.emit('disconnect')
}
if (state.wallet !== null && state.web3Provider.closeCallback) {
state.web3Provider.closeCallback();
// this.emit('close')
}
state.wallet = null;
this.emit(DISCONNECTED);
this.emit('close')
this.emit('disconnect')
}
);

Expand All @@ -338,12 +345,23 @@ export default class Integration extends EventEmitter {
() => {
this.popUpHandler.showNotice(messageConstants.disconnect);
MEWconnectWallet.setConnectionState(connection.lifeCycle.disconnected);
console.log('DISCONNECT NOTIFIER', state.web3Provider); // todo remove dev item
if (state.wallet !== null && state.web3Provider.disconnectCallback) {
console.log('disconnectCallback', typeof state.web3Provider.closeCallback); // todo remove dev item
state.web3Provider.disconnectCallback();
state.web3Provider.emit('disconnect')
this.emit('disconnect')
}
if (state.wallet !== null && state.web3Provider.closeCallback) {
state.web3Provider.closeCallback();
this.emit('close')
state.web3Provider.emit('close')
}

state.wallet = null;
this.emit(connection.lifeCycle.disconnected);
this.emit('close')
this.emit('disconnect')
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class HttpProvider {
});
},
notificationCallbacks: [],
disconnectCallbacks: [],
closeCallbacks: [],
createSubscriptions: (subscription, ) => {
requestManager.addSubscription()
},
Expand Down Expand Up @@ -81,8 +83,37 @@ class HttpProvider {
this.httpProvider.disconnectedCallback = callback;
break;
case 'disconnect':
this.httpProvider.disconnectCallbacks.push(callback)
this.httpProvider.disconnectCallback = callback;
break;
case 'close':
this.httpProvider.closeCallbacks.push(callback)
this.httpProvider.closeCallback = callback;
break;
}
},
emit:(type, data) => {
if (typeof type !== 'string')
throw new Error('The first parameter type must be a function.');

switch (type) {

// case 'accountsChanged':
// this.accountsChanged = callback;
// break;
// case 'disconnected':
// this.disconnectedCallback = callback;
// break;
case 'disconnect':
this.httpProvider.disconnectCallbacks.forEach(function(callback) {
if (typeof callback === 'function') callback(data);
});
break;
case 'close':
this.httpProvider.closeCallbacks.forEach(function(callback) {
if (typeof callback === 'function') callback(data);
});
break;
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const WebsocketProvider = function WebsocketProvider(url, options) {
const _this = this;
this.responseCallbacks = {};
this.notificationCallbacks = [];
this.closeCallbacks = [];
this.disconnectCallbacks = []

options = options || {};
this._customTimeout = options.timeout;
Expand Down Expand Up @@ -191,6 +193,7 @@ WebsocketProvider.prototype.send = function(payload, callback) {
WebsocketProvider.prototype.on = function(type, callback) {
if (typeof callback !== 'function')
throw new Error('The second parameter callback must be a function.');
console.log('set callback'); // todo remove dev item

switch (type) {
case 'data':
Expand All @@ -216,8 +219,39 @@ WebsocketProvider.prototype.on = function(type, callback) {
this.disconnectedCallback = callback;
break;
case 'disconnect':
this.disconnectCallbacks.push(callback)
this.disconnectCallback = callback;
break;
case 'close':
this.closeCallbacks.push(callback)
console.log('set close callback'); // todo remove dev item
this.closeCallback = callback;
break;
}
};

WebsocketProvider.prototype.emit = function(type, data) {
if (typeof type !== 'string')
throw new Error('The first parameter type must be a function.');

switch (type) {

// case 'accountsChanged':
// this.accountsChanged = callback;
// break;
// case 'disconnected':
// this.disconnectedCallback = callback;
// break;
case 'disconnect':
this.disconnectCallbacks.forEach(function(callback) {
if (_.isFunction(callback)) callback(data);
});
break;
case 'close':
this.closeCallbacks.forEach(function(callback) {
if (_.isFunction(callback)) callback(data);
});
break;
}
};

Expand All @@ -230,6 +264,22 @@ WebsocketProvider.prototype.removeListener = function(type, callback) {
if (cb === callback) _this.notificationCallbacks.splice(index, 1);
});
break;
// case 'accountsChanged':
// this.accountsChanged = callback;
// break;
// case 'disconnected':
// this.disconnectedCallback = callback;
// break;
case 'disconnect':
this.disconnectCallbacks.forEach(function(cb, index) {
if (cb === callback) _this.disconnectCallbacks.splice(index, 1);
});
break;
case 'close':
this.closeCallbacks.forEach(function(cb, index) {
if (cb === callback) _this.closeCallbacks.splice(index, 1);
});
break;
}
};
WebsocketProvider.prototype.removeAllListeners = function(type) {
Expand Down
21 changes: 17 additions & 4 deletions src/connectWindow/popUpCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,26 @@ export default class PopUpCreator extends EventEmitter{
}

closePopupWindow() {
// this.hideDialog();
this.container.dispatchEvent(new Event('mewModalClosed'));
this.container.replaceChildren();
this.popupWindowOpen = null;
try { // this.hideDialog();
this.popupWindowOpen = null;
document.querySelector('#Attach-Mew-Wallet-Modal').dispatchEvent(new Event('mewModalClosed'));
document.querySelector('#Attach-Mew-Wallet-Modal').replaceChildren();

// document.querySelector('#Attach-Mew-Wallet-Modal').classList.add('hidden');
} catch (e) {
this.popupWindowOpen = null;
document.querySelector('#Attach-Mew-Wallet-Modal').innerHTML = '';
}
}

handleBeforeUnload() {
// this.closePopupWindow();
}
resetSetup() {
this.popupWindowOpen = null;
this.hideNotifier();
this.closePopupWindow();
// document.getElementById('Attach-Mew-Wallet-Modal');

}
}
2 changes: 1 addition & 1 deletion test/clients/receiverV1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import CryptoUtils from '../utils/crypto-utils';
const debugLogger = require('debug');
const io = require('socket.io-client');
const EventEmitter = require('events').EventEmitter;
const MewConnectCrypto = require('../../dist/index.js').Crypto;
const MewConnectCrypto = require('../../src/connectClient/MewConnectCrypto').default;
const SimplePeer = require('simple-peer');
const wrtc = require('wrtc');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ describe('Pairing', () => {
// initiator.rtcSend({ type: 'address', data: '' });
});

initiator.on('displayCode', (data) => {
console.log(data); // todo remove dev item
if (!silent) console.log('address', data); // todo remove dev item
// initiator.rtcSend({ type: 'address', data: '' });
});

await receiver.setKeys(
initiator.publicKey,
initiator.privateKey,
Expand Down
Loading

0 comments on commit 4b39ccd

Please sign in to comment.