This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #518 from atom/mkt-ku-parallelize-git-ops
Parallelize Git operations
- Loading branch information
Showing
7 changed files
with
286 additions
and
157 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,72 @@ | ||
class Task { | ||
constructor(fn, parallel = true) { | ||
this.fn = fn; | ||
this.parallel = parallel; | ||
this.promise = new Promise((resolve, reject) => { | ||
this.resolve = resolve; | ||
this.reject = reject; | ||
}); | ||
} | ||
|
||
async execute() { | ||
try { | ||
const value = await this.fn.call(undefined); | ||
this.resolve(value); | ||
} catch (err) { | ||
this.reject(err); | ||
} | ||
} | ||
|
||
runsInParallel() { | ||
return this.parallel; | ||
} | ||
|
||
runsInSerial() { | ||
return !this.parallel; | ||
} | ||
|
||
getPromise() { | ||
return this.promise; | ||
} | ||
} | ||
|
||
export default class AsyncQueue { | ||
constructor() { | ||
this.commandQueue = []; | ||
this.running = false; | ||
} | ||
|
||
push(fn) { | ||
const p = new Promise((resolve, reject) => this.commandQueue.push({fn, resolve, reject})); | ||
if (!this.running) { this.processQueue(); } | ||
return p; | ||
} | ||
|
||
async processQueue() { | ||
this.running = true; | ||
let lastPromise = Promise.resolve(); | ||
while (this.commandQueue.length) { | ||
const {fn, resolve, reject} = this.commandQueue[0]; | ||
const run = () => fn(); | ||
lastPromise = lastPromise.then(run, run); | ||
lastPromise.then(resolve, reject); | ||
try { | ||
await lastPromise; // eslint-disable-line babel/no-await-in-loop | ||
} catch (e) { /* nothing */ } | ||
this.commandQueue.shift(); | ||
constructor(options = {}) { | ||
this.parallelism = options.parallelism || 1; | ||
this.nonParallelizableOperation = false; | ||
this.tasksInProgress = 0; | ||
this.queue = []; | ||
} | ||
|
||
push(fn, {parallel} = {parallel: true}) { | ||
const task = new Task(fn, parallel); | ||
this.queue.push(task); | ||
this.processQueue(); | ||
return task.getPromise(); | ||
} | ||
|
||
processQueue() { | ||
if (!this.queue.length || this.nonParallelizableOperation) { return; } | ||
|
||
const task = this.queue[0]; | ||
const canRunParallelOp = task.runsInParallel() && this.tasksInProgress < this.parallelism; | ||
const canRunSerialOp = task.runsInSerial() && this.tasksInProgress === 0; | ||
if (canRunSerialOp || canRunParallelOp) { | ||
this.processTask(task, task.runsInParallel()); | ||
this.queue.shift(); | ||
this.processQueue(); | ||
} | ||
this.running = false; | ||
} | ||
|
||
async processTask(task, runsInParallel) { | ||
this.tasksInProgress++; | ||
if (!runsInParallel) { | ||
this.nonParallelizableOperation = true; | ||
} | ||
|
||
await task.execute(); | ||
this.tasksInProgress--; | ||
this.nonParallelizableOperation = false; | ||
this.processQueue(); | ||
} | ||
} |
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
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
Oops, something went wrong.