Skip to content

Commit 42a8aec

Browse files
authored
feat: Add back the addToBeginning to the PoolManagers (#558)
1 parent 03cdd71 commit 42a8aec

File tree

1 file changed

+88
-69
lines changed

1 file changed

+88
-69
lines changed

Diff for: src/requestPool/RequestPoolManager.ts

+88-69
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,57 @@ type AdditionalDetails = {
1414
thumbnail: { [key: number]: [] }
1515
prefetch: { [key: number]: [] }
1616
}
17-
17+
1818
class RequestPoolManager {
1919
// priority is fixed for interaction and thumbnail to be 0, however,
2020
// the priority of prefetch can be configured and it can have priorities other
2121
// than 0 (highest priority)
22-
22+
2323
// TODO: Some of this stuff shouldn't be public but it's easier right now
2424
private requestPool: RequestPool;
2525
private awake: boolean;
26-
private numRequests: { interaction: number, thumbnail: number, prefetch: number };
27-
public maxNumRequests: { interaction: number, thumbnail: number, prefetch: number };
26+
private numRequests: {
27+
interaction: number;
28+
thumbnail: number;
29+
prefetch: number;
30+
};
31+
public maxNumRequests: {
32+
interaction: number;
33+
thumbnail: number;
34+
prefetch: number;
35+
};
2836
public grabDelay: number;
2937
private timeoutHandle: number;
30-
38+
3139
constructor() {
3240
this.requestPool = {
3341
interaction: { 0: [] },
3442
thumbnail: { 0: [] },
3543
prefetch: { 0: [] },
36-
}
37-
44+
};
45+
3846
this.awake = false;
3947
this.grabDelay = 5;
40-
41-
this.numRequests = {
48+
49+
this.numRequests = {
4250
interaction: 0,
4351
thumbnail: 0,
4452
prefetch: 0,
4553
};
46-
54+
4755
this.maxNumRequests = {
4856
interaction: 6,
4957
thumbnail: 6,
5058
prefetch: 5,
51-
}
59+
};
5260
}
53-
61+
5462
destroy() {
5563
if (this.timeoutHandle) {
56-
window.clearTimeout(this.timeoutHandle)
64+
window.clearTimeout(this.timeoutHandle);
5765
}
5866
}
59-
67+
6068
/**
6169
* Adds the requests to the pool of requests.
6270
*
@@ -75,30 +83,37 @@ type AdditionalDetails = {
7583
requestFn: () => Promise<void>,
7684
type: string,
7785
additionalDetails: Record<string, unknown>,
78-
priority = 0
86+
priority = 0,
87+
addToBeginning = false
7988
): void {
8089
// Describe the request
8190
const requestDetails: RequestDetailsInterface = {
8291
requestFn,
8392
type,
8493
additionalDetails,
85-
}
86-
94+
};
95+
8796
// Check if the priority group exists on the request type
8897
if (this.requestPool[type][priority] === undefined) {
89-
this.requestPool[type][priority] = []
98+
this.requestPool[type][priority] = [];
9099
}
91-
100+
92101
// Adding the request to the correct priority group of the request type
93-
this.requestPool[type][priority].push(requestDetails)
94-
102+
if (addToBeginning) {
103+
// Add it to the beginning of the stack
104+
this.requestPool[type][priority].unshift(requestDetails);
105+
} else {
106+
// Add it to the end of the stack
107+
this.requestPool[type][priority].push(requestDetails);
108+
}
109+
95110
// Wake up
96111
if (!this.awake) {
97-
this.awake = true
98-
this.startGrabbing()
112+
this.awake = true;
113+
this.startGrabbing();
99114
}
100115
}
101-
116+
102117
/**
103118
* Filter the requestPoolManager's pool of request based on the result of
104119
* provided filter function. The provided filter function needs to return false or true
@@ -110,17 +125,17 @@ type AdditionalDetails = {
110125
filterFunction: (requestDetails: RequestDetailsInterface) => boolean
111126
): void {
112127
Object.keys(this.requestPool).forEach((type: string) => {
113-
const requestType = this.requestPool[type]
128+
const requestType = this.requestPool[type];
114129
Object.keys(requestType).forEach((priority) => {
115130
requestType[priority] = requestType[priority].filter(
116131
(requestDetails: RequestDetailsInterface) => {
117-
return filterFunction(requestDetails)
132+
return filterFunction(requestDetails);
118133
}
119-
)
120-
})
121-
})
134+
);
135+
});
136+
});
122137
}
123-
138+
124139
/**
125140
* Clears the requests specific to the provided type. For instance, the
126141
* pool of requests of type 'interaction' can be cleared via this function.
@@ -131,110 +146,114 @@ type AdditionalDetails = {
131146
*/
132147
clearRequestStack(type: string): void {
133148
if (!this.requestPool[type]) {
134-
throw new Error(`No category for the type ${type} found`)
149+
throw new Error(`No category for the type ${type} found`);
135150
}
136-
this.requestPool[type] = { 0: [] }
151+
this.requestPool[type] = { 0: [] };
137152
}
138-
153+
139154
sendRequest({ requestFn, type }: RequestDetailsInterface) {
140155
// Increment the number of current requests of this type
141-
this.numRequests[type]++
142-
this.awake = true
143-
156+
this.numRequests[type]++;
157+
this.awake = true;
158+
144159
requestFn().finally(() => {
145-
this.numRequests[type]--
146-
147-
this.startAgain()
148-
})
160+
this.numRequests[type]--;
161+
162+
this.startAgain();
163+
});
149164
}
150-
165+
151166
startGrabbing(): void {
152167
// Begin by grabbing X images
153-
168+
154169
// TODO: This is the reason things aren't going as fast as expected
155170
// const maxSimultaneousRequests = getMaxSimultaneousRequests()
156171
// this.maxNumRequests = {
157172
// interaction: Math.max(maxSimultaneousRequests, 1),
158173
// thumbnail: Math.max(maxSimultaneousRequests - 2, 1),
159174
// prefetch: Math.max(maxSimultaneousRequests - 1, 1),
160175
// }
161-
176+
162177
const maxRequests =
163-
this.maxNumRequests.interaction + this.maxNumRequests.thumbnail + this.maxNumRequests.prefetch
178+
this.maxNumRequests.interaction +
179+
this.maxNumRequests.thumbnail +
180+
this.maxNumRequests.prefetch;
164181
const currentRequests =
165-
this.numRequests.interaction + this.numRequests.thumbnail + this.numRequests.prefetch
166-
const requestsToSend = maxRequests - currentRequests
182+
this.numRequests.interaction +
183+
this.numRequests.thumbnail +
184+
this.numRequests.prefetch;
185+
const requestsToSend = maxRequests - currentRequests;
167186
for (let i = 0; i < requestsToSend; i++) {
168-
const requestDetails = this.getNextRequest()
187+
const requestDetails = this.getNextRequest();
169188
if (requestDetails === false) {
170189
break;
171190
} else if (requestDetails) {
172-
this.sendRequest(requestDetails)
191+
this.sendRequest(requestDetails);
173192
}
174193
}
175194
}
176-
195+
177196
startAgain(): void {
178197
if (!this.awake) {
179-
return
198+
return;
180199
}
181-
200+
182201
if (this.grabDelay) {
183202
this.timeoutHandle = window.setTimeout(() => {
184-
this.startGrabbing()
185-
}, this.grabDelay)
203+
this.startGrabbing();
204+
}, this.grabDelay);
186205
} else {
187-
this.startGrabbing()
206+
this.startGrabbing();
188207
}
189208
}
190-
209+
191210
getSortedPriorityGroups(type: string): Array<number> {
192211
const priorities = Object.keys(this.requestPool[type])
193212
.map(Number)
194213
.filter((priority) => this.requestPool[type][priority].length)
195-
.sort()
196-
return priorities
214+
.sort();
215+
return priorities;
197216
}
198-
217+
199218
getNextRequest(): RequestDetailsInterface | false {
200-
const interactionPriorities = this.getSortedPriorityGroups('interaction')
219+
const interactionPriorities = this.getSortedPriorityGroups('interaction');
201220
for (const priority of interactionPriorities) {
202221
if (
203222
this.requestPool.interaction[priority].length &&
204223
this.numRequests.interaction < this.maxNumRequests.interaction
205224
) {
206-
return this.requestPool.interaction[priority].shift()
225+
return this.requestPool.interaction[priority].shift();
207226
}
208227
}
209-
const thumbnailPriorities = this.getSortedPriorityGroups('thumbnail')
228+
const thumbnailPriorities = this.getSortedPriorityGroups('thumbnail');
210229
for (const priority of thumbnailPriorities) {
211230
if (
212231
this.requestPool.thumbnail[priority].length &&
213232
this.numRequests.thumbnail < this.maxNumRequests.thumbnail
214233
) {
215-
return this.requestPool.thumbnail[priority].shift()
234+
return this.requestPool.thumbnail[priority].shift();
216235
}
217236
}
218-
const prefetchPriorities = this.getSortedPriorityGroups('prefetch')
237+
const prefetchPriorities = this.getSortedPriorityGroups('prefetch');
219238
for (const priority of prefetchPriorities) {
220239
if (
221240
this.requestPool.prefetch[priority].length &&
222241
this.numRequests.prefetch < this.maxNumRequests.prefetch
223242
) {
224-
return this.requestPool.prefetch[priority].shift()
243+
return this.requestPool.prefetch[priority].shift();
225244
}
226245
}
227-
246+
228247
if (
229248
!interactionPriorities.length &&
230249
!thumbnailPriorities.length &&
231250
!prefetchPriorities.length
232251
) {
233-
this.awake = false
252+
this.awake = false;
234253
}
235-
return false
254+
return false;
236255
}
237-
256+
238257
/**
239258
* Returns the request pool containing different categories, their priority and
240259
* the added request details.
@@ -243,7 +262,7 @@ type AdditionalDetails = {
243262
* @category requestPool
244263
*/
245264
getRequestPool(): RequestPool {
246-
return this.requestPool
265+
return this.requestPool;
247266
}
248267
}
249268

0 commit comments

Comments
 (0)