-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
624c8b2
commit 2b536f1
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
const dbName = "event_logs"; | ||
const objectStoreName = "events"; | ||
|
||
const logWidgetEvent = async (eventName, time) => { | ||
const widgetEventInfo = { | ||
type: eventName, | ||
time: time, | ||
}; | ||
|
||
const request = indexedDB.open(dbName); | ||
|
||
request.onerror = (event) => { | ||
console.log(`Failed to open db with: ${event.errorCode}`) | ||
}; | ||
|
||
request.onsuccess = (event) => { | ||
let db = event.target.result; | ||
const transaction = | ||
db.transaction(objectStoreName, "readwrite", { durability: "strict"}); | ||
|
||
transaction.oncomplete = (event) => { | ||
console.log("widget event logged to IndexedDB"); | ||
} | ||
|
||
transaction.onerror = (event) => { | ||
console.log(event.errorCode); | ||
} | ||
|
||
const objectStore = transaction.objectStore(objectStoreName); | ||
const request = objectStore.add(widgetEventInfo); | ||
}; | ||
|
||
request.onupgradeneeded = (event) => { | ||
const db = event.target.result; | ||
const objectStore = db.createObjectStore("events", { autoIncrement: true }); | ||
|
||
objectStore.createIndex("type", "type", { unique: false }); | ||
objectStore.createIndex("time", "time", { unique: false }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters