Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export class Collection<
/** Cached validated data */
#data?: Infer<Schema>
#views?: ViewsToQueryMethods<Views>
/** The load in progress, shared by concurrent callers */
#pendingLoad?: Promise<{ data: Infer<Schema>; views: ViewsToQueryMethods<Views> }>

/**
* Creates a new Collection instance.
Expand Down Expand Up @@ -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<Schema>; views: ViewsToQueryMethods<Views> }> {
if (this.#data && this.#views && this.#options.cache) {
debug('re-using data and views from cache')
return {
Expand All @@ -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<Schema>; views: ViewsToQueryMethods<Views> }> {
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<ViewsToQueryMethods<Views>>((result, view) => {
Expand All @@ -209,7 +228,7 @@ export class Collection<
}, {} as ViewsToQueryMethods<Views>)

return {
data: this.#data,
data,
views: this.#views,
}
}
Expand Down
43 changes: 43 additions & 0 deletions tests/collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Loading