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

Code Component Can Not Store State Between Executions #41

Open
jhorbulyk opened this issue Jun 26, 2020 · 1 comment
Open

Code Component Can Not Store State Between Executions #41

jhorbulyk opened this issue Jun 26, 2020 · 1 comment
Labels
Ready For ToDo The task can be transferred to Backlog

Comments

@jhorbulyk
Copy link

Steps to reproduce

  1. Create a flow: Simple Trigger -> JSONata Transform -> Code
    JSONata transform:
[{"one": "foo"}, {"two":"bar"}, {"three": "baz"}]

Code component:

let state = {}
async function run(msg, cfg, snapshot) {
    this.logger.info(`Pre State is: ${JSON.stringify(state)}`);
    state = _.merge(state, msg.body);
    this.logger.info(`Post State is: ${JSON.stringify(state)}`);
	await this.emit('data', { body : {} });
	this.logger.info('Execution finished');
}

2.Run the flow once and look at the logs of the code component

Actual Result

The logs contain

Pre State is: {}
Post State is: {"one":"foo"}
Pre State is: {}
Post State is: {"two":"bar"}
Pre State is: {}
Post State is: {"three":"baz"}

Expected Result

The logs contain

Pre State is: {}
Post State is: {"one":"foo"}
Pre State is: {"one":"foo"}
Post State is: {"one":"foo", "two":"bar"}
Pre State is: {"one":"foo", "two":"bar"}
Post State is: {"one":"foo", "two":"bar", "three":"baz"}
@jhorbulyk
Copy link
Author

Workaround

The process object is shared between messages

process.state = process.state || {};
async function run(msg, cfg, snapshot) {
    this.logger.info(`Pre State is: ${JSON.stringify(process.state)}`);
    state = _.merge(process.state, msg.body);
    this.logger.info(`Post State is: ${JSON.stringify(process.state)}`);
	await this.emit('data', { body : {} });
	this.logger.info('Execution finished');
}

@andrewreshitko andrewreshitko added the Ready For ToDo The task can be transferred to Backlog label Jun 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ready For ToDo The task can be transferred to Backlog
Projects
None yet
Development

No branches or pull requests

2 participants