view icon indicating copy to clipboard operation
view copied to clipboard

Use effects to access the render env, as well as locals from context

Open timriley opened this issue 5 years ago • 1 comments

In this PR we're achieving two things using dry-effects (the first I did way back in September 2020, and the second I'm adding just now):

Access to the render env

This removes the cyclic dependency between a render environment and its context, part builder, and scope builder.

It also means we don’t need to create new copies of the context, part builder, and scope for each render environment we create (typically, there are 3 environments for a given rendering: a base environment, then a template- and layout-specific environment).

This is a particularly helpful in the case of the context object, because application authors may want to set state in the context object from within the template rendering, to make it available when the layout renders. This was not possible to do until now because both the template and layout renderings had different context objects.

Access to locals from context

This is a significant change to the purpose and function of the context object - now you can access the current locals via a #locals reader effect.

This should enable us to create more streamlined helper APIs that require implicit access to locals

TODO

  • [x] Add a test that relies upon an identical context object being used in both template and layout renderings
  • [x] Remove Context#_options and #with
  • [x] Work out whether we need Part#new anymore (I think no)
  • [ ] We've disabled access to the context from within exposure blocks. Decide whether this is OK or needs to be reinstated somehow.
  • [ ] Rescue from Dry::Effects::Errors::MissingStateError for our render_env and raise a more helpful error in its place
  • [ ] (Later) Create some hanami-view-specific testing helpers so app authors don't need to include a dry effects handler directly in their tests

timriley avatar Sep 26 '20 13:09 timriley

Benchmarks update as of today.

Versus the main branch:

Comparison:

render-env-via-reader-effect:    10833.1 i/s
                        main:     8516.4 i/s - 1.27x  (± 0.00) slower

Versus rails and tilt:

Comparison:
                tilt:   102390.6 i/s
               rails:    18146.7 i/s - 5.64x  (± 0.00) slower
         hanami/view:    10850.3 i/s - 9.44x  (± 0.00) slower

timriley avatar Aug 15 '22 03:08 timriley

Closing in favour of #223.

timriley avatar Apr 02 '23 09:04 timriley