[Perf Improvement] Event Booking Date Picker (#1980)
* Memoize and remove repeat calls of functions * Better fn names * Remove unnecessary code change * Process dates asyncly * Avoid waste work * Add comments Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
3bae13eea8
commit
cf186e58bd
3 files changed
+145
-28
No files matched your search
@@ -0,0 +1,48 @@
|
||||
const data = {};
|
||||
/**
|
||||
* Starts an iteration from `0` to `length - 1` with batch size `batch`
|
||||
*
|
||||
* `callback` is called per iteration
|
||||
*
|
||||
* `done` is called when all iterations are done
|
||||
*
|
||||
* `name` is a unique identifier for the work. It allows the work that is not required to be dropped.
|
||||
*/
|
||||
export const doWorkAsync = ({
|
||||
length,
|
||||
name,
|
||||
callback,
|
||||
done,
|
||||
batch,
|
||||
offsetStart,
|
||||
__pending,
|
||||
}: {
|
||||
name: string;
|
||||
length: number;
|
||||
callback: Function;
|
||||
done: Function;
|
||||
batch: number;
|
||||
offsetStart?: number;
|
||||
__pending?: boolean;
|
||||
}) => {
|
||||
offsetStart = offsetStart || 0;
|
||||
|
||||
const stepLength = batch;
|
||||
const lastIndex = length - 1;
|
||||
const offsetEndExclusive = offsetStart + stepLength;
|
||||
if (!__pending && data[name]) {
|
||||
cancelAnimationFrame(data[name]);
|
||||
}
|
||||
if (offsetStart >= length) {
|
||||
done();
|
||||
return;
|
||||
}
|
||||
|
||||
for (let i = offsetStart; i < offsetEndExclusive && i < length; i++) {
|
||||
callback(i, offsetEndExclusive > lastIndex);
|
||||
}
|
||||
|
||||
data[name] = requestAnimationFrame(() => {
|
||||
doWorkAsync({ length, callback, name, batch, done, offsetStart: offsetEndExclusive, __pending: true });
|
||||
});
|
||||
};
|
||||
@@ -27,6 +27,8 @@ const getMinuteOffset = (date: Dayjs, frequency: number) => {
|
||||
const getSlots = ({ inviteeDate, frequency, minimumBookingNotice, workingHours }: GetSlots) => {
|
||||
// current date in invitee tz
|
||||
const startDate = dayjs().add(minimumBookingNotice, "minute");
|
||||
const startOfDay = dayjs.utc().startOf("day");
|
||||
const startOfInviteeDay = inviteeDate.startOf("day");
|
||||
// checks if the start date is in the past
|
||||
if (inviteeDate.isBefore(startDate, "day")) {
|
||||
return [];
|
||||
@@ -36,14 +38,14 @@ const getSlots = ({ inviteeDate, frequency, minimumBookingNotice, workingHours }
|
||||
{ utcOffset: -inviteeDate.utcOffset() },
|
||||
workingHours.map((schedule) => ({
|
||||
days: schedule.days,
|
||||
startTime: dayjs.utc().startOf("day").add(schedule.startTime, "minute"),
|
||||
endTime: dayjs.utc().startOf("day").add(schedule.endTime, "minute"),
|
||||
startTime: startOfDay.add(schedule.startTime, "minute"),
|
||||
endTime: startOfDay.add(schedule.endTime, "minute"),
|
||||
}))
|
||||
).filter((hours) => hours.days.includes(inviteeDate.day()));
|
||||
|
||||
const slots: Dayjs[] = [];
|
||||
for (let minutes = getMinuteOffset(inviteeDate, frequency); minutes < 1440; minutes += frequency) {
|
||||
const slot = dayjs(inviteeDate).startOf("day").add(minutes, "minute");
|
||||
const slot = startOfInviteeDay.add(minutes, "minute");
|
||||
// check if slot happened already
|
||||
if (slot.isBefore(startDate)) {
|
||||
continue;
|
||||
@@ -52,8 +54,8 @@ const getSlots = ({ inviteeDate, frequency, minimumBookingNotice, workingHours }
|
||||
if (
|
||||
localWorkingHours.some((hours) =>
|
||||
slot.isBetween(
|
||||
inviteeDate.startOf("day").add(hours.startTime, "minute"),
|
||||
inviteeDate.startOf("day").add(hours.endTime, "minute"),
|
||||
startOfInviteeDay.add(hours.startTime, "minute"),
|
||||
startOfInviteeDay.add(hours.endTime, "minute"),
|
||||
null,
|
||||
"[)"
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user