Skip to content
5 changes: 5 additions & 0 deletions .changeset/spicy-ties-sing.md
Original file line number Diff line number Diff line change
@@ -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)
43 changes: 20 additions & 23 deletions src/WebLocksMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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':
Expand All @@ -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);
Expand All @@ -130,19 +117,29 @@ export const WebLocksMixin = superclass => class extends superclass {
* @returns {number|Promise<number>}
*/
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
Expand Down
Loading