mirror of
https://github.com/zadam/trilium.git
synced 2025-11-01 19:05:59 +01:00
prototype of executing requests without network calls in electron
This commit is contained in:
@@ -8,22 +8,54 @@ const server = (function() {
|
||||
}
|
||||
|
||||
async function get(url) {
|
||||
return await ajax('GET', url);
|
||||
return await call('GET', url);
|
||||
}
|
||||
|
||||
async function post(url, data) {
|
||||
return await ajax('POST', url, data);
|
||||
return await call('POST', url, data);
|
||||
}
|
||||
|
||||
async function put(url, data) {
|
||||
return await ajax('PUT', url, data);
|
||||
return await call('PUT', url, data);
|
||||
}
|
||||
|
||||
async function remove(url) {
|
||||
return await ajax('DELETE', url);
|
||||
return await call('DELETE', url);
|
||||
}
|
||||
|
||||
async function ajax(method, url, data) {
|
||||
let i = 1;
|
||||
const reqResolves = {};
|
||||
|
||||
async function call(method, url, data) {
|
||||
if (isElectron()) {
|
||||
const ipc = require('electron').ipcRenderer;
|
||||
const requestId = i++;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
reqResolves[requestId] = resolve;
|
||||
|
||||
ipc.send('server-request', {
|
||||
requestId: requestId,
|
||||
method: method,
|
||||
url: "/" + baseApiUrl + url,
|
||||
data: data
|
||||
});
|
||||
});
|
||||
}
|
||||
else {
|
||||
return await ajax(url, method, data);
|
||||
}
|
||||
}
|
||||
|
||||
if (isElectron()) {
|
||||
const ipc = require('electron').ipcRenderer;
|
||||
|
||||
ipc.on('server-response', (event, arg) => {
|
||||
reqResolves[arg.requestId](arg.body);
|
||||
});
|
||||
}
|
||||
|
||||
async function ajax(url, method, data) {
|
||||
const options = {
|
||||
url: baseApiUrl + url,
|
||||
type: method
|
||||
@@ -39,6 +71,7 @@ const server = (function() {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
initAjax();
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user