2018-05-12 00:01:24 +08:00
|
|
|
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';
|
2018-05-12 00:01:24 +08:00
|
|
|
|
|
|
|
const Cursor = new Mongo.Collection(null);
|
2019-10-25 04:48:03 +08:00
|
|
|
let cursorStreamListener = null;
|
2018-05-12 00:01:24 +08:00
|
|
|
|
2019-07-18 08:30:28 +08:00
|
|
|
function updateCursor(userId, payload) {
|
2018-05-12 00:01:24 +08:00
|
|
|
const selector = {
|
|
|
|
userId,
|
2018-07-27 03:05:55 +08:00
|
|
|
whiteboardId: payload.whiteboardId,
|
2018-05-12 00:01:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const modifier = {
|
|
|
|
$set: {
|
|
|
|
userId,
|
2018-07-27 03:05:55 +08:00
|
|
|
...payload,
|
2018-05-12 00:01:24 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-04-04 01:45:53 +08:00
|
|
|
return Cursor.upsert(selector, modifier);
|
2018-05-12 00:01:24 +08:00
|
|
|
}
|
|
|
|
|
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', {
|
2020-02-07 04:47:28 +08:00
|
|
|
userId: Auth.userID,
|
2019-10-25 04:48:03 +08:00
|
|
|
payload,
|
|
|
|
});
|
|
|
|
}
|
2018-05-12 00:01:24 +08:00
|
|
|
|
2019-07-18 08:30:28 +08:00
|
|
|
return updateCursor(Auth.userID, payload);
|
2018-05-12 00:01:24 +08:00
|
|
|
}
|
|
|
|
|
2019-10-25 04:48:03 +08:00
|
|
|
export function initCursorStreamListener() {
|
2019-12-07 01:46:58 +08:00
|
|
|
logger.info({
|
2019-12-04 23:42:41 +08:00
|
|
|
logCode: 'init_cursor_stream_listener',
|
2019-12-07 01:46:58 +08:00
|
|
|
extraInfo: { meetingId: Auth.meetingID, userId: Auth.userID },
|
2019-12-04 23:42:41 +08:00
|
|
|
}, 'initCursorStreamListener called');
|
|
|
|
|
2020-01-13 20:34:54 +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
|
|
|
|
2020-01-13 20:34:54 +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-13 20:34:54 +08:00
|
|
|
|
2020-01-14 00:43:31 +08:00
|
|
|
if (!streamHandlersSize) {
|
2020-01-13 20:34:54 +08:00
|
|
|
resolve(clearInterval(checkStreamHandlersInterval));
|
|
|
|
}
|
|
|
|
}, 250);
|
|
|
|
});
|
|
|
|
|
|
|
|
startStreamHandlersPromise.then(() => {
|
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]);
|
|
|
|
});
|
|
|
|
});
|
2020-01-13 20:34:54 +08:00
|
|
|
});
|
2019-10-25 04:48:03 +08:00
|
|
|
}
|
|
|
|
|
2018-05-12 00:01:24 +08:00
|
|
|
export default Cursor;
|