Skip to content

Commit 97be9f4

Browse files
Merge remote-tracking branch 'upstream/master' into test-update
2 parents 1290c3d + 7f9841e commit 97be9f4

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

src/WebLocksMixin.js

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,7 @@ export const WebLocksMixin = superclass => class extends superclass {
4848
*/
4949
async jLock(fileId, lockType) {
5050
try {
51-
// Create state on first lock.
52-
if (!this.#mapIdToState.has(fileId)) {
53-
const name = this.getFilename(fileId);
54-
const state = {
55-
baseName: name,
56-
type: VFS.SQLITE_LOCK_NONE,
57-
writeHint: false
58-
};
59-
this.#mapIdToState.set(fileId, state);
60-
}
61-
62-
const lockState = this.#mapIdToState.get(fileId);
51+
const lockState = this.#getLockState(fileId);
6352
if (lockType <= lockState.type) return VFS.SQLITE_OK;
6453

6554
switch (this.#options.lockPolicy) {
@@ -82,10 +71,8 @@ export const WebLocksMixin = superclass => class extends superclass {
8271
*/
8372
async jUnlock(fileId, lockType) {
8473
try {
85-
// SQLite can call xUnlock() without ever calling xLock() so
86-
// the state may not exist.
87-
const lockState = this.#mapIdToState.get(fileId);
88-
if (!(lockType < lockState?.type)) return VFS.SQLITE_OK;
74+
const lockState = this.#getLockState(fileId);
75+
if (!(lockType < lockState.type)) return VFS.SQLITE_OK;
8976

9077
switch (this.#options.lockPolicy) {
9178
case 'exclusive':
@@ -107,7 +94,7 @@ export const WebLocksMixin = superclass => class extends superclass {
10794
*/
10895
async jCheckReservedLock(fileId, pResOut) {
10996
try {
110-
const lockState = this.#mapIdToState.get(fileId);
97+
const lockState = this.#getLockState(fileId);
11198
switch (this.#options.lockPolicy) {
11299
case 'exclusive':
113100
return this.#checkReservedExclusive(lockState, pResOut);
@@ -130,19 +117,29 @@ export const WebLocksMixin = superclass => class extends superclass {
130117
* @returns {number|Promise<number>}
131118
*/
132119
jFileControl(fileId, op, pArg) {
133-
const lockState = this.#mapIdToState.get(fileId) ??
134-
(() => {
135-
// Call jLock() to create the lock state.
136-
this.jLock(fileId, VFS.SQLITE_LOCK_NONE);
137-
return this.#mapIdToState.get(fileId);
138-
})();
139120
if (op === WebLocksMixin.WRITE_HINT_OP_CODE &&
140121
this.#options.lockPolicy === 'shared+hint'){
122+
const lockState = this.#getLockState(fileId);
141123
lockState.writeHint = true;
142124
}
143125
return VFS.SQLITE_NOTFOUND;
144126
}
145127

128+
#getLockState(fileId) {
129+
let lockState = this.#mapIdToState.get(fileId);
130+
if (!lockState) {
131+
// The state doesn't exist yet so create it.
132+
const name = this.getFilename(fileId);
133+
lockState = {
134+
baseName: name,
135+
type: VFS.SQLITE_LOCK_NONE,
136+
writeHint: false
137+
};
138+
this.#mapIdToState.set(fileId, lockState);
139+
}
140+
return lockState
141+
}
142+
146143
/**
147144
* @param {LockState} lockState
148145
* @param {number} lockType

0 commit comments

Comments
 (0)