refactor: make IPollFunctions emit consistent with trigger emit (#4201)

* refactor: make IPollFunctions emit consistent with trigger emit

* refactor: re-add underscores to poll emits

* chore: update emit override message
This commit is contained in:
Valya 2022-11-08 13:29:20 +00:00 committed by GitHub
parent 77644860c0
commit ebf17e1827
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 16 deletions

View file

@ -613,7 +613,7 @@ export class ActiveWorkflowRunner {
/**
* Return poll function which gets the global functions from n8n-core
* and overwrites the __emit to be able to start it in subprocess
* and overwrites the emit to be able to start it in subprocess
*
*/
getExecutePollFunctions(
@ -630,19 +630,38 @@ export class ActiveWorkflowRunner {
mode,
activation,
);
// eslint-disable-next-line no-underscore-dangle
returnFunctions.__emit = async (
data: INodeExecutionData[][] | ExecutionError,
): Promise<void> => {
if (data instanceof Error) {
await createErrorExecution(data, node, workflowData, workflow, mode);
this.executeErrorWorkflow(data, workflowData, mode);
return;
}
returnFunctions.__emit = (
data: INodeExecutionData[][],
responsePromise?: IDeferredPromise<IExecuteResponsePromiseData>,
donePromise?: IDeferredPromise<IRun | undefined>,
): void => {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
Logger.debug(`Received event to trigger execution for workflow "${workflow.name}"`);
WorkflowHelpers.saveStaticData(workflow);
this.runWorkflow(workflowData, node, data, additionalData, mode);
const executePromise = this.runWorkflow(
workflowData,
node,
data,
additionalData,
mode,
responsePromise,
);
if (donePromise) {
executePromise.then((executionId) => {
activeExecutions
.getPostExecutePromise(executionId)
.then(donePromise.resolve)
.catch(donePromise.reject);
});
} else {
executePromise.catch(console.error);
}
};
returnFunctions.__emitError = async (error: ExecutionError): Promise<void> => {
await createErrorExecution(error, node, workflowData, workflow, mode);
this.executeErrorWorkflow(error, workflowData, mode);
};
return returnFunctions;
};

View file

@ -160,7 +160,6 @@ export class ActiveWorkflows {
const pollResponse = await workflow.runPoll(node, pollFunctions);
if (pollResponse !== null) {
// eslint-disable-next-line no-underscore-dangle
pollFunctions.__emit(pollResponse);
}
} catch (error) {
@ -170,8 +169,7 @@ export class ActiveWorkflows {
if (testingTrigger) {
throw error;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, no-underscore-dangle
pollFunctions.__emit(error);
pollFunctions.__emitError(error);
}
};

View file

@ -1874,7 +1874,12 @@ export function getExecutePollFunctions(
return ((workflow: Workflow, node: INode) => {
return {
__emit: (data: INodeExecutionData[][]): void => {
throw new Error('Overwrite NodeExecuteFunctions.getExecutePullFunctions.__emit function!');
throw new Error('Overwrite NodeExecuteFunctions.getExecutePollFunctions.__emit function!');
},
__emitError(error: Error) {
throw new Error(
'Overwrite NodeExecuteFunctions.getExecutePollFunctions.__emitError function!',
);
},
async getCredentials(type: string): Promise<ICredentialDataDecryptedObject> {
return getCredentials(workflow, node, type, additionalData, mode);

View file

@ -720,7 +720,12 @@ export interface IHookFunctions {
}
export interface IPollFunctions {
__emit(data: INodeExecutionData[][] | NodeApiError): void;
__emit(
data: INodeExecutionData[][],
responsePromise?: IDeferredPromise<IExecuteResponsePromiseData>,
donePromise?: IDeferredPromise<IRun>,
): void;
__emitError(error: Error, responsePromise?: IDeferredPromise<IExecuteResponsePromiseData>): void;
getCredentials(type: string): Promise<ICredentialDataDecryptedObject>;
getMode(): WorkflowExecuteMode;
getActivationMode(): WorkflowActivateMode;