From e45917127c62f08426dc8addf9c992e7b3e401f9 Mon Sep 17 00:00:00 2001 From: Patrick Lee Scott Date: Tue, 1 Mar 2022 20:16:28 -0600 Subject: [PATCH] fix: lint --- package.json | 4 +-- src/index.ts | 90 ++++++++++++++++++++++++---------------------------- 2 files changed, 44 insertions(+), 50 deletions(-) diff --git a/package.json b/package.json index bc5291f..0b0d675 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index d5e7125..2be5479 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; }