Refactor Journal singletone to use customHooks
The main issue with current implementation is that creating(initialising) Journal object requires async operation (reading data from a file or saving to a file):
async create(directory: string, journalData: JournalSchema) {
_journalUri = directory;
JournalManager.delete();
validateJournal(journalData);
const journal = new Journal({ journalData });
await JournalManager.saveToDevice();
_journalInstance = journal;
return _journalInstance;
},
async open(
journalUri: string,
errorCallback?: (e: unknown) => void,
): Promise<Journal> {
_journalUri = journalUri;
JournalManager.delete();
try {
const fileContent = await Journal.deviceReader(_journalUri);
const unvalidatedJournal: unknown = JSON.parse(fileContent);
if (typeof unvalidatedJournal !== "object" || unvalidatedJournal === null)
throw new Error("Can't read a journal. Is it in SavNote format?");
const validatedJournal = validateJournal(unvalidatedJournal);
const journal = new Journal({
journalData: validatedJournal,
});
_journalInstance = journal;
return _journalInstance;
} catch (e) {
console.error(e);
if (errorCallback) {
errorCallback(e);
}
throw e;
}
},
This forced me to create JournalManager which responsible for managing Journal's lifecycle and creating Journal's singletone instance.. This solution over-complicates the code.
The goal of refactoring is to simplify things by relying more on react patterns.
Also It is necessary to implement DI for easier testing. Probably there is no need in implementing DI container for now
Refactor Journal singletone to use customHooks
The main issue with current implementation is that creating(initialising) Journal object requires async operation (reading data from a file or saving to a file):
This forced me to create
JournalManagerwhich responsible for managing Journal's lifecycle and creating Journal's singletone instance.. This solution over-complicates the code.The goal of refactoring is to simplify things by relying more on react patterns.
Also It is necessary to implement DI for easier testing. Probably there is no need in implementing DI container for now