Skip to content

Commit

Permalink
Specify time conversion unit (outline#7458)
Browse files Browse the repository at this point in the history
* fix: specificity in time units

* fix: milliseconds -> ms
  • Loading branch information
apoorv-mishra committed Aug 25, 2024
1 parent 2578a1f commit 5d85a3a
Show file tree
Hide file tree
Showing 18 changed files with 53 additions and 35 deletions.
4 changes: 2 additions & 2 deletions app/hooks/useAutoRefresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let isReloaded = false;
export default function useAutoRefresh() {
const [minutes, setMinutes] = React.useState(0);
const isVisible = usePageVisibility();
const isIdle = useIdle(15 * Minute);
const isIdle = useIdle(15 * Minute.ms);

useInterval(() => {
setMinutes((prev) => prev + 1);
Expand All @@ -39,5 +39,5 @@ export default function useAutoRefresh() {
window.location.reload();
isReloaded = true;
}
}, Minute);
}, Minute.ms);
}
2 changes: 1 addition & 1 deletion app/hooks/useIdle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const activityEvents = [
* @returns boolean if the user is idle
*/
export default function useIdle(
timeToIdle: number = 3 * Minute,
timeToIdle: number = 3 * Minute.ms,
events = activityEvents
) {
const isMounted = useIsMounted();
Expand Down
2 changes: 1 addition & 1 deletion app/scenes/Document/components/CommentThreadItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function useShowTime(

return (
!msSincePreviousComment ||
(msSincePreviousComment > 15 * Minute &&
(msSincePreviousComment > 15 * Minute.ms &&
previousTimeStamp !== currentTimeStamp)
);
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/github/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if (enabled) {
},
{
type: Hook.UnfurlProvider,
value: { unfurl: GitHub.unfurl, cacheExpiry: Minute / 1000 },
value: { unfurl: GitHub.unfurl, cacheExpiry: Minute.seconds },
},
{
type: Hook.Uninstall,
Expand Down
2 changes: 1 addition & 1 deletion plugins/iframely/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if (enabled) {
PluginManager.add([
{
type: Hook.UnfurlProvider,
value: { unfurl: Iframely.unfurl, cacheExpiry: Day / 1000 },
value: { unfurl: Iframely.unfurl, cacheExpiry: Day.seconds },

// Make sure this is last in the stack to be evaluated after all other unfurl providers
priority: PluginPriority.VeryLow,
Expand Down
2 changes: 1 addition & 1 deletion plugins/slack/server/processors/SlackProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default class SlackProcessor extends BaseProcessor {
if (
event.name === "revisions.create" &&
differenceInMilliseconds(document.updatedAt, document.publishedAt) <
Minute
Minute.ms
) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion server/collaboration/ViewsExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class ViewsExtension implements Extension {
const lastUpdate = this.lastViewBySocket.get(socketId);
const [, documentId] = documentName.split(".");

if (!lastUpdate || Date.now() - lastUpdate.getTime() > Minute) {
if (!lastUpdate || Date.now() - lastUpdate.getTime() > Minute.ms) {
this.lastViewBySocket.set(socketId, new Date());

Logger.debug(
Expand Down
2 changes: 1 addition & 1 deletion server/emails/templates/CommentCreatedEmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default class CommentCreatedEmail extends BaseEmail<
content = await TextHelper.attachmentsToSignedUrls(
content,
document.teamId,
(4 * Day) / 1000
4 * Day.seconds
);

if (content) {
Expand Down
2 changes: 1 addition & 1 deletion server/emails/templates/CommentMentionedEmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default class CommentMentionedEmail extends BaseEmail<
content = await TextHelper.attachmentsToSignedUrls(
content,
document.teamId,
(4 * Day) / 1000
4 * Day.seconds
);

if (content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class DocumentPublishedOrUpdatedEmail extends BaseEmail<
const content = await DocumentHelper.toEmailDiff(before, revision, {
includeTitle: false,
centered: false,
signedUrls: (4 * Day) / 1000,
signedUrls: 4 * Day.seconds,
baseUrl: props.teamUrl,
});

Expand Down
2 changes: 1 addition & 1 deletion server/queues/HealthMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ export default class HealthMonitor {
}
);
}
}, 30 * Second);
}, 30 * Second.ms);
}
}
10 changes: 5 additions & 5 deletions server/queues/processors/EmailsProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default class EmailsProcessor extends BaseProcessor {
},
{ notificationId }
).schedule({
delay: Minute,
delay: Minute.ms,
});
return;
}
Expand All @@ -76,7 +76,7 @@ export default class EmailsProcessor extends BaseProcessor {
},
{ notificationId }
).schedule({
delay: Minute,
delay: Minute.ms,
});
return;
}
Expand Down Expand Up @@ -107,7 +107,7 @@ export default class EmailsProcessor extends BaseProcessor {
},
{ notificationId: notification.id }
).schedule({
delay: Minute,
delay: Minute.ms,
});
return;
}
Expand All @@ -122,7 +122,7 @@ export default class EmailsProcessor extends BaseProcessor {
},
{ notificationId: notification.id }
).schedule({
delay: Minute,
delay: Minute.ms,
});
return;
}
Expand All @@ -139,7 +139,7 @@ export default class EmailsProcessor extends BaseProcessor {
},
{ notificationId: notification.id }
).schedule({
delay: Minute,
delay: Minute.ms,
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/queues/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function createQueue(
setInterval(async () => {
Metrics.gauge(`${prefix}.count`, await queue.count());
Metrics.gauge(`${prefix}.delayed_count`, await queue.getDelayedCount());
}, 5 * Second);
}, 5 * Second.ms);
}

ShutdownHelper.add(name, ShutdownOrder.normal, async () => {
Expand Down
8 changes: 4 additions & 4 deletions server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ router.use(["/images/*", "/email/*", "/fonts/*"], async (ctx, next) => {
done = await send(ctx, ctx.path, {
root: path.resolve(__dirname, "../../../public"),
// 7 day expiry, these assets are mostly static but do not contain a hash
maxAge: Day * 7,
maxAge: Day.ms * 7,
setHeaders: (res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
},
Expand Down Expand Up @@ -71,7 +71,7 @@ if (env.isProduction) {
await send(ctx, pathname, {
root: path.join(__dirname, "../../app/"),
// Hashed static assets get 1 year expiry plus immutable flag
maxAge: Day * 365,
maxAge: Day.ms * 365,
immutable: true,
setHeaders: (res) => {
res.setHeader("Service-Worker-Allowed", "/");
Expand Down Expand Up @@ -105,7 +105,7 @@ router.get("/locales/:lng.json", async (ctx) => {
await send(ctx, path.join(lng, "translation.json"), {
setHeaders: (res, _, stats) => {
res.setHeader("Last-Modified", formatRFC7231(stats.mtime));
res.setHeader("Cache-Control", `public, max-age=${(7 * Day) / 1000}`);
res.setHeader("Cache-Control", `public, max-age=${7 * Day.seconds}`);
res.setHeader(
"ETag",
crypto.createHash("md5").update(stats.mtime.toISOString()).digest("hex")
Expand All @@ -121,7 +121,7 @@ router.get("/robots.txt", (ctx) => {

router.get("/opensearch.xml", (ctx) => {
ctx.type = "text/xml";
ctx.response.set("Cache-Control", `public, max-age=${(7 * Day) / 1000}`);
ctx.response.set("Cache-Control", `public, max-age=${7 * Day.seconds}`);
ctx.body = opensearchResponse(ctx.request.URL.origin);
});

Expand Down
6 changes: 3 additions & 3 deletions server/services/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ export default function init() {
}
}

setInterval(() => void run(TaskSchedule.Daily), Day);
setInterval(() => void run(TaskSchedule.Hourly), Hour);
setInterval(() => void run(TaskSchedule.Daily), Day.ms);
setInterval(() => void run(TaskSchedule.Hourly), Hour.ms);

// Just give everything time to startup before running the first time. Not
// _technically_ required to function.
setTimeout(() => {
void run(TaskSchedule.Daily);
void run(TaskSchedule.Hourly);
}, 30 * Second);
}, 30 * Second.ms);
}
2 changes: 1 addition & 1 deletion server/services/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default function init(app: Koa = new Koa(), server?: Server) {
}
Metrics.gaugePerInstance("connections.count", count);
});
}, 5 * Second);
}, 5 * Second.ms);
}

ShutdownHelper.add("connections", ShutdownOrder.normal, async () => {
Expand Down
2 changes: 1 addition & 1 deletion server/utils/CacheHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Redis from "@server/storage/redis";
*/
export class CacheHelper {
// Default expiry time for cache data in seconds
private static defaultDataExpiry = Day / 1000;
private static defaultDataExpiry = Day.seconds;

/**
* Given a key, gets the data from cache store
Expand Down
34 changes: 26 additions & 8 deletions shared/utils/time.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
/** A second in ms */
export const Second = 1000;
export class Second {
/** Milliseconds in a second */
public static ms = 1000;
}

/** A minute in ms */
export const Minute = 60 * Second;
export class Minute {
/** Milliseconds in a minute */
public static ms = 60 * Second.ms;
/** Seconds in a minute */
public static seconds = 60;
}

/** An hour in ms */
export const Hour = 60 * Minute;
export class Hour {
/** Milliseconds in an hour */
public static ms = 60 * Minute.ms;
/** Seconds in an hour */
public static seconds = 60 * Minute.seconds;
/** Minutes in an hour */
public static minutes = 60;
}

/** A day in ms */
export const Day = 24 * Hour;
export class Day {
/** Milliseconds in a day */
public static ms = 24 * Hour.ms;
/** Seconds in a day */
public static seconds = 24 * Hour.seconds;
/** Minutes in a day */
public static minutes = 24 * Hour.minutes;
}

0 comments on commit 5d85a3a

Please sign in to comment.