[IMPROVE] Cache livechat get agent trigger call (#12083)

pull/10094/head
Diego Sampaio 7 years ago committed by GitHub
parent 933378a7f6
commit fec4041f71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      packages/rocketchat-livechat/.app/client/lib/triggers.js

@ -3,8 +3,14 @@ import visitor from '../../imports/client/visitor';
const firedTriggers = JSON.parse(localStorage.getItem('rocketChatFiredTriggers')) || [];
// promise cache for multiple calls (let's say multiple triggers running before the previous finished)
const agentCacheExpiry = 3600000;
let agentPromise;
function getAgent(triggerAction) {
return new Promise((resolve, reject) => {
if (agentPromise) {
return agentPromise;
}
agentPromise = new Promise((resolve, reject) => {
const { params } = triggerAction;
if (params.sender === 'queue') {
const cache = localStorage.getItem('triggerAgent');
@ -12,7 +18,7 @@ function getAgent(triggerAction) {
const cacheAgent = JSON.parse(cache);
// cache valid for 1h
if (cacheAgent.ts && Date.now() - cacheAgent.ts < 3600000) {
if (cacheAgent.ts && Date.now() - cacheAgent.ts < agentCacheExpiry) {
return resolve(cacheAgent.agent);
}
}
@ -39,6 +45,13 @@ function getAgent(triggerAction) {
reject('Unknown sender');
}
});
// expire the promise cache as well
setTimeout(() => {
agentPromise = null;
}, agentCacheExpiry);
return agentPromise;
}
this.Triggers = (function() {

Loading…
Cancel
Save