|
4 | 4 |
|
5 | 5 | // Demonstrate how to register services |
6 | 6 | // In this case it is a simple value service. |
7 | | -angular.module('myApp.services', []) |
| 7 | +angular.module('sc.services', ['angular-cache']) |
8 | 8 | .constant('serviceControlUrl', 'http://localhost:33333/api') |
9 | | - .factory('streamService', ['$log', '$rootScope', 'serviceControlUrl', function($log, $rootScope, serviceControlUrl) { |
| 9 | + .run(function ($http, $angularCacheFactory) { |
| 10 | + $angularCacheFactory('defaultCache', { |
| 11 | + capacity: 1000, // This cache can hold 1000 items, |
| 12 | + maxAge: 90000, // Items added to this cache expire after 15 minutes |
| 13 | + aggressiveDelete: true, // Items will be actively deleted when they expire |
| 14 | + cacheFlushInterval: 3600000, // This cache will clear itself every hour, |
| 15 | + storageMode: 'localStorage' |
| 16 | + }); |
10 | 17 |
|
| 18 | + //$http.defaults.cache = $angularCacheFactory.get('defaultCache'); |
| 19 | + }) |
| 20 | + .factory('streamService', ['$log', '$rootScope', 'serviceControlUrl', function($log, $rootScope, serviceControlUrl) { |
| 21 | + var prefix = 'signalr::'; |
11 | 22 | var connection = $.connection(serviceControlUrl + '/messagestream'); |
12 | | - |
| 23 | + var registrations = {}; |
| 24 | + |
13 | 25 | connection.received(function(data) { |
14 | 26 | $log.info('SignalR data received'); |
15 | 27 | $log.info(data); |
16 | | - $rootScope.$broadcast(data.type, data.message); |
| 28 | + $rootScope.$broadcast(prefix + data.type, data.message); |
17 | 29 | }); |
18 | 30 |
|
19 | 31 | connection.start().done(function() { |
20 | 32 | $log.info('SignalR started'); |
21 | 33 | }); |
22 | 34 |
|
23 | 35 | var onSubscribe = function($scope, messageType, handler) { |
24 | | - $scope.$on(messageType, function(event, message) { |
25 | | - handler(message); |
| 36 | + var deregFunc = $scope.$on(prefix + messageType, function (event, message) { |
| 37 | + $scope.$apply(function(_) { |
| 38 | + handler(message); |
| 39 | + }); |
26 | 40 | }); |
| 41 | + |
| 42 | + registrations[messageType + $scope.$id] = deregFunc; |
27 | 43 | }; |
28 | 44 |
|
29 | 45 | var onUnsubscribe = function($scope, messageType) { |
| 46 | + var deregFunc = registrations[messageType + $scope.$id]; |
30 | 47 |
|
| 48 | + if (deregFunc !== null) { |
| 49 | + deregFunc(); |
| 50 | + } |
| 51 | + |
| 52 | + delete registrations[messageType + $scope.$id]; |
31 | 53 | }; |
32 | 54 |
|
33 | 55 | return { |
|
0 commit comments