mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: use coll.watch to stream events
This commit is contained in:
1 parent
c25176f483
commit
429bacfaf2
3 files changed
+11
-20
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hydrooj",
|
"name": "hydrooj",
|
||||||
"version": "3.11.9",
|
"version": "3.11.10",
|
||||||
"bin": "bin/hydrooj.js",
|
"bin": "bin/hydrooj.js",
|
||||||
"main": "src/loader",
|
"main": "src/loader",
|
||||||
"module": "src/loader",
|
"module": "src/loader",
|
||||||
|
|||||||
@@ -562,7 +562,7 @@ export interface FileNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface EventDoc {
|
export interface EventDoc {
|
||||||
ack: string[];
|
sender: string;
|
||||||
event: number | string;
|
event: number | string;
|
||||||
payload: string;
|
payload: string;
|
||||||
expire: Date;
|
expire: Date;
|
||||||
|
|||||||
@@ -161,27 +161,18 @@ bus.once('app/started', async () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
await collEvent.createIndex({ expire: 1 }, { expireAfterSeconds: 0 });
|
await collEvent.createIndex({ expire: 1 }, { expireAfterSeconds: 0 });
|
||||||
(async () => {
|
const stream = collEvent.watch([{ $match: { sender: { $ne: id } } }]);
|
||||||
// eslint-disable-next-line no-constant-condition
|
stream.on('change', async (change) => {
|
||||||
while (true) {
|
if (change.operationType !== 'insert') return;
|
||||||
// eslint-disable-next-line no-await-in-loop
|
const doc = change.fullDocument;
|
||||||
const res = await collEvent.findOneAndUpdate(
|
const payload = JSON.parse(doc.payload);
|
||||||
{ ack: { $nin: [id] } },
|
if (process.send) process.send({ type: 'hydro:broadcast', data: { event: doc.event, payload } });
|
||||||
{ $push: { ack: id } },
|
if (doc) bus.parallel(doc.event, ...payload);
|
||||||
);
|
});
|
||||||
// eslint-disable-next-line no-await-in-loop
|
|
||||||
if (!res.value) await sleep(100);
|
|
||||||
else {
|
|
||||||
const payload = JSON.parse(res.value.payload);
|
|
||||||
if (process.send) process.send({ type: 'hydro:broadcast', data: { event: res.value.event, payload } });
|
|
||||||
if (res.value) bus.parallel(res.value.event, ...payload);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
});
|
});
|
||||||
bus.on('bus/broadcast', (event, payload) => {
|
bus.on('bus/broadcast', (event, payload) => {
|
||||||
collEvent.insertOne({
|
collEvent.insertOne({
|
||||||
ack: [id],
|
sender: id,
|
||||||
event,
|
event,
|
||||||
payload: JSON.stringify(payload),
|
payload: JSON.stringify(payload),
|
||||||
expire: new Date(Date.now() + 10000),
|
expire: new Date(Date.now() + 10000),
|
||||||
|
|||||||
Reference in new issue
Block a user