Skip to content

Commit

Permalink
Merge commit '51d4bc9a75cdf28831b941f8ed73bd4644a2ca4e'
Browse files Browse the repository at this point in the history
  • Loading branch information
janproch committed May 20, 2024
2 parents df3313e + 51d4bc9 commit 781ee15
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
3 changes: 2 additions & 1 deletion packages/api/src/controllers/serverConnections.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ module.exports = {
},

ping_meta: true,
async ping({ conidArray }) {
async ping({ conidArray, strmid }) {
await Promise.all(
_.uniq(conidArray).map(async conid => {
const last = this.lastPinged[conid];
Expand All @@ -169,6 +169,7 @@ module.exports = {
}
})
);
socket.setStreamIdFilter(strmid, { conid: conidArray });
return { status: 'ok' };
},

Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function start() {
}

app.get(getExpressPath('/stream'), async function (req, res) {
const strmid = req.query.strmid;
res.set({
'Cache-Control': 'no-cache',
'Content-Type': 'text/event-stream',
Expand All @@ -87,9 +88,9 @@ function start() {

// Tell the client to retry every 10 seconds if connectivity is lost
res.write('retry: 10000\n\n');
socket.addSseResponse(res);
socket.addSseResponse(res, strmid);
onFinished(req, () => {
socket.removeSseResponse(res);
socket.removeSseResponse(strmid);
});
});

Expand Down
35 changes: 28 additions & 7 deletions packages/api/src/utility/socket.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const _ = require('lodash');
const stableStringify = require('json-stable-stringify');

const sseResponses = [];
const sseResponses = {};
let electronSender = null;
let pingConfigured = false;

Expand All @@ -12,12 +12,15 @@ module.exports = {
pingConfigured = true;
}
},
addSseResponse(value) {
sseResponses.push(value);
addSseResponse(value, strmid) {
sseResponses[strmid] = {
response: value,
filter: {},
};
this.ensurePing();
},
removeSseResponse(value) {
_.remove(sseResponses, x => x == value);
removeSseResponse(strmid) {
delete sseResponses[strmid];
},
setElectronSender(value) {
electronSender = value;
Expand All @@ -27,13 +30,31 @@ module.exports = {
if (electronSender) {
electronSender.send(message, data == null ? null : data);
}
for (const res of sseResponses) {
res.write(`event: ${message}\ndata: ${stableStringify(data == null ? null : data)}\n\n`);
for (const strmid in sseResponses) {
let skipThisStream = false;
for (const key in sseResponses[strmid].filter) {
if (data && data[key]) {
if (!sseResponses[strmid].filter[key].includes(data[key])) {
skipThisStream = true;
break;
}
}
}
if (skipThisStream) {
continue;
}

sseResponses[strmid].response.write(
`event: ${message}\ndata: ${stableStringify(data == null ? null : data)}\n\n`
);
}
},
emitChanged(key, params = undefined) {
// console.log('EMIT CHANGED', key);
this.emit('changed-cache', { key, ...params });
// this.emit(key);
},
setStreamIdFilter(strmid, filter) {
sseResponses[strmid].filter = filter;
},
};
5 changes: 4 additions & 1 deletion packages/web/src/utility/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { isOauthCallback, redirectToLogin } from '../clientAuth';
import { showModal } from '../modals/modalTools';
import DatabaseLoginModal, { isDatabaseLoginVisible } from '../modals/DatabaseLoginModal.svelte';
import _ from 'lodash';
import uuidv1 from 'uuid/v1';

export const strmid = uuidv1();

let eventSource;
let apiLogging = false;
Expand Down Expand Up @@ -49,7 +52,7 @@ export function removeVolatileMapping(conid) {

function wantEventSource() {
if (!eventSource) {
eventSource = new EventSource(`${resolveApi()}/stream`);
eventSource = new EventSource(`${resolveApi()}/stream?strmid=${strmid}`);
// eventSource.addEventListener('clean-cache', e => cacheClean(JSON.parse(e.data)));
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/utility/connectionsPinger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash';
import { openedConnections, currentDatabase } from '../stores';
import { apiCall } from './api';
import { apiCall, strmid } from './api';
import { getConnectionList } from './metadataLoaders';

// const doServerPing = async value => {
Expand All @@ -10,7 +10,7 @@ import { getConnectionList } from './metadataLoaders';
// };

const doServerPing = value => {
apiCall('server-connections/ping', { conidArray: value });
apiCall('server-connections/ping', { conidArray: value, strmid });
};

const doDatabasePing = value => {
Expand Down

0 comments on commit 781ee15

Please sign in to comment.