From b3699e2b5ae695d43aa9ae908a7562b20555ede8 Mon Sep 17 00:00:00 2001 From: Caleb Owens Date: Wed, 20 Mar 2024 21:08:03 +0000 Subject: [PATCH] Refactor away random.ts Removed unused/duplicate methods and moved unsubscribe() to a better named file --- .../src/lib/components/Navigation.svelte | 2 +- gitbutler-ui/src/lib/utils/random.ts | 19 ------------------- gitbutler-ui/src/lib/utils/unsubscribe.ts | 7 +++++++ gitbutler-ui/src/routes/+layout.svelte | 2 +- .../src/routes/[projectId]/+layout.svelte | 2 +- 5 files changed, 10 insertions(+), 22 deletions(-) delete mode 100644 gitbutler-ui/src/lib/utils/random.ts create mode 100644 gitbutler-ui/src/lib/utils/unsubscribe.ts diff --git a/gitbutler-ui/src/lib/components/Navigation.svelte b/gitbutler-ui/src/lib/components/Navigation.svelte index a88fa2997..3b1c923c2 100644 --- a/gitbutler-ui/src/lib/components/Navigation.svelte +++ b/gitbutler-ui/src/lib/components/Navigation.svelte @@ -8,7 +8,7 @@ import { persisted } from '$lib/persisted/persisted'; import { SETTINGS_CONTEXT, type SettingsStore } from '$lib/settings/userSettings'; import * as hotkeys from '$lib/utils/hotkeys'; - import { unsubscribe } from '$lib/utils/random'; + import { unsubscribe } from '$lib/utils/unsubscribe'; import { platform } from '@tauri-apps/api/os'; import { from } from 'rxjs'; import { onMount } from 'svelte'; diff --git a/gitbutler-ui/src/lib/utils/random.ts b/gitbutler-ui/src/lib/utils/random.ts deleted file mode 100644 index d822bf8b9..000000000 --- a/gitbutler-ui/src/lib/utils/random.ts +++ /dev/null @@ -1,19 +0,0 @@ -export function debounce any>(fn: T, delay: number) { - let timeout: ReturnType; - return (...args: any[]) => { - clearTimeout(timeout); - timeout = setTimeout(() => fn(...args), delay); - }; -} - -export function clone(obj: T): T { - return structuredClone(obj); -} - -type MaybePromise = T | Promise; - -export function unsubscribe(...unsubscribers: MaybePromise<() => void>[]) { - return () => { - unsubscribers.forEach((unsubscriber) => Promise.resolve(unsubscriber).then((fn) => fn())); - }; -} diff --git a/gitbutler-ui/src/lib/utils/unsubscribe.ts b/gitbutler-ui/src/lib/utils/unsubscribe.ts new file mode 100644 index 000000000..782347317 --- /dev/null +++ b/gitbutler-ui/src/lib/utils/unsubscribe.ts @@ -0,0 +1,7 @@ +type MaybePromise = T | Promise; + +export function unsubscribe(...unsubscribers: MaybePromise<() => void>[]) { + return () => { + unsubscribers.forEach((unsubscriber) => Promise.resolve(unsubscriber).then((fn) => fn())); + }; +} diff --git a/gitbutler-ui/src/routes/+layout.svelte b/gitbutler-ui/src/routes/+layout.svelte index 1e69a2ae9..5ad99d215 100644 --- a/gitbutler-ui/src/routes/+layout.svelte +++ b/gitbutler-ui/src/routes/+layout.svelte @@ -15,8 +15,8 @@ import { UserService } from '$lib/stores/user'; import * as events from '$lib/utils/events'; import * as hotkeys from '$lib/utils/hotkeys'; - import { unsubscribe } from '$lib/utils/random'; import { initTheme } from '$lib/utils/theme'; + import { unsubscribe } from '$lib/utils/unsubscribe'; import { onMount, setContext } from 'svelte'; import { Toaster } from 'svelte-french-toast'; import type { LayoutData } from './$types'; diff --git a/gitbutler-ui/src/routes/[projectId]/+layout.svelte b/gitbutler-ui/src/routes/[projectId]/+layout.svelte index c5b56fb40..a6af74bea 100644 --- a/gitbutler-ui/src/routes/[projectId]/+layout.svelte +++ b/gitbutler-ui/src/routes/[projectId]/+layout.svelte @@ -7,7 +7,7 @@ import ProblemLoadingRepo from '$lib/components/ProblemLoadingRepo.svelte'; import { subscribe as menuSubscribe } from '$lib/menu'; import * as hotkeys from '$lib/utils/hotkeys'; - import { unsubscribe } from '$lib/utils/random'; + import { unsubscribe } from '$lib/utils/unsubscribe'; import { BranchController } from '$lib/vbranches/branchController'; import { BaseBranchService } from '$lib/vbranches/branchStoresCache'; import { BaseBranch } from '$lib/vbranches/types';