@@ -28,7 +28,6 @@ function createFile (name, opts) {
28
28
const mutex = new Mutex ( )
29
29
30
30
var fs = null
31
- var file = null
32
31
var entry = null
33
32
var toDestroy = null
34
33
var readers = [ ]
@@ -37,22 +36,24 @@ function createFile (name, opts) {
37
36
return ras ( { read, write, open, stat, close, destroy} )
38
37
39
38
function read ( req ) {
40
- const r = readers . pop ( ) || new ReadRequest ( readers , file , entry , mutex )
39
+ const r = readers . pop ( ) || new ReadRequest ( readers , entry , mutex )
41
40
r . run ( req )
42
41
}
43
42
44
43
function write ( req ) {
45
- const w = writers . pop ( ) || new WriteRequest ( writers , file , entry , mutex )
44
+ const w = writers . pop ( ) || new WriteRequest ( writers , entry , mutex )
46
45
w . run ( req )
47
46
}
48
47
49
48
function close ( req ) {
50
- readers = writers = entry = file = fs = null
49
+ readers = writers = entry = fs = null
51
50
req . callback ( null )
52
51
}
53
52
54
53
function stat ( req ) {
55
- req . callback ( null , file )
54
+ entry . file ( file => {
55
+ req . callback ( null , file )
56
+ } , err => req . callback ( err ) )
56
57
}
57
58
58
59
function destroy ( req ) {
@@ -77,10 +78,7 @@ function createFile (name, opts) {
77
78
mkdirp ( parentFolder ( name ) , function ( ) {
78
79
fs . root . getFile ( name , { create : true } , function ( e ) {
79
80
entry = toDestroy = e
80
- entry . file ( function ( f ) {
81
- file = f
82
- req . callback ( null )
83
- } , onerror )
81
+ req . callback ( null )
84
82
} , onerror )
85
83
} )
86
84
} , onerror )
@@ -96,7 +94,7 @@ function createFile (name, opts) {
96
94
}
97
95
98
96
function onerror ( err ) {
99
- fs = file = entry = null
97
+ fs = entry = null
100
98
req . callback ( err )
101
99
}
102
100
}
@@ -109,13 +107,12 @@ function parentFolder (path) {
109
107
return / ^ \w : $ / . test ( p ) ? '' : p
110
108
}
111
109
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 ) {
117
111
this . pool = pool
112
+ this . entry = entry
118
113
this . mutex = mutex
114
+ this . writer = null
115
+ this . req = null
119
116
this . locked = false
120
117
this . truncating = false
121
118
}
@@ -167,19 +164,21 @@ WriteRequest.prototype.lock = function () {
167
164
}
168
165
169
166
WriteRequest . 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 ( )
172
170
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
175
173
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
+ }
180
178
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 ) )
183
182
}
184
183
185
184
function Mutex ( ) {
@@ -203,13 +202,13 @@ Mutex.prototype.lock = function (req) {
203
202
return true
204
203
}
205
204
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
207
209
this . reader = new FileReader ( )
208
- this . file = file
209
210
this . req = null
210
- this . pool = pool
211
211
this . retry = true
212
- this . mutex = mutex
213
212
this . locked = false
214
213
215
214
const self = this
@@ -252,8 +251,10 @@ ReadRequest.prototype.onread = function (err, buf) {
252
251
}
253
252
254
253
ReadRequest . 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 ) )
259
260
}
0 commit comments