flamenco/web/app/src/stores/api-query-count.js
Sybren A. Stüvel 4129ed11bb Web: use wrapper for the OpenAPI client to track requests
A wrapper for the generated `ApiClient` class tracks the number of running
queries. This makes it much simpler to show the "API calls pending" UI
element, regardless of which part of the webapp performs the queries.
2022-04-22 11:46:33 +02:00

47 lines
1.2 KiB
JavaScript

import { defineStore } from "pinia";
import * as API from "@/manager-api";
import * as urls from '@/urls'
/**
* Keep track of running API queries.
*/
export const useAPIQueryCount = defineStore("apiQueryCount", {
state: () => ({
/**
* Number of running queries.
*/
num: 0,
}),
actions: {
/**
* Track this promise, counting it as a query for the spinner.
* @param {Promise} promise
*/
async track(promise) {
this.num++;
try {
return await promise;
} finally {
this.num--;
}
},
},
});
export class CountingApiClient extends API.ApiClient {
callApi(path, httpMethod, pathParams, queryParams, headerParams, formParams,
bodyParam, authNames, contentTypes, accepts, returnType, apiBasePath ) {
const apiQueryCount = useAPIQueryCount();
apiQueryCount.num++;
return super
.callApi(path, httpMethod, pathParams, queryParams, headerParams, formParams,
bodyParam, authNames, contentTypes, accepts, returnType, apiBasePath)
.finally(() => {
apiQueryCount.num--;
});
}
}
export const apiClient = new CountingApiClient(urls.api());;