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

middlewares should be triggered also on binding #647

Open
FabienArcellier opened this issue Nov 23, 2024 · 0 comments
Open

middlewares should be triggered also on binding #647

FabienArcellier opened this issue Nov 23, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@FabienArcellier
Copy link
Collaborator

Middlewares trigger exclusively on events that are explicitly attached. If a variable is bound, they are not triggered. It's not practical.

Here is the example of a weather station where I would like to write a file with the state each time a variable changes. I have to explicitly attach an event handler for the middleware mechanism to work.

import io
import json

import writer as wf

@wf.middleware()
def on_change(state: wf.WriterState):
    yield

    with io.open("weather.json", "w") as f:
        f.write(json.dumps(state.to_dict()))

# I have to attach this event handler. 
# I would prefer to trigger on any event
def on_trigger():
    pass

initial_state = wf.init_state({
    "outside_atmospheric_pressure_now": 1026.0,
    "outside_humidity": 0,
    "outside_wind_speed": 0,
    "outside_temperature": 22
})

@wf.middleware(event_handler=True, binding=True)  # default
@wf.middleware(binding=False)
@FabienArcellier FabienArcellier added the enhancement New feature or request label Nov 23, 2024
@FabienArcellier FabienArcellier changed the title Middlewares trigger also on binding middlewares should be triggered also on binding Nov 23, 2024
@FabienArcellier FabienArcellier self-assigned this Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant