-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Languages: Page through to load all configured languages #22765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
....Web.UI.Client/src/packages/core/utils/pagination/offset/fetch-all-pages.function.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import { expect } from '@open-wc/testing'; | ||
| import { fetchAllPages } from './fetch-all-pages.function.js'; | ||
| import type { UmbDataSourceResponse, UmbPagedModel } from '@umbraco-cms/backoffice/repository'; | ||
|
|
||
| interface TestItem { | ||
| id: number; | ||
| } | ||
|
|
||
| const buildFakeFetcher = (allItems: Array<TestItem>) => { | ||
| const calls: Array<{ skip: number; take: number }> = []; | ||
| const fetchPage = async (skip: number, take: number) => { | ||
| calls.push({ skip, take }); | ||
| return { data: { items: allItems.slice(skip, skip + take), total: allItems.length } }; | ||
| }; | ||
| return { fetchPage, calls }; | ||
| }; | ||
|
|
||
| describe('fetchAllPages', () => { | ||
| it('returns all items in a single call when total <= take', async () => { | ||
| const all: Array<TestItem> = [{ id: 1 }, { id: 2 }]; | ||
| const { fetchPage, calls } = buildFakeFetcher(all); | ||
|
|
||
| const { data } = await fetchAllPages(fetchPage, 10); | ||
|
|
||
| expect(data?.items).to.eql(all); | ||
| expect(data?.total).to.equal(2); | ||
| expect(calls).to.have.lengthOf(1); | ||
| expect(calls[0]).to.eql({ skip: 0, take: 10 }); | ||
| }); | ||
|
|
||
| it('pages through and returns all items when total > take', async () => { | ||
| const all: Array<TestItem> = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }]; | ||
| const { fetchPage, calls } = buildFakeFetcher(all); | ||
|
|
||
| const { data } = await fetchAllPages(fetchPage, 2); | ||
|
|
||
| expect(data?.items).to.eql(all); | ||
| expect(data?.total).to.equal(5); | ||
| expect(calls.map((c) => c.skip)).to.eql([0, 2, 4]); | ||
| }); | ||
|
|
||
| it('makes no extra call when the last page exactly fills `take`', async () => { | ||
| const all: Array<TestItem> = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }]; | ||
| const { fetchPage, calls } = buildFakeFetcher(all); | ||
|
|
||
| const { data } = await fetchAllPages(fetchPage, 2); | ||
|
|
||
| expect(data?.items).to.eql(all); | ||
| expect(data?.total).to.equal(4); | ||
| expect(calls).to.have.lengthOf(2); | ||
| }); | ||
|
|
||
| it('returns an empty result when there are no items', async () => { | ||
| const { fetchPage, calls } = buildFakeFetcher([]); | ||
|
|
||
| const { data } = await fetchAllPages(fetchPage, 100); | ||
|
|
||
| expect(data?.items).to.eql([]); | ||
| expect(data?.total).to.equal(0); | ||
| expect(calls).to.have.lengthOf(1); | ||
| }); | ||
|
|
||
| it('returns the error and stops paging when a page fetch fails', async () => { | ||
| let callCount = 0; | ||
| const fetchPage = async (skip: number, take: number) => { | ||
| callCount++; | ||
| if (callCount === 1) { | ||
| return { data: { items: [{ id: 1 }, { id: 2 }] as Array<TestItem>, total: 100 } }; | ||
| } | ||
| return { error: new Error('boom') }; | ||
| }; | ||
|
|
||
| const { data, error } = await fetchAllPages<TestItem>(fetchPage, 2); | ||
|
|
||
| expect(data).to.be.undefined; | ||
| expect(error).to.exist; | ||
| expect(callCount).to.equal(2); | ||
| }); | ||
|
|
||
| it('returns a synthesised error when the fetcher returns neither data nor error', async () => { | ||
| const fetchPage = async () => ({}) as UmbDataSourceResponse<UmbPagedModel<TestItem>>; | ||
|
|
||
| const { data, error } = await fetchAllPages<TestItem>(fetchPage, 2); | ||
|
|
||
| expect(data).to.be.undefined; | ||
| expect(error).to.be.an.instanceOf(Error); | ||
| }); | ||
|
|
||
| it('rejects when `take` is not a positive finite number', async () => { | ||
| const { fetchPage } = buildFakeFetcher([{ id: 1 }, { id: 2 }]); | ||
|
|
||
| for (const invalid of [0, -1, NaN, Number.POSITIVE_INFINITY]) { | ||
| let thrown: unknown; | ||
| try { | ||
| await fetchAllPages(fetchPage, invalid); | ||
| } catch (e) { | ||
| thrown = e; | ||
| } | ||
| expect(thrown, `take=${invalid}`).to.be.an.instanceOf(RangeError); | ||
| } | ||
| }); | ||
|
|
||
| it('returns an error if the server delivers an empty page before reaching the reported total', async () => { | ||
| // Surfaces (rather than masks) a server that reports more items than it returns. Also guards against | ||
| // an infinite loop if `total` and the actual items disagree. | ||
| let callCount = 0; | ||
| const fetchPage = async (skip: number, take: number) => { | ||
| callCount++; | ||
| if (callCount === 1) { | ||
| return { data: { items: [{ id: 1 }, { id: 2 }] as Array<TestItem>, total: 10 } }; | ||
| } | ||
| return { data: { items: [] as Array<TestItem>, total: 10 } }; | ||
| }; | ||
|
|
||
| const { data, error } = await fetchAllPages<TestItem>(fetchPage, 2); | ||
|
|
||
| expect(data).to.be.undefined; | ||
| expect(error).to.be.an.instanceOf(Error); | ||
| expect(callCount).to.equal(2); | ||
| }); | ||
| }); |
59 changes: 59 additions & 0 deletions
59
...braco.Web.UI.Client/src/packages/core/utils/pagination/offset/fetch-all-pages.function.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import type { UmbDataSourceResponse, UmbPagedModel } from '@umbraco-cms/backoffice/repository'; | ||
|
|
||
| /** | ||
| * A function that returns a single page of an offset-paginated collection. | ||
| * @template T - The type of items in the page. | ||
| */ | ||
| export type UmbOffsetPageFetcher<T> = ( | ||
| skip: number, | ||
| take: number, | ||
| ) => Promise<UmbDataSourceResponse<UmbPagedModel<T>>>; | ||
|
|
||
| /** | ||
| * Pages through an offset-paginated data source, accumulating every item until `total` has been reached. | ||
| * Use when a caller genuinely needs the full set rather than a single page — for example, populating a | ||
| * dropdown of every configured language. Returns the same `{ data: { items, total } }` shape as a | ||
| * single-page fetch, or `{ error }` if any page fails. | ||
| * | ||
| * If the server reports a higher `total` than it actually delivers — i.e. an empty page is returned | ||
| * before `allItems.length` reaches `total` — the function fails with an error rather than silently | ||
| * truncating, since a partial "fetch all" result would mislead the caller. | ||
| * @param {UmbOffsetPageFetcher} fetchPage - Called once per page with the current `skip` and `take`. | ||
| * @param {number} take - Page size used for every request. Must be a positive finite number. | ||
| * @returns {Promise} A promise resolving to all items, or the first error encountered. | ||
| * @throws {RangeError} If `take` is not a positive finite number. | ||
| */ | ||
| export async function fetchAllPages<T>( | ||
| fetchPage: UmbOffsetPageFetcher<T>, | ||
| take: number, | ||
| ): Promise<UmbDataSourceResponse<UmbPagedModel<T>>> { | ||
| if (!Number.isFinite(take) || take <= 0) { | ||
| throw new RangeError(`fetchAllPages: \`take\` must be a positive finite number, got ${take}.`); | ||
| } | ||
|
|
||
| const allItems: Array<T> = []; | ||
| let skip = 0; | ||
| let total = Number.POSITIVE_INFINITY; | ||
|
|
||
| while (allItems.length < total) { | ||
| const { data, error } = await fetchPage(skip, take); | ||
| if (error) return { error }; | ||
| if (!data) return { error: new Error('fetchAllPages: page fetcher returned neither data nor error.') }; | ||
|
|
||
| // If the server reports more items than it delivers, fail rather than silently truncating — | ||
| // also guards against an infinite loop on a misbehaving source. | ||
| if (data.items.length === 0 && allItems.length < data.total) { | ||
| return { | ||
| error: new Error( | ||
| `fetchAllPages: page fetcher returned an empty page after ${allItems.length} items but reported a total of ${data.total}.`, | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| allItems.push(...data.items); | ||
| total = data.total; | ||
| skip += data.items.length; | ||
| } | ||
|
|
||
| return { data: { items: allItems, total: allItems.length } }; | ||
| } | ||
|
Check warning on line 59 in src/Umbraco.Web.UI.Client/src/packages/core/utils/pagination/offset/fetch-all-pages.function.ts
|
||
1 change: 1 addition & 0 deletions
1
src/Umbraco.Web.UI.Client/src/packages/core/utils/pagination/offset/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from './fetch-all-pages.function.js'; | ||
| export * from './is-offset-request.guard.js'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.