Fix: blocking HTTP requests that would freeze the panel. (#1007)
This commit is contained in:
@@ -161,16 +161,30 @@ class HttpClient {
|
|||||||
* @param message - Prepared Soup message to send
|
* @param message - Prepared Soup message to send
|
||||||
* @param options - Request configuration options
|
* @param options - Request configuration options
|
||||||
*/
|
*/
|
||||||
private _sendRequest(
|
private async _sendRequest(
|
||||||
resolve: (value: RestResponse | PromiseLike<RestResponse>) => void,
|
resolve: (value: RestResponse | PromiseLike<RestResponse>) => void,
|
||||||
reject: (reason?: unknown) => void,
|
reject: (reason?: unknown) => void,
|
||||||
message: Soup.Message,
|
message: Soup.Message,
|
||||||
options: RequestOptions,
|
options: RequestOptions,
|
||||||
): void {
|
): Promise<void> {
|
||||||
const cancellable = options.signal ?? null;
|
const cancellable = options.signal ?? null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const bytes = this._session.send_and_read(message, cancellable);
|
const bytes = await new Promise<GLib.Bytes | null>((resolveAsync, rejectAsync) => {
|
||||||
|
this._session.send_and_read_async(
|
||||||
|
message,
|
||||||
|
GLib.PRIORITY_DEFAULT,
|
||||||
|
cancellable,
|
||||||
|
(_, result) => {
|
||||||
|
try {
|
||||||
|
const bytes = this._session.send_and_read_finish(result);
|
||||||
|
resolveAsync(bytes);
|
||||||
|
} catch (error) {
|
||||||
|
rejectAsync(error);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
const {
|
const {
|
||||||
response: responseText,
|
response: responseText,
|
||||||
|
|||||||
@@ -293,7 +293,8 @@ export default class WeatherService {
|
|||||||
`${provider.baseUrl}?location=${formattedLocation}&key=${weatherKey}`;
|
`${provider.baseUrl}?location=${formattedLocation}&key=${weatherKey}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await httpClient.get(url);
|
const WEATHER_FETCH_TIMEOUT_MS = 10000;
|
||||||
|
const response = await httpClient.get(url, { timeout: WEATHER_FETCH_TIMEOUT_MS });
|
||||||
|
|
||||||
if (response.data && provider.adapter) {
|
if (response.data && provider.adapter) {
|
||||||
const transformedData = provider.adapter.toStandardFormat(response.data);
|
const transformedData = provider.adapter.toStandardFormat(response.data);
|
||||||
|
|||||||
Reference in New Issue
Block a user