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,47 +7,52 @@ 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 filePath = getChromeStoragePath(storageType, extensionId)
if (!fs.existsSync(filePath)) return null
return fs.readFileSync(filePath, 'utf8')
}
const writeChromeStorageFile = (storageType, extensionId, data) => { const readChromeStorageFile = (storageType, extensionId, cb) => {
const filePath = getChromeStoragePath(storageType, extensionId) const filePath = getChromeStoragePath(storageType, extensionId)
try { fs.readFile(filePath, 'utf8', (err, data) => {
fs.mkdirSync(path.dirname(filePath)) if (err && err.code === 'ENOENT') {
} catch (error) { return cb(null, null)
// Ignore error
} }
return fs.writeFileSync(filePath, data) cb(err, data)
})
} }
const getStorage = (storageType, extensionId) => { const writeChromeStorageFile = (storageType, extensionId, data, cb) => {
const data = readChromeStorageFile(storageType, extensionId) const filePath = getChromeStoragePath(storageType, extensionId)
if (data != null) {
return JSON.parse(data) fs.mkdir(path.dirname(filePath), err => {
if (err) { /* we just ignore the errors of mkdir */ }
fs.writeFile(filePath, data, cb)
})
}
const getStorage = (storageType, extensionId, cb) => {
readChromeStorageFile(storageType, extensionId, (err, data) => {
if (err) throw err
if (!cb) throw new TypeError('No callback provided')
if (data !== null) {
cb(JSON.parse(data))
} else { } else {
return {} 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) {
@ -61,7 +66,7 @@ const getStorageManager = (storageType, extensionId) => {
} }
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) {
@ -69,24 +74,22 @@ const getStorageManager = (storageType, extensionId) => {
if (value == null) value = defaults[key] if (value == null) value = defaults[key]
items[key] = value items[key] = value
}) })
scheduleCallback(items, callback) callback(items)
})
}, },
set (items, callback) { set (items, callback) {
const storage = getStorage(storageType, extensionId) getStorage(storageType, extensionId, storage => {
Object.keys(items).forEach(function (name) { Object.keys(items).forEach(function (name) {
storage[name] = items[name] storage[name] = items[name]
}) })
setStorage(storageType, extensionId, storage) setStorage(storageType, extensionId, storage, callback)
})
setTimeout(callback)
}, },
remove (keys, callback) { remove (keys, callback) {
const storage = getStorage(storageType, extensionId) getStorage(storageType, extensionId, storage => {
if (!Array.isArray(keys)) { if (!Array.isArray(keys)) {
keys = [keys] keys = [keys]
} }
@ -94,15 +97,12 @@ const getStorageManager = (storageType, extensionId) => {
delete storage[key] delete storage[key]
}) })
setStorage(storageType, extensionId, storage) setStorage(storageType, extensionId, storage, callback)
})
setTimeout(callback)
}, },
clear (callback) { clear (callback) {
setStorage(storageType, extensionId, {}) setStorage(storageType, extensionId, {}, callback)
setTimeout(callback)
} }
} }
} }