Skip to content

Commit 3604577

Browse files
committed
fix: rm update from queue if not-exists...
calling code handles that case. related: added try/catch/throw to lookup of this data
1 parent e7d0f4c commit 3604577

File tree

2 files changed

+92
-27
lines changed

2 files changed

+92
-27
lines changed

packages/db/src/impl/couch/updateQueue.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,33 @@ export default class UpdateQueue extends Loggable {
1515
private readDB: PouchDB.Database; // Database for read operations
1616
private writeDB: PouchDB.Database; // Database for write operations (local-first)
1717

18+
/**
19+
* Queues an update for a document and applies it with conflict resolution.
20+
*
21+
* @param id - Document ID to update
22+
* @param update - Partial object or function that transforms the document
23+
* @returns Promise resolving to the updated document
24+
*
25+
* @throws {PouchError} with status 404 if document doesn't exist
26+
*
27+
* @remarks
28+
* **Error Handling Pattern:**
29+
* - This method does NOT create documents if they don't exist
30+
* - Callers are responsible for handling 404 errors and creating documents
31+
* - This design maintains separation of concerns (UpdateQueue handles conflicts, callers handle lifecycle)
32+
*
33+
* @example
34+
* ```typescript
35+
* try {
36+
* await updateQueue.update(docId, (doc) => ({ ...doc, field: newValue }));
37+
* } catch (e) {
38+
* if ((e as PouchError).status === 404) {
39+
* // Create the document with initial values
40+
* await db.put({ _id: docId, field: newValue, ...initialFields });
41+
* }
42+
* }
43+
* ```
44+
*/
1845
public update<T extends PouchDB.Core.Document<object>>(
1946
id: PouchDB.Core.DocumentId,
2047
update: Update<T>
@@ -57,7 +84,6 @@ export default class UpdateQueue extends Loggable {
5784
for (let i = 0; i < MAX_RETRIES; i++) {
5885
try {
5986
const doc = await this.readDB.get<T>(id);
60-
logger.debug(`Retrieved doc: ${id}`);
6187

6288
// Create a new doc object to apply updates to for this attempt
6389
let updatedDoc = { ...doc };
@@ -77,7 +103,6 @@ export default class UpdateQueue extends Loggable {
77103
}
78104

79105
await this.writeDB.put<T>(updatedDoc);
80-
logger.debug(`Put doc: ${id}`);
81106

82107
// Success! Remove the updates we just applied.
83108
this.pendingUpdates[id].splice(0, updatesToApply.length);
@@ -98,6 +123,7 @@ export default class UpdateQueue extends Loggable {
98123
} else if (e.name === 'not_found' && i === 0) {
99124
// Document not present - throw to caller for initialization
100125
logger.warn(`Update failed for ${id} - does not exist. Throwing to caller.`);
126+
delete this.inprogressUpdates[id];
101127
throw e; // Let caller handle
102128
} else {
103129
// Max retries reached or a non-conflict error

packages/db/src/study/services/ResponseProcessor.ts

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,71 @@ export class ResponseProcessor {
6060
};
6161
}
6262

63-
const history = await cardHistory;
63+
// Debug logging for response processing
64+
// logger.debug('[ResponseProcessor] Processing response', {
65+
// cardId,
66+
// courseId,
67+
// isCorrect: cardRecord.isCorrect,
68+
// performance: cardRecord.performance,
69+
// priorAttempts: cardRecord.priorAttemps,
70+
// currentSessionViews: sessionViews,
71+
// maxSessionViews,
72+
// maxAttemptsPerView,
73+
// currentCardRecordsLength: currentCard.records.length,
74+
// studySessionSourceType: studySessionItem.contentSourceType,
75+
// studySessionSourceID: studySessionItem.contentSourceID,
76+
// studySessionItemId: studySessionItem.cardID,
77+
// studySessionItemType: studySessionItem.contentSourceType,
6478

65-
// Handle correct responses
66-
if (cardRecord.isCorrect) {
67-
return this.processCorrectResponse(
68-
cardRecord,
69-
history,
70-
studySessionItem,
71-
courseRegistrationDoc,
72-
currentCard,
73-
courseId,
74-
cardId
75-
);
76-
} else {
77-
// Handle incorrect responses
78-
return this.processIncorrectResponse(
79-
cardRecord,
80-
history,
81-
courseRegistrationDoc,
82-
currentCard,
83-
courseId,
84-
cardId,
85-
maxAttemptsPerView,
86-
maxSessionViews,
87-
sessionViews
88-
);
79+
// cardRecordTimestamp: cardRecord.timeStamp,
80+
// cardRecordResponseTime: cardRecord.timeSpent,
81+
// });
82+
83+
try {
84+
const history = await cardHistory;
85+
86+
// Debug logging for card history
87+
// logger.debug('[ResponseProcessor] History loaded:', {
88+
// cardId,
89+
// historyRecordsCount: history.records.length,
90+
// historyRecords: history.records.map((record) => ({
91+
// timeStamp: record.timeStamp,
92+
// isCorrect: 'isCorrect' in record ? record.isCorrect : 'N/A',
93+
// performance: 'performance' in record ? record.performance : 'N/A',
94+
// priorAttempts: 'priorAttemps' in record ? record.priorAttemps : 'N/A',
95+
// })),
96+
// firstInteraction: history.records.length === 1,
97+
// lastRecord: history.records[history.records.length - 1],
98+
// });
99+
100+
// Handle correct responses
101+
if (cardRecord.isCorrect) {
102+
return this.processCorrectResponse(
103+
cardRecord,
104+
history,
105+
studySessionItem,
106+
courseRegistrationDoc,
107+
currentCard,
108+
courseId,
109+
cardId
110+
);
111+
} else {
112+
// Handle incorrect responses
113+
return this.processIncorrectResponse(
114+
cardRecord,
115+
history,
116+
courseRegistrationDoc,
117+
currentCard,
118+
courseId,
119+
cardId,
120+
maxAttemptsPerView,
121+
maxSessionViews,
122+
sessionViews
123+
);
124+
}
125+
} catch (e: unknown) {
126+
logger.error('[ResponseProcessor] Failed to load card history', { e, cardId });
127+
throw e;
89128
}
90129
}
91130

0 commit comments

Comments
 (0)