-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackgroundDataService.js
More file actions
152 lines (127 loc) · 5.71 KB
/
BackgroundDataService.js
File metadata and controls
152 lines (127 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*!
* background-data-service
* @author Chuck Holbrook chuck@streetdeck.com
* See LICENSE in this repository for license information
*/
angular.module('backgroundData', []).config(function ($httpProvider) {
$httpProvider.interceptors.push(globalBackgroundDataInterceptor);
});
var globalBackgroundDataInterceptor;
(function() {
var _updateServiceTime;
globalBackgroundDataInterceptor = function () { /* jshint ignore:line */
return {
'request': function (config) {
if (_updateServiceTime) {
_updateServiceTime();
}
return config;
},
'response': function (response) {
if (_updateServiceTime) {
_updateServiceTime();
}
return response;
}
};
};
'use strict';
/**
* We have some data that is global and likely to be used every time a user logs into the app at some point. Its
* not a priority until we go to the page, but if we have free service time, we should try and download it in the
* background so we have it when we go to that page and render immediatly. i.e. A recent items list
* are good examples of this type of data
* @example
* The init function should be called from your app.js either when the app starts or when logging in. You should
* call stop when you log out assuming you need to be authorized to call the services
*
//Its usually best to call this after you log in
BackgroundDataService.init();
// Data to get in the background when idle
var deferredRecent = $q.defer();
deferredRecent.promise.then(function(){
RecentService.getRecent('patient');
FavoritesService.getFavoritePrescriptions();
});
BackgroundDataService.callMeWhenIdle(deferredRecent);
*
* @returns {{}}
* @constructor
*/
function BackgroundDataServiceFn($log, $timeout, $interval){
var BackgroundDataService = {};
//Wait for 5 seconds of being idle before trying to call signaling we are service call idle
var _idleTimeoutSeconds = 3;
//The time the last service call request or response was made
var _lastServiceTime;
var _idleIntervalId;
var _isIdle = false;
//Set to true when the backgroun data services are running
var _isInitialized = false;
//The promises that will be triggered in order
var _deferralsToTrigger = [];
_updateServiceTime = function() {
_lastServiceTime = moment();
_isIdle = false;
};
/**
* Wait a few seconds after starting, the start looking at all network calls and waiting for a quiet period.
*/
BackgroundDataService.stop = function() {
_isInitialized = false;
$interval.cancel(_idleIntervalId);
_idleIntervalId = undefined;
};
/**
* Wait a few seconds after starting, then start looking at all network calls and waiting for a quiet period.
* @params idleTimeoutSeconds Override the idle timeout, the amount of time without any other service calls
* happening to then gather our background data
*/
BackgroundDataService.init = function initialize(idleTimeoutSeconds, debugLogging) {
_isInitialized = true;
if (idleTimeoutSeconds > 0) {
_idleTimeoutSeconds = idleTimeoutSeconds;
}
if (!_idleIntervalId) {
$timeout(function(){
if (!_idleIntervalId && _isInitialized) {
//Check once a second to see if we are idle
_idleIntervalId = $interval(function(){
if (moment().diff(_lastServiceTime, 'seconds') > _idleTimeoutSeconds) {
if (debugLogging) {
$log.info('We are idle (' + moment().diff(_lastServiceTime, 'seconds') + ' seconds) and ready to call more services!');
}
_isIdle = true;
if (_deferralsToTrigger.length > 0) {
if (debugLogging) {
$log.info('Triggering idle promise (' + _deferralsToTrigger.length + ')!');
}
_deferralsToTrigger.pop().resolve();
} else {
if (debugLogging) {
$log.info('No more idle promises to trigger, shutting down!');
}
BackgroundDataService.stop();
}
}
}, 1000);
}
}, 1000);
}
};
/**
* Register a promise that will be executed when idle. The promises will be called in FIFO order and after
* trigger when promise when idle, it will wait a second and then wait until we are idle again to trigger
* the next
*/
BackgroundDataService.callMeWhenIdle = function(deferral){
_deferralsToTrigger.push(deferral);
//If we are not actively watching the services, restart now
if (_isInitialized && !_idleIntervalId) {
BackgroundDataService.init();
}
};
return BackgroundDataService;
}
angular.module('backgroundData').factory('BackgroundDataService', BackgroundDataServiceFn);
})();