49 lines
1.6 KiB
JavaScript
49 lines
1.6 KiB
JavaScript
function createCommentNext({ post, queue, canQueue, accept = () => undefined, complete }) {
|
|
let inFlight = null;
|
|
|
|
function submit(item, body, operationId = '') {
|
|
if (inFlight) return inFlight;
|
|
inFlight = (async () => {
|
|
try {
|
|
let comment;
|
|
try {
|
|
comment = await post(item, body);
|
|
} catch (error) {
|
|
if (!canQueue(error)) throw error;
|
|
const admission = await queue({
|
|
kind: item.kind === 'pull' ? 'pull-comment' : 'issue-comment',
|
|
repository: item.repository,
|
|
number: item.number,
|
|
body,
|
|
operationId: typeof operationId === 'function' ? operationId() : operationId,
|
|
});
|
|
if (!admission || (!admission.item && admission.durable !== true)) {
|
|
throw new Error('Comment was not saved for delivery.');
|
|
}
|
|
accept(item, { delivery: admission.background ? 'queued' : 'saved' });
|
|
return {
|
|
accepted: true,
|
|
delivery: admission.background ? 'queued' : 'saved',
|
|
background: Boolean(admission.background),
|
|
completed: Boolean(complete(item)),
|
|
};
|
|
}
|
|
accept(item, { delivery: 'posted', comment });
|
|
return {
|
|
accepted: true,
|
|
delivery: 'posted',
|
|
comment,
|
|
completed: Boolean(complete(item)),
|
|
};
|
|
} finally {
|
|
inFlight = null;
|
|
}
|
|
})();
|
|
return inFlight;
|
|
}
|
|
|
|
return { submit, busy: () => Boolean(inFlight) };
|
|
}
|
|
|
|
if (typeof module !== 'undefined' && module.exports) module.exports = createCommentNext;
|