forked from mystiq/hydrogen-web
Convert LockMap to ts
This commit is contained in:
parent
c8eb7ea7ac
commit
1549d8add0
2 changed files with 7 additions and 7 deletions
|
@ -34,7 +34,7 @@ import {Encryption as MegOlmEncryption} from "./e2ee/megolm/Encryption.js";
|
||||||
import {MEGOLM_ALGORITHM} from "./e2ee/common.js";
|
import {MEGOLM_ALGORITHM} from "./e2ee/common.js";
|
||||||
import {RoomEncryption} from "./e2ee/RoomEncryption.js";
|
import {RoomEncryption} from "./e2ee/RoomEncryption.js";
|
||||||
import {DeviceTracker} from "./e2ee/DeviceTracker.js";
|
import {DeviceTracker} from "./e2ee/DeviceTracker.js";
|
||||||
import {LockMap} from "../utils/LockMap.js";
|
import {LockMap} from "../utils/LockMap";
|
||||||
import {groupBy} from "../utils/groupBy";
|
import {groupBy} from "../utils/groupBy";
|
||||||
import {
|
import {
|
||||||
keyFromCredential as ssssKeyFromCredential,
|
keyFromCredential as ssssKeyFromCredential,
|
||||||
|
|
|
@ -17,11 +17,9 @@ limitations under the License.
|
||||||
import {Lock} from "./Lock";
|
import {Lock} from "./Lock";
|
||||||
|
|
||||||
export class LockMap {
|
export class LockMap {
|
||||||
constructor() {
|
private readonly _map: Map<unknown, Lock> = new Map();
|
||||||
this._map = new Map();
|
|
||||||
}
|
|
||||||
|
|
||||||
async takeLock(key) {
|
async takeLock(key: unknown): Promise<Lock> {
|
||||||
let lock = this._map.get(key);
|
let lock = this._map.get(key);
|
||||||
if (lock) {
|
if (lock) {
|
||||||
await lock.take();
|
await lock.take();
|
||||||
|
@ -31,10 +29,10 @@ export class LockMap {
|
||||||
this._map.set(key, lock);
|
this._map.set(key, lock);
|
||||||
}
|
}
|
||||||
// don't leave old locks lying around
|
// don't leave old locks lying around
|
||||||
lock.released().then(() => {
|
lock.released()!.then(() => {
|
||||||
// give others a chance to take the lock first
|
// give others a chance to take the lock first
|
||||||
Promise.resolve().then(() => {
|
Promise.resolve().then(() => {
|
||||||
if (!lock.isTaken) {
|
if (!lock!.isTaken) {
|
||||||
this._map.delete(key);
|
this._map.delete(key);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -67,6 +65,7 @@ export function tests() {
|
||||||
ranSecond = true;
|
ranSecond = true;
|
||||||
assert.equal(returnedLock.isTaken, true);
|
assert.equal(returnedLock.isTaken, true);
|
||||||
// peek into internals, naughty
|
// peek into internals, naughty
|
||||||
|
// @ts-ignore
|
||||||
assert.equal(lockMap._map.get("foo"), returnedLock);
|
assert.equal(lockMap._map.get("foo"), returnedLock);
|
||||||
});
|
});
|
||||||
lock.release();
|
lock.release();
|
||||||
|
@ -84,6 +83,7 @@ export function tests() {
|
||||||
// double delay to make sure cleanup logic ran
|
// double delay to make sure cleanup logic ran
|
||||||
await Promise.resolve();
|
await Promise.resolve();
|
||||||
await Promise.resolve();
|
await Promise.resolve();
|
||||||
|
// @ts-ignore
|
||||||
assert.equal(lockMap._map.has("foo"), false);
|
assert.equal(lockMap._map.has("foo"), false);
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue