Skip to content

Commit bb1c38d

Browse files
committed
Use injected STACK_SERVICE instead of passing StackService
Remove StackService/HooksService parameters from various components and handlers and inject STACK_SERVICE (and HOOKS_SERVICE where needed) directly within drop handler modules. This simplifies signatures and usage by treating stack service as a singleton injectable rather than passing it through many component props and handler constructors, reducing prop drilling and aligning with the stack service being a singleton.
1 parent 7d5afdd commit bb1c38d

8 files changed

Lines changed: 26 additions & 41 deletions

File tree

apps/desktop/src/components/BranchCard.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
>
169169
{#if args.type === "stack-branch"}
170170
{@const moveHandler = args.stackId
171-
? new MoveCommitDzHandler(stackService, args.stackId, projectId)
171+
? new MoveCommitDzHandler(args.stackId, projectId)
172172
: undefined}
173173
{#if !args.prNumber && args.stackId}
174174
<PrNumberUpdater {projectId} stackId={args.stackId} {branchName} />

apps/desktop/src/components/BranchCommitList.svelte

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,6 @@
306306
{@const { amendHandler, squashHandler, hunkHandler } = createCommitDropHandlers({
307307
projectId,
308308
stackId,
309-
stackService,
310-
hooksService,
311309
commit: dzCommit,
312310
runHooks: $runHooks,
313311
okWithForce: true,

apps/desktop/src/components/BranchInsertion.svelte

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import Dropzone from "$components/Dropzone.svelte";
55
import { MoveBranchDzHandler } from "$lib/branches/dropHandler";
66
import type { ForgePrService } from "$lib/forge/interface/forgePrService";
7-
import type { StackService } from "$lib/stacks/stackService.svelte";
87
98
interface Props {
109
projectId: string;
@@ -13,7 +12,6 @@
1312
lineColor: string;
1413
isCommitting: boolean;
1514
baseBranchName: string | undefined;
16-
stackService: StackService;
1715
prService: ForgePrService | undefined;
1816
isFirst?: boolean;
1917
}
@@ -25,15 +23,13 @@
2523
lineColor,
2624
isCommitting,
2725
baseBranchName,
28-
stackService,
2926
prService,
3027
isFirst = false,
3128
}: Props = $props();
3229
</script>
3330

3431
{#if !isCommitting && baseBranchName}
3532
{@const moveBranchHandler = new MoveBranchDzHandler(
36-
stackService,
3733
prService,
3834
projectId,
3935
stackId,

apps/desktop/src/components/BranchList.svelte

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@
145145
{lineColor}
146146
isCommitting={controller.isCommitting}
147147
{baseBranchName}
148-
{stackService}
149148
prService={forge.current.prService}
150149
isFirst={firstBranch}
151150
/>

apps/desktop/src/components/StackDetails.svelte

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,6 @@
148148
? createCommitDropHandlers({
149149
projectId: controller.projectId,
150150
stackId: controller.stackId,
151-
stackService,
152-
hooksService,
153151
commit: dzCommit,
154152
runHooks: $runHooks,
155153
okWithForce: true,

apps/desktop/src/components/WorktreeChanges.svelte

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import { FILE_SELECTION_MANAGER } from "$lib/selection/fileSelectionManager.svelte";
1919
import { createWorktreeSelection } from "$lib/selection/key";
2020
import { UNCOMMITTED_SERVICE } from "$lib/selection/uncommittedService.svelte";
21-
import { STACK_SERVICE } from "$lib/stacks/stackService.svelte";
2221
import { UI_STATE } from "$lib/state/uiState.svelte";
2322
import { inject, injectOptional } from "@gitbutler/core/context";
2423
@@ -61,7 +60,6 @@
6160
// Create a unique persist ID based on stackId and mode (both are static props)
6261
const persistId = stackId ? `worktree-${mode}-${stackId}` : `worktree-${mode}`;
6362
64-
const stackService = inject(STACK_SERVICE);
6563
const diffService = inject(DIFF_SERVICE);
6664
const uncommittedService = inject(UNCOMMITTED_SERVICE);
6765
const uiState = inject(UI_STATE);
@@ -80,7 +78,7 @@
8078
// Create selectionId for this worktree lane
8179
const selectionId = $derived(createWorktreeSelection({ stackId }));
8280
83-
const uncommitDzHandler = $derived(new UncommitDzHandler(projectId, stackService, stackId));
81+
const uncommitDzHandler = $derived(new UncommitDzHandler(projectId, stackId));
8482
8583
const projectState = $derived(uiState.project(projectId));
8684
const exclusiveAction = $derived(projectState.exclusiveAction.current);

apps/desktop/src/lib/branches/dropHandler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { FileChangeDropData, FolderChangeDropData, HunkDropDataV3 } from "$lib/dragging/draggables";
22
import { updateStackPrs } from "$lib/forge/shared/prFooter";
33
import { UNCOMMITTED_SERVICE } from "$lib/selection/uncommittedService.svelte";
4+
import { STACK_SERVICE } from "$lib/stacks/stackService.svelte";
45
import { UI_STATE } from "$lib/state/uiState.svelte";
56
import { inject } from "@gitbutler/core/context";
67
import type { DropzoneHandler } from "$lib/dragging/handler";
78
import type { ForgePrService } from "$lib/forge/interface/forgePrService";
8-
import type { StackService } from "$lib/stacks/stackService.svelte";
99

1010
export class BranchDropData {
1111
constructor(
@@ -24,8 +24,9 @@ export class BranchDropData {
2424
}
2525

2626
export class MoveBranchDzHandler implements DropzoneHandler {
27+
private readonly stackService = inject(STACK_SERVICE);
28+
2729
constructor(
28-
private readonly stackService: StackService,
2930
private readonly prService: ForgePrService | undefined,
3031
private readonly projectId: string,
3132
private readonly stackId: string,

apps/desktop/src/lib/commits/dropHandler.ts

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import {
99
HunkDropDataV3,
1010
type ChangeDropData,
1111
} from "$lib/dragging/draggables";
12-
import { type HooksService } from "$lib/hooks/hooksService";
12+
import { HOOKS_SERVICE } from "$lib/hooks/hooksService";
1313
import { showToast } from "$lib/notifications/toasts";
14+
import { STACK_SERVICE } from "$lib/stacks/stackService.svelte";
15+
import { UI_STATE, type UiState } from "$lib/state/uiState.svelte";
16+
import { inject } from "@gitbutler/core/context";
1417
import { untrack } from "svelte";
1518
import type { DropzoneHandler } from "$lib/dragging/handler";
16-
import type { StackService } from "$lib/stacks/stackService.svelte";
17-
import type { UiState } from "$lib/state/uiState.svelte";
1819

1920
/** Details about a commit belonging to a drop zone. */
2021
export type DzCommitData = {
@@ -37,9 +38,9 @@ export class CommitDropData {
3738
/** Handler that can move commits between stacks. */
3839
export class MoveCommitDzHandler implements DropzoneHandler {
3940
private readonly uiState = inject(UI_STATE);
41+
private readonly stackService = inject(STACK_SERVICE);
4042

4143
constructor(
42-
private stackService: StackService,
4344
private stackId: string,
4445
private projectId: string,
4546
) {}
@@ -83,15 +84,16 @@ export class MoveCommitDzHandler implements DropzoneHandler {
8384
* Handler that will be able to amend a commit using `TreeChange`.
8485
*/
8586
export class AmendCommitWithChangeDzHandler implements DropzoneHandler {
87+
private readonly uiState = inject(UI_STATE);
88+
private readonly stackService = inject(STACK_SERVICE);
89+
private readonly hooksService = inject(HOOKS_SERVICE);
90+
8691
constructor(
8792
private projectId: string,
88-
private readonly stackService: StackService,
89-
private readonly hooksService: HooksService,
9093
private stackId: string,
9194
private runHooks: boolean,
9295
private commit: DzCommitData,
9396
private onresult: (result: string) => void,
94-
private readonly uiState: UiState,
9597
) {}
9698
accepts(data: unknown): boolean {
9799
if (!(data instanceof FileChangeDropData || data instanceof FolderChangeDropData)) return false;
@@ -156,10 +158,10 @@ export class AmendCommitWithChangeDzHandler implements DropzoneHandler {
156158

157159
export class UncommitDzHandler implements DropzoneHandler {
158160
private readonly uiState = inject(UI_STATE);
161+
private readonly stackService = inject(STACK_SERVICE);
159162

160163
constructor(
161164
private projectId: string,
162-
private readonly stackService: StackService,
163165
private readonly assignTo?: string,
164166
) {}
165167

@@ -250,11 +252,11 @@ export class UncommitDzHandler implements DropzoneHandler {
250252
*/
251253
export class AmendCommitWithHunkDzHandler implements DropzoneHandler {
252254
private readonly uiState = inject(UI_STATE);
255+
private readonly stackService = inject(STACK_SERVICE);
256+
private readonly hooksService = inject(HOOKS_SERVICE);
253257

254258
constructor(
255259
private args: {
256-
stackService: StackService;
257-
hooksService: HooksService;
258260
okWithForce: boolean;
259261
projectId: string;
260262
stackId: string;
@@ -276,7 +278,7 @@ export class AmendCommitWithHunkDzHandler implements DropzoneHandler {
276278
}
277279

278280
async ondrop(data: HunkDropDataV3): Promise<void> {
279-
const { stackService, projectId, stackId, commit, okWithForce, runHooks } = this.args;
281+
const { projectId, stackId, commit, okWithForce, runHooks } = this.args;
280282
if (!okWithForce && commit.isRemote) return;
281283

282284
if (data instanceof HunkDropDataV3) {
@@ -288,7 +290,7 @@ export class AmendCommitWithHunkDzHandler implements DropzoneHandler {
288290
throw new Error("Can't receive a change without it's source or commit");
289291
}
290292

291-
const { replacedCommits } = await stackService.moveChangesBetweenCommits({
293+
const { replacedCommits } = await this.stackService.moveChangesBetweenCommits({
292294
projectId,
293295
destinationStackId: stackId,
294296
destinationCommitId: commit.id,
@@ -334,20 +336,20 @@ export class AmendCommitWithHunkDzHandler implements DropzoneHandler {
334336

335337
if (runHooks) {
336338
try {
337-
await this.args.hooksService.runPreCommitHooks(projectId, worktreeChanges);
339+
await this.hooksService.runPreCommitHooks(projectId, worktreeChanges);
338340
} catch {
339341
return;
340342
}
341343
}
342-
stackService.amendCommitMutation({
344+
this.stackService.amendCommitMutation({
343345
projectId,
344346
stackId,
345347
commitId: commit.id,
346348
worktreeChanges,
347349
});
348350
if (runHooks) {
349351
try {
350-
await this.args.hooksService.runPostCommitHooks(projectId);
352+
await this.hooksService.runPostCommitHooks(projectId);
351353
} catch {
352354
return;
353355
}
@@ -360,9 +362,10 @@ export class AmendCommitWithHunkDzHandler implements DropzoneHandler {
360362
* Handler that is able to squash two commits using `DzCommitData`.
361363
*/
362364
export class SquashCommitDzHandler implements DropzoneHandler {
365+
private readonly stackService = inject(STACK_SERVICE);
366+
363367
constructor(
364368
private args: {
365-
stackService: StackService;
366369
projectId: string;
367370
stackId: string;
368371
commit: DzCommitData;
@@ -381,9 +384,9 @@ export class SquashCommitDzHandler implements DropzoneHandler {
381384
}
382385

383386
async ondrop(data: unknown) {
384-
const { stackService, projectId, stackId, commit } = this.args;
387+
const { projectId, stackId, commit } = this.args;
385388
if (data instanceof CommitDropData) {
386-
await stackService.squashCommits({
389+
await this.stackService.squashCommits({
387390
projectId,
388391
stackId,
389392
sourceCommitIds: [data.commit.id],
@@ -413,8 +416,6 @@ function updateUiState(
413416
export function createCommitDropHandlers(args: {
414417
projectId: string;
415418
stackId: string | undefined;
416-
stackService: StackService;
417-
hooksService: HooksService;
418419
commit: DzCommitData;
419420
runHooks: boolean;
420421
onCommitIdChange?: (newCommitId: string) => void;
@@ -436,27 +437,21 @@ export function createCommitDropHandlers(args: {
436437

437438
const amendHandler = new AmendCommitWithChangeDzHandler(
438439
args.projectId,
439-
args.stackService,
440-
args.hooksService,
441440
stackId,
442441
args.runHooks,
443442
commit,
444443
(newId) => {
445444
onCommitIdChange?.(newId);
446445
},
447-
args.uiState,
448446
);
449447

450448
const squashHandler = new SquashCommitDzHandler({
451-
stackService: args.stackService,
452449
projectId: args.projectId,
453450
stackId,
454451
commit,
455452
});
456453

457454
const hunkHandler = new AmendCommitWithHunkDzHandler({
458-
stackService: args.stackService,
459-
hooksService: args.hooksService,
460455
projectId: args.projectId,
461456
stackId,
462457
commit,

0 commit comments

Comments
 (0)