Skip to content

Instantly share code, notes, and snippets.

@jlongster

jlongster/save-messages.js Secret

Created Dec 3, 2020
Embed
What would you like to do?
export async function save(messages) {
let db = await getDatabase();
let fileId = prefs.getPrefs().id;
let transaction = db.transaction(['messages'], 'readwrite');
let objectStore = transaction.objectStore('messages');
let promise = new Promise((resolve, reject) => {
transaction.onerror = e => reject(e);
transaction.oncomplete = e => resolve();
});
for (let message of messages) {
message = {
...message,
timestamp: message.timestamp.toString(),
fileId
};
objectStore.put(message);
}
// Don't do anything if it fails
try {
await promise;
} catch (e) {
console.log('Error while saving web storage:', e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment