Skip to content

Commit

Permalink
Updated idb mutable file wrapper to fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
RangerMauve committed Jul 8, 2019
1 parent 10fb48b commit f214ca9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 75 deletions.
76 changes: 1 addition & 75 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if (requestFileSystem) {
} else if (mutableFile) {
storage = (options = {}) => {
if (typeof options === 'string') options = { name: options }
return mutableStorage(options)
return require('./mutable-file-wrapper.js')(options)
}
} else if (idb) {
storage = (options = {}) => {
Expand All @@ -32,77 +32,3 @@ if (requestFileSystem) {
}

module.exports = storage

function mutableStorage (options) {
const randomAccess = require('random-access-storage')
const mutableAccess = require('random-access-idb-mutable-file')

let mounted = null
let loading = null

function doMount () {
return mutableAccess.mount(options).then((requestFile) => {
mounted = requestFile
loading = null
})
}

return (name) => {
let file = null

return randomAccess({
open: function (req) {
if (!mounted) {
loading = doMount()
}
if (loading) {
loading.then(() => {
this._open(req)
}, (err) => {
req.callback(err)
})
return
}

file = mounted(name)

file._open(req)
},
openReadonly: function (req) {
if (!mounted) {
loading = doMount()
}
if (loading) {
loading.then(() => {
this._openReadonly(req)
}, (err) => {
req.callback(err)
})
return
}

file = mounted(name)

file._openReadonly(req)
},
write: function (req) {
file._write(req)
},
read: function (req) {
file._read(req)
},
del: function (req) {
file._del(req)
},
stat: function (req) {
file._stat(req)
},
close: function (req) {
file._close(req)
},
destroy: function (req) {
file._destroy(req)
}
})
}
}
69 changes: 69 additions & 0 deletions mutable-file-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

module.exports = function mutableStorage (options) {
const randomAccess = require('random-access-storage')
const mutableAccess = require('random-access-idb-mutable-file')

let mounted = null
let loading = null

function doMount () {
return mutableAccess.mount(options).then((requestFile) => {
mounted = requestFile
loading = null
})
}

return (name) => {
let file = null

return randomAccess({
open: function (req) {
if (!mounted && !loading) {
loading = doMount()
}
if(loading) {
loading.then(() => {
this._open(req)
}, (err) => {
req.callback(err)
})
return
}

file = mounted(name)

req.callback()
},
write: function (req) {
file.write(req.offset, req.data, function(err, data) {
req.callback(err, data)
})
},
read: function (req) {
file.read(req.offset, req.size, function(err, data) {
req.callback(err, data)
})
},
del: function (req) {
file.del(req.offset, req.size, function(err, data) {
req.callback(err, data)
})
},
stat: function (req) {
file.stat( function(err, data) {
req.callback(err, data)
})
},
close: function (req) {
file.close( function(err, data) {
req.callback(err, data)
})
},
destroy: function (req) {
file.destroy( function(err, data) {
req.callback(err, data)
})
}
})
}
}

0 comments on commit f214ca9

Please sign in to comment.