Skip to content

Commit

Permalink
refactor: use updateModelState to apply side effects
Browse files Browse the repository at this point in the history
  • Loading branch information
senyai committed Nov 23, 2024
1 parent 8415156 commit b82edc1
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 93 deletions.
4 changes: 1 addition & 3 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,7 @@ export class Model implements Disposable {
for (const { oldUri, newUri } of e.files) {
const repository = this.getRepository(oldUri);
if (repository) {
await repository.updateModelState(
'file rename event' as Reason
);
await repository.updateStatus('file rename event' as Reason);
if (
repository.isInAnyGroup(oldUri) ||
repository.isDirInAnyGroup(oldUri)
Expand Down
76 changes: 33 additions & 43 deletions src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,8 @@ export class Repository implements IDisposable, InteractionAPI {
this.statusBar,
this.disposables
);
this.updateModelState('opening repository' as Reason).then(() =>
this.updateAutoSyncInterval(typedConfig.autoSyncIntervalMs)
this.updateModelState(UpdateAll, 'opening repository' as Reason).then(
() => this.updateAutoSyncInterval(typedConfig.autoSyncIntervalMs)
);
}

Expand Down Expand Up @@ -428,7 +428,7 @@ export class Repository implements IDisposable, InteractionAPI {
@throttle
private async updateWhenIdleAndWait(): Promise<void> {
await this.whenIdleAndFocused();
await this.updateModelState('idle update' as Reason);
await this.updateModelState(UpdateStatus, 'idle update' as Reason);
await delay(5000);
}

Expand Down Expand Up @@ -902,40 +902,10 @@ export class Repository implements IDisposable, InteractionAPI {

try {
const operationResult = await runOperation();
const sidePromises: Promise<void>[] = [];
if (sideEffects.status) {
sidePromises.push(
this.updateStatus(
'Triggered by previous operation' as Reason
).then(err => {
if (err) {
if (
err.fossilErrorCode ===
'NotAFossilRepository'
) {
this.state = RepositoryState.Disposed;
} else {
throw new Error(
`Unexpected fossil result: ${String(
err
)}`
);
}
}
})
);
}
if (sideEffects.changes) {
sidePromises.push(
this.updateChanges(
'Triggered by previous operation' as Reason
)
);
}
if (sideEffects.branch) {
sidePromises.push(this.updateBranch());
}
await Promise.all(sidePromises);
await this.updateModelState(
sideEffects,
'Triggered by previous operation' as Reason
);
return operationResult;
} finally {
this._operations.delete(key);
Expand Down Expand Up @@ -1045,15 +1015,35 @@ export class Repository implements IDisposable, InteractionAPI {
*/
@throttle
public async updateModelState(
sideEffects: SideEffects,
reason: Reason = 'model state is updating' as Reason
): Promise<ExecFailure | undefined> {
const res = await this.updateStatus(reason);
await this.updateChanges(reason);
await this.updateBranch();
return res;
): Promise<void> {
const sidePromises: Promise<void>[] = [];
if (sideEffects.status) {
sidePromises.push(
this.updateStatus(reason).then(err => {
if (err) {
if (err.fossilErrorCode === 'NotAFossilRepository') {
this.state = RepositoryState.Disposed;
} else {
throw new Error(
`Unexpected fossil result: ${String(err)}`
);
}
}
})
);
}
if (sideEffects.changes) {
sidePromises.push(this.updateChanges(reason));
}
if (sideEffects.branch) {
sidePromises.push(this.updateBranch());
}
await Promise.all(sidePromises);
}

private async updateStatus(
public async updateStatus(
reason?: Reason
): Promise<ExecFailure | undefined> {
const result = await this.repository.getStatus(reason);
Expand Down
18 changes: 9 additions & 9 deletions src/test/suite/commandSuites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function StatusSuite(this: Suite): void {
);
await fs.unlink(path.fsPath);
const repository = getRepository();
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);
assertGroups(repository, {
working: [[path.fsPath, ResourceStatus.MISSING]],
});
Expand All @@ -58,14 +58,14 @@ export function StatusSuite(this: Suite): void {
const oldFilename = 'sriciscp-new.txt';
const newFilename = 'sriciscp-renamed.txt';
const oldUri = await add(oldFilename, 'test\n', `add ${oldFilename}`);
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);
assertGroups(repository, {});

const openedRepository: OpenedRepository = (repository as any)
.repository;

await openedRepository.exec(['mv', oldFilename, newFilename, '--hard']);
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);
const barPath = Uri.joinPath(oldUri, '..', newFilename).fsPath;
assertGroups(repository, {
working: [[barPath, ResourceStatus.RENAMED]],
Expand Down Expand Up @@ -96,7 +96,7 @@ export function StatusSuite(this: Suite): void {

await openedRepository.exec(['update', 'trunk']);
await openedRepository.exec(['merge', 'test_brunch']);
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);
assertGroups(repository, {
working: [
[barPath, ResourceStatus.ADDED],
Expand Down Expand Up @@ -174,7 +174,7 @@ export function StatusSuite(this: Suite): void {
await fs.unlink(not_file_path);
await fs.mkdir(not_file_path);

await repository.updateModelState('Test' as Reason);
await repository.updateStatus('Test' as Reason);
assertGroups(repository, {
working: [
[executable_path, ResourceStatus.MODIFIED],
Expand All @@ -196,7 +196,7 @@ export function StatusSuite(this: Suite): void {
const repository = getRepository();
const execStub = getExecStub(this.ctx.sandbox);
await fakeFossilStatus(execStub, status);
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);
const root = vscode.workspace.workspaceFolders![0].uri;
const uriBefore = Uri.joinPath(root, before);
const uriAfter = Uri.joinPath(root, after);
Expand Down Expand Up @@ -317,7 +317,7 @@ export function CleanSuite(this: Suite): void {
.withArgs(sinon.match.array.startsWith(['clean']))
.resolves();
await fakeFossilStatus(execStub, 'EXTRA a.txt\nEXTRA b.txt');
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);
assertGroups(repository, {
untracked: [
[Uri.joinPath(rootUri, 'a.txt').fsPath, ResourceStatus.EXTRA],
Expand Down Expand Up @@ -358,7 +358,7 @@ export function CleanSuite(this: Suite): void {
execStub,
'EXTRA a.txt\nEXTRA b.txt\nEXTRA c.txt'
);
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);
assertGroups(repository, {
untracked: [
[Uri.joinPath(rootUri, 'a.txt').fsPath, ResourceStatus.EXTRA],
Expand Down Expand Up @@ -429,7 +429,7 @@ export function DiffSuite(this: Suite): void {
const uri = Uri.joinPath(rootUri, 'a_path.txt');
const execStub = getExecStub(this.ctx.sandbox);
const statusCall = fakeFossilStatus(execStub, 'ADDED a_path.txt');
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);
sinon.assert.calledOnce(statusCall);

const testTd = { isUntitled: false } as vscode.TextDocument;
Expand Down
22 changes: 11 additions & 11 deletions src/test/suite/commitSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const commitStagedTest = async (
const commitStub = execStub
.withArgs(sinon.match.array.startsWith(['commit']))
.resolves(fakeExecutionResult());
await repository.updateModelState();
await repository.updateStatus();
sinon.assert.calledOnce(statusStub);
await commands.executeCommand('fossil.stageAll');
sinon.assert.calledOnce(statusStub);
Expand Down Expand Up @@ -62,7 +62,7 @@ const singleFileCommitSetup = async (
const repository = getRepository();
const execStub = getExecStub(sandbox);
const statusStub = fakeFossilStatus(execStub, 'ADDED minimal.txt\n');
await repository.updateModelState('test' as Reason);
await repository.updateStatus('test' as Reason);
sinon.assert.calledOnce(statusStub);
assertGroups(repository, {
working: [
Expand Down Expand Up @@ -98,7 +98,7 @@ export function CommitSuite(this: Suite): void {
const repository = getRepository();
const execStub = getExecStub(this.ctx.sandbox);
const statusStub = fakeFossilStatus(execStub, 'ADDED fake.txt\n');
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);
sinon.assert.calledOnce(statusStub);
assertGroups(repository, {
working: [
Expand Down Expand Up @@ -143,7 +143,7 @@ export function CommitSuite(this: Suite): void {
const commitStub = execStub
.withArgs(sinon.match.array.startsWith(['commit']))
.resolves(fakeExecutionResult());
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);
assert.ok(repository.workingGroup.resourceStates[1]);
await commands.executeCommand(
'fossil.stage',
Expand Down Expand Up @@ -173,7 +173,7 @@ export function CommitSuite(this: Suite): void {
const repository = getRepository();
const execStub = getExecStub(this.ctx.sandbox);
const statusStub = fakeFossilStatus(execStub, '\n');
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);
sinon.assert.calledOnce(statusStub);
assertGroups(repository, {});

Expand All @@ -195,7 +195,7 @@ export function CommitSuite(this: Suite): void {
await fs.writeFile(uri.fsPath, 'content');

const execStub = getExecStub(this.ctx.sandbox);
await repository.updateModelState('Test' as Reason);
await repository.updateStatus('Test' as Reason);
assertGroups(repository, {
untracked: [[uri.fsPath, ResourceStatus.EXTRA]],
});
Expand Down Expand Up @@ -234,7 +234,7 @@ export function CommitSuite(this: Suite): void {

const repository = getRepository();
repository.sourceControl.inputBox.value = 'creating new branch';
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);
const resource = repository.untrackedGroup.getResource(branchPath);
assert.ok(resource);
await commands.executeCommand('fossil.add', resource);
Expand Down Expand Up @@ -272,7 +272,7 @@ export function CommitSuite(this: Suite): void {
await fs.writeFile(uri1.fsPath, 'warning test');
await fs.writeFile(uri2.fsPath, 'warning test');
const repository = getRepository();
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);
const resource1 = repository.workingGroup.getResource(uri1);
assert.ok(resource1);
await commands.executeCommand('fossil.stage', resource1);
Expand Down Expand Up @@ -330,7 +330,7 @@ export function CommitSuite(this: Suite): void {
const repository = getRepository();
const execStub = getExecStub(this.ctx.sandbox);
const statusStub = fakeFossilStatus(execStub, 'ADDED a\nCONFLICT b');
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);
sinon.assert.calledOnce(statusStub);
repository.sourceControl.inputBox.value = 'must not be committed';
const swm: sinon.SinonStub = this.ctx.sandbox
Expand All @@ -354,7 +354,7 @@ export function CommitSuite(this: Suite): void {
execStub,
'ADDED a\nMISSING b\nMISSING c'
);
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);
sinon.assert.calledOnce(statusStub);
const swm: sinon.SinonStub = this.ctx.sandbox
.stub(window, 'showWarningMessage')
Expand Down Expand Up @@ -392,7 +392,7 @@ export function CommitSuite(this: Suite): void {
const repository = getRepository();
const execStub = getExecStub(this.ctx.sandbox);
const statusStub = fakeFossilStatus(execStub, 'ADDED a\nMISSING b');
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);
sinon.assert.calledOnce(statusStub);
repository.sourceControl.inputBox.value = 'must not commit';
const swm: sinon.SinonStub = this.ctx.sandbox
Expand Down
2 changes: 1 addition & 1 deletion src/test/suite/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export async function cleanupFossil(repository: Repository): Promise<void> {
);
assert.equal(cleanRes1.exitCode, 0);

const updateRes = await repository.updateModelState(
const updateRes = await repository.updateStatus(
'Test: cleanupFossil' as Reason
);
assert.equal(updateRes, undefined);
Expand Down
10 changes: 5 additions & 5 deletions src/test/suite/mergeSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function MergeSuite(this: Suite): void {
await openedRepository.exec(['update', 'trunk']);

await commands.executeCommand('fossil.refresh');
await repository.updateModelState();
await repository.updateStatus('Test' as Reason); // ToDo: after refresh???
assertGroups(repository, {});

const sqp: sinon.SinonStub = this.ctx.sandbox.stub(
Expand All @@ -119,7 +119,7 @@ export function MergeSuite(this: Suite): void {
sinon.assert.calledOnce(sqp);
sinon.assert.calledOnce(sib);

await repository.updateModelState('test' as Reason);
await repository.updateStatus('test' as Reason);
assertGroups(repository, {});
}).timeout(5000);

Expand All @@ -129,7 +129,7 @@ export function MergeSuite(this: Suite): void {
.withArgs(['branch', 'ls', '-t'])
.resolves(fakeExecutionResult({ stdout: ' * a\n b\n c\n' }));
fakeFossilStatus(execStub, 'INTEGRATE 0123456789');
await getRepository().updateModelState();
await getRepository().updateStatus('Test' as Reason);
const sqp = this.ctx.sandbox.stub(window, 'showQuickPick');
const swm: sinon.SinonStub = this.ctx.sandbox
.stub(window, 'showWarningMessage')
Expand All @@ -154,7 +154,7 @@ export function MergeSuite(this: Suite): void {
.withArgs(['branch', 'ls', '-t'])
.resolves(fakeExecutionResult({ stdout: ' * a\n b\n c\n' }));
fakeFossilStatus(execStub, 'INTEGRATE 0123456789');
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);
const mergeStub = execStub
.withArgs(['merge', 'c', '--integrate'])
.resolves(fakeExecutionResult());
Expand Down Expand Up @@ -208,7 +208,7 @@ export function MergeSuite(this: Suite): void {
.resolves(fakeExecutionResult({ stdout: ' * a\n b\n c\n' }));
fakeFossilStatus(execStub, '');
const repository = getRepository();
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);
let hash = '';
const mergeCallStub = execStub
.withArgs(sinon.match.array.startsWith(['merge']))
Expand Down
6 changes: 3 additions & 3 deletions src/test/suite/renameSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function RenameSuite(this: Suite): void {
const repository = getRepository();
await answeredYes;
await eventToPromise(repository.onDidRunOperation);
await repository.updateModelState();
await repository.updateStatus('Test' as Reason);

assertGroups(repository, {
working: [[newFilePath.fsPath, ResourceStatus.RENAMED]],
Expand Down Expand Up @@ -168,7 +168,7 @@ export function RenameSuite(this: Suite): void {

await answeredYes;
await eventToPromise(repository.onDidRunOperation);
await repository.updateModelState('Test' as Reason);
await repository.updateStatus('Test' as Reason);

assertGroups(repository, {
working: newUris.map((url: Uri) => [
Expand All @@ -192,7 +192,7 @@ export function RenameSuite(this: Suite): void {
'ADDED'
);
await fs.rename(oldUri.fsPath, newUri.fsPath);
await repository.updateModelState('Test' as Reason);
await repository.updateStatus('Test' as Reason);
assertGroups(repository, {
working: [[oldUri.fsPath, ResourceStatus.MISSING]],
untracked: [[newUri.fsPath, ResourceStatus.EXTRA]],
Expand Down
Loading

0 comments on commit b82edc1

Please sign in to comment.