@@ -34,22 +34,8 @@ function cleanParams2(oldFilepath, newFilepath) {
3434}
3535
3636module . 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 )
5339 this . readFile = this . _wrap ( this . readFile , false )
5440 this . writeFile = this . _wrap ( this . writeFile , true )
5541 this . unlink = this . _wrap ( this . unlink , true )
@@ -63,17 +49,60 @@ module.exports = class PromisifiedFS {
6349 this . symlink = this . _wrap ( this . symlink , true )
6450 this . backFile = this . _wrap ( this . backFile , true )
6551
52+ this . saveSuperblock = debounce ( ( ) => {
53+ this . _saveSuperblock ( ) ;
54+ } , 500 ) ;
55+
6656 this . _deactivationPromise = null
6757 this . _deactivationTimeout = null
6858 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+ }
6995 // 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+ }
77106 }
78107 _wrap ( fn , mutating ) {
79108 let i = 0
@@ -97,6 +126,8 @@ module.exports = class PromisifiedFS {
97126 }
98127 }
99128 async _activate ( ) {
129+ if ( ! this . _initPromise ) console . warn ( new Error ( `Attempted to use LightningFS ${ this . _name } before it was initialized.` ) )
130+ await this . _initPromise
100131 if ( this . _deactivationTimeout ) {
101132 clearTimeout ( this . _deactivationTimeout )
102133 this . _deactivationTimeout = null
@@ -138,6 +169,7 @@ module.exports = class PromisifiedFS {
138169 if ( this . _activationPromise ) await this . _activationPromise
139170 if ( ! this . _deactivationPromise ) this . _deactivationPromise = this . __deactivate ( )
140171 this . _activationPromise = null
172+ if ( this . _gracefulShutdownResolve ) this . _gracefulShutdownResolve ( )
141173 return this . _deactivationPromise
142174 }
143175 async __deactivate ( ) {
0 commit comments