Change storage implementation to async

This commit is contained in:
Alexandre Lachèze 2017-07-11 00:35:53 +02:00
parent 9aac8967aa
commit c85f3cbd2c

View file

@ -7,102 +7,102 @@ const getChromeStoragePath = (storageType, extensionId) => {
return path.join( return path.join(
app.getPath('userData'), `/Chrome Storage/${extensionId}-${storageType}.json`) app.getPath('userData'), `/Chrome Storage/${extensionId}-${storageType}.json`)
} }
const readChromeStorageFile = (storageType, extensionId) => {
const readChromeStorageFile = (storageType, extensionId, cb) => {
const filePath = getChromeStoragePath(storageType, extensionId) const filePath = getChromeStoragePath(storageType, extensionId)
if (!fs.existsSync(filePath)) return null fs.readFile(filePath, 'utf8', (err, data) => {
return fs.readFileSync(filePath, 'utf8') if (err && err.code === 'ENOENT') {
return cb(null, null)
}
cb(err, data)
})
} }
const writeChromeStorageFile = (storageType, extensionId, data) => { const writeChromeStorageFile = (storageType, extensionId, data, cb) => {
const filePath = getChromeStoragePath(storageType, extensionId) const filePath = getChromeStoragePath(storageType, extensionId)
try {
fs.mkdirSync(path.dirname(filePath)) fs.mkdir(path.dirname(filePath), err => {
} catch (error) { if (err) { /* we just ignore the errors of mkdir */ }
// Ignore error fs.writeFile(filePath, data, cb)
} })
return fs.writeFileSync(filePath, data)
} }
const getStorage = (storageType, extensionId) => { const getStorage = (storageType, extensionId, cb) => {
const data = readChromeStorageFile(storageType, extensionId) readChromeStorageFile(storageType, extensionId, (err, data) => {
if (data != null) { if (err) throw err
return JSON.parse(data) if (!cb) throw new TypeError('No callback provided')
} else {
return {} if (data !== null) {
} cb(JSON.parse(data))
} else {
cb({})
}
})
} }
const setStorage = (storageType, extensionId, storage) => { const setStorage = (storageType, extensionId, storage, cb) => {
const json = JSON.stringify(storage) const json = JSON.stringify(storage)
writeChromeStorageFile(storageType, extensionId, json) writeChromeStorageFile(storageType, extensionId, json, err => {
} if (err) throw err
if (cb) cb()
const scheduleCallback = (items, callback) => {
setTimeout(function () {
callback(items)
}) })
} }
const getStorageManager = (storageType, extensionId) => { const getStorageManager = (storageType, extensionId) => {
return { return {
get (keys, callback) { get (keys, callback) {
const storage = getStorage(storageType, extensionId) getStorage(storageType, extensionId, storage => {
if (keys == null) return scheduleCallback(storage, callback) if (keys == null) return callback(storage)
let defaults = {} let defaults = {}
switch (typeof keys) { switch (typeof keys) {
case 'string': case 'string':
keys = [keys] keys = [keys]
break break
case 'object': case 'object':
if (!Array.isArray(keys)) { if (!Array.isArray(keys)) {
defaults = keys defaults = keys
keys = Object.keys(keys) keys = Object.keys(keys)
} }
break break
} }
if (keys.length === 0) return scheduleCallback({}, callback) if (keys.length === 0) return callback({})
let items = {} let items = {}
keys.forEach(function (key) { keys.forEach(function (key) {
var value = storage[key] var value = storage[key]
if (value == null) value = defaults[key] if (value == null) value = defaults[key]
items[key] = value items[key] = value
})
callback(items)
}) })
scheduleCallback(items, callback)
}, },
set (items, callback) { set (items, callback) {
const storage = getStorage(storageType, extensionId) getStorage(storageType, extensionId, storage => {
Object.keys(items).forEach(function (name) {
storage[name] = items[name]
})
Object.keys(items).forEach(function (name) { setStorage(storageType, extensionId, storage, callback)
storage[name] = items[name]
}) })
setStorage(storageType, extensionId, storage)
setTimeout(callback)
}, },
remove (keys, callback) { remove (keys, callback) {
const storage = getStorage(storageType, extensionId) getStorage(storageType, extensionId, storage => {
if (!Array.isArray(keys)) {
keys = [keys]
}
keys.forEach(function (key) {
delete storage[key]
})
if (!Array.isArray(keys)) { setStorage(storageType, extensionId, storage, callback)
keys = [keys]
}
keys.forEach(function (key) {
delete storage[key]
}) })
setStorage(storageType, extensionId, storage)
setTimeout(callback)
}, },
clear (callback) { clear (callback) {
setStorage(storageType, extensionId, {}) setStorage(storageType, extensionId, {}, callback)
setTimeout(callback)
} }
} }
} }