tremor-runtime
tremor-runtime copied to clipboard
Add `init` section to script operator
Describe the problem you are trying to solve
With more complex pipelines and scripts that make use of the state a repeated pattern is initializing the state. As of now, this has to happen in every invocation of the script matching on state being set or not. This makes the script logic harder to follow and adds often unnecessary performance overhead.
Describe the solution you'd like
A init (or state?) section is called at the start of the script returning the initial state of the script operator. An example would be:
define script apply_rate
script
match state of
case null => let state = 0
case _ => null
end;
match event of
case %{ present update_rate } => let state = event.update_rate, drop
case _ => null
end;
let $class = "default";
let $rate = state
end
could become:
define script apply_rate
init
0
script
match event of
case %{ present update_rate } => let state = event.update_rate, drop
case _ => null
end;
let $class = "default";
let $rate = state
end
Alternatively, init could process events to build up an initial state until it doesn't drop an event, which would allow seeding a script operator with multiple events to build a state.
Notes
This does likely require an RFC.
If this is an implementation of an RFC provide a URL to the RFC this enhancement implements.
If this is a major enhancement or contribution an RFC may be required. It is ok to submit an enhancement first and our core team will assist with major contributions. In general, major contributions should be discussed with the community before submission.
I really really like that!
I also think this needs an RFC.
Ja I'm writing one already for this and #933 as they kind of touch on the same area
This is the RFC for this: https://github.com/tremor-rs/tremor-rfcs/pull/46
this is implemenmted