Skip to content

Commit d704a65

Browse files
committed
implement queueTask
1 parent 210dfd6 commit d704a65

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

lib/environment.js

+36-27
Original file line numberDiff line numberDiff line change
@@ -1025,14 +1025,7 @@ class Environment extends Base {
10251025
};
10261026

10271027
if (schedule) {
1028-
this.runLoop.add('environment:run', async (done, stop) => {
1029-
try {
1030-
await runGenerator();
1031-
done();
1032-
} catch (error) {
1033-
stop(error);
1034-
}
1035-
});
1028+
this.queueTask('environment:run', () => runGenerator());
10361029
} else {
10371030
await runGenerator();
10381031
}
@@ -1352,38 +1345,26 @@ class Environment extends Base {
13521345
queueConflicter() {
13531346
const queueCommit = () => {
13541347
debug('Queueing conflicts task');
1355-
this.runLoop.add(
1348+
this.queueTask(
13561349
'environment:conflicts',
1357-
(done, stop) => {
1350+
async () => {
13581351
let customCommitTask = this.findGeneratorCustomCommitTask();
13591352
if (customCommitTask !== undefined && customCommitTask) {
13601353
if (typeof customCommitTask !== 'function') {
1361-
done();
1354+
// There is a custom commit task or just disabled
13621355
return;
13631356
}
13641357
} else {
1358+
// Using default commit task
13651359
customCommitTask = this.commitSharedFs.bind(this);
13661360
}
13671361

1362+
await customCommitTask();
1363+
13681364
if (this.enableConflicterIgnore) {
13691365
debug('Adding queueCommit event listener');
13701366
this.sharedFs.once('change', queueCommit);
13711367
}
1372-
1373-
const result = customCommitTask();
1374-
if (!result || !result.then) {
1375-
done();
1376-
return;
1377-
}
1378-
1379-
return result.then(() => {
1380-
if (!this.enableConflicterIgnore) {
1381-
debug('Adding queueCommit event listener');
1382-
this.sharedFs.once('change', queueCommit);
1383-
}
1384-
1385-
done();
1386-
}, stop);
13871368
},
13881369
{
13891370
once: 'write memory fs to disk',
@@ -1398,7 +1379,35 @@ class Environment extends Base {
13981379
* Queue environment's package manager install task.
13991380
*/
14001381
queuePackageManagerInstall() {
1401-
this.runLoop.add('install', (done, stop) => this.packageManagerInstallTask().then(done, stop), { once: 'package manager install' });
1382+
this.queueTask('install', () => this.packageManagerInstallTask(), { once: 'package manager install' });
1383+
}
1384+
1385+
/**
1386+
* Queue tasks
1387+
* @param {string} priority
1388+
* @param {(...args: any[]) => void | Promise<void>} task
1389+
* @param {{ once?: string, startQueue?: boolean }} [options]
1390+
*/
1391+
queueTask(priority, task, options) {
1392+
return new Promise((resolve, reject) => {
1393+
this.runLoop.add(
1394+
priority,
1395+
async (done, stop) => {
1396+
try {
1397+
const result = await task();
1398+
done(result);
1399+
resolve(result);
1400+
} catch (error) {
1401+
stop(error);
1402+
reject(error);
1403+
}
1404+
},
1405+
{
1406+
once: options.once,
1407+
run: options.startQueue ?? false,
1408+
},
1409+
);
1410+
});
14021411
}
14031412
}
14041413
Object.assign(Environment.prototype, resolver);

test/environment.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -244,17 +244,17 @@ describe('Environment', () => {
244244
});
245245

246246
it('should schedule generator queue', async function () {
247-
this.env.runLoop.add = sinon.spy();
247+
this.env.queueTask = sinon.spy();
248248
await this.env.composeWith('stub');
249-
assert(this.env.runLoop.add.calledOnce);
250-
assert(this.env.runLoop.add.getCall(0).firstArg === 'environment:run');
249+
assert(this.env.queueTask.calledOnce);
250+
assert(this.env.queueTask.getCall(0).firstArg === 'environment:run');
251251
});
252252

253253
describe('passing false schedule parameter', () => {
254254
it('should queue generator tasks', async function () {
255-
this.env.runLoop.add = sinon.spy();
255+
this.env.queueTask = sinon.spy();
256256
await this.env.composeWith('stub', [], {}, false);
257-
assert(this.env.runLoop.add.getCall(0).firstArg !== 'environment:run');
257+
assert(this.env.queueTask.notCalled);
258258
});
259259
});
260260

0 commit comments

Comments
 (0)