refactor(geomap): avoid parsing XML twice

This commit is contained in:
Elian Doran 2025-06-01 14:18:27 +03:00
parent c5d64c182b
commit 365fd37be5
No known key found for this signature in database
2 changed files with 18 additions and 7 deletions

View File

@ -58,8 +58,11 @@ async function getWithSilentNotFound<T>(url: string, componentId?: string) {
return await call<T>("GET", url, componentId, { silentNotFound: true }); return await call<T>("GET", url, componentId, { silentNotFound: true });
} }
async function get<T>(url: string, componentId?: string) { /**
return await call<T>("GET", url, componentId); * @param raw if `true`, the value will be returned as a string instead of a JavaScript object if JSON, XMLDocument if XML, etc.
*/
async function get<T>(url: string, componentId?: string, raw?: boolean) {
return await call<T>("GET", url, componentId, { raw });
} }
async function post<T>(url: string, data?: unknown, componentId?: string) { async function post<T>(url: string, data?: unknown, componentId?: string) {
@ -102,6 +105,8 @@ let maxKnownEntityChangeId = 0;
interface CallOptions { interface CallOptions {
data?: unknown; data?: unknown;
silentNotFound?: boolean; silentNotFound?: boolean;
// If `true`, the value will be returned as a string instead of a JavaScript object if JSON, XMLDocument if XML, etc.
raw?: boolean;
} }
async function call<T>(method: string, url: string, componentId?: string, options: CallOptions = {}) { async function call<T>(method: string, url: string, componentId?: string, options: CallOptions = {}) {
@ -132,7 +137,7 @@ async function call<T>(method: string, url: string, componentId?: string, option
}); });
})) as any; })) as any;
} else { } else {
resp = await ajax(url, method, data, headers, !!options.silentNotFound); resp = await ajax(url, method, data, headers, !!options.silentNotFound, options.raw);
} }
const maxEntityChangeIdStr = resp.headers["trilium-max-entity-change-id"]; const maxEntityChangeIdStr = resp.headers["trilium-max-entity-change-id"];
@ -144,7 +149,10 @@ async function call<T>(method: string, url: string, componentId?: string, option
return resp.body as T; return resp.body as T;
} }
function ajax(url: string, method: string, data: unknown, headers: Headers, silentNotFound: boolean): Promise<Response> { /**
* @param raw if `true`, the value will be returned as a string instead of a JavaScript object if JSON, XMLDocument if XML, etc.
*/
function ajax(url: string, method: string, data: unknown, headers: Headers, silentNotFound: boolean, raw?: boolean): Promise<Response> {
return new Promise((res, rej) => { return new Promise((res, rej) => {
const options: JQueryAjaxSettings = { const options: JQueryAjaxSettings = {
url: window.glob.baseApiUrl + url, url: window.glob.baseApiUrl + url,
@ -186,6 +194,10 @@ function ajax(url: string, method: string, data: unknown, headers: Headers, sile
} }
}; };
if (raw) {
options.dataType = "text";
}
if (data) { if (data) {
try { try {
options.data = JSON.stringify(data); options.data = JSON.stringify(data);

View File

@ -224,13 +224,12 @@ export default class GeoMapTypeWidget extends TypeWidget {
this.gpxLoaded = true; this.gpxLoaded = true;
} }
const xmlResponse = await server.get<XMLDocument | Uint8Array>(`notes/${note.noteId}/open`); const xmlResponse = await server.get<string | Uint8Array>(`notes/${note.noteId}/open`, undefined, true);
let stringResponse: string; let stringResponse: string;
if (xmlResponse instanceof Uint8Array) { if (xmlResponse instanceof Uint8Array) {
stringResponse = new TextDecoder().decode(xmlResponse); stringResponse = new TextDecoder().decode(xmlResponse);
} else { } else {
// TODO: This is not very efficient as it's probably a string response that is parsed and then converted back to string and parsed again. stringResponse = xmlResponse;
stringResponse = new XMLSerializer().serializeToString(xmlResponse)
} }
const track = new this.L.GPX(stringResponse, {}); const track = new this.L.GPX(stringResponse, {});