Skip to content

Commit df55059

Browse files
committed
Refactor Database class: streamline entry management by consolidating entry updates and improving memory handling for time entries.
1 parent a45790a commit df55059

1 file changed

Lines changed: 15 additions & 21 deletions

File tree

src/database.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,54 +14,48 @@ export interface SummaryData {
1414

1515
export class Database {
1616
private context: vscode.ExtensionContext;
17+
private entries: TimeEntry[] | null = null;
1718

1819
constructor(context: vscode.ExtensionContext) {
1920
this.context = context;
2021
// Initialize storage if empty
2122
if (!this.context.globalState.get('timeEntries')) {
2223
this.context.globalState.update('timeEntries', []);
2324
}
24-
25-
// Debug logging
26-
const entries = this.getEntries();
27-
console.log('Database initialized with entries:', entries);
28-
29-
// Log the storage URI path
30-
const storagePath = this.context.storageUri?.fsPath;
31-
if (storagePath) {
32-
console.log('Extension Data Storage Path:', storagePath);
33-
//vscode.window.showInformationMessage(`Data is stored at: ${storagePath}`);
34-
}
25+
// Load entries into memory
26+
this.entries = this.context.globalState.get<TimeEntry[]>('timeEntries', []);
3527
}
3628

3729
async addEntry(date: Date, project: string, timeSpent: number) {
3830
const dateString = date.toISOString().split('T')[0];
3931
const entries = this.getEntries();
4032

41-
console.log('Adding entry:', { date: dateString, project, timeSpent });
42-
console.log('Current entries:', entries);
43-
4433
const existingEntryIndex = entries.findIndex(entry => entry.date === dateString && entry.project === project);
4534

4635
if (existingEntryIndex !== -1) {
4736
entries[existingEntryIndex].timeSpent += timeSpent;
48-
console.log('Updated existing entry:', entries[existingEntryIndex]);
4937
} else {
5038
entries.push({ date: dateString, project, timeSpent });
51-
console.log('Added new entry');
5239
}
5340

5441
try {
55-
await this.context.globalState.update('timeEntries', entries);
56-
console.log('Successfully saved entries:', this.getEntries());
42+
await this.updateEntries(entries);
5743
} catch (error) {
5844
console.error('Error saving entry:', error);
5945
vscode.window.showErrorMessage('Failed to save time entry');
6046
}
6147
}
6248

6349
getEntries(): TimeEntry[] {
64-
return this.context.globalState.get<TimeEntry[]>('timeEntries', []);
50+
if (!this.entries) {
51+
this.entries = this.context.globalState.get<TimeEntry[]>('timeEntries', []);
52+
}
53+
return this.entries;
54+
}
55+
56+
private async updateEntries(entries: TimeEntry[]): Promise<void> {
57+
this.entries = entries;
58+
await this.context.globalState.update('timeEntries', entries);
6559
}
6660

6761
async getSummaryData(): Promise<SummaryData> {
@@ -96,10 +90,10 @@ export class Database {
9690
const today = new Date().toISOString().split('T')[0];
9791
const entries = this.getEntries();
9892
const updatedEntries = entries.filter(entry => entry.date !== today);
99-
await this.context.globalState.update('timeEntries', updatedEntries);
93+
await this.updateEntries(updatedEntries);
10094
}
10195

10296
async resetAllTime(): Promise<void> {
103-
await this.context.globalState.update('timeEntries', []);
97+
await this.updateEntries([]);
10498
}
10599
}

0 commit comments

Comments
 (0)