From dadd8fe571a1cab41705c53f72ae5d82d5c6807b Mon Sep 17 00:00:00 2001 From: Ray Foss Date: Thu, 4 May 2023 23:59:08 -0500 Subject: [PATCH 1/5] new: next --- src/hooks/next.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/hooks/next.ts diff --git a/src/hooks/next.ts b/src/hooks/next.ts new file mode 100644 index 00000000..22ad18fa --- /dev/null +++ b/src/hooks/next.ts @@ -0,0 +1,20 @@ +import type { HookContext } from '@feathersjs/feathers'; + +/** + * Set a generator on paginated results that returns an iterator with result arrays + * @see https://hooks-common.feathersjs.com/hooks.html#next + */ + +async function* generateNext({ result, service, args = { query: {} } }) { + yield result.data + let pager = result + while (true) { + args.query.$skip = (pager.limit + pager.skip) % pager.total + pager = await service.find(args) + yield pager.data + if (pager.total < pager.limit + pager.skip) break + } +} +export function next(ctx) { + return (context: H) => (context.result.next = context.result.skip === 0 ? next(context) : undefined) +} From 08e399cf46765a3c79d1de147bfb628fdc394465 Mon Sep 17 00:00:00 2001 From: Ray Foss Date: Fri, 5 May 2023 00:24:00 -0500 Subject: [PATCH 2/5] improve loop --- src/hooks/next.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/hooks/next.ts b/src/hooks/next.ts index 22ad18fa..89596f9e 100644 --- a/src/hooks/next.ts +++ b/src/hooks/next.ts @@ -8,11 +8,10 @@ import type { HookContext } from '@feathersjs/feathers'; async function* generateNext({ result, service, args = { query: {} } }) { yield result.data let pager = result - while (true) { + while (pager.data.length > 0) { args.query.$skip = (pager.limit + pager.skip) % pager.total pager = await service.find(args) yield pager.data - if (pager.total < pager.limit + pager.skip) break } } export function next(ctx) { From 2f7b52761ee9fdeb1a1e426a93d1b00ea12926af Mon Sep 17 00:00:00 2001 From: Ray Foss Date: Fri, 5 May 2023 00:26:08 -0500 Subject: [PATCH 3/5] remove unnecessary code --- src/hooks/next.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/next.ts b/src/hooks/next.ts index 89596f9e..71b175d6 100644 --- a/src/hooks/next.ts +++ b/src/hooks/next.ts @@ -9,7 +9,7 @@ async function* generateNext({ result, service, args = { query: {} } }) { yield result.data let pager = result while (pager.data.length > 0) { - args.query.$skip = (pager.limit + pager.skip) % pager.total + args.query.$skip = pager.limit + pager.skip pager = await service.find(args) yield pager.data } From fb87ad88f21acbba9a7ef90bc8a76527f8ed724e Mon Sep 17 00:00:00 2001 From: Ray Foss Date: Sat, 20 May 2023 16:24:54 +0900 Subject: [PATCH 4/5] Update next.ts --- src/hooks/next.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/next.ts b/src/hooks/next.ts index 71b175d6..d5c5b340 100644 --- a/src/hooks/next.ts +++ b/src/hooks/next.ts @@ -15,5 +15,5 @@ async function* generateNext({ result, service, args = { query: {} } }) { } } export function next(ctx) { - return (context: H) => (context.result.next = context.result.skip === 0 ? next(context) : undefined) + return (context: H) => (context.result.next = context.result.skip === 0 ? generateNext(context) : undefined) } From d0cf6e0d4cc843397ae6ae908d9fa9273c225144 Mon Sep 17 00:00:00 2001 From: Ray Foss Date: Sat, 20 May 2023 16:25:40 +0900 Subject: [PATCH 5/5] Update next.ts --- src/hooks/next.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/next.ts b/src/hooks/next.ts index d5c5b340..5c5ccbd9 100644 --- a/src/hooks/next.ts +++ b/src/hooks/next.ts @@ -14,6 +14,6 @@ async function* generateNext({ result, service, args = { query: {} } }) { yield pager.data } } -export function next(ctx) { +export function next(context) { return (context: H) => (context.result.next = context.result.skip === 0 ? generateNext(context) : undefined) }