Thursday, December 7, 2023
1 change · master
Code cleanup and technical improvements
This update simplifies how Odoo's web framework makes server requests, making the code easier for developers to use and maintain. The change is mostly internal, but it touches many apps and may slightly affect how background requests behave when screens are closed or changed.
Original PR description
This PR aims at simplifying the way developpers can do rpcs with our framework. Before this commit, in components, they had to import the hook `useService`, and in the setup do something like…
This PR aims at simplifying the way developpers can do rpcs
with our framework. Before this commit, in components, they had to
import the hook `useService`, and in the setup do something like
`this.rpc = useService("rpc")`, and then use `this.rpc` where they
wanted. In services, they had to explicitely declare the dependency
to the rpc service. In any other functions (like helper functions),
it was a bit painful: the function had to take an env (or the rpc
function directly) in arguments, which wasn't ideal.
This PR makes doing rpcs easier. The rpc service service has
been removed. The `jsonrpc` function has been renamed into `rpc`
(and the file `rpc_service` into `rpc`). One can now simply do
`import { rpc } from "@web/core/network/rpc"` and use `rpc` where
he wants.
Note that this change has a (maybe) not insignificant side-effect.
We no longer have the "protection" on rpc. Before, when a component
used `useService("rpc")`, the returned function was tied to that
component instance. If the component was destroyed when calling the
function, it crashed (s.t. a destroyed component didn't do rpc). If
the component was destroyed during the rpc, the promise was left
pending forever (s.t. subsequent callbacks weren't executed). This
protection was sometimes annoying, and people started to bypass it
by directly calling `env.services.rpc` (i.e. a version which isn't
tied to the component). We believe this protection is not really
necessary. In the future, we may introduce an `alive` function to
wrap rpc calls, and use it at some places where it is indeed
necessary (i.e. move it user-space instead of enforced by the
framework).
Part of task~3621046