Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move all state reponsibility inside of RepositoryState #463

Merged
merged 5 commits into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions extension/src/Repository/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ beforeEach(() => {

describe('Repository', () => {
const dvcRoot = resolve(__dirname, '..', '..', 'demo')
const emptyState = new RepositoryState(dvcRoot).getState()

describe('ready', () => {
it('should wait for the state to be ready before resolving', async () => {
Expand Down Expand Up @@ -116,7 +117,6 @@ describe('Repository', () => {

expect(repository.getState()).toEqual(
expect.objectContaining({
dispose: Disposable.fn(),
deleted: emptySet,
notInCache: emptySet,
new: emptySet,
Expand Down Expand Up @@ -175,7 +175,7 @@ describe('Repository', () => {
{ path: model }
] as ListOutput[])

expect(repository.getState()).toEqual(new RepositoryState())
expect(repository.getState()).toEqual(emptyState)

await repository.resetState()

Expand Down Expand Up @@ -204,11 +204,10 @@ describe('Repository', () => {
)

expect(repository.getState()).toEqual({
dispose: Disposable.fn(),
new: emptySet,
deleted,
modified: emptySet,
new: emptySet,
notInCache: emptySet,
deleted,
tracked,
untracked: emptySet
})
Expand Down Expand Up @@ -272,7 +271,7 @@ describe('Repository', () => {
])
mockedGetAllUntracked.mockResolvedValueOnce(untracked)

expect(repository.getState()).toEqual(new RepositoryState())
expect(repository.getState()).toEqual(emptyState)

await repository.resetState()

Expand Down Expand Up @@ -303,11 +302,10 @@ describe('Repository', () => {
)

expect(repository.getState()).toEqual({
dispose: Disposable.fn(),
new: new Set(),
deleted,
modified,
new: new Set(),
notInCache,
deleted,
tracked,
untracked
})
Expand Down
158 changes: 86 additions & 72 deletions extension/src/Repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from './views/SourceControlManagement'
import { DecorationProvider, DecorationState } from './DecorationProvider'
import { Deferred } from '@hediet/std/synchronization'
import { status, listDvcOnlyRecursive } from '../cli/reader'
import { status, listDvcOnlyRecursive, ListOutput } from '../cli/reader'
import { dirname, join } from 'path'
import { observable, makeObservable } from 'mobx'
import { getExecutionOptions } from '../cli/execution'
Expand Down Expand Up @@ -36,48 +36,14 @@ export class RepositoryState
implements DecorationState, SourceControlManagementState {
public dispose = Disposable.fn()

public tracked: Set<string>
public deleted: Set<string>
public modified: Set<string>
public new: Set<string>
public notInCache: Set<string>
public untracked: Set<string>

constructor() {
this.tracked = new Set<string>()
this.deleted = new Set<string>()
this.modified = new Set<string>()
this.new = new Set<string>()
this.notInCache = new Set<string>()
this.untracked = new Set<string>()
}
}

export class Repository {
public readonly dispose = Disposable.fn()

private readonly deferred = new Deferred()
private readonly initialized = this.deferred.promise

public isReady() {
return this.initialized
}

public getState() {
return this.state
}

public getTracked() {
return this.state.tracked
}

@observable
private state: RepositoryState

private config: Config
private dvcRoot: string
private decorationProvider?: DecorationProvider
private sourceControlManagement: SourceControlManagement

public deleted: Set<string> = new Set()
public modified: Set<string> = new Set()
public new: Set<string> = new Set()
public notInCache: Set<string> = new Set()
public tracked: Set<string> = new Set()
public untracked: Set<string> = new Set()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[C] Matches the style that we agreed to a couple of weeks ago.


private filterRootDir(dirs: string[] = []) {
return dirs.filter(dir => dir !== this.dvcRoot)
Expand All @@ -93,19 +59,6 @@ export class Repository {
)
}

public async updateList(): Promise<void> {
const options = getExecutionOptions(this.config, this.dvcRoot)
const listOutput = await listDvcOnlyRecursive(options)
const trackedPaths = listOutput.map(tracked => tracked.path)

const absoluteTrackedPaths = this.getAbsolutePath(trackedPaths)

this.state.tracked = new Set([
...absoluteTrackedPaths,
...this.getAbsoluteParentPath(trackedPaths)
])
}

private getChangedOutsStatuses(
fileOrStage: StatusesOrAlwaysChanged[]
): PathStatus[] {
Expand Down Expand Up @@ -144,24 +97,85 @@ export class Repository {
return Object.values(filteredStatusOutput).reduce(statusReducer, {})
}

private async getStatus(): Promise<Partial<Record<Status, Set<string>>>> {
public updateStatus(statusOutput: StatusOutput) {
const status = this.reduceToChangedOutsStatuses(statusOutput)

this.modified = status.modified || new Set<string>()
this.deleted = status.deleted || new Set<string>()
this.new = status.new || new Set<string>()
this.notInCache = status['not in cache'] || new Set<string>()
}

public updateTracked(listOutput: ListOutput[]): void {
const trackedPaths = listOutput.map(tracked => tracked.path)

const absoluteTrackedPaths = this.getAbsolutePath(trackedPaths)

this.tracked = new Set([
...absoluteTrackedPaths,
...this.getAbsoluteParentPath(trackedPaths)
])
}

public updateUntracked(untracked: Set<string>): void {
this.untracked = untracked
}

public getState() {
return {
deleted: this.deleted,
modified: this.modified,
new: this.new,
notInCache: this.notInCache,
tracked: this.tracked,
untracked: this.untracked
}
}

constructor(dvcRoot: string) {
this.dvcRoot = dvcRoot
}
}

export class Repository {
public readonly dispose = Disposable.fn()

private readonly deferred = new Deferred()
private readonly initialized = this.deferred.promise

public isReady() {
return this.initialized
}

public getState() {
return this.state.getState()
}

@observable
private state: RepositoryState

private config: Config
private dvcRoot: string
private decorationProvider?: DecorationProvider
private sourceControlManagement: SourceControlManagement

private async updateTracked(): Promise<void> {
const options = getExecutionOptions(this.config, this.dvcRoot)
const statusOutput = (await status(options)) as StatusOutput
const listOutput = await listDvcOnlyRecursive(options)

return this.reduceToChangedOutsStatuses(statusOutput)
this.state.updateTracked(listOutput)
}

public async updateStatus() {
const status = await this.getStatus()
private async updateStatus() {
const options = getExecutionOptions(this.config, this.dvcRoot)
const statusOutput = (await status(options)) as StatusOutput

this.state.modified = status.modified || new Set<string>()
this.state.deleted = status.deleted || new Set<string>()
this.state.new = status.new || new Set<string>()
this.state.notInCache = status['not in cache'] || new Set<string>()
this.state.updateStatus(statusOutput)
}

public async updateUntracked() {
this.state.untracked = await getAllUntracked(this.dvcRoot)
private async updateUntracked() {
const untracked = await getAllUntracked(this.dvcRoot)
this.state.updateUntracked(untracked)
}

private updateStatuses() {
Expand All @@ -171,18 +185,18 @@ export class Repository {
public async resetState() {
const statusesUpdated = this.updateStatuses()

const slowerTrackedUpdated = this.updateList()
const slowerTrackedUpdated = this.updateTracked()

await statusesUpdated
this.sourceControlManagement.setState(this.state)
this.sourceControlManagement.setState(this.state.getState())

await slowerTrackedUpdated
this.decorationProvider?.setState(this.state)
this.decorationProvider?.setState(this.state.getState())
}

private setState() {
this.sourceControlManagement.setState(this.state)
this.decorationProvider?.setState(this.state)
this.sourceControlManagement.setState(this.state.getState())
this.decorationProvider?.setState(this.state.getState())
}

public async updateState() {
Expand All @@ -204,7 +218,7 @@ export class Repository {
this.config = config
this.decorationProvider = decorationProvider
this.dvcRoot = dvcRoot
this.state = this.dispose.track(new RepositoryState())
this.state = this.dispose.track(new RepositoryState(dvcRoot))

this.sourceControlManagement = this.dispose.track(
new SourceControlManagement(this.dvcRoot, this.state)
Expand Down