Add cache for working moments (#1048)
# What this PR does ## Which issue(s) this PR fixes Add cache to avoid heavy calculations to render working hours shades ## Checklist - [ ] Tests updated - [ ] Documentation added - [ ] `CHANGELOG.md` updated
This commit is contained in:
parent
1a8cff8c6c
commit
f0a34ffd1f
3 changed files with 23 additions and 2 deletions
|
|
@ -116,6 +116,7 @@
|
|||
"eslint-plugin-import": "^2.25.4",
|
||||
"mobx": "5.13.0",
|
||||
"mobx-react": "6.1.1",
|
||||
"object-hash": "^3.0.0",
|
||||
"prettier": "^2.8.2",
|
||||
"rc-table": "^7.17.1",
|
||||
"react-copy-to-clipboard": "^5.0.2",
|
||||
|
|
|
|||
|
|
@ -1,8 +1,21 @@
|
|||
import dayjs from 'dayjs';
|
||||
import hash from 'object-hash';
|
||||
|
||||
const workingMomentsCache = {};
|
||||
|
||||
const getKey = (startMoment, endMoment, workingHours, timezone) => {
|
||||
return `${startMoment}-${endMoment}-${hash(workingHours)}-${timezone}`;
|
||||
};
|
||||
|
||||
export const getWorkingMoments = (startMoment, endMoment, workingHours, timezone) => {
|
||||
const weekdays = dayjs.weekdays();
|
||||
|
||||
const key = getKey(startMoment, endMoment, workingHours, timezone);
|
||||
|
||||
if (workingMomentsCache[key]) {
|
||||
return workingMomentsCache[key];
|
||||
}
|
||||
|
||||
const momentToStartIteration = dayjs().tz(timezone).utcOffset() === 0 ? startMoment : startMoment.tz(timezone);
|
||||
|
||||
const dayOfWeekToStartIteration = momentToStartIteration.format('dddd');
|
||||
|
|
@ -64,14 +77,16 @@ export const getWorkingMoments = (startMoment, endMoment, workingHours, timezone
|
|||
}
|
||||
}
|
||||
|
||||
workingMomentsCache[key] = workingMoments;
|
||||
|
||||
return workingMoments;
|
||||
};
|
||||
|
||||
export const getNonWorkingMoments = (startMoment, endMoment, workingHours) => {
|
||||
export const getNonWorkingMoments = (startMoment, endMoment, workingMoments) => {
|
||||
const nonWorkingMoments = [{ start: startMoment, end: endMoment }];
|
||||
|
||||
let lastNonWorkingRange = nonWorkingMoments[0];
|
||||
for (const [_i, range] of workingHours.entries()) {
|
||||
for (const [_i, range] of workingMoments.entries()) {
|
||||
lastNonWorkingRange.end = range.start;
|
||||
|
||||
lastNonWorkingRange = { start: range.end, end: undefined };
|
||||
|
|
|
|||
|
|
@ -9802,6 +9802,11 @@ object-copy@^0.1.0:
|
|||
define-property "^0.2.5"
|
||||
kind-of "^3.0.3"
|
||||
|
||||
object-hash@^3.0.0:
|
||||
version "3.0.0"
|
||||
resolved "https://registry.yarnpkg.com/object-hash/-/object-hash-3.0.0.tgz#73f97f753e7baffc0e2cc9d6e079079744ac82e9"
|
||||
integrity sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==
|
||||
|
||||
object-inspect@^1.12.2, object-inspect@^1.9.0:
|
||||
version "1.12.2"
|
||||
resolved "https://registry.yarnpkg.com/object-inspect/-/object-inspect-1.12.2.tgz#c0641f26394532f28ab8d796ab954e43c009a8ea"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue