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

59 lines
1.4 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 });
throttledEmit('publish', {
credentials: Auth.credentials,
payload,
});
}
return updateCursor(Auth.userID, payload);
}
2019-10-25 04:48:03 +08:00
export function initCursorStreamListener() {
2019-12-04 23:42:41 +08:00
logger.debug({
logCode: 'init_cursor_stream_listener',
}, 'initCursorStreamListener called');
2019-10-25 04:48:03 +08:00
if (!cursorStreamListener) {
cursorStreamListener = new Meteor.Streamer(`cursor-${Auth.meetingID}`, { retransmit: false });
2019-12-04 23:42:41 +08:00
logger.debug({
logCode: 'init_cursor_stream_listener',
}, 'initCursorStreamListener called');
2019-10-25 04:48:03 +08:00
cursorStreamListener.on('message', ({ cursors }) => {
Object.keys(cursors).forEach((userId) => {
if (Auth.userID === userId) return;
updateCursor(userId, cursors[userId]);
});
});
}
}
export default Cursor;