-
Notifications
You must be signed in to change notification settings - Fork 456
/
Copy pathstackPrefetch.js
executable file
·415 lines (333 loc) · 10.7 KB
/
stackPrefetch.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import external from './../externalModules.js';
import loadHandlerManager from '../stateManagement/loadHandlerManager.js';
import { addToolState, getToolState } from '../stateManagement/toolState.js';
import { getLogger } from '../util/logger.js';
import triggerEvent from '../util/triggerEvent';
import EVENTS from '../events.js';
const logger = getLogger('stackTools:stackPrefetch');
const toolName = 'stackPrefetch';
const requestType = 'prefetch';
const priority = 0;
const addToBeginning = true;
let configuration = {
maxImagesToPrefetch: Infinity,
preserveExistingPool: false,
};
let resetPrefetchTimeout;
const resetPrefetchDelay = 10;
function range(lowEnd, highEnd) {
// Javascript version of Python's range function
// http://stackoverflow.com/questions/3895478/does-javascript-have-a-method-like-range-to-generate-an-array-based-on-suppl
lowEnd = Math.round(lowEnd) || 0;
highEnd = Math.round(highEnd) || 0;
const arr = [];
let c = highEnd - lowEnd + 1;
if (c <= 0) {
return arr;
}
while (c--) {
arr[c] = highEnd--;
}
return arr;
}
function nearestIndex(arr, x) {
// Return index of nearest values in array
// http://stackoverflow.com/questions/25854212/return-index-of-nearest-values-in-an-array
let low = 0;
let high = arr.length - 1;
arr.forEach((v, idx) => {
if (v < x) {
low = Math.max(idx, low);
} else if (v > x) {
high = Math.min(idx, high);
}
});
return { low, high };
}
function prefetch(element) {
// Check to make sure stack data exists
const stackData = getToolState(element, 'stack');
if (!stackData || !stackData.data || !stackData.data.length) {
return;
}
const stack = stackData.data[0];
// Get the stackPrefetch tool data
const stackPrefetchData = getToolState(element, toolName);
if (!stackPrefetchData) {
return;
}
const stackPrefetch = stackPrefetchData.data[0] || {};
// If all the requests are complete, disable the stackPrefetch tool
if (
!stackPrefetch.indicesToRequest ||
!stackPrefetch.indicesToRequest.length
) {
stackPrefetch.enabled = false;
}
// Make sure the tool is still enabled
if (stackPrefetch.enabled === false) {
return;
}
// Remove an imageIdIndex from the list of indices to request
// This fires when the individual image loading deferred is resolved
function removeFromList(imageIdIndex) {
const index = stackPrefetch.indicesToRequest.indexOf(imageIdIndex);
if (index > -1) {
// Don't remove last element if imageIdIndex not found
stackPrefetch.indicesToRequest.splice(index, 1);
}
}
// Remove all already cached images from the
// IndicesToRequest array
stackPrefetchData.data[0].indicesToRequest.sort((a, b) => a - b);
const indicesToRequestCopy = stackPrefetch.indicesToRequest.slice();
indicesToRequestCopy.forEach(function(imageIdIndex) {
const imageId = stack.imageIds[imageIdIndex];
if (!imageId) {
return;
}
const imageLoadObject = external.cornerstone.imageCache.getImageLoadObject(
imageId
);
if (imageLoadObject) {
removeFromList(imageIdIndex);
}
});
// Stop here if there are no images left to request
// After those in the cache have been removed
if (!stackPrefetch.indicesToRequest.length) {
return;
}
// Clear the requestPool of prefetch requests, if needed.
if (!configuration.preserveExistingPool) {
external.cornerstone.imageLoadPoolManager.clearRequestStack(requestType);
}
// Identify the nearest imageIdIndex to the currentImageIdIndex
const nearest = nearestIndex(
stackPrefetch.indicesToRequest,
stack.currentImageIdIndex
);
let imageId;
let nextImageIdIndex;
const preventCache = false;
function doneCallback(image) {
logger.log('prefetch done: %s', image.imageId);
const imageIdIndex = stack.imageIds.indexOf(image.imageId);
removeFromList(imageIdIndex);
triggerEvent(element, EVENTS.STACK_PREFETCH_IMAGE_LOADED, {
element,
imageId: image.imageId,
imageIndex: imageIdIndex,
stackPrefetch,
stack,
});
// If there are no more images to fetch
if (
!(
stackPrefetch.indicesToRequest &&
stackPrefetch.indicesToRequest.length > 0
)
) {
triggerEvent(element, EVENTS.STACK_PREFETCH_DONE, {
element,
stackPrefetch,
stack,
});
}
}
// Retrieve the errorLoadingHandler if one exists
const errorLoadingHandler = loadHandlerManager.getErrorLoadingHandler(
element
);
function failCallback(error) {
logger.log('prefetch errored: %o', error);
if (errorLoadingHandler) {
errorLoadingHandler(element, imageId, error, 'stackPrefetch');
}
}
// Prefetch images around the current image (before and after)
let lowerIndex = nearest.low;
let higherIndex = nearest.high;
const imageIdsToPrefetch = [];
while (
lowerIndex >= 0 ||
higherIndex < stackPrefetch.indicesToRequest.length
) {
const currentIndex = stack.currentImageIdIndex;
const shouldSkipLower =
currentIndex - stackPrefetch.indicesToRequest[lowerIndex] >
configuration.maxImagesToPrefetch;
const shouldSkipHigher =
stackPrefetch.indicesToRequest[higherIndex] - currentIndex >
configuration.maxImagesToPrefetch;
const shouldLoadLower = !shouldSkipLower && lowerIndex >= 0;
const shouldLoadHigher =
!shouldSkipHigher && higherIndex < stackPrefetch.indicesToRequest.length;
if (!shouldLoadHigher && !shouldLoadLower) {
break;
}
if (shouldLoadLower) {
nextImageIdIndex = stackPrefetch.indicesToRequest[lowerIndex--];
imageId = stack.imageIds[nextImageIdIndex];
imageIdsToPrefetch.push(imageId);
}
if (shouldLoadHigher) {
nextImageIdIndex = stackPrefetch.indicesToRequest[higherIndex++];
imageId = stack.imageIds[nextImageIdIndex];
imageIdsToPrefetch.push(imageId);
}
}
let requestFn;
const options = {
addToBeginning,
priority,
requestType,
};
if (preventCache) {
requestFn = id => external.cornerstone.loadImage(id, options);
} else {
requestFn = id => external.cornerstone.loadAndCacheImage(id, options);
}
imageIdsToPrefetch.reverse().forEach(imageId => {
external.cornerstone.imageLoadPoolManager.addRequest(
requestFn.bind(null, imageId),
requestType,
// Additional details
{
imageId,
},
priority,
addToBeginning
);
});
}
function getPromiseRemovedHandler(element) {
return function(e) {
const eventData = e.detail;
// When an imagePromise has been pushed out of the cache, re-add its index
// It to the indicesToRequest list so that it will be retrieved later if the
// CurrentImageIdIndex is changed to an image nearby
let stackData;
try {
// It will throw an exception in some cases (eg: thumbnails)
stackData = getToolState(element, 'stack');
} catch (error) {
return;
}
if (!stackData || !stackData.data || !stackData.data.length) {
return;
}
const stack = stackData.data[0];
const imageIdIndex = stack.imageIds.indexOf(eventData.imageId);
// Make sure the image that was removed is actually in this stack
// Before adding it to the indicesToRequest array
if (imageIdIndex < 0) {
return;
}
const stackPrefetchData = getToolState(element, toolName);
if (
!stackPrefetchData ||
!stackPrefetchData.data ||
!stackPrefetchData.data.length
) {
return;
}
stackPrefetchData.data[0].indicesToRequest.push(imageIdIndex);
};
}
function onImageUpdated(e) {
// Start prefetching again (after a delay)
// When the user has scrolled to a new image
clearTimeout(resetPrefetchTimeout);
resetPrefetchTimeout = setTimeout(function() {
const element = e.target;
// If playClip is enabled and the user loads a different series in the viewport
// An exception will be thrown because the element will not be enabled anymore
try {
prefetch(element);
} catch (error) {
return;
}
}, resetPrefetchDelay);
}
function enable(element) {
// Clear old prefetch data. Skipping this can cause problems when changing the series inside an element
const stackPrefetchDataArray = getToolState(element, toolName);
stackPrefetchDataArray.data = [];
// First check that there is stack data available
const stackData = getToolState(element, 'stack');
if (!stackData || !stackData.data || !stackData.data.length) {
return;
}
const stack = stackData.data[0];
// Check if we are allowed to cache images in this stack
if (stack.preventCache === true) {
logger.warn(
'A stack that should not be cached was given the stackPrefetch'
);
return;
}
// Use the currentImageIdIndex from the stack as the initalImageIdIndex
const stackPrefetchData = {
indicesToRequest: range(0, stack.imageIds.length - 1),
enabled: true,
direction: 1,
};
// Remove the currentImageIdIndex from the list to request
const indexOfCurrentImage = stackPrefetchData.indicesToRequest.indexOf(
stack.currentImageIdIndex
);
stackPrefetchData.indicesToRequest.splice(indexOfCurrentImage, 1);
addToolState(element, toolName, stackPrefetchData);
prefetch(element);
element.removeEventListener(
external.cornerstone.EVENTS.NEW_IMAGE,
onImageUpdated
);
element.addEventListener(
external.cornerstone.EVENTS.NEW_IMAGE,
onImageUpdated
);
const promiseRemovedHandler = getPromiseRemovedHandler(element);
external.cornerstone.events.removeEventListener(
external.cornerstone.EVENTS.IMAGE_CACHE_PROMISE_REMOVED,
promiseRemovedHandler
);
external.cornerstone.events.addEventListener(
external.cornerstone.EVENTS.IMAGE_CACHE_PROMISE_REMOVED,
promiseRemovedHandler
);
}
function disable(element) {
clearTimeout(resetPrefetchTimeout);
element.removeEventListener(
external.cornerstone.EVENTS.NEW_IMAGE,
onImageUpdated
);
const promiseRemovedHandler = getPromiseRemovedHandler(element);
external.cornerstone.events.removeEventListener(
external.cornerstone.EVENTS.IMAGE_CACHE_PROMISE_REMOVED,
promiseRemovedHandler
);
const stackPrefetchData = getToolState(element, toolName);
// If there is actually something to disable, disable it
if (stackPrefetchData && stackPrefetchData.data.length) {
stackPrefetchData.data[0].enabled = false;
// Clear current prefetch requests from the requestPool
external.cornerstone.imageLoadPoolManager.clearRequestStack(requestType);
}
}
function getConfiguration() {
return configuration;
}
function setConfiguration(config) {
configuration = config;
}
// Module/private exports
const stackPrefetch = {
enable,
disable,
getConfiguration,
setConfiguration,
};
export default stackPrefetch;