From 913f1c620eaa3155f425b3c8de58097652022fb7 Mon Sep 17 00:00:00 2001 From: tglide <26071571+TGlide@users.noreply.github.com> Date: Thu, 2 Feb 2023 17:12:17 +0000 Subject: [PATCH 1/5] feat: function execution timer --- src/lib/actions/timer.ts | 40 +++++++++++++++++++ .../executions/[[page]]/+page.svelte | 8 +++- tsconfig.json | 8 +++- 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 src/lib/actions/timer.ts diff --git a/src/lib/actions/timer.ts b/src/lib/actions/timer.ts new file mode 100644 index 000000000..1e5f9411e --- /dev/null +++ b/src/lib/actions/timer.ts @@ -0,0 +1,40 @@ +import { calculateTime } from '$lib/helpers/timeConversion'; +import type { Action } from 'svelte/action'; + +type Props = { + start?: string; + enabled?: boolean; +}; + +const defaultProps: Props = { + enabled: true +}; + +export const timer: Action = (node, props) => { + let startDate: Date; + let frame: number; + + function init(props: Props) { + const { start, enabled } = { ...defaultProps, ...props }; + startDate = start ? new Date(start) : new Date(); + frame = enabled ? window.requestAnimationFrame(step) : null; + } + + function step() { + const diffInSeconds = Math.floor((new Date().getTime() - startDate.getTime()) / 1000); + node.textContent = calculateTime(diffInSeconds); + frame = window.requestAnimationFrame(step); + } + + init(props); + + return { + update(props) { + window.cancelAnimationFrame(frame); + init(props); + }, + destroy() { + window.cancelAnimationFrame(frame); + } + }; +}; diff --git a/src/routes/console/project-[project]/functions/function-[function]/executions/[[page]]/+page.svelte b/src/routes/console/project-[project]/functions/function-[function]/executions/[[page]]/+page.svelte index 6c0ba8502..2e9c766e5 100644 --- a/src/routes/console/project-[project]/functions/function-[function]/executions/[[page]]/+page.svelte +++ b/src/routes/console/project-[project]/functions/function-[function]/executions/[[page]]/+page.svelte @@ -23,6 +23,7 @@ import { invalidate } from '$app/navigation'; import { Button } from '$lib/elements/forms'; import CreateDeployment from '../../create.svelte'; + import { timer } from '$lib/actions/timer'; export let data: PageData; let showCreate = false; @@ -83,7 +84,12 @@ - {calculateTime(execution.duration)} + {#if execution.duration === 0} + + {:else} + {calculateTime(execution.duration)} + {/if} +