Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickleet committed Mar 2, 2022
1 parent ca8b1fe commit e459171
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 50 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
],
"scripts": {
"build": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json",
"lint": "eslint src __tests__",
"lint:fix": "eslint --fix src __tests__",
"lint": "eslint src",
"lint:fix": "eslint --fix src",
"test": "DEBUG=sourced jest --config jest.json --coverage --verbose",
"test:watch": "DEBUG=sourced jest --config jest.json --watch --coverage --verbose",
"prepublish": "npm run build",
Expand Down
90 changes: 42 additions & 48 deletions src/index.ts
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;
}

0 comments on commit e459171

Please sign in to comment.