From ad03baf303cc08e731c30231ef39f40611f43d0c Mon Sep 17 00:00:00 2001 From: Caleb Owens Date: Wed, 19 Jun 2024 11:43:06 +0200 Subject: [PATCH] Enough factories to make the once-ler happy --- app/src/lib/components/CommitList.svelte | 13 +++++------ .../components/Dropzone/LineOverlay.svelte | 2 -- .../lib/dragging/reorderDropzoneManager.ts | 23 +++++++++++-------- app/src/lib/utils/context.ts | 17 -------------- app/src/routes/+layout.svelte | 5 ---- app/src/routes/[projectId]/+layout.svelte | 5 +++- app/src/routes/[projectId]/+layout.ts | 5 +++- 7 files changed, 27 insertions(+), 43 deletions(-) diff --git a/app/src/lib/components/CommitList.svelte b/app/src/lib/components/CommitList.svelte index 73134f998..6c27a42b0 100644 --- a/app/src/lib/components/CommitList.svelte +++ b/app/src/lib/components/CommitList.svelte @@ -4,7 +4,7 @@ import { Project } from '$lib/backend/projects'; import InsertEmptyCommitAction from '$lib/components/InsertEmptyCommitAction.svelte'; import { - getReorderDropzoneManager, + ReorderDropzoneManagerFactory, type ReorderDropzone } from '$lib/dragging/reorderDropzoneManager'; import { getAvatarTooltip } from '$lib/utils/avatar'; @@ -33,7 +33,7 @@ const project = getContext(Project); const branchController = getContext(BranchController); - const ReorderDropzoneManager = getReorderDropzoneManager(); + const reorderDropzoneManagerFactory = getContext(ReorderDropzoneManagerFactory); // Force the "base" commit lines to update when $branch updates. let tsKey: number | undefined; @@ -50,11 +50,10 @@ $: hasIntegratedCommits = $integratedCommits.length > 0; $: hasRemoteCommits = $remoteCommits.length > 0; $: hasShadowedCommits = $localCommits.some((c) => c.relatedTo); - $: reorderDropzoneManager = new ReorderDropzoneManager( - [...$localCommits, ...$remoteCommits], - $branch, - branchController - ); + $: reorderDropzoneManager = reorderDropzoneManagerFactory.build($branch, [ + ...$localCommits, + ...$remoteCommits + ]); $: forkPoint = $branch.forkPoint; $: upstreamForkPoint = $branch.upstreamData?.forkPoint; diff --git a/app/src/lib/components/Dropzone/LineOverlay.svelte b/app/src/lib/components/Dropzone/LineOverlay.svelte index cfc3b6de8..e4f58df81 100644 --- a/app/src/lib/components/Dropzone/LineOverlay.svelte +++ b/app/src/lib/components/Dropzone/LineOverlay.svelte @@ -8,8 +8,6 @@ } const { hovered, activated, yOffsetPx = 0 }: Props = $props(); - - console.log(yOffsetPx, pxToRem(yOffsetPx));
(); constructor( - commits: Commit[], + private branchController: BranchController, private branch: Branch, - private branchController: BranchController + commits: Commit[] ) { this.dropzoneIndexes.set('top', 0); @@ -66,7 +64,7 @@ export class ReorderDropzoneManager { get topDropzone() { const index = this.dropzoneIndexes.get('top') ?? 0; - return new ReorderDropzone(this.branch, index, this, this.branchController); + return new ReorderDropzone(this.branchController, this.branch, index, this); } dropzoneBelowCommit(commitId: string) { @@ -76,7 +74,7 @@ export class ReorderDropzoneManager { throw new Error(`Commit ${commitId} not found in dropzoneIndexes`); } - return new ReorderDropzone(this.branch, index, this, this.branchController); + return new ReorderDropzone(this.branchController, this.branch, index, this); } commitIndex(commitId: string) { @@ -109,5 +107,10 @@ export class ReorderDropzoneManager { } } -export const [getReorderDropzoneManager, setReorderDropzoneManager] = - buildConstructorStore('ReorderDropzoneManager'); +export class ReorderDropzoneManagerFactory { + constructor(private branchController: BranchController) {} + + build(branch: Branch, commits: Commit[]) { + return new ReorderDropzoneManager(this.branchController, branch, commits); + } +} diff --git a/app/src/lib/utils/context.ts b/app/src/lib/utils/context.ts index 2413ce3be..3b9a95726 100644 --- a/app/src/lib/utils/context.ts +++ b/app/src/lib/utils/context.ts @@ -100,20 +100,3 @@ export function getContextStoreBySymbol = Readable>( if (!instance) throw new Error(`no instance of \`Readable<${key.toString()}[]>\` in context`); return instance; } - -export function buildConstructorStore( - name: string -): [() => T, (constructor: T) => void] { - const identifier = Symbol(name); - - return [ - () => { - const constructor = svelteGetContext(identifier); - if (!constructor) throw new Error(`no constructor in context \`${name}\``); - return constructor; - }, - (constructor: T) => { - setContext(identifier, constructor); - } - ]; -} diff --git a/app/src/routes/+layout.svelte b/app/src/routes/+layout.svelte index 2117b2dfd..87ecea306 100644 --- a/app/src/routes/+layout.svelte +++ b/app/src/routes/+layout.svelte @@ -13,10 +13,6 @@ import GlobalSettingsMenuAction from '$lib/components/GlobalSettingsMenuAction.svelte'; import PromptModal from '$lib/components/PromptModal.svelte'; import ShareIssueModal from '$lib/components/ShareIssueModal.svelte'; - import { - ReorderDropzoneManager, - setReorderDropzoneManager - } from '$lib/dragging/reorderDropzoneManager'; import { GitHubService } from '$lib/github/service'; import ToastController from '$lib/notifications/ToastController.svelte'; import { RemotesService } from '$lib/remotes/service'; @@ -51,7 +47,6 @@ setContext(User, data.userService.user); setContext(RemotesService, data.remotesService); setContext(AIPromptService, data.aiPromptService); - setReorderDropzoneManager(ReorderDropzoneManager); let shareIssueModal: ShareIssueModal; diff --git a/app/src/routes/[projectId]/+layout.svelte b/app/src/routes/[projectId]/+layout.svelte index a8b7f41ef..d8d4d113e 100644 --- a/app/src/routes/[projectId]/+layout.svelte +++ b/app/src/routes/[projectId]/+layout.svelte @@ -10,6 +10,7 @@ import NotOnGitButlerBranch from '$lib/components/NotOnGitButlerBranch.svelte'; import ProblemLoadingRepo from '$lib/components/ProblemLoadingRepo.svelte'; import ProjectSettingsMenuAction from '$lib/components/ProjectSettingsMenuAction.svelte'; + import { ReorderDropzoneManagerFactory } from '$lib/dragging/reorderDropzoneManager'; import { HistoryService } from '$lib/history/history'; import { persisted } from '$lib/persisted/persisted'; import * as events from '$lib/utils/events'; @@ -34,7 +35,8 @@ branchService, branchController, branchDragActionsFactory, - commitDragActionsFactory + commitDragActionsFactory, + reorderDropzoneManagerFactory } = data); $: branchesError = vbranchService.branchesError; @@ -51,6 +53,7 @@ $: setContext(Project, project); $: setContext(BranchDragActionsFactory, branchDragActionsFactory); $: setContext(CommitDragActionsFactory, commitDragActionsFactory); + $: setContext(ReorderDropzoneManagerFactory, reorderDropzoneManagerFactory); const showHistoryView = persisted(false, 'showHistoryView'); diff --git a/app/src/routes/[projectId]/+layout.ts b/app/src/routes/[projectId]/+layout.ts index a8fa2d195..b3fd1af29 100644 --- a/app/src/routes/[projectId]/+layout.ts +++ b/app/src/routes/[projectId]/+layout.ts @@ -2,6 +2,7 @@ import { invoke } from '$lib/backend/ipc'; import { BranchDragActionsFactory } from '$lib/branches/dragActions.js'; import { BranchService } from '$lib/branches/service'; import { CommitDragActionsFactory } from '$lib/commits/dragActions.js'; +import { ReorderDropzoneManagerFactory } from '$lib/dragging/reorderDropzoneManager'; import { HistoryService } from '$lib/history/history'; import { getFetchNotifications } from '$lib/stores/fetches'; import { getHeads } from '$lib/stores/head'; @@ -67,6 +68,7 @@ export async function load({ params, parent }) { const branchDragActionsFactory = new BranchDragActionsFactory(branchController); const commitDragActionsFactory = new CommitDragActionsFactory(branchController, project); + const reorderDropzoneManagerFactory = new ReorderDropzoneManagerFactory(branchController); return { authService, @@ -83,6 +85,7 @@ export async function load({ params, parent }) { // These observables are provided for convenience gbBranchActive$, branchDragActionsFactory, - commitDragActionsFactory + commitDragActionsFactory, + reorderDropzoneManagerFactory }; }