-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add async initialization to ChangeSetFileElement #15761
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,7 +76,9 @@ export class ChangeSetFileElement implements ChangeSetElement { | |
| protected readonly toDispose = new DisposableCollection(); | ||
| protected _state: ChangeSetElementState; | ||
|
|
||
| protected originalContent: string | undefined; | ||
| private _originalContent: string | undefined; | ||
| protected _initialized = false; | ||
| protected _initializationPromise: Promise<void> | undefined; | ||
|
|
||
| protected readonly onDidChangeEmitter = new Emitter<void>(); | ||
| readonly onDidChange = this.onDidChangeEmitter.event; | ||
|
|
@@ -85,12 +87,38 @@ export class ChangeSetFileElement implements ChangeSetElement { | |
|
|
||
| @postConstruct() | ||
| init(): void { | ||
| this._initializationPromise = this.initializeAsync(); | ||
| this.toDispose.push(this.onDidChangeEmitter); | ||
| } | ||
|
|
||
| protected async initializeAsync(): Promise<void> { | ||
| await this.obtainOriginalContent(); | ||
| this.listenForOriginalFileChanges(); | ||
| this._initialized = true; | ||
| } | ||
|
|
||
| /** | ||
| * Ensures that the element is fully initialized before proceeding. | ||
| * This includes loading the original content from the file system. | ||
| */ | ||
| async ensureInitialized(): Promise<void> { | ||
| if (this._initializationPromise) { | ||
| await this._initializationPromise; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if the element has been fully initialized. | ||
| */ | ||
| get isInitialized(): boolean { | ||
| return this._initialized; | ||
| } | ||
|
|
||
| protected async obtainOriginalContent(): Promise<void> { | ||
| this.originalContent = await this.changeSetFileService.read(this.uri); | ||
| this.readOnlyResource.update({ contents: this.originalContent ?? '' }); | ||
| this._originalContent = await this.changeSetFileService.read(this.uri); | ||
| if (this._readOnlyResource) { | ||
| this.readOnlyResource.update({ contents: this._originalContent ?? '' }); | ||
| } | ||
| } | ||
|
|
||
| protected getInMemoryUri(uri: URI): ConfigurableMutableReferenceResource { | ||
|
|
@@ -103,7 +131,7 @@ export class ChangeSetFileElement implements ChangeSetElement { | |
| // If we are applied, the tricky thing becomes the question what to revert to; otherwise, what to apply. | ||
| const newContent = await this.changeSetFileService.read(this.uri).catch(() => ''); | ||
| this.readOnlyResource.update({ contents: newContent }); | ||
| if (newContent === this.originalContent) { | ||
| if (newContent === this._originalContent) { | ||
| this.state = 'pending'; | ||
| } else if (newContent === this.targetState) { | ||
| this.state = 'applied'; | ||
|
|
@@ -120,10 +148,19 @@ export class ChangeSetFileElement implements ChangeSetElement { | |
| protected get readOnlyResource(): ConfigurableMutableReferenceResource { | ||
| if (!this._readOnlyResource) { | ||
| this._readOnlyResource = this.getInMemoryUri(ChangeSetFileElement.toReadOnlyUri(this.uri, this.elementProps.chatSessionId)); | ||
| this._readOnlyResource.update({ autosaveable: false, readOnly: true }); | ||
| this._readOnlyResource.update({ | ||
| autosaveable: false, | ||
| readOnly: true, | ||
| contents: this._originalContent ?? '' | ||
| }); | ||
| this.toDispose.push(this._readOnlyResource); | ||
| this.obtainOriginalContent(); | ||
| this.listenForOriginalFileChanges(); | ||
|
|
||
| // If not yet initialized, update the resource once initialization completes | ||
| if (!this._initialized && this._initializationPromise) { | ||
| this._initializationPromise.then(() => { | ||
| this._readOnlyResource?.update({ contents: this._originalContent ?? '' }); | ||
| }); | ||
| } | ||
|
||
| } | ||
| return this._readOnlyResource; | ||
| } | ||
|
|
@@ -180,22 +217,41 @@ export class ChangeSetFileElement implements ChangeSetElement { | |
| return this.elementProps.data; | ||
| }; | ||
|
|
||
| get originalContent(): string | undefined { | ||
| if (!this._initialized && this._initializationPromise) { | ||
| console.warn('Accessing originalContent before initialization is complete. Consider using async methods.'); | ||
| } | ||
| return this._originalContent; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the original content of the file asynchronously. | ||
| * Ensures initialization is complete before returning the content. | ||
| */ | ||
| async getOriginalContent(): Promise<string | undefined> { | ||
| await this.ensureInitialized(); | ||
| return this._originalContent; | ||
| } | ||
|
|
||
| get targetState(): string { | ||
| return this.elementProps.targetState ?? ''; | ||
| } | ||
|
|
||
| async open(): Promise<void> { | ||
| await this.ensureInitialized(); | ||
| this.changeSetFileService.open(this); | ||
| } | ||
|
|
||
| async openChange(): Promise<void> { | ||
| await this.ensureInitialized(); | ||
| this.changeSetFileService.openDiff( | ||
| this.readOnlyUri, | ||
| this.changedUri | ||
| ); | ||
| } | ||
|
|
||
| async apply(contents?: string): Promise<void> { | ||
| await this.ensureInitialized(); | ||
| if (!await this.confirm('Apply')) { return; } | ||
| if (!(await this.changeSetFileService.trySave(this.changedUri))) { | ||
| if (this.type === 'delete') { | ||
|
|
@@ -214,16 +270,25 @@ export class ChangeSetFileElement implements ChangeSetElement { | |
| } | ||
|
|
||
| onShow(): void { | ||
| this.changeResource.update({ contents: this.targetState, onSave: content => this.writeChanges(content) }); | ||
| // Ensure we have the latest state when showing | ||
| if (!this._initialized && this._initializationPromise) { | ||
| this._initializationPromise.then(() => { | ||
| // Update the change resource with the correct target state after initialization | ||
| this.changeResource.update({ contents: this.targetState, onSave: content => this.writeChanges(content) }); | ||
| }); | ||
| } else { | ||
| this.changeResource.update({ contents: this.targetState, onSave: content => this.writeChanges(content) }); | ||
|
||
| } | ||
| } | ||
|
|
||
| async revert(): Promise<void> { | ||
| await this.ensureInitialized(); | ||
| if (!await this.confirm('Revert')) { return; } | ||
| this.state = 'pending'; | ||
| if (this.type === 'add') { | ||
| await this.changeSetFileService.delete(this.uri); | ||
| } else if (this.originalContent) { | ||
| await this.changeSetFileService.write(this.uri, this.originalContent); | ||
| } else if (this._originalContent) { | ||
| await this.changeSetFileService.write(this.uri, this._originalContent); | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.