Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement getResolve #99

Merged
merged 2 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions src/WorkerPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,35 @@ class PoolWorker {
break;
}
case 'resolve': {
const { context, request, questionId } = message;
const { context, request, options, questionId } = message;
const { data } = this.jobs[id];
data.resolve(context, request, (error, result) => {
this.writeJson({
type: 'result',
id: questionId,
error: error ? {
message: error.message,
details: error.details,
missing: error.missing,
} : null,
result,
if (options) {
data.getResolve(options)(context, request, (error, result) => {
this.writeJson({
type: 'result',
id: questionId,
error: error ? {
message: error.message,
details: error.details,
missing: error.missing,
} : null,
result,
});
});
});
} else {
data.resolve(context, request, (error, result) => {
this.writeJson({
type: 'result',
id: questionId,
error: error ? {
message: error.message,
details: error.details,
missing: error.missing,
} : null,
result,
});
});
}
finalCallback();
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function pitch() {
emitWarning: this.emitWarning,
loadModule: this.loadModule,
resolve: this.resolve,
getResolve: this.getResolve,
target: this.target,
minimize: this.minimize,
resourceQuery: this.resourceQuery,
Expand Down
35 changes: 26 additions & 9 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ function writeJson(data) {

const queue = asyncQueue(({ id, data }, taskCallback) => {
try {
const resolveWithOptions = (context, request, callback, options) => {
callbackMap[nextQuestionId] = callback;
writeJson({
type: 'resolve',
id,
questionId: nextQuestionId,
context,
request,
options,
});
nextQuestionId += 1;
};
loaderRunner.runLoaders({
loaders: data.loaders,
resource: data.resource,
Expand All @@ -119,15 +131,20 @@ const queue = asyncQueue(({ id, data }, taskCallback) => {
nextQuestionId += 1;
},
resolve: (context, request, callback) => {
callbackMap[nextQuestionId] = callback;
writeJson({
type: 'resolve',
id,
questionId: nextQuestionId,
context,
request,
});
nextQuestionId += 1;
resolveWithOptions(context, request, callback);
},
// eslint-disable-next-line consistent-return
getResolve: options => (context, request, callback) => {
if (callback) {
resolveWithOptions(context, request, callback, options);
} else {
return new Promise((resolve, reject) => {
resolveWithOptions(context, request, (err, result) => {
if (err) reject(err);
else resolve(result);
}, options);
});
}
},
emitWarning: (warning) => {
writeJson({
Expand Down