mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[FIX] store: properly observe all array methods
This commit is contained in:
+33
-10
@@ -158,17 +158,40 @@ export function makeObserver(): Observer {
|
||||
const ArrayProto = Array.prototype;
|
||||
const ModifiedArrayProto = Object.create(ArrayProto);
|
||||
|
||||
ModifiedArrayProto.push = function(...args) {
|
||||
observer.__rev__++;
|
||||
this.__rev__++;
|
||||
return ArrayProto.push.call(this, ...args);
|
||||
};
|
||||
const methodsToPatch = [
|
||||
"push",
|
||||
"pop",
|
||||
"shift",
|
||||
"unshift",
|
||||
"splice",
|
||||
"sort",
|
||||
"reverse"
|
||||
];
|
||||
|
||||
for (let method of methodsToPatch) {
|
||||
const initialMethod = ArrayProto[method];
|
||||
ModifiedArrayProto[method] = function(...args) {
|
||||
observer.__rev__++;
|
||||
this.__rev__++;
|
||||
let inserted;
|
||||
switch (method) {
|
||||
case "push":
|
||||
case "unshift":
|
||||
inserted = args;
|
||||
break;
|
||||
case "splice":
|
||||
inserted = args.slice(2);
|
||||
break;
|
||||
}
|
||||
if (inserted) {
|
||||
for (let elem of inserted) {
|
||||
observe(elem);
|
||||
}
|
||||
}
|
||||
return initialMethod.call(this, ...args);
|
||||
};
|
||||
}
|
||||
|
||||
ModifiedArrayProto.pop = function(...args) {
|
||||
observer.__rev__++;
|
||||
this.__rev__++;
|
||||
return ArrayProto.pop.call(this, ...args);
|
||||
};
|
||||
function observeArr(arr: Array<any>) {
|
||||
(<any>arr).__rev__ = 0;
|
||||
Object.defineProperty(arr, "__rev__", { enumerable: false });
|
||||
|
||||
Reference in New Issue
Block a user