Skip to content

Commit 71a463a

Browse files
author
Ritika Mishra
committed
improved values passing of canvascount
1 parent bd61a8e commit 71a463a

File tree

3 files changed

+69
-88
lines changed

3 files changed

+69
-88
lines changed

src/components/Connection.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,14 @@ const Connection: React.FC<ConnectionProps> = ({
229229
});
230230
}
231231
};
232+
const setCanvasCountInWorker = (canvasCount:number) => {
233+
if (!workerRef.current) {
234+
initializeWorker();
235+
}
236+
// Send canvasCount independently to the worker
237+
workerRef.current?.postMessage({ action: 'setCanvasCount', canvasCount: canvasnumbersRef.current });
238+
};
239+
setCanvasCountInWorker(canvasnumbersRef.current);
232240

233241
const processBuffer = async (bufferIndex: number, canvasCount: number) => {
234242
if (!workerRef.current) {

workers/indexedDBWorker.ts

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ self.onmessage = async (event) => {
1111
canvasCount = event.data.canvasCount; // Update canvas count independently
1212
self.postMessage({ success: true, message: 'Canvas count updated' });
1313
break;
14+
case 'write':
15+
const success = await writeToIndexedDB(db, data, filename, canvasCount);
16+
self.postMessage({ success });
17+
break;
18+
case 'getAllData':
19+
try {
20+
const allData = await getAllDataFromIndexedDB(db);
21+
self.postMessage({ allData });
22+
} catch (error) {
23+
self.postMessage({ error: 'Failed to retrieve all data from IndexedDB' });
24+
}
25+
break;
1426
case 'getFileCountFromIndexedDB':
1527
try {
1628
const allData = await getFileCountFromIndexedDB(db);
@@ -58,6 +70,55 @@ const openIndexedDB = async (): Promise<IDBDatabase> => {
5870
});
5971
};
6072

73+
// Function to write data to IndexedDB
74+
const writeToIndexedDB = async (db: IDBDatabase, data: number[][], filename: string, canvasCount: number): Promise<boolean> => {
75+
return new Promise((resolve, reject) => {
76+
const tx = db.transaction("ChordsRecordings", "readwrite");
77+
const store = tx.objectStore("ChordsRecordings");
78+
79+
const getRequest = store.get(filename);
80+
81+
getRequest.onsuccess = () => {
82+
const existingRecord = getRequest.result;
83+
84+
if (existingRecord) {
85+
existingRecord.content.push(...data);
86+
const putRequest = store.put(existingRecord);
87+
putRequest.onsuccess = () => resolve(true);
88+
putRequest.onerror = () => reject(false);
89+
} else {
90+
const newRecord = { filename, content: [...data] };
91+
const putRequest = store.put(newRecord);
92+
putRequest.onsuccess = () => resolve(true);
93+
putRequest.onerror = () => reject(false);
94+
}
95+
};
96+
getRequest.onerror = () => reject(false);
97+
});
98+
};
99+
100+
// Function to get all data
101+
const getAllDataFromIndexedDB = async (db: IDBDatabase): Promise<any[]> => {
102+
return new Promise((resolve, reject) => {
103+
const tx = db.transaction(["ChordsRecordings"], "readonly");
104+
const store = tx.objectStore("ChordsRecordings");
105+
const request = store.getAll();
106+
107+
request.onsuccess = () => {
108+
const data = request.result.map((item: any, index: number) => ({
109+
id: index + 1,
110+
...item,
111+
}));
112+
resolve(data);
113+
};
114+
115+
request.onerror = (error) => {
116+
console.error("Error retrieving data from IndexedDB:", error);
117+
reject(error);
118+
};
119+
});
120+
};
121+
61122
// Function to convert data to CSV
62123
const convertToCSV = (data: any[], canvasCount: number): string => {
63124
if (!Array.isArray(data) || data.length === 0) return "";
@@ -122,7 +183,6 @@ const saveAllDataAsZip = async (canvasCount: number): Promise<Blob> => {
122183

123184
const saveDataByFilename = async (filename: string, canvasCount: number): Promise<Blob> => {
124185
try {
125-
console.log("filename",filename);
126186
const dbRequest = indexedDB.open("ChordsRecordings");
127187

128188
return new Promise((resolve, reject) => {
@@ -141,7 +201,6 @@ const saveDataByFilename = async (filename: string, canvasCount: number): Promis
141201

142202
getRequest.onsuccess = () => {
143203
const result = getRequest.result;
144-
console.log("Retrieved IndexedDB result:", result);
145204

146205
if (!result || !Array.isArray(result.content)) {
147206
reject(new Error("No data found for the given filename or invalid data format."));

workers/writeWorker.ts

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)