forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-cache-common.ts
More file actions
186 lines (151 loc) · 5.49 KB
/
image-cache-common.ts
File metadata and controls
186 lines (151 loc) · 5.49 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
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
import definition = require("ui/image-cache");
import observable = require("data/observable");
import imageSource = require("image-source");
export interface DownloadRequest {
url: string;
key: string;
completed?: (image: any, key: string) => void;
}
export class Cache extends observable.Observable implements definition.Cache {
public static downloadedEvent = "downloaded";
public placeholder: imageSource.ImageSource;
public maxRequests = 5;
private _enabled = true;
private _pendingDownloads = {};
private _queue: Array<DownloadRequest> = [];
private _currentDownloads = 0;
public enableDownload() {
if (this._enabled) {
return;
}
// schedule all pending downloads
this._enabled = true;
var request: DownloadRequest;
while (this._queue.length > 0 && this._currentDownloads < this.maxRequests) {
request = this._queue.pop();
if (!(request.key in this._pendingDownloads)) {
this._download(request);
}
}
}
public disableDownload() {
if (!this._enabled) {
return;
}
this._enabled = false;
}
public push(request: DownloadRequest): void {
this._addRequest(request, true);
}
public enqueue(request: DownloadRequest): void {
this._addRequest(request, false);
}
private _addRequest(request: DownloadRequest, onTop: boolean): void {
if (request.key in this._pendingDownloads) {
var existingRequest = <DownloadRequest>this._pendingDownloads[request.key];
this._mergeRequests(existingRequest, request);
}
else {
// TODO: Potential performance bottleneck - traversing the whole queue on each download request.
var queueRequest: DownloadRequest;
for (var i = 0; i < this._queue.length; i++) {
if (this._queue[i].key === request.key) {
queueRequest = this._queue[i];
break;
}
}
if (queueRequest) {
this._mergeRequests(queueRequest, request);
}
else {
if (this._shouldDownload(request, onTop)) {
this._download(request);
}
}
}
}
private _mergeRequests(existingRequest: DownloadRequest, newRequest: DownloadRequest) {
if (existingRequest.completed) {
if (newRequest.completed) {
var existingCompleted = existingRequest.completed;
var stackCompleted = function (result: imageSource.ImageSource, key: string) {
existingCompleted(result, key);
newRequest.completed(result, key);
}
existingRequest.completed = stackCompleted;
}
}
else {
existingRequest.completed = newRequest.completed;
}
}
public get(key: string): any {
// This method is intended to be overridden by the android and ios implementations
throw new Error("Abstract");
}
public set(key: string, image: any): void {
// This method is intended to be overridden by the android and ios implementations
throw new Error("Abstract");
}
public remove(key: string): void {
// This method is intended to be overridden by the android and ios implementations
throw new Error("Abstract");
}
public clear() {
// This method is intended to be overridden by the android and ios implementations
throw new Error("Abstract");
}
/* tslint:disable:no-unused-variable */
public _downloadCore(request: definition.DownloadRequest) {
// This method is intended to be overridden by the android and ios implementations
throw new Error("Abstract");
}
/* tslint:enable:no-unused-variable */
public _onDownloadCompleted(key: string, image: any) {
var request = <DownloadRequest>this._pendingDownloads[key];
if (request.key && image) {
this.set(request.key, image);
}
this._currentDownloads--;
if (request.completed) {
request.completed(image, request.key);
}
if (this.hasListeners(Cache.downloadedEvent)) {
this.notify({
eventName: Cache.downloadedEvent,
object: this,
key: key,
image: image
});
}
delete this._pendingDownloads[request.key];
this._updateQueue();
}
private _shouldDownload(request: definition.DownloadRequest, onTop: boolean): boolean {
if (this.get(request.key) || request.key in this._pendingDownloads) {
return false;
}
if (this._currentDownloads >= this.maxRequests || !this._enabled) {
if (onTop) {
this._queue.push(request);
}
else {
this._queue.unshift(request);
}
return false;
}
return true;
}
private _download(request: definition.DownloadRequest) {
this._currentDownloads++;
this._pendingDownloads[request.key] = request;
this._downloadCore(request);
}
private _updateQueue() {
if (!this._enabled || this._queue.length === 0 || this._currentDownloads === this.maxRequests) {
return;
}
var request = this._queue.pop();
this._download(request);
}
}