port react-native storage module to async functions

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

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

Loading…
Cancel
Save