2022-11-04 00:55:07 +08:00
|
|
|
import { iterator } from "./iterator";
|
2023-10-26 04:27:07 +08:00
|
|
|
function paginate(octokit, route, parameters, mapFn) {
|
|
|
|
if (typeof parameters === "function") {
|
|
|
|
mapFn = parameters;
|
|
|
|
parameters = void 0;
|
|
|
|
}
|
|
|
|
return gather(
|
|
|
|
octokit,
|
|
|
|
[],
|
|
|
|
iterator(octokit, route, parameters)[Symbol.asyncIterator](),
|
|
|
|
mapFn
|
|
|
|
);
|
2022-11-04 00:55:07 +08:00
|
|
|
}
|
2023-10-26 04:27:07 +08:00
|
|
|
function gather(octokit, results, iterator2, mapFn) {
|
|
|
|
return iterator2.next().then((result) => {
|
|
|
|
if (result.done) {
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
let earlyExit = false;
|
|
|
|
function done() {
|
|
|
|
earlyExit = true;
|
|
|
|
}
|
|
|
|
results = results.concat(
|
|
|
|
mapFn ? mapFn(result.value, done) : result.value.data
|
|
|
|
);
|
|
|
|
if (earlyExit) {
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
return gather(octokit, results, iterator2, mapFn);
|
|
|
|
});
|
2022-11-04 00:55:07 +08:00
|
|
|
}
|
2023-10-26 04:27:07 +08:00
|
|
|
export {
|
|
|
|
paginate
|
|
|
|
};
|