Code Library

  1. const round = (number, step) => {
      return Math.round(number / step) * step;
    };
  2. <script>
      import {onMount, createEventDispatcher} from 'svelte';
      export let $foo = 0;
    
      const dispatch = createEventDispatcher();
    
      onMount(() => {
       const t = setTimeout(() => {
         foo++;
         dispatch("fooupdate", foo)
       }, 100);
       return (() => {
         clearTimeout(t)
       });
      });
    </script>
  3. export const latest = <A extends Array<unknown>, T>(fn: (...args: A) => Promise<T>) => {
      let invocationId: object;
    
      return async (...args: A) => {
        const thisInvocationId = (invocationId = new Object());
        const result = await fn(...args);
    
        if (invocationId !== thisInvocationId) {
          throw new Error('Function invocation superseded', { cause: 'SUPERSEDED' });
        }
        return result;
      };
    };
  4. self.addEventListener('install', () => self.skipWaiting());
    self.addEventListener('activate', () =>
      self.registration
        .unregister()
        .then(() => self.clients.matchAll())
        .then((clients) => clients.forEach((client) => client.navigate(client.url)))
    );