Skip to content

Commit

Permalink
fix(db): crash on safari that reports transaction ends
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Jul 10, 2019
1 parent d54c0e4 commit 1c08c8c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
9 changes: 3 additions & 6 deletions src/database/avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ export async function storeAvatarDB(id: IdentityWithAvatar, avatar: ArrayBuffer)
lastAccessTime: new Date(),
}
const t = (await db).transaction(['avatars', 'metadata'], 'readwrite')
const a = t.objectStore('avatars').put(avatar, id.toText())
await t.objectStore('avatars').put(avatar, id.toText())
await t.objectStore('metadata').put(meta)
await a
return
}
/**
Expand Down Expand Up @@ -110,11 +109,9 @@ export async function isAvatarOutdatedDB(
*/
export async function deleteAvatarsDB(ids: IdentityWithAvatar[]) {
const t = (await db).transaction(['avatars', 'metadata'], 'readwrite')
const promises: Promise<void>[] = []
for (const id of ids) {
const a = t.objectStore('avatars').delete(id.toText())
const b = t.objectStore('metadata').delete(id.toText())
promises.push(a, b)
t.objectStore('avatars').delete(id.toText())
t.objectStore('metadata').delete(id.toText())
}
return
}
16 changes: 11 additions & 5 deletions src/database/people.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,11 @@ const db = openDB<PeopleDB>('maskbook-people-v2', 1, {
* @param record - PersonRecord
*/
export async function storeNewPersonDB(record: PersonRecord): Promise<void> {
// ! Add await between a transaction and function end will cause the transaction closes !
// ! Do ALL async works before opening an transaction
const data = await toDb(record)
const t = (await db).transaction('people', 'readwrite')
await t.objectStore('people').put(await toDb(record))
await t.objectStore('people').put(data)
sendNewPersonMessageDB(record)
return
}
Expand Down Expand Up @@ -161,10 +164,11 @@ export async function queryPersonDB(id: PersonIdentifier): Promise<null | Person
* @param person - Partial of person record
*/
export async function updatePersonDB(person: Partial<PersonRecord> & Pick<PersonRecord, 'identifier'>): Promise<void> {
const t = (await db).transaction('people', 'readwrite')
const full = await t.objectStore('people').get(person.identifier.toText())
const full = await queryPersonDB(person.identifier)
if (!full) throw new Error('Person is not in the db')
const o: PersonRecordInDatabase = { ...full, ...(await toDb(person as PersonRecord)) }
const o: PersonRecordInDatabase = { ...(await toDb(full)), ...(await toDb(person as PersonRecord)) }

const t = (await db).transaction('people', 'readwrite')
await t.objectStore('people').put(o)
sendNewPersonMessageDB(await outDb(o))
}
Expand Down Expand Up @@ -204,8 +208,10 @@ export async function removeMyIdentityAtDB(id: PersonIdentifier): Promise<void>
export async function storeMyIdentityDB(record: PersonRecordPublicPrivate): Promise<void> {
if (!record.publicKey || !record.privateKey)
throw new TypeError('No public/private key pair found when store self identity')
const data = await toDb(record)

const t = (await db).transaction('myself', 'readwrite')
await t.objectStore('myself').put(await toDb(record))
await t.objectStore('myself').put(data)
}
/**
* Get all my identities.
Expand Down
2 changes: 1 addition & 1 deletion src/database/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const db = openDB<PostDB>('maskbook-post-v2', 1, {
})
export async function storePostCryptoKeyDB(record: PostOutDBRecordV40): Promise<void> {
const t = (await db).transaction('post', 'readwrite')
const o: typeof record = { ...record, identifier: record.identifier.toText() as any }
// const o: typeof record = { ...record, identifier: record.identifier.toText() as any }
await t.objectStore('post').put(toDb(record))
}
export async function queryPostCryptoKeyDB(record: PostIdentifier): Promise<PostOutDBRecordV40 | null> {
Expand Down

0 comments on commit 1c08c8c

Please sign in to comment.