mirror of https://github.com/BTCPrivate/copay.git
refactory of the socket service
This commit is contained in:
parent
58a15502d1
commit
0455d5ee69
|
@ -211,7 +211,7 @@
|
|||
<h3>Address</h3>
|
||||
<div class="row">
|
||||
<div class="large-6 columns">
|
||||
<a class="panel db" ng-repeat="addr in addrs" ng-click="selectAddr(addr)">{{addr}} <span class="right">{{balanceByAddr[addr] || 0}} BTC ></span></a>
|
||||
<a class="panel db" ng-repeat="addr in addrs" ng-click="selectAddr(addr)">{{addr}} <span class="right">{{$root.balanceByAddr[addr] || 0}} BTC ></span></a>
|
||||
</div>
|
||||
<div class="large-3 columns line-dashed-v text-center">
|
||||
<qrcode size="160" data="{{selectedAddr}}"></qrcode>
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copay.backup').controller('BackupController',
|
||||
function($scope, $rootScope, $location, $window, $timeout, Socket, controllerUtils) {
|
||||
controllerUtils.handleTransactionByAddress($scope);
|
||||
|
||||
function($scope, $rootScope, $location, $window, $timeout) {
|
||||
$scope.title = 'Backup';
|
||||
|
||||
var filename = $rootScope.wallet.id + '.json.aes';
|
||||
|
|
|
@ -24,6 +24,12 @@ angular.module('copay.header').controller('HeaderController',
|
|||
'link': '#/backup'
|
||||
}];
|
||||
|
||||
$rootScope.$watch('wallet', function(wallet) {
|
||||
if (wallet) {
|
||||
controllerUtils.setSocketHandlers();
|
||||
}
|
||||
});
|
||||
|
||||
$scope.isActive = function(item) {
|
||||
if (item.link && item.link.replace('#','') == $location.path()) {
|
||||
return true;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copay.home').controller('HomeController',
|
||||
function($scope, $rootScope, $location, Socket, controllerUtils) {
|
||||
function($scope, $rootScope, controllerUtils) {
|
||||
$scope.title = 'Home';
|
||||
$scope.oneAtATime = true;
|
||||
$scope.addrBalance = {};
|
||||
|
@ -10,18 +10,19 @@ angular.module('copay.home').controller('HomeController',
|
|||
|
||||
var _updateBalance = function () {
|
||||
w.getBalance(function (balance, balanceByAddr) {
|
||||
$scope.balanceByAddr = balanceByAddr;
|
||||
$scope.addrs = Object.keys(balanceByAddr);
|
||||
$scope.selectedAddr = $scope.addrs[0];
|
||||
$scope.$digest();
|
||||
$rootScope.$apply(function() {
|
||||
$rootScope.balanceByAddr = balanceByAddr;
|
||||
$scope.addrs = Object.keys(balanceByAddr);
|
||||
$scope.selectedAddr = $scope.addrs[0];
|
||||
});
|
||||
});
|
||||
var socket = Socket($scope);
|
||||
controllerUtils.handleTransactionByAddress($scope, _updateBalance);
|
||||
};
|
||||
|
||||
$scope.newAddr = function() {
|
||||
var a = w.generateAddress().toString();
|
||||
w.generateAddress().toString();
|
||||
_updateBalance();
|
||||
|
||||
controllerUtils.setSocketHandlers();
|
||||
};
|
||||
|
||||
$scope.selectAddr = function(addr) {
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copay.peer').controller('PeerController',
|
||||
function($scope, $rootScope, $location, $routeParams, Socket, controllerUtils) {
|
||||
controllerUtils.handleTransactionByAddress($scope);
|
||||
function($scope, $rootScope, $location, $routeParams) {
|
||||
});
|
||||
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copay.send').controller('SendController',
|
||||
function($scope, $rootScope, $location, Socket, controllerUtils) {
|
||||
function($scope, $rootScope, $location) {
|
||||
$scope.title = 'Send';
|
||||
|
||||
controllerUtils.handleTransactionByAddress($scope);
|
||||
|
||||
$scope.sendTest = function() {
|
||||
var w = $rootScope.wallet;
|
||||
w.createTx( 'mimoZNLcP2rrMRgdeX5PSnR7AjCqQveZZ4', '12345',function() {
|
||||
|
|
|
@ -24,7 +24,7 @@ angular.module('copay.setup').controller('SetupController',
|
|||
|
||||
$scope.$watch('totalCopayers', function(tc) {
|
||||
updateRCSelect(tc);
|
||||
})
|
||||
});
|
||||
|
||||
$scope.create = function(totalCopayers, requiredCopayers) {
|
||||
$scope.loading = true;
|
||||
|
|
|
@ -2,15 +2,8 @@
|
|||
|
||||
angular.module('copay.signin').controller('SigninController',
|
||||
function($scope, $rootScope, $location, walletFactory, controllerUtils) {
|
||||
|
||||
// var peerData = Storage.get($rootScope.walletId, 'peerData');
|
||||
// $rootScope.peerId = peerData ? peerData.peerId : null;
|
||||
//
|
||||
|
||||
$scope.loading = false;
|
||||
|
||||
$scope.walletIds = walletFactory.getWalletIds();
|
||||
|
||||
$scope.selectedWalletId = $scope.walletIds.length ? $scope.walletIds[0]:null;
|
||||
|
||||
$scope.create = function() {
|
||||
|
@ -25,39 +18,15 @@ angular.module('copay.signin').controller('SigninController',
|
|||
w.netStart();
|
||||
};
|
||||
|
||||
$scope.join = function(cid) {
|
||||
$scope.join = function(peerId) {
|
||||
$scope.loading = true;
|
||||
walletFactory.network.on('openError', function() {
|
||||
controllerUtils.onError($scope);
|
||||
$rootScope.$digest();
|
||||
});
|
||||
walletFactory.connectTo(cid, function(w) {
|
||||
walletFactory.connectTo(peerId, function(w) {
|
||||
controllerUtils.setupUxHandlers(w);
|
||||
w.netStart();
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
//
|
||||
// if (cid) {
|
||||
// var w = walletFactory.(walletId);
|
||||
//TODO
|
||||
// Network.init(null, function() {
|
||||
// Network.connect(cid,
|
||||
// function() {
|
||||
// $location.path('peer');
|
||||
// $rootScope.$digest();
|
||||
// }, function() {
|
||||
// $rootScope.flashMessage = { message: 'Connection refussed', type: 'error'};
|
||||
// $location.path('home');
|
||||
// $rootScope.$digest();
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
|
||||
// if (peerData && peerData.peerId && peerData.connectedPeers.length > 0) {
|
||||
// $rootScope.peerId = peerData.peerId;
|
||||
// $scope.join(peerData.connectedPeers);
|
||||
// }
|
||||
});
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copay.transactions').controller('TransactionsController',
|
||||
function($scope, $rootScope, $location, Socket, controllerUtils) {
|
||||
function($scope, $rootScope, $location) {
|
||||
$scope.title = 'Transactions';
|
||||
|
||||
$scope.oneAtATime = true;
|
||||
|
@ -35,8 +35,6 @@ angular.module('copay.transactions').controller('TransactionsController',
|
|||
};
|
||||
|
||||
_updateTxs();
|
||||
var socket = Socket($scope);
|
||||
socket.on('connect', controllerUtils.handleTransactionByAddress($scope));
|
||||
|
||||
$scope.sign = function (ntxid) {
|
||||
var w = $rootScope.wallet;
|
||||
|
|
32
js/routes.js
32
js/routes.js
|
@ -7,31 +7,40 @@ angular
|
|||
|
||||
$routeProvider
|
||||
.when('/', {
|
||||
templateUrl: 'signin.html'
|
||||
templateUrl: 'signin.html',
|
||||
validate: false
|
||||
})
|
||||
.when('/signin', {
|
||||
templateUrl: 'signin.html'
|
||||
templateUrl: 'signin.html',
|
||||
validate: false
|
||||
})
|
||||
.when('/setup', {
|
||||
templateUrl: 'setup.html'
|
||||
templateUrl: 'setup.html',
|
||||
validate: false
|
||||
})
|
||||
.when('/home', {
|
||||
templateUrl: 'home.html'
|
||||
templateUrl: 'home.html',
|
||||
validate: true
|
||||
})
|
||||
.when('/join/:id', {
|
||||
templateUrl: 'join.html'
|
||||
templateUrl: 'join.html',
|
||||
validate: true
|
||||
})
|
||||
.when('/peer', {
|
||||
templateUrl: 'peer.html'
|
||||
templateUrl: 'peer.html',
|
||||
validate: true
|
||||
})
|
||||
.when('/transactions', {
|
||||
templateUrl: 'transactions.html'
|
||||
templateUrl: 'transactions.html',
|
||||
validate: true
|
||||
})
|
||||
.when('/send', {
|
||||
templateUrl: 'send.html'
|
||||
templateUrl: 'send.html',
|
||||
validate: true
|
||||
})
|
||||
.when('/backup', {
|
||||
templateUrl: 'backup.html'
|
||||
templateUrl: 'backup.html',
|
||||
validate: true
|
||||
})
|
||||
.otherwise({
|
||||
templateUrl: '404.html'
|
||||
|
@ -47,9 +56,8 @@ angular
|
|||
//.hashPrefix('!');
|
||||
})
|
||||
.run(function($rootScope, $location) {
|
||||
$rootScope.$on('$routeChangeStart', function() {
|
||||
if (!$rootScope.wallet || !$rootScope.wallet.id) {
|
||||
console.log('############ no wallet');
|
||||
$rootScope.$on('$routeChangeStart', function(event, next, current) {
|
||||
if ((!$rootScope.wallet || !$rootScope.wallet.id) && next.validate) {
|
||||
$location.path('signin');
|
||||
}
|
||||
});
|
||||
|
|
|
@ -21,9 +21,7 @@ angular.module('copay.controllerUtils').factory('controllerUtils', function ($ro
|
|||
$rootScope.$digest();
|
||||
}
|
||||
|
||||
|
||||
root.setupUxHandlers = function(w) {
|
||||
|
||||
w.on('badMessage', function(peerId) {
|
||||
$rootScope.flashMessage = {type:'error', message: 'Received wrong message from peer id:' + peerId};
|
||||
});
|
||||
|
@ -45,19 +43,24 @@ angular.module('copay.controllerUtils').factory('controllerUtils', function ($ro
|
|||
w.on('close', root.onErrorDigest);
|
||||
};
|
||||
|
||||
root.handleTransactionByAddress = function(scope, cb) {
|
||||
var socket = Socket(scope);
|
||||
root.setSocketHandlers = function(cb) {
|
||||
Socket.removeAllListeners();
|
||||
|
||||
var addrs = $rootScope.wallet.getAddressesStr();
|
||||
for(var i=0;i<addrs.length;i++) {
|
||||
socket.emit('subscribe', addrs[i]);
|
||||
for(var i = 0; i < addrs.length; i++) {
|
||||
console.log('### SUBSCRIBE TO', addrs[i]);
|
||||
Socket.emit('subscribe', addrs[i]);
|
||||
}
|
||||
|
||||
addrs.forEach(function(addr) {
|
||||
socket.on(addr, function(txid) {
|
||||
Socket.on(addr, function(txid) {
|
||||
console.log('Received!', txid);
|
||||
$rootScope.wallet.getBalance(function(balance) {
|
||||
scope.$apply(function() {
|
||||
$rootScope.wallet.getBalance(function(balance, balanceByAddr) {
|
||||
$rootScope.$apply(function() {
|
||||
$rootScope.totalBalance = balance;
|
||||
$rootScope.balanceByAddr = balanceByAddr;
|
||||
});
|
||||
|
||||
console.log('New balance:', balance);
|
||||
if (typeof cb === 'function') return cb();
|
||||
});
|
||||
|
|
|
@ -1,72 +1,49 @@
|
|||
'use strict';
|
||||
|
||||
var ScopedSocket = function(socket, $rootScope) {
|
||||
this.socket = socket;
|
||||
this.$rootScope = $rootScope;
|
||||
this.listeners = [];
|
||||
};
|
||||
|
||||
ScopedSocket.prototype.removeAllListeners = function(opts) {
|
||||
if (!opts) opts = {};
|
||||
for (var i = 0; i < this.listeners.length; i++) {
|
||||
var details = this.listeners[i];
|
||||
if (opts.skipConnect && details.event === 'connect') {
|
||||
continue;
|
||||
}
|
||||
this.socket.removeListener(details.event, details.fn);
|
||||
}
|
||||
this.listeners = [];
|
||||
};
|
||||
|
||||
ScopedSocket.prototype.on = function(event, callback) {
|
||||
var socket = this.socket;
|
||||
var $rootScope = this.$rootScope;
|
||||
|
||||
var wrapped_callback = function() {
|
||||
var args = arguments;
|
||||
$rootScope.$apply(function() {
|
||||
callback.apply(socket, args);
|
||||
});
|
||||
};
|
||||
socket.on(event, wrapped_callback);
|
||||
|
||||
this.listeners.push({
|
||||
event: event,
|
||||
fn: wrapped_callback
|
||||
});
|
||||
};
|
||||
|
||||
ScopedSocket.prototype.emit = function(event, data, callback) {
|
||||
var socket = this.socket;
|
||||
var $rootScope = this.$rootScope;
|
||||
|
||||
socket.emit(event, data, function() {
|
||||
var args = arguments;
|
||||
$rootScope.$apply(function() {
|
||||
if (callback) {
|
||||
callback.apply(socket, args);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
angular.module('copay.socket').factory('Socket',
|
||||
function($rootScope) {
|
||||
var server = 'http://' + config.socket.host + ':' + config.socket.port;
|
||||
var socket = io.connect(server, {
|
||||
var listeners = [];
|
||||
var url = 'http://' + config.socket.host + ':' + config.socket.port;
|
||||
var socket = io.connect(url, {
|
||||
'reconnect': true,
|
||||
'reconnection delay': 500,
|
||||
});
|
||||
return function(scope) {
|
||||
var scopedSocket = new ScopedSocket(socket, $rootScope);
|
||||
scope.$on('$destroy', function() {
|
||||
scopedSocket.removeAllListeners();
|
||||
});
|
||||
socket.on('connect', function() {
|
||||
scopedSocket.removeAllListeners({
|
||||
skipConnect: true
|
||||
|
||||
return {
|
||||
on: function(event, callback) {
|
||||
var wrappedCallback = function() {
|
||||
var args = arguments;
|
||||
$rootScope.$apply(function() {
|
||||
callback.apply(socket, args);
|
||||
});
|
||||
};
|
||||
|
||||
socket.on(event, wrappedCallback);
|
||||
|
||||
if (event !== 'connect') {
|
||||
listeners.push({
|
||||
event: event,
|
||||
fn: wrappedCallback
|
||||
});
|
||||
}
|
||||
},
|
||||
emit: function(event, data, callback) {
|
||||
socket.emit(event, data, function() {
|
||||
var args = arguments;
|
||||
$rootScope.$apply(function() {
|
||||
if (callback) {
|
||||
callback.apply(socket, args);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
return scopedSocket;
|
||||
},
|
||||
removeAllListeners: function() {
|
||||
for (var i = 0; i < listeners.length; i++) {
|
||||
var details = listeners[i];
|
||||
socket.removeListener(details.event, details.fn);
|
||||
}
|
||||
|
||||
listeners = [];
|
||||
}
|
||||
};
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue