tremor-runtime icon indicating copy to clipboard operation
tremor-runtime copied to clipboard

Add `init` section to script operator

Open Licenser opened this issue 4 years ago • 3 comments
trafficstars

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.

Licenser avatar Apr 28 '21 10:04 Licenser

I really really like that!

I also think this needs an RFC.

mfelsche avatar Apr 28 '21 10:04 mfelsche

Ja I'm writing one already for this and #933 as they kind of touch on the same area

Licenser avatar Apr 28 '21 11:04 Licenser

This is the RFC for this: https://github.com/tremor-rs/tremor-rfcs/pull/46

mfelsche avatar Sep 30 '21 08:09 mfelsche

this is implemenmted

Licenser avatar Oct 09 '23 10:10 Licenser