-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca8b1fe
commit e459171
Showing
2 changed files
with
44 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,73 @@ | ||
import { EventEmitter } from 'events'; | ||
import queue from 'fastq' | ||
import { EventEmitter } from "events"; | ||
import queue from "fastq"; | ||
|
||
class Lock extends EventEmitter { | ||
locked: boolean; | ||
|
||
constructor () { | ||
super() | ||
constructor() { | ||
super(); | ||
|
||
this.locked = false | ||
this.locked = false; | ||
} | ||
|
||
lock () { | ||
if (this.locked === true) return | ||
this.locked = true | ||
this.emit('locked') | ||
lock() { | ||
if (this.locked === true) return; | ||
this.locked = true; | ||
this.emit("locked"); | ||
} | ||
|
||
unlock () { | ||
if (this.locked === false) return | ||
this.locked = false | ||
this.emit('unlocked') | ||
unlock() { | ||
if (this.locked === false) return; | ||
this.locked = false; | ||
this.emit("unlocked"); | ||
} | ||
} | ||
|
||
export function QueuedRepo (repo) { | ||
const copy = Object.create(repo) | ||
export function QueuedRepo(repo) { | ||
const copy = Object.create(repo); | ||
|
||
copy._locks = {} | ||
copy._queues = {} | ||
copy._locks = {}; | ||
copy._queues = {}; | ||
|
||
copy._ensureQueue = function (id) { | ||
if (!this._queues[id]) { | ||
const lock = this._locks[id] = new Lock() | ||
const lock = (this._locks[id] = new Lock()); | ||
|
||
const doTask = async (id) => { | ||
lock.once('unlocked', () => { | ||
lock.once("unlocked", () => { | ||
return; | ||
}) | ||
lock.lock() | ||
return await this._get(id) | ||
|
||
} | ||
|
||
this._queues[id] = queue.promise(this, doTask, 1) | ||
|
||
}); | ||
lock.lock(); | ||
return await this._get(id); | ||
}; | ||
|
||
this._queues[id] = queue.promise(this, doTask, 1); | ||
} | ||
} | ||
}; | ||
|
||
copy._commit = copy.commit | ||
copy._get = copy.get | ||
copy._commit = copy.commit; | ||
copy._get = copy.get; | ||
|
||
copy.get = async function (id) { | ||
this._ensureQueue(id) | ||
|
||
const fn = this._get.bind(this, id) | ||
|
||
return await this._queues[id].push(id).catch((err) => console.error(err)) | ||
} | ||
this._ensureQueue(id); | ||
return await this._queues[id].push(id).catch((err) => console.error(err)); | ||
}; | ||
|
||
copy.lock = function (entity) { | ||
this._ensureQueue(entity.id) | ||
this._locks[entity.id].lock() | ||
} | ||
this._ensureQueue(entity.id); | ||
this._locks[entity.id].lock(); | ||
}; | ||
|
||
copy.commit = async function (entity) { | ||
const self = this | ||
this._ensureQueue(entity.id) | ||
await this._commit(entity) | ||
self._locks[entity.id].unlock() | ||
} | ||
this._ensureQueue(entity.id); | ||
await this._commit(entity); | ||
this._locks[entity.id].unlock(); | ||
}; | ||
|
||
copy.unlock = function (entity) { | ||
this._ensureQueue(entity.id) | ||
this._locks[entity.id].unlock() | ||
} | ||
this._ensureQueue(entity.id); | ||
this._locks[entity.id].unlock(); | ||
}; | ||
|
||
return copy | ||
return copy; | ||
} |