Skip to content

Instantly share code, notes, and snippets.

@aranajhonny
Last active March 14, 2023 14:36
Show Gist options
  • Save aranajhonny/f8f3a8ead59fbd71a5ea0acc4ab61b97 to your computer and use it in GitHub Desktop.
Save aranajhonny/f8f3a8ead59fbd71a5ea0acc4ab61b97 to your computer and use it in GitHub Desktop.
export const RepositoryCollection = {
...
allItems: async ({ self, args }) => {
// const repo = await $iter2(self, `{ name, description }`, { pageSize: 3 })[Symbol.asyncIterator]();
// console.log(JSON.stringify(await repo.next()));
// console.log(JSON.stringify(await repo.next()));
const repos: any[] = []
for await (const repo of $iter(self, `{ name, description }`, { pageSize: 3 })) {
repos.push(repo);
}
return repos;
},
};
const $iter = (gref, query, pageArgs) => {
let currentPage = 1;
let hasNextPage = true;
return {
async *[Symbol.asyncIterator]() {
while (hasNextPage) {
let page;
if (currentPage === 1) {
page = gref.page({ ...pageArgs });
} else {
page = await gref.page({ ...pageArgs }).next;
}
// get data from page ref
const items: any = await page.items.$query(query);
// check items in the page
if (items.length === 0) {
hasNextPage = false;
break;
}
// resolve each item
for (const item of items) {
yield item;
}
currentPage++;
}
},
};
};
// const $iter2 = (self, query, pageArgs) => {
// let currentPage = 1;
// let hasNextPage = true;
// let ref = self;
// let items = [];
// return {
// [Symbol.asyncIterator]() {
// return {
// async next() {
// // If there are no items left in the current page and there are more pages to fetch...
// if (items.length === 0 && hasNextPage) {
// if (currentPage === 1) {
// items = await ref.page({ ...pageArgs }).items().$query(query);
// } else {
// ref = await ref.page({ ...pageArgs }).next();
// items = ref.items().$query(query);
// }
// // there are no more pages to fetch.
// if (items.length === 0) {
// hasNextPage = false;
// return { value: undefined, done: true };
// }
// // Increment the current page counter.
// currentPage++;
// }
// if (items.length > 0) {
// // Remove the next item from the `items` array and return it.
// const item = items.shift();
// return { value: item, done: false };
// }
// // no more items to return
// return { value: undefined, done: true };
// },
// };
// },
// };
// };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment