Skip to content

[REF] owl-runtime: split TemplateSet from App - #2082

Draft
mcm-odoo wants to merge 11 commits into
odoo:masterfrom
mcm-odoo:master-templateset-split-mcm
Draft

mcm-odoo wants to merge 11 commits into
odoo:masterfrom
mcm-odoo:master-templateset-split-mcm

Conversation

@mcm-odoo

Copy link
Copy Markdown
Contributor

Depends on #2081: this branch is based on it, so its first
three commits belong to that PR. Only the last eight commits are part of this
one.

This is the third step of the series that clarifies the boundaries between the
packages. An App was a TemplateSet: the template registry and the
application were one object, and the template set depended on all the
component code. After this PR, an App holds a TemplateSet, and a
TemplateSet no longer depends on components. The next PR moves the
generated-code tests to owl-compiler and keeps the rendering tests in
owl-runtime.

Compiled templates no longer need an App

  • createComponent takes the app from the parent node instead of receiving it
    from the template. Compiled templates now only use their app argument to
    find the templates called with t-call, so it can be a plain template set.
  • TemplateSet no longer imports ComponentNode. It used it only to name the
    component in the "Missing template" error; the component node now adds its
    name itself. The message is unchanged.

App holds a TemplateSet

  • The App creates its template set and keeps it in app.templates.
  • AppConfig no longer extends TemplateSetConfig. It lists the template
    options it accepts, with the same names and types, and passes exactly these
    to its template set. Existing configurations keep working.
  • dev stays on the App, which passes it to its template set.
  • app.getTemplate, app.addTemplate, app.addTemplates and the static
    App.registerTemplate are kept, deprecated, and forward to app.templates
    (resp. TemplateSet.registerTemplate).

Template sets render components only when given the helpers

  • The template contract now separates TemplateHelpers from ComponentHelpers
    (createComponent, callSlot, toSignal).
  • The component helpers are in their own file. A template set receives them
    through the new componentHelpers option, which the App sets.
  • A template set without them still renders templates that contain components,
    as long as these are not rendered. Rendering a component, a slot or a
    .signal prop throws a clear OwlError. This already failed before (with a
    TypeError), since a component can only be rendered by an App.

Other changes

  • The compiler's dev mode no longer stores component props in a variable. It
    was a leftover of the props validation of Owl 2. In dev mode, the compiler
    now only adds the duplicate-key check of t-foreach.
  • The devtools read templates from app.templates, and from the app itself
    for older versions.
  • The App reference, the precompiling page and the agent reference
    (owl.llm.md) describe app.templates and the deprecated aliases. The agent
    reference also explains how to render a template with a standalone
    TemplateSet.

Breaking changes

  • Precompiled templates must be compiled again. The generated code calls
    createComponent(name, …) instead of createComponent(app, name, …).
  • App is no longer a TemplateSet. app instanceof TemplateSet is false,
    and the template set's fields (rawTemplates, translateFn,
    customDirectives, …) are no longer on the App.
  • app.templates is now the template set. It used to be the cache of
    compiled templates.

In Odoo (odoo, enterprise, o-spreadsheet), nothing reads the moved fields or
app.templates. o-spreadsheet's tooltip uses app.getTemplate, which keeps
working (deprecated): it should eventually use a TemplateSet. The template
mock of Odoo's test framework keeps working: it is called with the template set
as this, which still has rawTemplates and _compileTemplate.

Review notes

Many snapshot files change. These diffs only remove the app argument of
createComponent and, in dev mode, the props variable.

Checks

npm run build, npm run build:types, npm test (all packages),
npm run test:types, npm run check-formatting and npm run test:doc-links
pass. New tests cover the deprecated App methods and template sets without
component helpers.

Scopes carry the application they belong to, so that plugins and
components can reach it (e.g. to create new roots). The concrete App lives
in owl-runtime, so core typed it as `any`. Core actually only reads two
things from it: its plugin manager and its dev mode.

This describes them in a `ScopeApp` interface, used by Scope,
PluginManager and core's `useApp`. owl-runtime narrows it back:
ComponentNode declares its `app` as an App, and runtime's `useApp` still
returns an App.
A compiled template is created with `(app, bdom, helpers)` and calls the
blockdom functions and helpers it receives. Which functions exist and how
they are called was only an agreement between the code generator and
owl-runtime: renaming a helper or changing its arguments on one side
broke only when a template using it was rendered.

The contract is now described by two types in owl-compiler,
TemplateBlockDom and TemplateHelpers, and used on both sides:
- the code generator only accepts helper and blockdom names from them;
- owl-runtime's helpers and bdom objects must satisfy them.

Values the generated code only passes along (contexts, nodes, blocks,
the app) stay `any`: their shape belongs to the runtime. The generated
code is unchanged.
Compiled templates used it for t-memo, which was removed in Owl 3: the
compiler no longer emits it.
…te's app

Compiled templates receive the app (the TemplateSet that compiled them)
and passed it to `createComponent`, which gave it to the new
ComponentNode. The parent node already knows its app, and it is the same
one, since the parent got its template from it.

`createComponent` now takes the app from the parent node, and the
compiler no longer passes it. A compiled template now only uses its
`app` argument to find the templates it calls with t-call: it no longer
needs to be an App, which prepares splitting TemplateSet from App.

The generated code changes accordingly (snapshots), and templates
compiled before this change must be compiled again.
TemplateSet imported ComponentNode only to name the component in the
"Missing template" error, by looking at the current scope. A scope is
only active there when a component gets its own template, in its
constructor.

The component node now gets its template itself and adds its name to
the error. The message is unchanged.
In dev mode, the props of a component were stored in a variable before
creating it. In Owl 2, that variable was then given to
`helpers.validateProps`, which was removed in Owl 3: the variable now
only changed the shape of the generated code.

In dev mode, the compiler now only adds the check for duplicate keys in
t-foreach.
An App was a TemplateSet: the template registry and the application
were one object. The App now creates a TemplateSet from its
configuration and holds it in `app.templates`. Its configuration is
unchanged, and the App still owns `dev`, which it gives to its template
set.

`app.getTemplate`, `app.addTemplate`, `app.addTemplates` and the static
`App.registerTemplate` are kept, deprecated, and forward to the template
set (resp. `TemplateSet.registerTemplate`). The fields of the template
set (`rawTemplates`, `translateFn`, ...) are no longer on the App.

Components get their template from `app.templates`, and the `callTemplate`
helper receives the template set (the `app` argument of compiled
templates, which no longer is an App).

The devtools read the templates from `app.templates` when it is a
template set, and from the app itself for older versions.
Now that an App holds a TemplateSet instead of being one, its
configuration no longer extends the one of the template set: AppConfig
lists the template options it accepts (with the same names and types),
and the App gives exactly these to its template set.

An option added to TemplateSetConfig is no longer accepted by the App
without being declared there.
… components

The helpers compiled templates call are now described by two types:
TemplateHelpers for everything but components, and ComponentHelpers
(createComponent, callSlot and toSignal) for components and their slots.
Compiled templates still receive both.

This prepares template sets that render templates without components.
…e helpers

The helpers of compiled templates were all in one object, which every
TemplateSet gave to its templates. Those about components (createComponent,
callSlot, toSignal) depend on component nodes and fibers, so a
TemplateSet depended on all the component code.

They now are in their own file (component_helpers.ts). A TemplateSet
uses the template helpers, and the component helpers given by the new
`componentHelpers` option, which an App gives to its template set.

A template set without component helpers can still render templates that
contain components, as long as these are not rendered. Rendering a
component, a slot or a signal prop throws a clear error: it already
failed before (with a TypeError), since a component can only be rendered
by an App.

TemplateSet no longer imports any component code.
…ents

The agent reference now says where an app's templates are
(`app.templates`), how to add more after creation, that the template
methods of the App are deprecated, and how to render a template outside
any component with a standalone TemplateSet, which renders no
components.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant