<script lang="ts" context="module">
  const listeners: ((now: Date)=>void)[] = [];

  setInterval(() => {
    for (const listener of listeners) {
      listener(new Date);
    }
  }, 60000);
</script>


<script lang="ts">
  import { onMount } from "svelte";

  export let now: Date | number;

  function onMinute(date: Date) {
    if (typeof(now) === "number") {
      now = date.getTime();
    } else {
      now = date;
    }
  }

  onMount(() => {
    listeners.push(onMinute);

    return () => {
      const index = listeners.indexOf(onMinute);
      if (index !== -1) {
        listeners.splice(index, 1);
      }
    }
  })
</script>