diff --git a/.changeset/spicy-ties-sing.md b/.changeset/spicy-ties-sing.md new file mode 100644 index 00000000..efcd0765 --- /dev/null +++ b/.changeset/spicy-ties-sing.md @@ -0,0 +1,5 @@ +--- +'@journeyapps/wa-sqlite': patch +--- + +Update upstream to [v1.0.9](https://github.com/rhashimoto/wa-sqlite/releases/tag/v1.0.9) diff --git a/src/WebLocksMixin.js b/src/WebLocksMixin.js index 9b1e97b6..e5fd48f9 100644 --- a/src/WebLocksMixin.js +++ b/src/WebLocksMixin.js @@ -48,18 +48,7 @@ export const WebLocksMixin = superclass => class extends superclass { */ async jLock(fileId, lockType) { try { - // Create state on first lock. - if (!this.#mapIdToState.has(fileId)) { - const name = this.getFilename(fileId); - const state = { - baseName: name, - type: VFS.SQLITE_LOCK_NONE, - writeHint: false - }; - this.#mapIdToState.set(fileId, state); - } - - const lockState = this.#mapIdToState.get(fileId); + const lockState = this.#getLockState(fileId); if (lockType <= lockState.type) return VFS.SQLITE_OK; switch (this.#options.lockPolicy) { @@ -82,10 +71,8 @@ export const WebLocksMixin = superclass => class extends superclass { */ async jUnlock(fileId, lockType) { try { - // SQLite can call xUnlock() without ever calling xLock() so - // the state may not exist. - const lockState = this.#mapIdToState.get(fileId); - if (!(lockType < lockState?.type)) return VFS.SQLITE_OK; + const lockState = this.#getLockState(fileId); + if (!(lockType < lockState.type)) return VFS.SQLITE_OK; switch (this.#options.lockPolicy) { case 'exclusive': @@ -107,7 +94,7 @@ export const WebLocksMixin = superclass => class extends superclass { */ async jCheckReservedLock(fileId, pResOut) { try { - const lockState = this.#mapIdToState.get(fileId); + const lockState = this.#getLockState(fileId); switch (this.#options.lockPolicy) { case 'exclusive': return this.#checkReservedExclusive(lockState, pResOut); @@ -130,19 +117,29 @@ export const WebLocksMixin = superclass => class extends superclass { * @returns {number|Promise} */ jFileControl(fileId, op, pArg) { - const lockState = this.#mapIdToState.get(fileId) ?? - (() => { - // Call jLock() to create the lock state. - this.jLock(fileId, VFS.SQLITE_LOCK_NONE); - return this.#mapIdToState.get(fileId); - })(); if (op === WebLocksMixin.WRITE_HINT_OP_CODE && this.#options.lockPolicy === 'shared+hint'){ + const lockState = this.#getLockState(fileId); lockState.writeHint = true; } return VFS.SQLITE_NOTFOUND; } + #getLockState(fileId) { + let lockState = this.#mapIdToState.get(fileId); + if (!lockState) { + // The state doesn't exist yet so create it. + const name = this.getFilename(fileId); + lockState = { + baseName: name, + type: VFS.SQLITE_LOCK_NONE, + writeHint: false + }; + this.#mapIdToState.set(fileId, lockState); + } + return lockState + } + /** * @param {LockState} lockState * @param {number} lockType