Skip to content

Commit 6cfd6b9

Browse files
committed
update socket api again, add unsync method for cleanup
1 parent e228fe9 commit 6cfd6b9

File tree

6 files changed

+59
-27
lines changed

6 files changed

+59
-27
lines changed
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict'
22

33
angular.module('<%= scriptAppName %>').controller 'MainCtrl', ($scope, $http<% if(filters.socketio) { %>, socket<% } %>) ->
4+
$scope.awesomeThings = []
5+
46
$http.get('/api/things').success (awesomeThings) ->
57
$scope.awesomeThings = awesomeThings
6-
<% if(filters.socketio) { %>socket.syncArray $scope.awesomeThings, 'thing'<% } %>
8+
<% if(filters.socketio) { %>socket.syncUpdates 'thing', $scope.awesomeThings<% } %>
79
<% if(filters.mongoose) { %>
810
$scope.addThing = ->
911
return if $scope.newThing is ''
@@ -13,4 +15,7 @@ angular.module('<%= scriptAppName %>').controller 'MainCtrl', ($scope, $http<% i
1315
$scope.newThing = ''
1416

1517
$scope.deleteThing = (thing) ->
16-
$http.delete '/api/things/' + thing._id<% } %>
18+
$http.delete '/api/things/' + thing._id<% } %><% if(filters.socketio) { %>
19+
20+
$scope.$on '$destroy', ->
21+
socket.unsyncUpdates 'thing'<% } %>

app/templates/client/app/main/main.controller(js).js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
angular.module('<%= scriptAppName %>')
44
.controller('MainCtrl', function ($scope, $http<% if(filters.socketio) { %>, socket<% } %>) {
5+
$scope.awesomeThings = [];
6+
57
$http.get('/api/things').success(function(awesomeThings) {
68
$scope.awesomeThings = awesomeThings;<% if(filters.socketio) { %>
7-
socket.syncArray($scope.awesomeThings, 'thing');<% } %>
9+
socket.syncUpdates('thing', $scope.awesomeThings);<% } %>
810
});
911
<% if(filters.mongoose) { %>
1012
$scope.addThing = function() {
@@ -17,5 +19,9 @@ angular.module('<%= scriptAppName %>')
1719

1820
$scope.deleteThing = function(thing) {
1921
$http.delete('/api/things/' + thing._id);
20-
};<% } %>
22+
};<% } %><% if(filters.socketio) { %>
23+
24+
$scope.$on('$destroy', function () {
25+
socket.unsyncUpdates('thing');
26+
});<% } %>
2127
});

app/templates/client/components/socket(socketio)/socket.mock(coffee).coffee

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ angular.module('socketMock', []).factory 'socket', ->
1010

1111
receive: ->
1212

13-
syncArray: ->
13+
syncUpdates: ->
14+
unsyncUpdates: ->

app/templates/client/components/socket(socketio)/socket.mock.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ angular.module('socketMock', [])
1010
receive: function() {}
1111
},
1212

13-
syncArray: function() {}
13+
syncUpdates: function() {},
14+
unsyncUpdates: function() {}
1415
};
1516
});

app/templates/client/components/socket(socketio)/socket.service(coffee).coffee

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,34 @@ angular.module('<%= scriptAppName %>').factory 'socket', (socketFactory) ->
1010
'force new connection': true
1111
'max reconnection attempts': Infinity
1212
'reconnection limit': 10 * 1000
13+
# Send auth token on connection
14+
# 'query': 'token=' + Auth.getToken()
1315
)
1416

15-
# Send auth token on connection
16-
# 'query': 'token=' + Auth.getToken()
1717
retryTimer = setInterval(->
1818
ioSocket.connect() if not ioSocket.socket.connected and not ioSocket.socket.connecting and not ioSocket.socket.reconnecting
1919
, retryInterval)
2020
socket = socketFactory(ioSocket: ioSocket)
21+
2122
socket: socket
2223

2324
###
24-
Register listeners to sync an array with a database collection through socket.io
25+
Register listeners to sync an array with updates on a model
2526
26-
Takes the array we want to sync, the model namespace that socket updates are sent from,
27+
Takes the array we want to sync, the model name that socket updates are sent from,
2728
and an optional callback function after new items are updated.
2829
30+
@param {String} modelName
2931
@param {Array} array
30-
@param {String} namespace
3132
@param {Function} cb
3233
###
33-
syncArray: (array, namespace, cb) ->
34+
syncUpdates: (modelName, array, cb) ->
3435
cb = cb or angular.noop
3536

3637
###
3738
Syncs item creation/updates on 'model:save'
3839
###
39-
socket.on namespace + ":save", (newItem) ->
40+
socket.on modelName + ":save", (newItem) ->
4041
oldItem = _.find(array,
4142
_id: newItem._id
4243
)
@@ -50,12 +51,20 @@ angular.module('<%= scriptAppName %>').factory 'socket', (socketFactory) ->
5051
array.push newItem
5152
cb array
5253

53-
5454
###
5555
Syncs removed items on 'model:remove'
5656
###
57-
socket.on namespace + ":remove", (newItem) ->
57+
socket.on modelName + ":remove", (newItem) ->
5858
_.remove array,
5959
_id: newItem._id
6060

61-
cb array
61+
cb array
62+
63+
###
64+
Removes listeners for a models updates on the socket
65+
66+
@param modelName
67+
###
68+
unsyncUpdates: (modelName) ->
69+
socket.removeAllListeners modelName + ":save"
70+
socket.removeAllListeners modelName + ":remove"

app/templates/client/components/socket(socketio)/socket.service.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,43 +35,53 @@ angular.module('<%= scriptAppName %>')
3535
socket: socket,
3636

3737
/**
38-
* Register listeners to sync an array with a database collection through socket.io
38+
* Register listeners to sync an array with updates on a model
3939
*
40-
* Takes the array we want to sync, the model namespace that socket updates are sent from,
40+
* Takes the array we want to sync, the model name that socket updates are sent from,
4141
* and an optional callback function after new items are updated.
4242
*
43+
* @param {String} modelName
4344
* @param {Array} array
44-
* @param {String} namespace
4545
* @param {Function} cb
4646
*/
47-
syncArray: function(array, namespace, cb) {
47+
syncUpdates: function (modelName, array, cb) {
4848
cb = cb || angular.noop;
4949

5050
/**
5151
* Syncs item creation/updates on 'model:save'
5252
*/
53-
socket.on(namespace + ':save', function(newItem) {
54-
var oldItem = _.find(array, { _id: newItem._id });
53+
socket.on(modelName + ':save', function (newItem) {
54+
var oldItem = _.find(array, {_id: newItem._id});
5555
var index = array.indexOf(oldItem);
5656

5757
// replace oldItem if it exists
5858
// otherwise just add newItem to the collection
59-
if(oldItem) {
59+
if (oldItem) {
6060
array.splice(index, 1, newItem);
6161
} else {
6262
array.push(newItem);
6363
}
6464

65-
cb(array)
65+
cb(array);
6666
});
6767

6868
/**
6969
* Syncs removed items on 'model:remove'
7070
*/
71-
socket.on(namespace + ':remove', function(newItem) {
72-
_.remove(array, { _id: newItem._id });
73-
cb(array)
71+
socket.on(modelName + ':remove', function (newItem) {
72+
_.remove(array, {_id: newItem._id});
73+
cb(array);
7474
});
75+
},
76+
77+
/**
78+
* Removes listeners for a models updates on the socket
79+
*
80+
* @param modelName
81+
*/
82+
unsyncUpdates: function (modelName) {
83+
socket.removeAllListeners(modelName + ':save');
84+
socket.removeAllListeners(modelName + ':remove');
7585
}
7686
};
7787
});

0 commit comments

Comments
 (0)