mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dfc782599b | |||
| 71f545058b | |||
| 1a20cc57de | |||
| 25738a1bf0 | |||
| 4a96eff3c6 | |||
| d043d47754 | |||
| 3a10468f7b | |||
| 144b323d2b | |||
| bb64e87634 | |||
| 9a87b9a4a0 | |||
| 2a53a9592e | |||
| f54b9a4a0c | |||
| abb9d0b364 | |||
| 8483cc805e | |||
| 142f47ac82 | |||
| c8a27aa1a3 | |||
| 323cb61d2c | |||
| ac9cc91701 |
@@ -120,7 +120,7 @@ npm install @odoo/owl
|
||||
|
||||
If you want to use a simple `<script>` tag, the last release can be downloaded here:
|
||||
|
||||
- [owl-1.0.13](https://github.com/odoo/owl/releases/tag/v1.0.13)
|
||||
- [owl-1.2.2](https://github.com/odoo/owl/releases/tag/v1.2.2)
|
||||
|
||||
## License
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ class OrderLine extends Component {
|
||||
</div>`;
|
||||
|
||||
add() {
|
||||
this.trigger("add-to-order", { line: props.line });
|
||||
this.trigger("add-to-order", { line: this.props.line });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -545,7 +545,7 @@ application), since it involves extracting all task related code out of the
|
||||
components. Here is the new content of the `app.js` file:
|
||||
|
||||
```js
|
||||
const { Component, Store } = owl;
|
||||
const { Component, Store, mount } = owl;
|
||||
const { xml } = owl.tags;
|
||||
const { whenReady } = owl.utils;
|
||||
const { useRef, useDispatch, useStore } = owl.hooks;
|
||||
@@ -808,7 +808,7 @@ For reference, here is the final code:
|
||||
|
||||
```js
|
||||
(function () {
|
||||
const { Component, Store } = owl;
|
||||
const { Component, Store, mount } = owl;
|
||||
const { xml } = owl.tags;
|
||||
const { whenReady } = owl.utils;
|
||||
const { useRef, useDispatch, useState, useStore } = owl.hooks;
|
||||
|
||||
@@ -61,14 +61,14 @@ because a lot of the state is hidden in their internals.
|
||||
|
||||
React or Vue have a huge community, and a lot of effort have been made into their
|
||||
tooling. This is wonderful, but at the same time, a pretty big issue for Odoo:
|
||||
since the assets are totally dynamic (and could change whenever the user install
|
||||
or remove an addon), we need to have all that kind of tooling on the production
|
||||
since the assets are totally dynamic (and could change whenever the user installs
|
||||
or removes an addon), we need to have all that kind of tooling on the production
|
||||
servers. This is certainly not ideal.
|
||||
|
||||
Also, this makes it very complicated to setup Vue or React tools: Odoo code is
|
||||
not a simple file that import other files. It changes all the time, assets
|
||||
are bundled differently in different contexts. This is the reason why Odoo has
|
||||
its own module system, which are resolve at runtime, by the browser. The
|
||||
its own module system, which are resolved at runtime, by the browser. The
|
||||
dynamic nature of Odoo means that we often need to delay work as late as possible
|
||||
(in other word, we want a JIT user interface!)
|
||||
|
||||
@@ -78,12 +78,12 @@ deploy. Using React without JSX, or Vue without vue file is not very appealing.
|
||||
At the same time, Owl is designed to solve this issue: it compiles templates
|
||||
by the browser, it doesn't need much code for that, since we use the XML parser
|
||||
built into each browser. Owl works with or without any additional tooling. It
|
||||
can use template strings to write single file component, and is easy to integrate
|
||||
can use template strings to write single file components, and is easy to integrate
|
||||
in any html page, with a simple `<script>` tag.
|
||||
|
||||
## Template based
|
||||
|
||||
Odoo stores template as XML document in a database. This is very powerful, since
|
||||
Odoo stores templates as XML documents in a database. This is very powerful, since
|
||||
this allow the use of xpaths to customize other templates. This is a very
|
||||
important feature of odoo, and one of the key to Odoo modularity.
|
||||
|
||||
@@ -104,12 +104,12 @@ awkward, and very confusing.
|
||||
## Developer Experience
|
||||
|
||||
This brings us to the following point: developer experience. We see this choice
|
||||
as an investment for the future, and we want to make onboarding developer as
|
||||
as an investment for the future, and we want to make onboarding developers as
|
||||
easy as possible.
|
||||
|
||||
While many javascript professionals clearly think that react/vue is not difficult
|
||||
(which is true to some extent), it is alsy true that many non js specialists are
|
||||
overwhelmed with the frontend world: functional component, hooks, and many other
|
||||
overwhelmed with the frontend world: functional components, hooks, and many other
|
||||
fancy words. Also, what is available in the compilation context may be difficult,
|
||||
there is a lot of black magic going on in pretty much every framework. Vue
|
||||
somehow join various namespaces into one, under the hood, and add various internal
|
||||
@@ -135,7 +135,7 @@ needs: Odoo will fetch templates from the database and need to compile them only
|
||||
at the last possible moment, so we can apply all necessary xpaths.
|
||||
|
||||
Even more: Odoo needs to be able to generate (and compile) templates at runtime.
|
||||
Currently, Odoo form views interpret a xml description. But the form view code
|
||||
Currently, Odoo form views interpret an xml description. But the form view code
|
||||
then needs to do a lot of complicated operations. With Owl, we will be able to
|
||||
transform a view description into a QWeb template, then compile that and use it
|
||||
immediately.
|
||||
@@ -147,16 +147,16 @@ For example, the reactivity system. We like the way Vue did it, but it has a
|
||||
flaw: it is not really optional. There is actually a way to opt out of the reactivity
|
||||
system by freezing the state, but then, it is freezed.
|
||||
|
||||
And there certainly are situations where we need a state, which is not readonly,
|
||||
And there certainly are situations where we need a state, which is not read-only,
|
||||
and not observed. For example, imagine a spreadsheet component. It may have a
|
||||
very large internal state, and it knows exactly when it needs to be rendered
|
||||
(basically, whenever the user perform some action). Then, observing its state
|
||||
(basically, whenever the user performs some action). Then, observing its state
|
||||
is a net performance loss, both for the CPU and the memory.
|
||||
|
||||
## Concurrency
|
||||
|
||||
Many applications are happy to simply display a spinner whenever a new asynchronous
|
||||
action is performed, but Odoo want a different user experience: most asynchronous
|
||||
action is performed, but Odoo wants a different user experience: most asynchronous
|
||||
state changes are not displayed until ready. This is sometimes called a concurrent
|
||||
mode: the UI is rendered in memory, and displayed only when it is ready (and
|
||||
only if it has not been cancelled by subsequent user actions).
|
||||
@@ -175,6 +175,6 @@ that current standard frameworks are not tailored to our needs. It is perfectly
|
||||
fine, because they each chose a different set of tradeoffs.
|
||||
|
||||
However, we feel that there is still room in the framework world for something
|
||||
that is different. For a framework that make choices compatible with Odoo.
|
||||
that is different. For a framework that makes choices compatible with Odoo.
|
||||
|
||||
And that is why we built Owl 🦉.
|
||||
|
||||
@@ -17,6 +17,7 @@ You will find here a complete reference of every feature, class or object
|
||||
provided by Owl.
|
||||
|
||||
- [Animations](reference/animations.md)
|
||||
- [Browser](reference/browser.md)
|
||||
- [Component](reference/component.md)
|
||||
- [Content](reference/content.md)
|
||||
- [Concurrency Model](reference/concurrency_model.md)
|
||||
|
||||
@@ -52,21 +52,20 @@ sequence of events will happen:
|
||||
At node insertion:
|
||||
|
||||
- the css classes `name-enter` and `name-enter-active` will be added directly
|
||||
when the node is inserted into the DOM,
|
||||
when the node is inserted into the DOM.
|
||||
- on the next animation frame: the css class `name-enter` will be removed and the
|
||||
class `name-enter-to` will be added (so they can be used to trigger css
|
||||
transition effects),
|
||||
- the css class `name-enter-active` will be removed whenever a css transition
|
||||
ends.
|
||||
transition effects).
|
||||
- at the end of the transition, `name-enter-to` and `name-enter-active` will be removed.
|
||||
|
||||
At node destruction:
|
||||
|
||||
- the css classes `name-leave` and `name-leave-active` will be added before the
|
||||
node is removed to the DOM,
|
||||
- the css class `name-leave` will be removed on the next animation frame (so it
|
||||
can be used to trigger css transition effects),
|
||||
- the css class `name-leave-active` will be removed whenever a css transition
|
||||
ends. Only then will the element be removed from the DOM.
|
||||
node is removed to the DOM.
|
||||
- on the next animation frame: the css class `name-leave` will be removed and the
|
||||
class `name-leave-to` will be added (so they can be used to trigger css
|
||||
transition effects).
|
||||
- at the end of the transition, `name-leave-to` and `name-leave-active` will be removed.
|
||||
|
||||
For example, a simple fade in/out effect can be done with this:
|
||||
|
||||
@@ -93,3 +92,36 @@ Notes:
|
||||
|
||||
Owl does not support more than one transition on a single node, so the
|
||||
`t-transition` expression must be a single value (i.e. no space allowed).
|
||||
|
||||
## SCSS Mixins
|
||||
|
||||
If you use SCSS, you can use mixins to make generic animations. Here is an exemple with a fade in / fade out animation:
|
||||
|
||||
```scss
|
||||
@mixin animation-fade($time, $name) {
|
||||
.#{$name}_fade-enter-active,
|
||||
.#{$name}_fade-active {
|
||||
transition: all $time;
|
||||
}
|
||||
|
||||
.#{$name}_fade-enter {
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
.#{$name}_fade-leave-to {
|
||||
opacity: 0;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Usage:
|
||||
|
||||
```scss
|
||||
@include animation-fade(0.5s, "o_notification");
|
||||
```
|
||||
|
||||
You can now have in your template:
|
||||
|
||||
```xml
|
||||
<SomeTag t-transition="o_notification_fade"/>
|
||||
```
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
# 🦉 Browser 🦉
|
||||
|
||||
## Content
|
||||
|
||||
- [Overview](#overview)
|
||||
- [Browser Content](#browser-content)
|
||||
|
||||
## Overview
|
||||
|
||||
The browser object contains some browser native APIs, such as `setTimeout`, that
|
||||
are used by Owl and its utility functions. They are exposed with the intent of
|
||||
making them mockable if necessary.
|
||||
|
||||
```js
|
||||
owl.browser.setTimeout === window.setTimeout; // return true
|
||||
```
|
||||
|
||||
For now, this object contains some functions that are not used by Owl. They
|
||||
will eventually be removed in Owl 2.0.
|
||||
|
||||
## Browser Content
|
||||
|
||||
More specifically, the `browser` object contains the following methods and objects:
|
||||
|
||||
- `setTimeout`
|
||||
- `clearTimeout`
|
||||
- `setInterval`
|
||||
- `clearInterval`
|
||||
- `requestAnimationFrame`
|
||||
- `random`
|
||||
- `Date`
|
||||
- `fetch`
|
||||
- `localStorage`
|
||||
@@ -10,13 +10,21 @@
|
||||
- [Static Properties](#static-properties)
|
||||
- [Methods](#methods)
|
||||
- [Lifecycle](#lifecycle)
|
||||
- [`constructor(parent, props)`](#constructorparent-props)
|
||||
- [`willStart()`](#willstart)
|
||||
- [`mounted()`](#mounted)
|
||||
- [`willUpdateProps(nextProps)`](#willupdatepropsnextprops)
|
||||
- [`willPatch()`](#willpatch)
|
||||
- [`patched(snapshot)`](#patchedsnapshot)
|
||||
- [`willUnmount()`](#willunmount)
|
||||
- [`catchError(error)`](#catcherrorerror)
|
||||
- [Root Component](#root-component)
|
||||
- [Composition](#composition)
|
||||
- [Form Input Bindings](#form-input-bindings)
|
||||
- [References](#references)
|
||||
- [Dynamic sub components](#dynamic-sub-components)
|
||||
- [Functional Components](#functional-components)
|
||||
- [SVG components](#svg-components)
|
||||
- [SVG Components](#svg-components)
|
||||
|
||||
## Overview
|
||||
|
||||
@@ -289,8 +297,13 @@ We explain here all the public methods of the `Component` class.
|
||||
are updated. It returns a boolean, which indicates if the component should
|
||||
ignore a props update. If it returns false, then `willUpdateProps` will not
|
||||
be called, and no rendering will occur. Its default implementation is to
|
||||
always return true. This is an optimization, similar to React's `shouldComponentUpdate`. Most of the time, this should not be used, but it
|
||||
can be useful if we are handling large number of components.
|
||||
always return true. Note that this is an optimization, similar to React's `shouldComponentUpdate`. Most of the time, this should not be used, but it
|
||||
can be useful if we are handling large number of components. Since this is an
|
||||
optimization, Owl has the freedom to ignore the result of `shouldUpdate` in
|
||||
some cases (for example, if a component is remounted, or if we want to force
|
||||
a full rerender of the UI). However, if `shouldUpdate` returns true, then Owl
|
||||
provides the guarantee that the component will be rendered at some point in
|
||||
the future (except if the component is destroyed or if some part of the UI crashes).
|
||||
|
||||
* **`destroy()`**. As its name suggests, this method will remove the component,
|
||||
and perform all necessary cleanup, such as unmounting the component, its children,
|
||||
|
||||
@@ -7,6 +7,7 @@ For example, `Component` is available at `owl.Component` and `EventBus` is
|
||||
exported as `owl.core.EventBus`.
|
||||
|
||||
```
|
||||
browser
|
||||
Component misc
|
||||
Context AsyncRoot
|
||||
QWeb Portal
|
||||
@@ -28,6 +29,8 @@ hooks utils
|
||||
useContext
|
||||
useState
|
||||
useRef
|
||||
useComponent
|
||||
useEnv
|
||||
useSubEnv
|
||||
useStore
|
||||
useDispatch
|
||||
|
||||
@@ -133,17 +133,4 @@ the `QWeb` instance and a `browser` object:
|
||||
- `qweb` will be set to an empty `QWeb` instance. This is absolutely necessary
|
||||
for Owl to be able to render anything
|
||||
- `browser`: this is an object that contains some common access points to the
|
||||
browser methods with a side effect. This is particularly useful when one want
|
||||
to test more advanced components, and be able to mock those methods.
|
||||
|
||||
More specifically, the `browser` object contains the following methods and objects:
|
||||
|
||||
- `setTimeout`
|
||||
- `clearTimeout`
|
||||
- `setInterval`
|
||||
- `clearInterval`
|
||||
- `requestAnimationFrame`
|
||||
- `random`
|
||||
- `Date`
|
||||
- `fetch`
|
||||
- `localStorage`
|
||||
browser methods with a side effect. See [browser](browser.md) for more information. Note that the browser object will be removed from the environment in Owl 2.0.
|
||||
|
||||
+14
-4
@@ -21,6 +21,8 @@
|
||||
- [`useStore`](#usestore)
|
||||
- [`useDispatch`](#usedispatch)
|
||||
- [`useGetters`](#usegetters)
|
||||
- [`useComponent`](#usecomponent)
|
||||
- [`useEnv`](#useenv)
|
||||
- [Making customized hooks](#making-customized-hooks)
|
||||
|
||||
## Overview
|
||||
@@ -381,6 +383,16 @@ The `useDispatch` hook is the way for components to get a reference to the store
|
||||
The `useGetters` hook is the way for components to get a reference to the store
|
||||
getters. See the [store documentation](store.md) for more information.
|
||||
|
||||
### `useComponent`
|
||||
|
||||
The `useComponent` hook is useful as a building block for some customized hooks,
|
||||
that may need a reference to the component calling them.
|
||||
|
||||
### `useEnv`
|
||||
|
||||
The `useEnv` hook is useful as a building block for some customized hooks,
|
||||
that may need a reference to the env of the component calling them.
|
||||
|
||||
### Making customized hooks
|
||||
|
||||
Hooks are a wonderful way to organize the code of a complex component by feature
|
||||
@@ -435,13 +447,11 @@ not the solution to every problem.
|
||||
|
||||
```js
|
||||
function useRouter() {
|
||||
return Component.current.env.router;
|
||||
const env = useEnv();
|
||||
return env.router;
|
||||
}
|
||||
```
|
||||
|
||||
This means that we give control to the application developer to create the
|
||||
router, which is good, so they can set it up, subclass it, ... And then, to
|
||||
test our components, we can just add a mock router in the environment.
|
||||
|
||||
Note: the code above makes use of the `Component.current` property. This is the
|
||||
way hooks are able to get a reference to the component currently being created.
|
||||
|
||||
@@ -18,7 +18,7 @@ class Child extends Component {
|
||||
}
|
||||
|
||||
class Parent extends Component {
|
||||
static template = xml`<div><ComponentA a="state.a" b="'string'"/></div>`;
|
||||
static template = xml`<div><Child a="state.a" b="'string'"/></div>`;
|
||||
static components = { Child };
|
||||
state = useState({ a: "fromparent" });
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@odoo/owl",
|
||||
"version": "1.0.13",
|
||||
"version": "1.2.2",
|
||||
"description": "Odoo Web Library (OWL)",
|
||||
"main": "dist/owl.cjs.js",
|
||||
"browser": "dist/owl.iife.js",
|
||||
@@ -44,7 +44,7 @@
|
||||
"github-api": "^3.3.0",
|
||||
"jest": "^23.6.0",
|
||||
"jest-environment-jsdom": "^24.7.1",
|
||||
"live-server": "^1.2.1",
|
||||
"live-server": "^1.2.2",
|
||||
"npm-run-all": "^4.1.5",
|
||||
"prettier": "^2.0.4",
|
||||
"rollup": "^1.6.0",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
# 🦉 OWL Roadmap 🦉
|
||||
|
||||
- Current version: 1.0.13
|
||||
- Current version: 1.2.2
|
||||
- Status: stable
|
||||
|
||||
This roadmap is only an attempt at predicting Owl's future. Everything may
|
||||
|
||||
+8
-1
@@ -10,6 +10,8 @@ export interface Browser {
|
||||
localStorage: Window["localStorage"];
|
||||
}
|
||||
|
||||
let localStorage: Window["localStorage"] | null = null;
|
||||
|
||||
export const browser: Browser = {
|
||||
setTimeout: window.setTimeout.bind(window),
|
||||
clearTimeout: window.clearTimeout.bind(window),
|
||||
@@ -19,5 +21,10 @@ export const browser: Browser = {
|
||||
random: Math.random,
|
||||
Date: window.Date,
|
||||
fetch: (window.fetch || (() => {})).bind(window),
|
||||
localStorage: window.localStorage,
|
||||
get localStorage() {
|
||||
return localStorage || window.localStorage;
|
||||
},
|
||||
set localStorage(newLocalStorage: Window["localStorage"]) {
|
||||
localStorage = newLocalStorage;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -159,6 +159,7 @@ export class Component<Props extends {} = any, T extends Env = Env> {
|
||||
if (!this.env.qweb) {
|
||||
this.env.qweb = new QWeb();
|
||||
}
|
||||
// TODO: remove this in owl 2.0
|
||||
if (!this.env.browser) {
|
||||
this.env.browser = browser;
|
||||
}
|
||||
@@ -321,7 +322,14 @@ export class Component<Props extends {} = any, T extends Env = Env> {
|
||||
}
|
||||
if (__owl__.currentFiber) {
|
||||
const currentFiber = __owl__.currentFiber;
|
||||
if (currentFiber.target === target && currentFiber.position === position) {
|
||||
if (!currentFiber.target && !currentFiber.position) {
|
||||
// this means we have a pending rendering, but it was a render operation,
|
||||
// not a mount operation. We can simply update the fiber with the target
|
||||
// and the position
|
||||
currentFiber.target = target;
|
||||
currentFiber.position = position;
|
||||
return scheduler.addFiber(currentFiber);
|
||||
} else if (currentFiber.target === target && currentFiber.position === position) {
|
||||
return scheduler.addFiber(currentFiber);
|
||||
} else {
|
||||
scheduler.rejectFiber(currentFiber, "Mounting operation cancelled");
|
||||
@@ -332,7 +340,7 @@ export class Component<Props extends {} = any, T extends Env = Env> {
|
||||
message += `\nMaybe the DOM is not ready yet? (in that case, you can use owl.utils.whenReady)`;
|
||||
throw new Error(message);
|
||||
}
|
||||
const fiber = new Fiber(null, this, false, target, position);
|
||||
const fiber = new Fiber(null, this, true, target, position);
|
||||
fiber.shouldPatch = false;
|
||||
if (!__owl__.vnode) {
|
||||
this.__prepareAndRender(fiber, () => {});
|
||||
@@ -365,10 +373,7 @@ export class Component<Props extends {} = any, T extends Env = Env> {
|
||||
async render(force: boolean = false): Promise<void> {
|
||||
const __owl__ = this.__owl__;
|
||||
const currentFiber = __owl__.currentFiber;
|
||||
if (!__owl__.isMounted && !currentFiber) {
|
||||
// if we get here, this means that the component was either never mounted,
|
||||
// or was unmounted and some state change triggered a render. Either way,
|
||||
// we do not want to actually render anything in this case.
|
||||
if (!__owl__.vnode && !currentFiber) {
|
||||
return;
|
||||
}
|
||||
if (currentFiber && !currentFiber.isRendered && !currentFiber.isCompleted) {
|
||||
@@ -384,8 +389,6 @@ export class Component<Props extends {} = any, T extends Env = Env> {
|
||||
if (fiber.isCompleted) {
|
||||
return;
|
||||
}
|
||||
// we are mounted (__owl__.isMounted), or if we are currently being
|
||||
// mounted (!isMounted), so we call __render
|
||||
this.__render(fiber);
|
||||
} else {
|
||||
// we were mounted when render was called, but we aren't anymore, so we
|
||||
|
||||
+23
-17
@@ -82,6 +82,7 @@ export class Fiber {
|
||||
|
||||
let oldFiber = __owl__.currentFiber;
|
||||
if (oldFiber && !oldFiber.isCompleted) {
|
||||
this.force = true;
|
||||
if (oldFiber.root === oldFiber && !parent) {
|
||||
// both oldFiber and this fiber are root fibers
|
||||
this._reuseFiber(oldFiber);
|
||||
@@ -187,7 +188,8 @@ export class Fiber {
|
||||
complete() {
|
||||
let component = this.component;
|
||||
this.isCompleted = true;
|
||||
if (!this.target && !component.__owl__.isMounted) {
|
||||
const { isMounted, isDestroyed } = component.__owl__;
|
||||
if (isDestroyed) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -201,14 +203,16 @@ export class Fiber {
|
||||
const patchLen = patchQueue.length;
|
||||
|
||||
// call willPatch hook on each fiber of patchQueue
|
||||
for (let i = 0; i < patchLen; i++) {
|
||||
const fiber = patchQueue[i];
|
||||
if (fiber.shouldPatch) {
|
||||
component = fiber.component;
|
||||
if (component.__owl__.willPatchCB) {
|
||||
component.__owl__.willPatchCB();
|
||||
if (isMounted) {
|
||||
for (let i = 0; i < patchLen; i++) {
|
||||
const fiber = patchQueue[i];
|
||||
if (fiber.shouldPatch) {
|
||||
component = fiber.component;
|
||||
if (component.__owl__.willPatchCB) {
|
||||
component.__owl__.willPatchCB();
|
||||
}
|
||||
component.willPatch();
|
||||
}
|
||||
component.willPatch();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -270,16 +274,18 @@ export class Fiber {
|
||||
}
|
||||
|
||||
// call patched/mounted hook on each fiber of (reversed) patchQueue
|
||||
for (let i = patchLen - 1; i >= 0; i--) {
|
||||
const fiber = patchQueue[i];
|
||||
component = fiber.component;
|
||||
if (fiber.shouldPatch && !this.target) {
|
||||
component.patched();
|
||||
if (component.__owl__.patchedCB) {
|
||||
component.__owl__.patchedCB();
|
||||
if (isMounted || inDOM) {
|
||||
for (let i = patchLen - 1; i >= 0; i--) {
|
||||
const fiber = patchQueue[i];
|
||||
component = fiber.component;
|
||||
if (fiber.shouldPatch && !this.target) {
|
||||
component.patched();
|
||||
if (component.__owl__.patchedCB) {
|
||||
component.__owl__.patchedCB();
|
||||
}
|
||||
} else {
|
||||
component.__callMounted();
|
||||
}
|
||||
} else if (this.target ? inDOM : true) {
|
||||
component.__callMounted();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,16 +115,6 @@ export function useContextWithCB(ctx: Context, component: Component, method): an
|
||||
__owl__.observer = new Observer();
|
||||
__owl__.observer.notifyCB = component.render.bind(component);
|
||||
}
|
||||
const currentCB = __owl__.observer.notifyCB;
|
||||
__owl__.observer.notifyCB = function () {
|
||||
if (ctx.rev > mapping[id]) {
|
||||
// in this case, the context has been updated since we were rendering
|
||||
// last, and we do not need to render here with the observer. A
|
||||
// rendering is coming anyway, with the correct props.
|
||||
return;
|
||||
}
|
||||
currentCB();
|
||||
};
|
||||
|
||||
mapping[id] = 0;
|
||||
const renderFn = __owl__.renderFn;
|
||||
|
||||
+21
-1
@@ -1,4 +1,4 @@
|
||||
import { Component } from "./component/component";
|
||||
import { Component, Env } from "./component/component";
|
||||
import { Observer } from "./core/observer";
|
||||
|
||||
/**
|
||||
@@ -118,6 +118,26 @@ export function useRef<C extends Component = Component>(name: string): Ref<C> {
|
||||
};
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// "Builder" hooks
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* This hook is useful as a building block for some customized hooks, that may
|
||||
* need a reference to the component calling them.
|
||||
*/
|
||||
export function useComponent<P, E extends Env>(): Component<P, E> {
|
||||
return Component.current as any;
|
||||
}
|
||||
|
||||
/**
|
||||
* This hook is useful as a building block for some customized hooks, that may
|
||||
* need a reference to the env of the component calling them.
|
||||
*/
|
||||
export function useEnv<E extends Env>(): E {
|
||||
return Component.current.env as any;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// useSubEnv
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
@@ -22,6 +22,7 @@ import { Router } from "./router/router";
|
||||
export { Component, mount } from "./component/component";
|
||||
export { QWeb };
|
||||
export { config };
|
||||
export { browser } from "./browser";
|
||||
|
||||
export const Context = _context.Context;
|
||||
export const useState = _hooks.useState;
|
||||
|
||||
+3
-1
@@ -70,6 +70,7 @@ const TRANSLATABLE_ATTRS = ["label", "title", "placeholder", "alt"];
|
||||
|
||||
const lineBreakRE = /[\r\n]/;
|
||||
const whitespaceRE = /\s+/g;
|
||||
const translationRE = /^(\s*)([\s\S]+?)(\s*)$/;
|
||||
|
||||
const NODE_HOOKS_PARAMS = {
|
||||
create: "(_, n)",
|
||||
@@ -496,7 +497,8 @@ export class QWeb extends EventBus {
|
||||
}
|
||||
if (this.translateFn) {
|
||||
if ((node.parentNode as any).getAttribute("t-translation") !== "off") {
|
||||
text = this.translateFn(text);
|
||||
const match = translationRE.exec(text);
|
||||
text = match[1] + this.translateFn(match[2]) + match[3];
|
||||
}
|
||||
}
|
||||
if (ctx.parentNode) {
|
||||
|
||||
+15
-5
@@ -1,5 +1,4 @@
|
||||
import { Component } from "./component/component";
|
||||
import { Env } from "./component/component";
|
||||
import { Component, Env } from "./component/component";
|
||||
import { Context, useContextWithCB } from "./context";
|
||||
import { onWillUpdateProps } from "./hooks";
|
||||
|
||||
@@ -76,6 +75,11 @@ export class Store extends Context {
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
__notifyComponents(): Promise<void> {
|
||||
this.trigger("before-update");
|
||||
return super.__notifyComponents();
|
||||
}
|
||||
}
|
||||
|
||||
interface SelectorOptions {
|
||||
@@ -106,13 +110,16 @@ export function useStore(selector, options: SelectorOptions = {}): any {
|
||||
const newRevNumber = hashFn(result);
|
||||
if ((newRevNumber > 0 && revNumber !== newRevNumber) || !isEqual(oldResult, result)) {
|
||||
revNumber = newRevNumber;
|
||||
if (options.onUpdate) {
|
||||
options.onUpdate(result);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (options.onUpdate) {
|
||||
store.on("before-update", component, () => {
|
||||
const newValue = selector(store!.state, component.props!);
|
||||
options.onUpdate(newValue);
|
||||
});
|
||||
}
|
||||
store.updateFunctions[componentId].push(function (): boolean {
|
||||
return selectCompareUpdate(store!.state, component.props);
|
||||
});
|
||||
@@ -133,6 +140,9 @@ export function useStore(selector, options: SelectorOptions = {}): any {
|
||||
const __destroy = component.__destroy;
|
||||
component.__destroy = (parent) => {
|
||||
delete store.updateFunctions[componentId];
|
||||
if (options.onUpdate) {
|
||||
store.off("before-update", component);
|
||||
}
|
||||
__destroy.call(component, parent);
|
||||
};
|
||||
|
||||
|
||||
@@ -1405,4 +1405,160 @@ describe("async rendering", () => {
|
||||
expect(fixture.innerHTML).toBe("<div>2</div>");
|
||||
expect(Widget.prototype.__render).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
test("components with shouldUpdate=false", async () => {
|
||||
const state = { p: 1, cc: 10 };
|
||||
|
||||
class ChildChild extends Component {
|
||||
static template = xml`
|
||||
<div>
|
||||
child child: <t t-esc="state.cc"/>
|
||||
</div>`;
|
||||
state = state;
|
||||
shouldUpdate() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class Child extends Component {
|
||||
static components = { ChildChild };
|
||||
static template = xml`
|
||||
<div>
|
||||
child
|
||||
<ChildChild/>
|
||||
</div>`;
|
||||
|
||||
shouldUpdate() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
let parent: any;
|
||||
class Parent extends Component {
|
||||
static components = { Child };
|
||||
static template = xml`
|
||||
<div>
|
||||
parent: <t t-esc="state.p"/>
|
||||
<Child/>
|
||||
</div>`;
|
||||
|
||||
state = state;
|
||||
constructor(a, b) {
|
||||
super(a, b);
|
||||
parent = this;
|
||||
}
|
||||
shouldUpdate() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class App extends Component {
|
||||
static components = { Parent };
|
||||
static template = xml`
|
||||
<div>
|
||||
<Parent/>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
var div = document.createElement("div");
|
||||
fixture.appendChild(div);
|
||||
|
||||
const app = new App();
|
||||
|
||||
await app.mount(fixture);
|
||||
expect(fixture.innerHTML).toBe(
|
||||
"<div></div><div><div> parent: 1<div> child <div> child child: 10</div></div></div></div>"
|
||||
);
|
||||
app.mount(div);
|
||||
|
||||
// wait for rendering from second mount to go through parent
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
state.cc++;
|
||||
state.p++;
|
||||
parent.render();
|
||||
await nextTick();
|
||||
expect(fixture.innerHTML).toBe(
|
||||
"<div><div><div> parent: 2<div> child <div> child child: 11</div></div></div></div></div>"
|
||||
);
|
||||
});
|
||||
|
||||
test("components with shouldUpdate=false, part 2", async () => {
|
||||
const state = { p: 1, cc: 10 };
|
||||
let shouldUpdate = true;
|
||||
|
||||
class ChildChild extends Component {
|
||||
static template = xml`
|
||||
<div>
|
||||
child child: <t t-esc="state.cc"/>
|
||||
</div>`;
|
||||
state = state;
|
||||
shouldUpdate() {
|
||||
return shouldUpdate;
|
||||
}
|
||||
}
|
||||
|
||||
class Child extends Component {
|
||||
static components = { ChildChild };
|
||||
static template = xml`
|
||||
<div>
|
||||
child
|
||||
<ChildChild/>
|
||||
</div>`;
|
||||
|
||||
shouldUpdate() {
|
||||
return shouldUpdate;
|
||||
}
|
||||
}
|
||||
|
||||
let parent: any;
|
||||
class Parent extends Component {
|
||||
static components = { Child };
|
||||
static template = xml`
|
||||
<div>
|
||||
parent: <t t-esc="state.p"/>
|
||||
<Child/>
|
||||
</div>`;
|
||||
|
||||
state = state;
|
||||
constructor(a, b) {
|
||||
super(a, b);
|
||||
parent = this;
|
||||
}
|
||||
shouldUpdate() {
|
||||
return shouldUpdate;
|
||||
}
|
||||
}
|
||||
|
||||
class App extends Component {
|
||||
static components = { Parent };
|
||||
static template = xml`
|
||||
<div>
|
||||
<Parent/>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
const app = new App();
|
||||
|
||||
await app.mount(fixture);
|
||||
expect(fixture.innerHTML).toBe(
|
||||
"<div><div> parent: 1<div> child <div> child child: 10</div></div></div></div>"
|
||||
);
|
||||
|
||||
state.cc++;
|
||||
state.p++;
|
||||
app.render();
|
||||
|
||||
// wait for rendering to go through child
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
|
||||
shouldUpdate = false;
|
||||
parent.render();
|
||||
await nextTick();
|
||||
expect(fixture.innerHTML).toBe(
|
||||
"<div><div> parent: 2<div> child <div> child child: 11</div></div></div></div>"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -962,6 +962,69 @@ describe("lifecycle hooks", () => {
|
||||
"parent:patched",
|
||||
]);
|
||||
});
|
||||
|
||||
test("willPatch/patched hook is not called if not mounted in DOM", async () => {
|
||||
const steps: string[] = [];
|
||||
|
||||
class ChildWidget extends Component {
|
||||
static template = xml`<div/>`;
|
||||
constructor(parent, props) {
|
||||
super(parent, props);
|
||||
steps.push("child:constructor");
|
||||
}
|
||||
mounted() {
|
||||
steps.push("child:mounted");
|
||||
}
|
||||
willPatch() {
|
||||
steps.push("child:willPatch");
|
||||
}
|
||||
patched() {
|
||||
steps.push("child:patched");
|
||||
}
|
||||
}
|
||||
class ParentWidget extends Component {
|
||||
static template = xml`
|
||||
<div>
|
||||
<t t-component="child" v="state.n"/>
|
||||
</div>
|
||||
`;
|
||||
static components = { child: ChildWidget };
|
||||
state = useState({ n: 1 });
|
||||
constructor() {
|
||||
super();
|
||||
steps.push("parent:constructor");
|
||||
}
|
||||
mounted() {
|
||||
steps.push("parent:mounted");
|
||||
}
|
||||
willPatch() {
|
||||
steps.push("parent:willPatch");
|
||||
}
|
||||
patched() {
|
||||
steps.push("parent:patched");
|
||||
}
|
||||
}
|
||||
|
||||
const div = document.createElement("div");
|
||||
const widget = new ParentWidget();
|
||||
await widget.mount(div);
|
||||
expect(steps).toEqual(["parent:constructor", "child:constructor"]);
|
||||
|
||||
widget.state.n = 2;
|
||||
await nextTick();
|
||||
|
||||
expect(steps).toEqual(["parent:constructor", "child:constructor"]);
|
||||
|
||||
// then we remount the component in the dom
|
||||
await widget.mount(fixture);
|
||||
|
||||
expect(steps).toEqual([
|
||||
"parent:constructor",
|
||||
"child:constructor",
|
||||
"child:mounted",
|
||||
"parent:mounted",
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("destroy method", () => {
|
||||
|
||||
@@ -323,6 +323,54 @@ describe("unmounting and remounting", () => {
|
||||
expect(steps).toEqual([2, 2, 3]);
|
||||
});
|
||||
|
||||
test("change state and render while mounted in detached dom", async () => {
|
||||
class App extends Component {
|
||||
static template = xml`<div><t t-esc="state.val"/></div>`;
|
||||
state = useState({ val: 1 });
|
||||
}
|
||||
|
||||
const detachedDiv = document.createElement("div");
|
||||
const app = await mount(App, { target: detachedDiv });
|
||||
|
||||
expect(detachedDiv.innerHTML).toBe("<div>1</div>");
|
||||
app.state.val = 2;
|
||||
await nextTick();
|
||||
expect(detachedDiv.innerHTML).toBe("<div>2</div>");
|
||||
});
|
||||
|
||||
test("change state and render while not mounted ", async () => {
|
||||
class App extends Component {
|
||||
static template = xml`<div><t t-esc="state.val"/></div>`;
|
||||
state = useState({ val: 1 });
|
||||
}
|
||||
|
||||
const app = new App(null);
|
||||
|
||||
app.state.val = 2; // will call the render method (before being mounted)
|
||||
await nextTick();
|
||||
|
||||
await app.mount(fixture);
|
||||
|
||||
expect(fixture.innerHTML).toBe("<div>2</div>");
|
||||
});
|
||||
|
||||
test("destroy and change state after mounted in detached dom", async () => {
|
||||
class App extends Component {
|
||||
static template = xml`<div><t t-esc="state.val"/></div>`;
|
||||
state = useState({ val: 1 });
|
||||
}
|
||||
|
||||
const detachedDiv = document.createElement("div");
|
||||
const app = await mount(App, { target: detachedDiv });
|
||||
|
||||
expect(detachedDiv.innerHTML).toBe("<div>1</div>");
|
||||
|
||||
app.destroy();
|
||||
app.state.val = 2;
|
||||
await nextTick();
|
||||
expect(detachedDiv.innerHTML).toBe("");
|
||||
});
|
||||
|
||||
test("change state while component is unmounted", async () => {
|
||||
let child;
|
||||
class Child extends Component {
|
||||
@@ -600,4 +648,51 @@ describe("unmounting and remounting", () => {
|
||||
await parent.render();
|
||||
expect(fixture.textContent).toBe("fixedsome text");
|
||||
});
|
||||
|
||||
test("remounting component tree where a component implement shouldupdate", async () => {
|
||||
let state: any;
|
||||
const steps = [];
|
||||
class Child extends Component {
|
||||
static template = xml`<div><t t-esc="state.word"/><t t-esc="props.name"/></div>`;
|
||||
|
||||
state = useState({ word: "hello" });
|
||||
|
||||
constructor(parent, props) {
|
||||
super(parent, props);
|
||||
state = this.state;
|
||||
}
|
||||
patched() {
|
||||
steps.push("patched");
|
||||
}
|
||||
mounted() {
|
||||
steps.push("mounted");
|
||||
}
|
||||
willUnmount() {
|
||||
steps.push("willUnmount");
|
||||
}
|
||||
shouldUpdate() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class Parent extends Component {
|
||||
static template = xml`<div><Child name="state.name"/></div>`;
|
||||
static components = { Child };
|
||||
state = useState({ name: "World" });
|
||||
}
|
||||
|
||||
const parent = await mount(Parent, { target: fixture });
|
||||
expect(fixture.innerHTML).toBe("<div><div>helloWorld</div></div>");
|
||||
|
||||
parent.unmount();
|
||||
expect(fixture.innerHTML).toBe("");
|
||||
await parent.mount(fixture);
|
||||
expect(fixture.innerHTML).toBe("<div><div>helloWorld</div></div>");
|
||||
|
||||
state.word = "test";
|
||||
|
||||
await nextTick();
|
||||
expect(fixture.innerHTML).toBe("<div><div>testWorld</div></div>");
|
||||
expect(steps).toEqual(["mounted", "willUnmount", "mounted", "patched"]);
|
||||
});
|
||||
});
|
||||
|
||||
+20
-1
@@ -289,7 +289,26 @@ describe("Context", () => {
|
||||
expect(testContext.subscriptions.update.length).toBe(0);
|
||||
});
|
||||
|
||||
test("concurrent renderings", async () => {
|
||||
test.skip("concurrent renderings", async () => {
|
||||
/**
|
||||
* Note: this test is interesting, but sadly just an incomplete attempt at
|
||||
* protecting users against themselves. With the context API, it is not
|
||||
* possible for the framework to protect completely against crashes. Maybe
|
||||
* like in this case, when a component is in a simple hierarchy where all
|
||||
* renderings come from the context changes, but in a real case, where some
|
||||
* code can trigger a rendering independently, it is insufficient.
|
||||
*
|
||||
* The main problem is that the sub component depends on some external state,
|
||||
* which may be modified, and then incompatible with the component actual
|
||||
* state (for example, if the sub component has an id key related to some
|
||||
* object that has been removed from the context).
|
||||
*
|
||||
* For now, sadly, the only solution is that components that depends on external
|
||||
* state should guarantee their own integrity themselves. Then maybe this
|
||||
* could be solved at the level of a state management solution that has a
|
||||
* more advanced API, to let components determine if they should be updated
|
||||
* or not (so, something slightly more advanced that the useStore hook).
|
||||
*/
|
||||
const testContext = new Context({ x: { n: 1 }, key: "x" });
|
||||
const def = makeDeferred();
|
||||
let stateC;
|
||||
|
||||
@@ -9,8 +9,10 @@ import {
|
||||
onWillPatch,
|
||||
onWillStart,
|
||||
onWillUpdateProps,
|
||||
useEnv,
|
||||
useSubEnv,
|
||||
useExternalListener,
|
||||
useComponent,
|
||||
} from "../src/hooks";
|
||||
import { xml } from "../src/tags";
|
||||
|
||||
@@ -520,6 +522,19 @@ describe("hooks", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("can use useEnv", async () => {
|
||||
expect.assertions(1);
|
||||
class TestComponent extends Component {
|
||||
static template = xml`<div><t t-esc="env.val"/></div>`;
|
||||
constructor() {
|
||||
super();
|
||||
expect(useEnv()).toBe(env);
|
||||
}
|
||||
}
|
||||
const component = new TestComponent();
|
||||
await component.mount(fixture);
|
||||
});
|
||||
|
||||
test("can use sub env", async () => {
|
||||
class TestComponent extends Component {
|
||||
static template = xml`<div><t t-esc="env.val"/></div>`;
|
||||
@@ -535,6 +550,19 @@ describe("hooks", () => {
|
||||
expect(component.env).toHaveProperty("val");
|
||||
});
|
||||
|
||||
test("can use useComponent", async () => {
|
||||
expect.assertions(1);
|
||||
class TestComponent extends Component {
|
||||
static template = xml`<div></div>`;
|
||||
constructor() {
|
||||
super();
|
||||
expect(useComponent()).toBe(this);
|
||||
}
|
||||
}
|
||||
const component = new TestComponent();
|
||||
await component.mount(fixture);
|
||||
});
|
||||
|
||||
test("parent and child env", async () => {
|
||||
class Child extends Component {
|
||||
static template = xml`<div><t t-esc="env.val"/></div>`;
|
||||
|
||||
@@ -3924,6 +3924,18 @@ exports[`translation support some attributes are translated 1`] = `
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`translation support translation is done on the trimmed text, with extra spaces readded after 1`] = `
|
||||
"function anonymous(context, extra
|
||||
) {
|
||||
// Template name: \\"test\\"
|
||||
let h = this.h;
|
||||
let c1 = [], p1 = {key:1};
|
||||
let vn1 = h('div', p1, c1);
|
||||
c1.push({text: \` mot \`});
|
||||
return vn1;
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`whitespace handling consecutives whitespaces are condensed into a single space 1`] = `
|
||||
"function anonymous(context, extra
|
||||
) {
|
||||
|
||||
@@ -2154,6 +2154,17 @@ describe("translation support", () => {
|
||||
'<div><p label="mot">mot</p><p title="mot">mot</p><p placeholder="mot">mot</p><p alt="mot">mot</p><p something="word">mot</p></div>'
|
||||
);
|
||||
});
|
||||
|
||||
test("translation is done on the trimmed text, with extra spaces readded after", () => {
|
||||
const translations = {
|
||||
word: "mot",
|
||||
};
|
||||
const translateFn = jest.fn((expr) => translations[expr] || expr);
|
||||
const qweb = new QWeb({ translateFn });
|
||||
qweb.addTemplate("test", "<div> word </div>");
|
||||
expect(renderToString(qweb, "test")).toBe("<div> mot </div>");
|
||||
expect(translateFn).toHaveBeenCalledWith("word");
|
||||
});
|
||||
});
|
||||
|
||||
describe("t-key tests", () => {
|
||||
|
||||
+161
-3
@@ -1,4 +1,4 @@
|
||||
import { Component, Env } from "../src/component/component";
|
||||
import { Component, Env, mount } from "../src/component/component";
|
||||
import { Store, useStore, useDispatch, useGetters, EnvWithStore } from "../src/store";
|
||||
import { useState } from "../src/hooks";
|
||||
import { xml } from "../src/tags";
|
||||
@@ -571,12 +571,12 @@ describe("connecting a component to store", () => {
|
||||
app.state.beerId = 2;
|
||||
await nextTick();
|
||||
expect(fixture.innerHTML).toBe("<div><span>kwak</span></div>");
|
||||
expect(counter).toBe(1);
|
||||
expect(counter).toBe(0);
|
||||
|
||||
store.dispatch("renameBeer", { id: 2, name: "orval" });
|
||||
await nextTick();
|
||||
expect(fixture.innerHTML).toBe("<div><span>orval</span></div>");
|
||||
expect(counter).toBe(2);
|
||||
expect(counter).toBe(1);
|
||||
});
|
||||
|
||||
test("connected component is properly cleaned up on destroy", async () => {
|
||||
@@ -1241,4 +1241,162 @@ describe("various scenarios", () => {
|
||||
await nextTick();
|
||||
expect(fixture.innerHTML).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test("component with store, useState and shouldUpdate=false", async () => {
|
||||
let state: any;
|
||||
|
||||
const store = new Store({ state: { rev: 0 } });
|
||||
|
||||
class Child extends Component {
|
||||
static template = xml`<div><t t-esc="state.word"/><t t-esc="props.name"/></div>`;
|
||||
|
||||
state = useState({ word: "hello" });
|
||||
|
||||
constructor(parent, props) {
|
||||
super(parent, props);
|
||||
state = this.state;
|
||||
useStore((props) => {
|
||||
return 1;
|
||||
});
|
||||
}
|
||||
|
||||
shouldUpdate() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class Parent extends Component {
|
||||
static template = xml`<div><Child name="state.name"/></div>`;
|
||||
static components = { Child };
|
||||
|
||||
state = useState({ name: "World" });
|
||||
|
||||
constructor(parent, props) {
|
||||
super(parent, props);
|
||||
useStore((props) => store.state.rev);
|
||||
}
|
||||
}
|
||||
(env as any).store = store;
|
||||
|
||||
await mount(Parent, { target: fixture, env });
|
||||
expect(fixture.innerHTML).toBe("<div><div>helloWorld</div></div>");
|
||||
|
||||
store.state.rev++;
|
||||
// this is the key to the bug, it makes Parent be in "render" state but not
|
||||
// yet rendered while the change of state happens
|
||||
await Promise.resolve();
|
||||
state.word = "test";
|
||||
await nextTick();
|
||||
expect(fixture.innerHTML).toBe("<div><div>testWorld</div></div>");
|
||||
});
|
||||
|
||||
test("component with store, useState, shouldUpdate=false and child with shouldupdate false", async () => {
|
||||
let state: any;
|
||||
|
||||
const store = new Store({ state: { rev: 0 } });
|
||||
|
||||
class ChildChild extends Component {
|
||||
static template = xml`<div><t t-esc="props.value"/></div>`;
|
||||
|
||||
shouldUpdate() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class Child extends Component {
|
||||
static template = xml`<div><t t-esc="state.word"/><t t-esc="props.name"/><ChildChild value="state.value"/></div>`;
|
||||
static components = { ChildChild };
|
||||
state = useState({ word: "hello", value: 3 });
|
||||
|
||||
constructor(parent, props) {
|
||||
super(parent, props);
|
||||
state = this.state;
|
||||
useStore((props) => {
|
||||
return 1;
|
||||
});
|
||||
}
|
||||
|
||||
shouldUpdate() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class Parent extends Component {
|
||||
static template = xml`<div><Child name="state.name"/></div>`;
|
||||
static components = { Child };
|
||||
|
||||
state = useState({ name: "World" });
|
||||
constructor(parent, props) {
|
||||
super(parent, props);
|
||||
useStore((props) => store.state.rev);
|
||||
}
|
||||
}
|
||||
(env as any).store = store;
|
||||
|
||||
await mount(Parent, { target: fixture, env });
|
||||
expect(fixture.innerHTML).toBe("<div><div>helloWorld<div>3</div></div></div>");
|
||||
|
||||
store.state.rev++;
|
||||
// this is the key to the bug, it makes Parent be in "render" state but not
|
||||
// yet rendered while the change of state happens
|
||||
await Promise.resolve();
|
||||
state.word = "test";
|
||||
state.value = 44;
|
||||
await nextTick();
|
||||
expect(fixture.innerHTML).toBe("<div><div>testWorld<div>3</div></div></div>");
|
||||
});
|
||||
|
||||
test("parent/children with store, parent is remounted", async () => {
|
||||
const store = new Store({ state: { a: 1, b: 1 } });
|
||||
|
||||
class Child extends Component {
|
||||
static template = xml`<div><t t-esc="a"/></div>`;
|
||||
a: any;
|
||||
constructor(parent, props) {
|
||||
super(parent, props);
|
||||
this.a = useStore(
|
||||
(state, props) => {
|
||||
return state.a;
|
||||
},
|
||||
{
|
||||
onUpdate: (a) => {
|
||||
this.a = a;
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Parent extends Component {
|
||||
static template = xml`
|
||||
<div>
|
||||
parent: <t t-esc="b"/>
|
||||
<Child/>
|
||||
</div>`;
|
||||
static components = { Child };
|
||||
|
||||
b: any;
|
||||
constructor(parent, props) {
|
||||
super(parent, props);
|
||||
this.b = useStore((state, props) => {
|
||||
return state.b;
|
||||
});
|
||||
}
|
||||
}
|
||||
(env as any).store = store;
|
||||
|
||||
const div = document.createElement("div");
|
||||
fixture.appendChild(div);
|
||||
|
||||
// initial mounting
|
||||
const parent = await mount(Parent, { target: fixture, env });
|
||||
expect(fixture.innerHTML).toBe("<div></div><div> parent: 1<div>1</div></div>");
|
||||
|
||||
// remounting component, then immediately update store.state
|
||||
parent.mount(div);
|
||||
store.state.a++;
|
||||
|
||||
await nextTick();
|
||||
expect(fixture.innerHTML).toBe("<div><div> parent: 1<div>2</div></div></div>");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -8,7 +8,7 @@ import * as owl from "../../src/index";
|
||||
|
||||
import { Component, Env } from "../../src/component/component";
|
||||
import { xml } from "../../src/tags";
|
||||
import { makeTestFixture, makeTestEnv } from "../helpers";
|
||||
import { makeTestFixture, makeTestEnv, nextTick } from "../helpers";
|
||||
|
||||
let fixture: HTMLElement = makeTestFixture();
|
||||
let env: Env = makeTestEnv();
|
||||
@@ -31,6 +31,7 @@ test("log a specific message for render method calls if component is not mounted
|
||||
parent.unmount();
|
||||
parent.state.value = 2;
|
||||
|
||||
await nextTick();
|
||||
expect(steps).toEqual([
|
||||
"[OWL_DEBUG] Parent<id=1> constructor, props={}",
|
||||
"[OWL_DEBUG] Parent<id=1> mount",
|
||||
@@ -40,7 +41,10 @@ test("log a specific message for render method calls if component is not mounted
|
||||
"[OWL_DEBUG] Parent<id=1> mounted",
|
||||
"[OWL_DEBUG] scheduler: stop running tasks queue",
|
||||
"[OWL_DEBUG] Parent<id=1> willUnmount",
|
||||
"[OWL_DEBUG] Parent<id=1> render (warning: component is not mounted, this render has no effect)",
|
||||
"[OWL_DEBUG] Parent<id=1> render (warning: component is not mounted)",
|
||||
"[OWL_DEBUG] scheduler: start running tasks queue",
|
||||
"[OWL_DEBUG] Parent<id=1> rendering template",
|
||||
"[OWL_DEBUG] scheduler: stop running tasks queue",
|
||||
]);
|
||||
console.log = log;
|
||||
});
|
||||
|
||||
+1
-1
@@ -102,7 +102,7 @@
|
||||
const __owl__ = component.__owl__;
|
||||
let msg = `render`;
|
||||
if (!__owl__.isMounted && !__owl__.currentFiber) {
|
||||
msg += ` (warning: component is not mounted, this render has no effect)`;
|
||||
msg += ` (warning: component is not mounted)`;
|
||||
}
|
||||
log(msg);
|
||||
return render(...args);
|
||||
|
||||
Reference in New Issue
Block a user