Skip to content
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

feat: Add abort signal option to CozyClient.query() #1355

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
7 changes: 4 additions & 3 deletions packages/cozy-client/src/CozyClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -1065,14 +1065,15 @@ client.query(Q('io.cozy.bills'))`)
* @param {QueryDefinition} definition QueryDefinition to be executed
* @returns {Promise<import("./types").ClientResponse>}
*/
async requestQuery(definition) {
const mainResponse = await this.chain.request(definition)
async requestQuery(definition, options = {}) {
const mainResponse = await this.chain.request(definition, options)
if (!definition.includes) {
return mainResponse
}
const withIncluded = await this.fetchRelationships(
mainResponse,
this.getIncludesRelationships(definition)
this.getIncludesRelationships(definition),
options
)
return withIncluded
}
Expand Down
15 changes: 8 additions & 7 deletions packages/cozy-client/src/StackLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,20 @@ export default class StackLink extends CozyLink {
this.stackClient = null
}

request(operation, result, forward) {
request(operation, result, forward, options = {}) {
if (operation.mutationType) {
return this.executeMutation(operation, result, forward)
}
return this.executeQuery(operation)
return this.executeQuery(operation, options)
}
/**
*
* @param {QueryDefinition} query - Query to execute
* @returns {Promise<import("./types").ClientResponse>}
*/
executeQuery(query) {
const { doctype, selector, id, ids, referenced, ...options } = query
executeQuery(query, options = {}) {
const { doctype, selector, id, ids, referenced, ...opts } = query
const mergedOptions = { ...opts, ...options }
if (!doctype) {
logger.warn('Bad query', query)
throw new Error('No doctype found in a query definition')
Expand All @@ -102,12 +103,12 @@ export default class StackLink extends CozyLink {
return collection.getAll(ids)
}
if (referenced) {
return collection.findReferencedBy(referenced, options)
return collection.findReferencedBy(referenced, mergedOptions)
}
if (hasFindOptions(query)) {
return collection.find(selector, options)
return collection.find(selector, mergedOptions)
} else {
return collection.all(options)
return collection.all(mergedOptions)
}
}

Expand Down
5 changes: 4 additions & 1 deletion packages/cozy-stack-client/src/FileCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,7 @@ class FileCollection extends DocumentCollection {
'fields',
'includeDocs',
'includeFilePath',
'signal',
'skipDeleted',
'skipTrashed'
]),
Expand All @@ -1243,7 +1244,9 @@ class FileCollection extends DocumentCollection {
last_seq: newLastSeq,
pending,
results
} = await this.stackClient.fetchJSON('GET', url)
} = await this.stackClient.fetchJSON('GET', url, null, {
signal: opts.signal
})

return { newLastSeq, pending, results }
}
Expand Down