Skip to content

Commit f214ca9

Browse files
committed
Updated idb mutable file wrapper to fix bugs
1 parent 10fb48b commit f214ca9

File tree

2 files changed

+70
-75
lines changed

2 files changed

+70
-75
lines changed

index.js

+1-75
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ if (requestFileSystem) {
2121
} else if (mutableFile) {
2222
storage = (options = {}) => {
2323
if (typeof options === 'string') options = { name: options }
24-
return mutableStorage(options)
24+
return require('./mutable-file-wrapper.js')(options)
2525
}
2626
} else if (idb) {
2727
storage = (options = {}) => {
@@ -32,77 +32,3 @@ if (requestFileSystem) {
3232
}
3333

3434
module.exports = storage
35-
36-
function mutableStorage (options) {
37-
const randomAccess = require('random-access-storage')
38-
const mutableAccess = require('random-access-idb-mutable-file')
39-
40-
let mounted = null
41-
let loading = null
42-
43-
function doMount () {
44-
return mutableAccess.mount(options).then((requestFile) => {
45-
mounted = requestFile
46-
loading = null
47-
})
48-
}
49-
50-
return (name) => {
51-
let file = null
52-
53-
return randomAccess({
54-
open: function (req) {
55-
if (!mounted) {
56-
loading = doMount()
57-
}
58-
if (loading) {
59-
loading.then(() => {
60-
this._open(req)
61-
}, (err) => {
62-
req.callback(err)
63-
})
64-
return
65-
}
66-
67-
file = mounted(name)
68-
69-
file._open(req)
70-
},
71-
openReadonly: function (req) {
72-
if (!mounted) {
73-
loading = doMount()
74-
}
75-
if (loading) {
76-
loading.then(() => {
77-
this._openReadonly(req)
78-
}, (err) => {
79-
req.callback(err)
80-
})
81-
return
82-
}
83-
84-
file = mounted(name)
85-
86-
file._openReadonly(req)
87-
},
88-
write: function (req) {
89-
file._write(req)
90-
},
91-
read: function (req) {
92-
file._read(req)
93-
},
94-
del: function (req) {
95-
file._del(req)
96-
},
97-
stat: function (req) {
98-
file._stat(req)
99-
},
100-
close: function (req) {
101-
file._close(req)
102-
},
103-
destroy: function (req) {
104-
file._destroy(req)
105-
}
106-
})
107-
}
108-
}

mutable-file-wrapper.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
module.exports = function mutableStorage (options) {
3+
const randomAccess = require('random-access-storage')
4+
const mutableAccess = require('random-access-idb-mutable-file')
5+
6+
let mounted = null
7+
let loading = null
8+
9+
function doMount () {
10+
return mutableAccess.mount(options).then((requestFile) => {
11+
mounted = requestFile
12+
loading = null
13+
})
14+
}
15+
16+
return (name) => {
17+
let file = null
18+
19+
return randomAccess({
20+
open: function (req) {
21+
if (!mounted && !loading) {
22+
loading = doMount()
23+
}
24+
if(loading) {
25+
loading.then(() => {
26+
this._open(req)
27+
}, (err) => {
28+
req.callback(err)
29+
})
30+
return
31+
}
32+
33+
file = mounted(name)
34+
35+
req.callback()
36+
},
37+
write: function (req) {
38+
file.write(req.offset, req.data, function(err, data) {
39+
req.callback(err, data)
40+
})
41+
},
42+
read: function (req) {
43+
file.read(req.offset, req.size, function(err, data) {
44+
req.callback(err, data)
45+
})
46+
},
47+
del: function (req) {
48+
file.del(req.offset, req.size, function(err, data) {
49+
req.callback(err, data)
50+
})
51+
},
52+
stat: function (req) {
53+
file.stat( function(err, data) {
54+
req.callback(err, data)
55+
})
56+
},
57+
close: function (req) {
58+
file.close( function(err, data) {
59+
req.callback(err, data)
60+
})
61+
},
62+
destroy: function (req) {
63+
file.destroy( function(err, data) {
64+
req.callback(err, data)
65+
})
66+
}
67+
})
68+
}
69+
}

0 commit comments

Comments
 (0)