@@ -28,7 +28,6 @@ function createFile (name, opts) {
2828 const mutex = new Mutex ( )
2929
3030 var fs = null
31- var file = null
3231 var entry = null
3332 var toDestroy = null
3433 var readers = [ ]
@@ -37,22 +36,24 @@ function createFile (name, opts) {
3736 return ras ( { read, write, open, stat, close, destroy} )
3837
3938 function read ( req ) {
40- const r = readers . pop ( ) || new ReadRequest ( readers , file , entry , mutex )
39+ const r = readers . pop ( ) || new ReadRequest ( readers , entry , mutex )
4140 r . run ( req )
4241 }
4342
4443 function write ( req ) {
45- const w = writers . pop ( ) || new WriteRequest ( writers , file , entry , mutex )
44+ const w = writers . pop ( ) || new WriteRequest ( writers , entry , mutex )
4645 w . run ( req )
4746 }
4847
4948 function close ( req ) {
50- readers = writers = entry = file = fs = null
49+ readers = writers = entry = fs = null
5150 req . callback ( null )
5251 }
5352
5453 function stat ( req ) {
55- req . callback ( null , file )
54+ entry . file ( file => {
55+ req . callback ( null , file )
56+ } , err => req . callback ( err ) )
5657 }
5758
5859 function destroy ( req ) {
@@ -77,10 +78,7 @@ function createFile (name, opts) {
7778 mkdirp ( parentFolder ( name ) , function ( ) {
7879 fs . root . getFile ( name , { create : true } , function ( e ) {
7980 entry = toDestroy = e
80- entry . file ( function ( f ) {
81- file = f
82- req . callback ( null )
83- } , onerror )
81+ req . callback ( null )
8482 } , onerror )
8583 } )
8684 } , onerror )
@@ -96,7 +94,7 @@ function createFile (name, opts) {
9694 }
9795
9896 function onerror ( err ) {
99- fs = file = entry = null
97+ fs = entry = null
10098 req . callback ( err )
10199 }
102100 }
@@ -109,13 +107,12 @@ function parentFolder (path) {
109107 return / ^ \w : $ / . test ( p ) ? '' : p
110108}
111109
112- function WriteRequest ( pool , file , entry , mutex ) {
113- this . writer = null
114- this . entry = entry
115- this . file = file
116- this . req = null
110+ function WriteRequest ( pool , entry , mutex ) {
117111 this . pool = pool
112+ this . entry = entry
118113 this . mutex = mutex
114+ this . writer = null
115+ this . req = null
119116 this . locked = false
120117 this . truncating = false
121118}
@@ -167,19 +164,21 @@ WriteRequest.prototype.lock = function () {
167164}
168165
169166WriteRequest . prototype . run = function ( req ) {
170- this . req = req
171- if ( ! this . writer || this . writer . length !== this . file . size ) return this . makeWriter ( )
167+ this . entry . file ( file => {
168+ this . req = req
169+ if ( ! this . writer || this . writer . length !== file . size ) return this . makeWriter ( )
172170
173- const end = req . offset + req . size
174- if ( end > this . file . size && ! this . lock ( ) ) return
171+ const end = req . offset + req . size
172+ if ( end > file . size && ! this . lock ( ) ) return
175173
176- if ( req . offset > this . writer . length ) {
177- if ( req . offset > this . file . size ) return this . truncate ( )
178- return this . makeWriter ( )
179- }
174+ if ( req . offset > this . writer . length ) {
175+ if ( req . offset > file . size ) return this . truncate ( )
176+ return this . makeWriter ( )
177+ }
180178
181- this . writer . seek ( req . offset )
182- this . writer . write ( new Blob ( [ req . data ] , TYPE ) )
179+ this . writer . seek ( req . offset )
180+ this . writer . write ( new Blob ( [ req . data ] , TYPE ) )
181+ } , err => req . callback ( err ) )
183182}
184183
185184function Mutex ( ) {
@@ -203,13 +202,13 @@ Mutex.prototype.lock = function (req) {
203202 return true
204203}
205204
206- function ReadRequest ( pool , file , entry , mutex ) {
205+ function ReadRequest ( pool , entry , mutex ) {
206+ this . pool = pool
207+ this . entry = entry
208+ this . mutex = mutex
207209 this . reader = new FileReader ( )
208- this . file = file
209210 this . req = null
210- this . pool = pool
211211 this . retry = true
212- this . mutex = mutex
213212 this . locked = false
214213
215214 const self = this
@@ -252,8 +251,10 @@ ReadRequest.prototype.onread = function (err, buf) {
252251}
253252
254253ReadRequest . prototype . run = function ( req ) {
255- const end = req . offset + req . size
256- this . req = req
257- if ( end > this . file . size ) return this . onread ( new Error ( 'Could not satisfy length' ) , null )
258- this . reader . readAsArrayBuffer ( this . file . slice ( req . offset , end ) )
254+ this . entry . file ( file => {
255+ const end = req . offset + req . size
256+ this . req = req
257+ if ( end > file . size ) return this . onread ( new Error ( 'Could not satisfy length' ) , null )
258+ this . reader . readAsArrayBuffer ( file . slice ( req . offset , end ) )
259+ } , err => req . callback ( err ) )
259260}
0 commit comments