@@ -519,13 +519,16 @@ export class NativeEditorStorage implements INotebookStorage {
519
519
return isUntitledFile ( file ) ;
520
520
}
521
521
522
- public getBackupId ( model : INotebookModel ) : string {
523
- const key = this . getStorageKey ( model . file ) ;
524
- return this . getHashedFileName ( key ) ;
522
+ public generateBackupId ( model : INotebookModel ) : string {
523
+ return `${ path . basename ( model . file . fsPath ) } -${ uuid ( ) } ` ;
525
524
}
526
525
527
- public load ( file : Uri , possibleContents ?: string , skipDirtyContents ?: boolean ) : Promise < INotebookModel > {
528
- return this . loadFromFile ( file , possibleContents , skipDirtyContents ) ;
526
+ public load ( file : Uri , possibleContents ?: string , backupId ?: string ) : Promise < INotebookModel > ;
527
+ // tslint:disable-next-line: unified-signatures
528
+ public load ( file : Uri , possibleContents ?: string , skipDirtyContents ?: boolean ) : Promise < INotebookModel > ;
529
+ // tslint:disable-next-line: no-any
530
+ public load ( file : Uri , possibleContents ?: string , options ?: any ) : Promise < INotebookModel > {
531
+ return this . loadFromFile ( file , possibleContents , options ) ;
529
532
}
530
533
public async save ( model : INotebookModel , _cancellation : CancellationToken ) : Promise < void > {
531
534
const contents = model . getContent ( ) ;
@@ -552,15 +555,15 @@ export class NativeEditorStorage implements INotebookStorage {
552
555
} ) ;
553
556
this . savedAs . fire ( { new : file , old } ) ;
554
557
}
555
- public async backup ( model : INotebookModel , cancellation : CancellationToken ) : Promise < void > {
558
+ public async backup ( model : INotebookModel , cancellation : CancellationToken , backupId ?: string ) : Promise < void > {
556
559
// If we are already backing up, save this request replacing any other previous requests
557
560
if ( this . backingUp ) {
558
561
this . backupRequested = { model, cancellation } ;
559
562
return ;
560
563
}
561
564
this . backingUp = true ;
562
565
// Should send to extension context storage path
563
- return this . storeContentsInHotExitFile ( model , cancellation ) . finally ( ( ) => {
566
+ return this . storeContentsInHotExitFile ( model , cancellation , backupId ) . finally ( ( ) => {
564
567
this . backingUp = false ;
565
568
566
569
// If there is a backup request waiting, then clear and start it
@@ -579,17 +582,21 @@ export class NativeEditorStorage implements INotebookStorage {
579
582
await this . loadFromFile ( model . file ) ;
580
583
}
581
584
582
- public async deleteBackup ( model : INotebookModel ) : Promise < void > {
583
- return this . clearHotExit ( model . file ) ;
585
+ public async deleteBackup ( model : INotebookModel , backupId : string ) : Promise < void > {
586
+ return this . clearHotExit ( model . file , backupId ) ;
584
587
}
585
588
/**
586
589
* Stores the uncommitted notebook changes into a temporary location.
587
590
* Also keep track of the current time. This way we can check whether changes were
588
591
* made to the file since the last time uncommitted changes were stored.
589
592
*/
590
- private async storeContentsInHotExitFile ( model : INotebookModel , cancelToken ?: CancellationToken ) : Promise < void > {
593
+ private async storeContentsInHotExitFile (
594
+ model : INotebookModel ,
595
+ cancelToken ?: CancellationToken ,
596
+ backupId ?: string
597
+ ) : Promise < void > {
591
598
const contents = model . getContent ( ) ;
592
- const key = this . getStorageKey ( model . file ) ;
599
+ const key = backupId || this . getStaticStorageKey ( model . file ) ;
593
600
const filePath = this . getHashedFileName ( key ) ;
594
601
595
602
// Keep track of the time when this data was saved.
@@ -599,8 +606,8 @@ export class NativeEditorStorage implements INotebookStorage {
599
606
return this . writeToStorage ( filePath , specialContents , cancelToken ) ;
600
607
}
601
608
602
- private async clearHotExit ( file : Uri ) : Promise < void > {
603
- const key = this . getStorageKey ( file ) ;
609
+ private async clearHotExit ( file : Uri , backupId ?: string ) : Promise < void > {
610
+ const key = backupId || this . getStaticStorageKey ( file ) ;
604
611
const filePath = this . getHashedFileName ( key ) ;
605
612
await this . writeToStorage ( filePath , undefined ) ;
606
613
}
@@ -613,8 +620,10 @@ export class NativeEditorStorage implements INotebookStorage {
613
620
if ( ! cancelToken ?. isCancellationRequested ) {
614
621
await this . fileSystem . writeFile ( filePath , contents ) ;
615
622
}
616
- } else if ( await this . fileSystem . fileExists ( filePath ) ) {
617
- await this . fileSystem . deleteFile ( filePath ) ;
623
+ } else {
624
+ await this . fileSystem
625
+ . deleteFile ( filePath )
626
+ . catch ( ( ex ) => traceError ( 'Failed to delete hotExit file. Possible it does not exist' , ex ) ) ;
618
627
}
619
628
}
620
629
} catch ( exc ) {
@@ -658,24 +667,32 @@ export class NativeEditorStorage implements INotebookStorage {
658
667
noop ( ) ;
659
668
}
660
669
}
670
+ private loadFromFile ( file : Uri , possibleContents ?: string , backupId ?: string ) : Promise < INotebookModel > ;
671
+ // tslint:disable-next-line: unified-signatures
672
+ private loadFromFile ( file : Uri , possibleContents ?: string , skipDirtyContents ?: boolean ) : Promise < INotebookModel > ;
661
673
private async loadFromFile (
662
674
file : Uri ,
663
675
possibleContents ?: string ,
664
- skipDirtyContents ?: boolean
676
+ options ?: boolean | string
665
677
) : Promise < INotebookModel > {
666
678
try {
667
679
// Attempt to read the contents if a viable file
668
680
const contents = NativeEditorStorage . isUntitledFile ( file )
669
681
? possibleContents
670
682
: await this . fileSystem . readFile ( file . fsPath ) ;
671
683
684
+ const skipDirtyContents = typeof options === 'boolean' ? options : ! ! options ;
685
+ // Use backupId provided, else use static storage key.
686
+ const backupId =
687
+ typeof options === 'string' ? options : skipDirtyContents ? undefined : this . getStaticStorageKey ( file ) ;
688
+
672
689
// If skipping dirty contents, delete the dirty hot exit file now
673
690
if ( skipDirtyContents ) {
674
- await this . clearHotExit ( file ) ;
691
+ await this . clearHotExit ( file , backupId ) ;
675
692
}
676
693
677
694
// See if this file was stored in storage prior to shutdown
678
- const dirtyContents = skipDirtyContents ? undefined : await this . getStoredContents ( file ) ;
695
+ const dirtyContents = skipDirtyContents ? undefined : await this . getStoredContents ( file , backupId ) ;
679
696
if ( dirtyContents ) {
680
697
// This means we're dirty. Indicate dirty and load from this content
681
698
return this . loadContents ( file , dirtyContents , true ) ;
@@ -748,7 +765,7 @@ export class NativeEditorStorage implements INotebookStorage {
748
765
) ;
749
766
}
750
767
751
- private getStorageKey ( file : Uri ) : string {
768
+ private getStaticStorageKey ( file : Uri ) : string {
752
769
return `${ KeyPrefix } ${ file . toString ( ) } ` ;
753
770
}
754
771
@@ -760,8 +777,8 @@ export class NativeEditorStorage implements INotebookStorage {
760
777
* @returns {(Promise<string | undefined>) }
761
778
* @memberof NativeEditor
762
779
*/
763
- private async getStoredContents ( file : Uri ) : Promise < string | undefined > {
764
- const key = this . getStorageKey ( file ) ;
780
+ private async getStoredContents ( file : Uri , backupId ?: string ) : Promise < string | undefined > {
781
+ const key = backupId || this . getStaticStorageKey ( file ) ;
765
782
766
783
// First look in the global storage file location
767
784
let result = await this . getStoredContentsFromFile ( file , key ) ;
0 commit comments