mendel-collaboration-kit
Version:
82 lines (70 loc) • 2.14 kB
JavaScript
var PusherClient = require("pusher-js");
const actions = require("./dsl/actions");
var channel;
const channelName = "mendel_onsite";
var pusherClient;
var postFunc;
var repeatedEvents = {};
var repeatedEventsData = {};
const init = postFun => {
postFunc = postFun;
pusherClient = new Pusher("264eba399a6836b215f1", {
cluster: "us2",
forceTLS: true
});
// internal_app_id = app_id;
channel = pusherClient.subscribe(channelName);
};
const getEventScope = (event, isPublic) =>
isPublic ? event : `INTERNAL_${event}`;
const fire = (event, userId, data, isPublic) => {
callFireBackEnd(event, isPublic, userId, data);
if (event == actions.PATIENT_OPENED) {
repeatedEventsData[actions.PATIENT_OPENED] = {
event,
isPublic,
userId,
data
};
let interRes = setInterval(() => {
callFireBackEnd(
repeatedEventsData[actions.PATIENT_OPENED].event,
repeatedEventsData[actions.PATIENT_OPENED].isPublic,
repeatedEventsData[actions.PATIENT_OPENED].userId,
repeatedEventsData[actions.PATIENT_OPENED].data
);
}, 6000);
if (repeatedEvents[event + userId]) {
clearInterval(repeatedEvents[event + userId]);
delete repeatedEvents[event + userId];
}
repeatedEvents[event + userId] = interRes;
} else if (event == actions.PATIENT_CLOSED) {
if (repeatedEvents[actions.PATIENT_OPENED + userId]) {
clearInterval(repeatedEvents[actions.PATIENT_OPENED + userId]);
delete repeatedEvents[actions.PATIENT_OPENED + userId];
}
}
};
const monitor = (event, isPublic, callback) => {
if (!channel) throw new Error("init not called");
channel.bind(getEventScope(event, isPublic), callback);
};
const ignore = (event, isPublic) => {
if (!channel) throw new Error("init not called");
channel.unbind(getEventScope(event, isPublic));
};
module.exports = {
init,
fire,
monitor,
ignore,
actions
};
function callFireBackEnd(event, isPublic, userId, data) {
postFunc("https://taher.mendel.ai/v1/pusher/trigger", {
eventName: getEventScope(event, isPublic),
user: userId,
data: data
});
}