Skip to content

Commit 63a1cfd

Browse files
committed
use exponential backoff for pagination
1 parent e1ce22f commit 63a1cfd

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/main.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import {
2626
import { translate } from "./translator/translator.ts";
2727

2828
const DICTIONARY_AUTO_PARSE_THRESHOLD = 9000;
29-
const PAGE_SIZE = 100;
29+
const INITIAL_PAGE_SIZE = 100;
30+
const MAX_PAGE_SIZE = 10000;
3031

3132
// never change this
3233
const DICTIONARY_KEY = "dictionary";
@@ -190,6 +191,7 @@ function main() {
190191

191192
// state for output
192193
let output: null | Generator<Result<string>> = null;
194+
let size = 0;
193195

194196
// load custom dictionary
195197
if (!currentDictionary.isError()) {
@@ -254,13 +256,14 @@ function main() {
254256
errorDisplay.innerText = "";
255257
loadMoreButton.style.display = "";
256258
output = translate(inputTextBox.value).iterable();
259+
size = 0;
257260
moreOutput();
258261
}
259262
function moreOutput() {
260263
const errors: Array<ResultError> = [];
261264
let yielded = false;
262265
let i = 0;
263-
while (i < PAGE_SIZE) {
266+
while (i < Math.min(INITIAL_PAGE_SIZE * 2 ** size, MAX_PAGE_SIZE)) {
264267
const next = output!.next();
265268
if (!next.done) {
266269
const result = next.value;
@@ -282,6 +285,9 @@ function main() {
282285
break;
283286
}
284287
}
288+
if (size < Math.log2(MAX_PAGE_SIZE / INITIAL_PAGE_SIZE)) {
289+
size++;
290+
}
285291
if (!yielded) {
286292
switch (errors.length) {
287293
case 0:

0 commit comments

Comments
 (0)