@@ -34,22 +34,8 @@ function cleanParams2(oldFilepath, newFilepath) {
34
34
}
35
35
36
36
module . exports = class PromisifiedFS {
37
- constructor ( name , { wipe, url, urlauto } = { } ) {
38
- this . _name = name
39
- this . _idb = new IdbBackend ( name ) ;
40
- this . _mutex = navigator . locks ? new Mutex2 ( name ) : new Mutex ( name ) ;
41
- this . _cache = new CacheFS ( name ) ;
42
- this . _opts = { wipe, url } ;
43
- this . _needsWipe = ! ! wipe ;
44
- this . saveSuperblock = debounce ( ( ) => {
45
- this . _saveSuperblock ( ) ;
46
- } , 500 ) ;
47
- if ( url ) {
48
- this . _http = new HttpBackend ( url )
49
- this . _urlauto = ! ! urlauto
50
- }
51
- this . _operations = new Set ( )
52
-
37
+ constructor ( name , options ) {
38
+ this . init = this . init . bind ( this )
53
39
this . readFile = this . _wrap ( this . readFile , false )
54
40
this . writeFile = this . _wrap ( this . writeFile , true )
55
41
this . unlink = this . _wrap ( this . unlink , true )
@@ -63,17 +49,60 @@ module.exports = class PromisifiedFS {
63
49
this . symlink = this . _wrap ( this . symlink , true )
64
50
this . backFile = this . _wrap ( this . backFile , true )
65
51
52
+ this . saveSuperblock = debounce ( ( ) => {
53
+ this . _saveSuperblock ( ) ;
54
+ } , 500 ) ;
55
+
66
56
this . _deactivationPromise = null
67
57
this . _deactivationTimeout = null
68
58
this . _activationPromise = null
59
+
60
+ this . _operations = new Set ( )
61
+
62
+ if ( name ) {
63
+ this . init ( name , options )
64
+ }
65
+ }
66
+ async init ( ...args ) {
67
+ if ( this . _initPromiseResolve ) await this . _initPromise ;
68
+ this . _initPromise = this . _init ( ...args )
69
+ return this . _initPromise
70
+ }
71
+ async _init ( name , {
72
+ wipe,
73
+ url,
74
+ urlauto,
75
+ fileDbName = name ,
76
+ fileStoreName = name + "_files" ,
77
+ lockDbName = name + "_lock" ,
78
+ lockStoreName = name + "_lock" ,
79
+ } = { } ) {
80
+ await this . _gracefulShutdown ( )
81
+ this . _name = name
82
+ this . _idb = new IdbBackend ( fileDbName , fileStoreName ) ;
83
+ this . _mutex = navigator . locks ? new Mutex2 ( name ) : new Mutex ( lockDbName , lockStoreName ) ;
84
+ this . _cache = new CacheFS ( name ) ;
85
+ this . _opts = { wipe, url } ;
86
+ this . _needsWipe = ! ! wipe ;
87
+ if ( url ) {
88
+ this . _http = new HttpBackend ( url )
89
+ this . _urlauto = ! ! urlauto
90
+ }
91
+ if ( this . _initPromiseResolve ) {
92
+ this . _initPromiseResolve ( ) ;
93
+ this . _initPromiseResolve = null ;
94
+ }
69
95
// The fs is initially activated when constructed (in order to wipe/save the superblock)
70
- // but there might not be any other fs operations needed until later. Therefore we
71
- // need to attempt to release the mutex
72
- this . _activate ( ) . then ( ( ) => {
73
- if ( this . _operations . size === 0 && ! this . _deactivationTimeout ) {
74
- this . _deactivationTimeout = setTimeout ( this . _deactivate . bind ( this ) , 100 )
75
- }
76
- } )
96
+ // This is not awaited, because that would create a cycle.
97
+ this . stat ( '/' )
98
+ }
99
+ async _gracefulShutdown ( ) {
100
+ if ( this . _operations . size > 0 ) {
101
+ this . _isShuttingDown = true
102
+ await new Promise ( resolve => this . _gracefulShutdownResolve = resolve ) ;
103
+ this . _isShuttingDown = false
104
+ this . _gracefulShutdownResolve = null
105
+ }
77
106
}
78
107
_wrap ( fn , mutating ) {
79
108
let i = 0
@@ -97,6 +126,8 @@ module.exports = class PromisifiedFS {
97
126
}
98
127
}
99
128
async _activate ( ) {
129
+ if ( ! this . _initPromise ) console . warn ( new Error ( `Attempted to use LightningFS ${ this . _name } before it was initialized.` ) )
130
+ await this . _initPromise
100
131
if ( this . _deactivationTimeout ) {
101
132
clearTimeout ( this . _deactivationTimeout )
102
133
this . _deactivationTimeout = null
@@ -138,6 +169,7 @@ module.exports = class PromisifiedFS {
138
169
if ( this . _activationPromise ) await this . _activationPromise
139
170
if ( ! this . _deactivationPromise ) this . _deactivationPromise = this . __deactivate ( )
140
171
this . _activationPromise = null
172
+ if ( this . _gracefulShutdownResolve ) this . _gracefulShutdownResolve ( )
141
173
return this . _deactivationPromise
142
174
}
143
175
async __deactivate ( ) {
0 commit comments