diff --git a/src/collection.ts b/src/collection.ts index f71b873..51714a9 100644 --- a/src/collection.ts +++ b/src/collection.ts @@ -51,6 +51,8 @@ export class Collection< /** Cached validated data */ #data?: Infer #views?: ViewsToQueryMethods + /** The load in progress, shared by concurrent callers */ + #pendingLoad?: Promise<{ data: Infer; views: ViewsToQueryMethods }> /** * Creates a new Collection instance. @@ -180,13 +182,14 @@ export class Collection< /** * Loads and validates data using the configured loader and schema. * Returns cached data if caching is enabled and data was previously loaded. + * Concurrent calls share the load in progress, so the loader runs once. * * @example * ```ts * const data = await posts.hydrate() * ``` */ - async hydrate() { + async hydrate(): Promise<{ data: Infer; views: ViewsToQueryMethods }> { if (this.#data && this.#views && this.#options.cache) { debug('re-using data and views from cache') return { @@ -195,12 +198,28 @@ export class Collection< } } + if (this.#pendingLoad) { + debug('waiting for the load in progress') + return this.#pendingLoad + } + + this.#pendingLoad = this.#computeData().finally(() => { + this.#pendingLoad = undefined + }) + return this.#pendingLoad + } + + /** + * Runs the loader, and computes the views from its data + */ + async #computeData(): Promise<{ data: Infer; views: ViewsToQueryMethods }> { debug('computing data') - this.#data = await this.#options.loader.load(this.#options.schema, { + const data = await this.#options.loader.load(this.#options.schema, { vite: Collection.#vite, app: Collection.#app, ...this.#options.validatorMetaData, }) + this.#data = data const views = this.#options.views ?? ({} as Views) this.#views = Object.keys(views).reduce>((result, view) => { @@ -209,7 +228,7 @@ export class Collection< }, {} as ViewsToQueryMethods) return { - data: this.#data, + data, views: this.#views, } } diff --git a/tests/collection.spec.ts b/tests/collection.spec.ts index c908252..4ae9861 100644 --- a/tests/collection.spec.ts +++ b/tests/collection.spec.ts @@ -168,6 +168,49 @@ test.group('Collection | types', () => { assert.deepEqual(query1.list(), [{ permalink: '/foo' }]) }) + test('run the loader once for concurrent loads', async ({ assert }) => { + let calls = 0 + const collection = new Collection({ + cache: true, + schema: vine.array(vine.object({ permalink: vine.string() })), + loader: { + async load(schema) { + calls++ + await new Promise((resolve) => setTimeout(resolve, 10)) + return vine.validate({ schema, data: [{ permalink: '/foo' }] }) + }, + }, + }) + + const queries = await Promise.all([collection.load(), collection.load(), collection.load()]) + assert.equal(calls, 1) + for (const query of queries) { + assert.deepEqual(query.all(), [{ permalink: '/foo' }]) + } + }) + + test('run the loader again after a failed load', async ({ assert }) => { + let calls = 0 + const collection = new Collection({ + cache: true, + schema: vine.array(vine.object({ permalink: vine.string() })), + loader: { + async load(schema) { + calls++ + if (calls === 1) { + throw new Error('Source unavailable') + } + return vine.validate({ schema, data: [{ permalink: '/foo' }] }) + }, + }, + }) + + await assert.rejects(() => collection.load(), 'Source unavailable') + const query = await collection.load() + assert.equal(calls, 2) + assert.deepEqual(query.all(), [{ permalink: '/foo' }]) + }) + test('define multiple collection', async ({ assert, expectTypeOf }) => { const docsSchema = vine.array( vine.object({