bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/cursor/service.js

75 lines
2.1 KiB
JavaScript
Raw Normal View History

import Auth from '/imports/ui/services/auth';
import { throttle } from 'lodash';
2019-12-04 23:42:41 +08:00
import logger from '/imports/startup/client/logger';
const Cursor = new Mongo.Collection(null);
2019-10-25 04:48:03 +08:00
let cursorStreamListener = null;
function updateCursor(userId, payload) {
const selector = {
userId,
2018-07-27 03:05:55 +08:00
whiteboardId: payload.whiteboardId,
};
const modifier = {
$set: {
userId,
2018-07-27 03:05:55 +08:00
...payload,
},
};
return Cursor.upsert(selector, modifier);
}
2018-07-27 03:05:55 +08:00
export function publishCursorUpdate(payload) {
2019-10-25 04:48:03 +08:00
if (cursorStreamListener) {
const throttledEmit = throttle(cursorStreamListener.emit.bind(cursorStreamListener), 30, { trailing: true });
2021-04-14 01:36:46 +08:00
throttledEmit('publish', payload);
2019-10-25 04:48:03 +08:00
}
return updateCursor(Auth.userID, payload);
}
2019-10-25 04:48:03 +08:00
export function initCursorStreamListener() {
2021-04-14 01:36:46 +08:00
logger.info({
logCode: 'init_cursor_stream_listener',
extraInfo: { meetingId: Auth.meetingID, userId: Auth.userID },
}, 'initCursorStreamListener called');
2019-12-04 23:42:41 +08:00
/**
* We create a promise to add the handlers after a ddp subscription stop.
* The problem was caused because we add handlers to stream before the onStop event happens,
* which set the handlers to undefined.
*/
cursorStreamListener = new Meteor.Streamer(`cursor-${Auth.meetingID}`, { retransmit: false });
2019-10-25 04:48:03 +08:00
const startStreamHandlersPromise = new Promise((resolve) => {
const checkStreamHandlersInterval = setInterval(() => {
2020-01-14 00:43:31 +08:00
const streamHandlersSize = Object.values(Meteor.StreamerCentral.instances[`cursor-${Auth.meetingID}`].handlers)
.filter(el => el != undefined)
.length;
2020-01-14 00:43:31 +08:00
if (!streamHandlersSize) {
resolve(clearInterval(checkStreamHandlersInterval));
}
}, 250);
});
startStreamHandlersPromise.then(() => {
logger.debug({ logCode: 'cursor_stream_handler_attach' }, 'Attaching handlers for cursor stream');
2019-12-04 23:42:41 +08:00
2019-10-25 04:48:03 +08:00
cursorStreamListener.on('message', ({ cursors }) => {
2021-04-14 01:36:46 +08:00
Object.keys(cursors).forEach((cursorId) => {
const cursor = cursors[cursorId];
const userId = cursor.userId;
delete cursor.userId;
2019-10-25 04:48:03 +08:00
if (Auth.userID === userId) return;
2021-04-14 01:36:46 +08:00
updateCursor(userId, cursor);
2019-10-25 04:48:03 +08:00
});
});
});
2019-10-25 04:48:03 +08:00
}
export default Cursor;