port react-native storage module to async functions

pull/11/head
Timothée Rebours 3 years ago
parent c480c1230c
commit 5f8850df1b
  1. 132
      browser-version/lib/storage.react-native.js

@ -7,80 +7,120 @@
* This version is the react-native version
*/
const AsyncStorage = require('@react-native-async-storage/async-storage').default
const { callbackify } = require('util')
const exists = (filename, cback) => {
// eslint-disable-next-line node/handle-callback-err
AsyncStorage.getItem(filename, (err, value) => {
if (value !== null) {
return cback(true)
} else {
return cback(false)
}
})
const existsAsync = async filename => {
try {
const value = await AsyncStorage.getItem(filename)
if (value !== null) return true // Even if value is undefined, localforage returns null
return false
} catch (error) {
return false
}
}
const rename = (filename, newFilename, callback) => {
// eslint-disable-next-line node/handle-callback-err
AsyncStorage.getItem(filename, (err, value) => {
if (value === null) {
this.storage.removeItem(newFilename, callback)
} else {
this.storage.setItem(newFilename, value, () => {
this.storage.removeItem(filename, callback)
})
const exists = callbackify(existsAsync)
const renameAsync = async (filename, newFilename) => {
try {
const value = await AsyncStorage.getItem(filename)
if (value === null) await AsyncStorage.removeItem(newFilename)
else {
await AsyncStorage.setItem(newFilename, value)
await AsyncStorage.removeItem(filename)
}
})
} catch (err) {
console.warn('An error happened while renaming, skip')
}
}
const writeFile = (filename, contents, options, callback) => {
// Options do not matter in a react-native setup
if (typeof options === 'function') { callback = options }
AsyncStorage.setItem(filename, contents, callback)
const rename = callbackify(renameAsync)
const writeFileAsync = async (filename, contents, options) => {
// Options do not matter in browser setup
try {
await AsyncStorage.setItem(filename, contents)
} catch (error) {
console.warn('An error happened while writing, skip')
}
}
const appendFile = (filename, toAppend, options, callback) => {
// Options do not matter in a react-native setup
if (typeof options === 'function') { callback = options }
const writeFile = callbackify(writeFileAsync)
// eslint-disable-next-line node/handle-callback-err
AsyncStorage.getItem(filename, (err, contents) => {
contents = contents || ''
contents += toAppend
AsyncStorage.setItem(filename, contents, callback)
})
const appendFileAsync = async (filename, toAppend, options) => {
// Options do not matter in browser setup
try {
const contents = (await AsyncStorage.getItem(filename)) || ''
await AsyncStorage.setItem(filename, contents + toAppend)
} catch (error) {
console.warn('An error happened appending to file writing, skip')
}
}
const readFile = (filename, options, callback) => {
// Options do not matter in a react-native setup
if (typeof options === 'function') { callback = options }
// eslint-disable-next-line node/handle-callback-err
AsyncStorage.getItem(filename, (err, contents) => {
return callback(null, contents || '')
})
const appendFile = callbackify(appendFileAsync)
const readFileAsync = async (filename, options) => {
try {
return (await AsyncStorage.getItem(filename)) || ''
} catch (error) {
console.warn('An error happened while reading, skip')
return ''
}
}
const unlink = (filename, callback) => {
AsyncStorage.removeItem(filename, callback)
const readFile = callbackify(readFileAsync)
const unlinkAsync = async filename => {
try {
await AsyncStorage.removeItem(filename)
} catch (error) {
console.warn('An error happened while unlinking, skip')
}
}
const unlink = callbackify(unlinkAsync)
// Nothing to do, no directories will be used on react-native
const mkdir = (dir, options, callback) => callback()
const mkdirAsync = (dir, options) => Promise.resolve()
const mkdir = callbackify(mkdirAsync)
// Nothing to do, no data corruption possible on react-native
const ensureDatafileIntegrity = (filename, callback) => callback(null)
// Nothing to do, no data corruption possible in the browser
const ensureDatafileIntegrityAsync = (filename) => Promise.resolve()
const crashSafeWriteFileLines = (filename, lines, callback) => {
const ensureDatafileIntegrity = callbackify(ensureDatafileIntegrityAsync)
const crashSafeWriteFileLinesAsync = async (filename, lines) => {
lines.push('') // Add final new line
writeFile(filename, lines.join('\n'), callback)
await writeFileAsync(filename, lines.join('\n'))
}
const crashSafeWriteFileLines = callbackify(crashSafeWriteFileLinesAsync)
// Interface
module.exports.exists = exists
module.exports.existsAsync = existsAsync
module.exports.rename = rename
module.exports.renameAsync = renameAsync
module.exports.writeFile = writeFile
module.exports.writeFileAsync = writeFileAsync
module.exports.crashSafeWriteFileLines = crashSafeWriteFileLines
module.exports.crashSafeWriteFileLinesAsync = crashSafeWriteFileLinesAsync
module.exports.appendFile = appendFile
module.exports.appendFileAsync = appendFileAsync
module.exports.readFile = readFile
module.exports.readFileAsync = readFileAsync
module.exports.unlink = unlink
module.exports.unlinkAsync = unlinkAsync
module.exports.mkdir = mkdir
module.exports.mkdirAsync = mkdirAsync
module.exports.ensureDatafileIntegrity = ensureDatafileIntegrity
module.exports.ensureDatafileIntegrityAsync = ensureDatafileIntegrityAsync

Loading…
Cancel
Save