44 *--------------------------------------------------------------------------------------------*/
55import * as vscode from 'vscode' ;
66import Logger from './logger' ;
7+ import { dispose } from './utils' ;
78
89let tempState : TemporaryState | undefined ;
910
1011export class TemporaryState extends vscode . Disposable {
1112 private readonly SUBPATH = 'temp' ;
1213 private readonly disposables : vscode . Disposable [ ] = [ ] ;
14+ private readonly persistInSessionDisposables : vscode . Disposable [ ] = [ ] ;
1315
1416 constructor ( private _storageUri : vscode . Uri ) {
1517 super ( ( ) => this . disposables . forEach ( disposable => disposable . dispose ( ) ) ) ;
@@ -19,15 +21,24 @@ export class TemporaryState extends vscode.Disposable {
1921 return vscode . Uri . joinPath ( this . _storageUri , this . SUBPATH ) ;
2022 }
2123
22- private addDisposable ( disposable : vscode . Disposable ) {
23- if ( this . disposables . length > 30 ) {
24- const oldDisposable = this . disposables . shift ( ) ;
25- oldDisposable ?. dispose ( ) ;
24+ dispose ( ) {
25+ dispose ( this . disposables ) ;
26+ dispose ( this . persistInSessionDisposables ) ;
27+ }
28+
29+ private addDisposable ( disposable : vscode . Disposable , persistInSession : boolean ) {
30+ if ( persistInSession ) {
31+ this . persistInSessionDisposables . push ( disposable ) ;
32+ } else {
33+ if ( this . disposables . length > 30 ) {
34+ const oldDisposable = this . disposables . shift ( ) ;
35+ oldDisposable ?. dispose ( ) ;
36+ }
37+ this . disposables . push ( disposable ) ;
2638 }
27- this . disposables . push ( disposable ) ;
2839 }
2940
30- private async writeState ( subpath : string , filename : string , contents : Uint8Array ) : Promise < vscode . Uri > {
41+ private async writeState ( subpath : string , filename : string , contents : Uint8Array , persistInSession : boolean ) : Promise < vscode . Uri > {
3142 let filePath : vscode . Uri = this . path ;
3243 const workspace = ( vscode . workspace . workspaceFolders && vscode . workspace . workspaceFolders . length > 0 )
3344 ? vscode . workspace . workspaceFolders [ 0 ] . name : undefined ;
@@ -45,10 +56,14 @@ export class TemporaryState extends vscode.Disposable {
4556
4657 const dispose = {
4758 dispose : ( ) => {
48- return vscode . workspace . fs . delete ( file , { recursive : true } ) ;
59+ try {
60+ return vscode . workspace . fs . delete ( file , { recursive : true } ) ;
61+ } catch ( e ) {
62+ // No matter the error, we do not want to throw in dispose.
63+ }
4964 }
5065 } ;
51- this . addDisposable ( dispose ) ;
66+ this . addDisposable ( dispose , persistInSession ) ;
5267 return file ;
5368 }
5469
@@ -83,12 +98,12 @@ export class TemporaryState extends vscode.Disposable {
8398 }
8499 }
85100
86- static async write ( subpath : string , filename : string , contents : Uint8Array ) : Promise < vscode . Uri | undefined > {
101+ static async write ( subpath : string , filename : string , contents : Uint8Array , persistInSession : boolean = false ) : Promise < vscode . Uri | undefined > {
87102 if ( ! tempState ) {
88103 return ;
89104 }
90105
91- return tempState . writeState ( subpath , filename , contents ) ;
106+ return tempState . writeState ( subpath , filename , contents , persistInSession ) ;
92107 }
93108
94109 static async read ( subpath : string , filename : string ) : Promise < Uint8Array | undefined > {
0 commit comments