mirror of
https://github.com/Nystik-gh/ignis.git
synced 2026-06-17 04:35:53 +00:00
expose Ignis API, implement shared ws client
This commit is contained in:
@@ -2,57 +2,26 @@ const CHANNEL = "plugin:headless-sync";
|
||||
|
||||
class SyncBroadcaster {
|
||||
constructor(wss) {
|
||||
this._wss = wss;
|
||||
this._logSubscriptions = new Map();
|
||||
}
|
||||
|
||||
subscribeToLogs(vaultId) {
|
||||
this._logSubscriptions.set(vaultId, { expires: Date.now() + 10000 });
|
||||
this._channel = wss.channel(CHANNEL);
|
||||
}
|
||||
|
||||
broadcastLog(vaultId, line) {
|
||||
if (!this._wss?.clients) {
|
||||
return;
|
||||
}
|
||||
|
||||
const sub = this._logSubscriptions.get(vaultId);
|
||||
|
||||
if (!sub || Date.now() > sub.expires) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._send({
|
||||
channel: CHANNEL,
|
||||
this._channel.broadcastToVault(vaultId, {
|
||||
type: "sync-log",
|
||||
payload: { vaultId, line },
|
||||
});
|
||||
}
|
||||
|
||||
broadcastStatus(state) {
|
||||
if (!state) {
|
||||
if (!state || !state.vaultId) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._send({
|
||||
channel: CHANNEL,
|
||||
this._channel.broadcastToVault(state.vaultId, {
|
||||
type: "sync-status",
|
||||
payload: state,
|
||||
});
|
||||
}
|
||||
|
||||
_send(msg) {
|
||||
if (!this._wss?.clients) {
|
||||
return;
|
||||
}
|
||||
|
||||
const data = JSON.stringify(msg);
|
||||
|
||||
for (const client of this._wss.clients) {
|
||||
if (client.readyState === 1) {
|
||||
client.send(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { SyncBroadcaster };
|
||||
|
||||
Reference in New Issue
Block a user