-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32678 from tienifr/fix/32232
Fix: PDF is not cached
- Loading branch information
Showing
9 changed files
with
92 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {exists, unlink} from 'react-native-fs'; | ||
import Onyx from 'react-native-onyx'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import type {Add, Clear, ClearAll, ClearByKey} from './types'; | ||
|
||
/* | ||
* We need to save the paths of PDF files so we can delete them later. | ||
* This is to remove the cached PDFs when an attachment is deleted or the user logs out. | ||
*/ | ||
let pdfPaths: Record<string, string> = {}; | ||
Onyx.connect({ | ||
key: ONYXKEYS.CACHED_PDF_PATHS, | ||
callback: (val) => { | ||
pdfPaths = val ?? {}; | ||
}, | ||
}); | ||
|
||
const add: Add = (id: string, path: string) => { | ||
if (pdfPaths[id]) { | ||
return Promise.resolve(); | ||
} | ||
return Onyx.merge(ONYXKEYS.CACHED_PDF_PATHS, {[id]: path}); | ||
}; | ||
|
||
const clear: Clear = (path: string) => { | ||
if (!path) { | ||
return Promise.resolve(); | ||
} | ||
return new Promise((resolve) => { | ||
exists(path).then((exist) => { | ||
if (!exist) { | ||
resolve(); | ||
} | ||
return unlink(path); | ||
}); | ||
}); | ||
}; | ||
|
||
const clearByKey: ClearByKey = (id: string) => { | ||
clear(pdfPaths[id] ?? '').then(() => Onyx.merge(ONYXKEYS.CACHED_PDF_PATHS, {[id]: null})); | ||
}; | ||
|
||
const clearAll: ClearAll = () => { | ||
Promise.all(Object.values(pdfPaths).map(clear)).then(() => Onyx.merge(ONYXKEYS.CACHED_PDF_PATHS, {})); | ||
}; | ||
|
||
export {add, clearByKey, clearAll}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type {Add, ClearAll, ClearByKey} from './types'; | ||
|
||
const add: Add = () => Promise.resolve(); | ||
|
||
const clearByKey: ClearByKey = () => {}; | ||
|
||
const clearAll: ClearAll = () => {}; | ||
|
||
export {add, clearByKey, clearAll}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
type Add = (id: string, path: string) => Promise<void>; | ||
type Clear = (path: string) => Promise<void>; | ||
type ClearAll = () => void; | ||
type ClearByKey = (id: string) => void; | ||
|
||
export type {Add, Clear, ClearAll, ClearByKey}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters