Skip to content

Commit

Permalink
Added cacheOngoingCalculation so that ongoing calculations are cached…
Browse files Browse the repository at this point in the history
… even if extension is closed. Calculations only stored when stop calculation is clicked

Rename emissionsRepository to calculationsRepository
  • Loading branch information
juskek committed Jul 6, 2023
1 parent 40c524d commit 1a686f7
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 10 deletions.
49 changes: 47 additions & 2 deletions src/data/calculations/CalculationsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ export class CalculationsRepository implements ICalculationsRepository {
}
}

async cacheOngoingCalculation(
calculationData: CalculationData
): Promise<void> {
try {
await this.remoteDataSource.set({
ongoingCalculation: JSON.stringify(calculationData),
});
} catch (e: unknown) {
throw Error(e as string);
}
}

async clearOngoingCalculation(): Promise<void> {
try {
await this.remoteDataSource.set({
ongoingCalculation: null,
});
} catch (e: unknown) {
throw Error(e as string);
}
}

async getAllCalculations(): Promise<CalculationData[]> {
try {
const data = await this.remoteDataSource.get({
Expand All @@ -33,13 +55,36 @@ export class CalculationsRepository implements ICalculationsRepository {
}
}

async getLastCalculation(): Promise<CalculationData | undefined> {
async _getOngoingCalculation(): Promise<CalculationData | null> {
try {
const data = await this.remoteDataSource.get({
ongoingCalculation: null,
});
console.log("data", data);
if (data["ongoingCalculation"] !== null) {
return JSON.parse(
data["ongoingCalculation"] as string
) as CalculationData;
}
return null;
} catch (e: unknown) {
throw Error(e as string);
}
}

async getLastCalculation(): Promise<CalculationData | null> {
try {
const ongoingCalculation = await this._getOngoingCalculation();
if (ongoingCalculation !== null) {
console.log("ongoingCalculation", ongoingCalculation);
return ongoingCalculation;
}
const oldCalculations = await this.getAllCalculations();
if (oldCalculations.length > 0) {
console.log("oldCalculations", oldCalculations);
return oldCalculations[0];
}
return undefined;
return null;
} catch (e: unknown) {
throw Error(e as string);
}
Expand Down
8 changes: 7 additions & 1 deletion src/data/calculations/ICalculationsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ export abstract class ICalculationsRepository {

abstract storeCalculation(calculationData: CalculationData): Promise<void>;

abstract getLastCalculation(): Promise<CalculationData | undefined>;
abstract cacheOngoingCalculation(
calculationData: CalculationData
): Promise<void>;

abstract clearOngoingCalculation(): Promise<void>;

abstract getLastCalculation(): Promise<CalculationData | null>;

abstract getAllCalculations(): Promise<CalculationData[]>;
}
Expand Down
21 changes: 19 additions & 2 deletions src/data/calculations/TestCalculationsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,34 @@ import {

export class TestCalculationsRepository implements ICalculationsRepository {
private _allCalculations: CalculationData[] = [];
private _ongoingCalculation: CalculationData | null = null;

async storeCalculation(calculationData: CalculationData): Promise<void> {
const tempArray = [calculationData, ...this._allCalculations];
this._allCalculations = tempArray;
}

async cacheOngoingCalculation(
calculationData: CalculationData
): Promise<void> {
this._ongoingCalculation = calculationData;
}

async clearOngoingCalculation(): Promise<void> {
this._ongoingCalculation = null;
}

async getAllCalculations(): Promise<CalculationData[]> {
return this._allCalculations;
}

async getLastCalculation(): Promise<CalculationData> {
return this._allCalculations[0];
async getLastCalculation(): Promise<CalculationData | null> {
if (this._ongoingCalculation !== null) {
return this._ongoingCalculation;
}
if (this._allCalculations.length > 0) {
return this._allCalculations[0];
}
return null;
}
}
2 changes: 1 addition & 1 deletion src/data/storage/IStorageRepository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { StorageRepository } from "./StorageRepository";
import { TestStorageRepository } from "./TestStorageRepository";

export type StorageDataType = string | number;
export type StorageDataType = string | number | null;
export abstract class IStorageRepository {
private static _instance: IStorageRepository;
static get instance(): IStorageRepository {
Expand Down
15 changes: 11 additions & 4 deletions src/usePopup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { IBytesRepository } from "./data/bytes/IBytesRepository";
export const usePopup = () => {
const selectedCountriesRepository: ISelectedCountriesRepository =
ISelectedCountriesRepository.instance;
const emissionsRepository: ICalculationsRepository =
const calculationsRepository: ICalculationsRepository =
ICalculationsRepository.instance;
const bytesRepository: IBytesRepository = IBytesRepository.instance;

Expand Down Expand Up @@ -113,6 +113,13 @@ export const usePopup = () => {
}
}
});
calculationsRepository.storeCalculation({
bytes: totalBytesTransferred,
emissions: emissions,
specificEmissions: averageSpecificEmissions,
selectedCountries: selectedCountries,
});
calculationsRepository.clearOngoingCalculation();
};

const addSelectedCountry = async (country: CountryName) => {
Expand Down Expand Up @@ -142,7 +149,7 @@ export const usePopup = () => {
selectedCountries
);
setEmissions(_emissions);
emissionsRepository.storeCalculation({
calculationsRepository.cacheOngoingCalculation({
bytes: changes.totalBytesTransferred.newValue,
emissions: _emissions,
specificEmissions: averageSpecificEmissions,
Expand All @@ -160,7 +167,7 @@ export const usePopup = () => {
totalBytesTransferredListener
);
};
}, [selectedCountries, averageSpecificEmissions, emissionsRepository]);
}, [selectedCountries, averageSpecificEmissions, calculationsRepository]);

useMountEffect(() => {
selectedCountriesRepository
Expand All @@ -171,7 +178,7 @@ export const usePopup = () => {
});

useMountEffect(() => {
emissionsRepository.getLastCalculation().then((calculationData) => {
calculationsRepository.getLastCalculation().then((calculationData) => {
settotalBytesTransferred(calculationData?.bytes ?? 0);
setEmissions(calculationData?.emissions ?? 0);
setAverageSpecificEmissions(
Expand Down

0 comments on commit 1a686f7

Please sign in to comment.