Enough factories to make the once-ler happy

This commit is contained in:
Caleb Owens 2024-06-19 11:43:06 +02:00
parent 80209b877e
commit ad03baf303
No known key found for this signature in database
7 changed files with 27 additions and 43 deletions

View File

@ -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;

View File

@ -8,8 +8,6 @@
}
const { hovered, activated, yOffsetPx = 0 }: Props = $props();
console.log(yOffsetPx, pxToRem(yOffsetPx));
</script>
<div

View File

@ -1,5 +1,4 @@
import { DraggableCommit } from '$lib/dragging/draggables';
import { buildConstructorStore } from '$lib/utils/context';
import type { BranchController } from '$lib/vbranches/branchController';
import type { Branch, Commit } from '$lib/vbranches/types';
@ -22,15 +21,14 @@ import type { Branch, Commit } from '$lib/vbranches/types';
// Exported for type access only
export class ReorderDropzone {
constructor(
private branchController: BranchController,
private branch: Branch,
private index: number,
private indexer: ReorderDropzoneManager,
private branchController: BranchController
private indexer: ReorderDropzoneManager
) {}
accepts(data: any) {
if (!(data instanceof DraggableCommit)) return false;
console.log(this.branch);
if (data.branchId !== this.branch.id) return false;
if (this.indexer.dropzoneCommitOffset(this.index, data.commit.id) === 0) return false;
@ -51,9 +49,9 @@ export class ReorderDropzoneManager {
private commitIndexes = new Map<string, number>();
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<typeof ReorderDropzoneManager>('ReorderDropzoneManager');
export class ReorderDropzoneManagerFactory {
constructor(private branchController: BranchController) {}
build(branch: Branch, commits: Commit[]) {
return new ReorderDropzoneManager(this.branchController, branch, commits);
}
}

View File

@ -100,20 +100,3 @@ export function getContextStoreBySymbol<T, S extends Readable<T> = Readable<T>>(
if (!instance) throw new Error(`no instance of \`Readable<${key.toString()}[]>\` in context`);
return instance;
}
export function buildConstructorStore<T extends Class>(
name: string
): [() => T, (constructor: T) => void] {
const identifier = Symbol(name);
return [
() => {
const constructor = svelteGetContext<T | undefined>(identifier);
if (!constructor) throw new Error(`no constructor in context \`${name}\``);
return constructor;
},
(constructor: T) => {
setContext(identifier, constructor);
}
];
}

View File

@ -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;

View File

@ -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');

View File

@ -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
};
}