mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[IMP] reactivity: toRaw now works with non reactive objects
This commit is contained in:
committed by
Samuel Degueldre
parent
bb9d65e95b
commit
12b8ce963e
@@ -117,3 +117,10 @@ rawState.value = 3; // will NOT be picked up by the reactivity system!!!
|
|||||||
Here again, this is useful in some situations where we want to explicitely bypass
|
Here again, this is useful in some situations where we want to explicitely bypass
|
||||||
Owl, but using this function means that the responsability of coordinating
|
Owl, but using this function means that the responsability of coordinating
|
||||||
state update is given to the user code, instead of Owl. Subtle bugs may arise!
|
state update is given to the user code, instead of Owl. Subtle bugs may arise!
|
||||||
|
|
||||||
|
Also, normal (non-reactive objects) will be directly returned by `toRaw`:
|
||||||
|
|
||||||
|
```js
|
||||||
|
const obj = { a: 1 };
|
||||||
|
console.log(toRaw(obj) === obj); // true
|
||||||
|
```
|
||||||
|
|||||||
+1
-1
@@ -57,7 +57,7 @@ export function markRaw<T extends Target>(value: T): NonReactive<T> {
|
|||||||
* @returns the underlying value
|
* @returns the underlying value
|
||||||
*/
|
*/
|
||||||
export function toRaw<T extends object>(value: Reactive<T>): T {
|
export function toRaw<T extends object>(value: Reactive<T>): T {
|
||||||
return value[TARGET];
|
return value[TARGET] || value;
|
||||||
}
|
}
|
||||||
|
|
||||||
const targetToKeysToCallbacks = new WeakMap<Target, Map<ObjectKey, Set<Callback>>>();
|
const targetToKeysToCallbacks = new WeakMap<Target, Map<ObjectKey, Set<Callback>>>();
|
||||||
|
|||||||
@@ -1131,6 +1131,11 @@ describe("toRaw", () => {
|
|||||||
expect(reactiveObj).not.toBe(obj);
|
expect(reactiveObj).not.toBe(obj);
|
||||||
expect(toRaw(reactiveObj as Reactive<typeof obj>)).toBe(obj);
|
expect(toRaw(reactiveObj as Reactive<typeof obj>)).toBe(obj);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("giving a non reactive to toRaw return the object itself", () => {
|
||||||
|
const obj = { value: 1 };
|
||||||
|
expect(toRaw(obj as Reactive<typeof obj>)).toBe(obj);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Reactivity: useState", () => {
|
describe("Reactivity: useState", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user