mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
7538aeae0e
Before this commit, most of the time, components are destroyed when the
virtual dom is patched and a component node is removed. However, since
Owl is asynchronous and a component may take some time to get ready
(with onWillStart), it can happen that a component is created, then
before it is ready, it is recreated. In that case, the initial instance
has to be destroyed.
Before this commit, the destroy operation was done immediately, when we
cancel the current fibers. However, this means that we cannot have a
guarantee between micro task ticks that a component has not been
destroyed in the meantime.
For example, in Odoo, it is common to use the rpc service, which will
throw an error if called by a destroyed component. But because of the
possible destruction of a component at any time, the following code is
unsafe:
async loadSomeData() {
// guaranteed to be called when component is alive
await Promise.resolve();
// however here, component may have been destroyed
this.rpc(...)
}
So, to prevent this issue, we can slightly delay the destroy operation.
It is not entirely trivial, since we need to find a way to neutralize
the component in the meantime. But it seems like performing all that
kind of operation at the "commit" phase (so, the request animation frame
callback) makes sense to me.
So, this commit modifies the code to add a new component status
(cancelled) and use it to cancel components that are waiting to be
destroyed. These components will then be destroyed as soon as the
requestanimation frame starts, before all other dom operations.
🦉 Owl overview 🦉
Here is a list of everything exported by the Owl library:
Main entities:
App: represent an Owl application (mainly a root component,a set of templates, and a config)Component: the main class to define a concrete Owl componentmount: main entry point for most application: mount a component to a targetxml: helper to define an inline template
Reactivity
useState: create a reactive object (hook, linked to a specific component)reactive: create a reactive object (not linked to any component)markRaw: mark an object or array so that it is ignored by the reactivity systemtoRaw: given a reactive objet, return the raw (non reactive) underlying object
Lifecycle hooks:
onWillStart: hook to define asynchronous code that should be executed before component is renderedonMounted: hook to define code that should be executed when component is mountedonWillPatch: hook to define code that should be executed before component is patchedonWillUpdateProps: hook to define code that should be executed before component is updatedonPatched: hook to define code that should be executed when component is patchedonWillRender: hook to define code that should be executed before component is renderedonRendered: hook to define code that should be executed after component is renderedonWillUnmount: hook to define code that should be executed before component is unmountedonWillDestroy: hook to define code that should be executed before component is destroyedonError: hook to define a Owl error handler
Other hooks:
useComponent: return a reference to the current component (useful to create derived hooks)useEffect: define an effect with its dependenciesuseEnv: return a reference to the current envuseExternalListener: add a listener outside of a component DOMuseRef: get an object representing a reference (t-ref)useChildSubEnv: extend the current env with additional information (for child components)useSubEnv: extend the current env with additional information (for current component and child components)
Utility/helpers:
EventBus: a simple event busloadFile: an helper to load a file from the servermarkup: utility function to define strings that represent html (should not be escaped)status: utility function to get the status of a component (new, mounted or destroyed)validate: validates if an object satisfies a specified schemawhenReady: utility function to execute code when DOM is ready