Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onStorageReady helper to handle non-existent file paths #33

Merged
merged 1 commit into from
Jun 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 58 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@

import RNFetchBlob from 'rn-fetch-blob'

const createStoragePathIfNeeded = path =>
RNFetchBlob.fs.exists(path).then(exists => {
return exists
? new Promise(resolve => resolve(true))
: RNFetchBlob.fs.mkdir(path);
});
const onStorageReadyFactory = (storagePath) => {
return (func) => {
const storage = createStoragePathIfNeeded(storagePath);
return (...args) => storage.then(() => func(...args));
}
};

const defaultStoragePath = `${RNFetchBlob.fs.dirs.DocumentDir}/persistStore`

let onStorageReady = onStorageReadyFactory(defaultStoragePath);
let options = {
storagePath: `${RNFetchBlob.fs.dirs.DocumentDir}/persistStore`,
storagePath: defaultStoragePath,
encoding: 'utf8',
toFileName: (name: string) => name.split(':').join('-'),
fromFileName: (name: string) => name.split('-').join(':'),
}
};

const pathForKey = (key: string) => `${options.storagePath}/${options.toFileName(key)}`

Expand All @@ -21,6 +37,7 @@ const FilesystemStorage = {
...options,
...customOptions,
}
onStorageReady = onStorageReadyFactory(options.storagePath);
},

setItem: (
Expand All @@ -32,19 +49,36 @@ const FilesystemStorage = {
.then(() => callback && callback())
.catch(error => callback && callback(error)),

getItem: (
getItem: onStorageReady((
key: string,
callback: (error: ?Error, result: ?string) => void
) =>
RNFetchBlob.fs.readFile(pathForKey(options.toFileName(key)), options.encoding)
) => {
const filePath = pathForKey(options.toFileName(key));

return RNFetchBlob.fs.readFile(filePath, options.encoding)
.then(data => {
callback && callback(null, data)
if (!callback) return data
})
.catch(error => {
callback && callback(error)
if (!callback) throw error
}),
return RNFetchBlob.fs
.exists(filePath)
.then(exists => {
if (exists) {
// The error is not related to the existence of the file
callback && callback(error);
if (!callback) throw error;
}

return '';
})
.catch(() => {
// We throw the original error
callback && callback(error);
if (!callback) throw error;
});
});
}),

removeItem: (
key: string,
Expand All @@ -61,21 +95,21 @@ const FilesystemStorage = {
callback: (error: ?Error, keys: ?Array<string>) => void,
) =>
RNFetchBlob.fs.exists(options.storagePath)
.then(exists =>
exists ? true : RNFetchBlob.fs.mkdir(options.storagePath)
)
.then(() =>
RNFetchBlob.fs.ls(options.storagePath)
.then(files => files.map(file => options.fromFileName(file)))
.then(files => {
callback && callback(null, files)
if (!callback) return files
})
)
.catch(error => {
callback && callback(error)
if (!callback) throw error
}),
.then(exists =>
exists ? true : RNFetchBlob.fs.mkdir(options.storagePath)
)
.then(() =>
RNFetchBlob.fs.ls(options.storagePath)
.then(files => files.map(file => options.fromFileName(file)))
.then(files => {
callback && callback(null, files)
if (!callback) return files
})
)
.catch(error => {
callback && callback(error)
if (!callback) throw error
}),
}

FilesystemStorage.clear = (
Expand Down Expand Up @@ -104,4 +138,4 @@ FilesystemStorage.clear = (
if (!callback) throw error
})

export default FilesystemStorage
export default FilesystemStorage