2019-05-08 11:23:48 +02:00
|
|
|
/**
|
|
|
|
|
* Owl Observer
|
|
|
|
|
*
|
|
|
|
|
* This code contains the logic that allows Owl to observe and react to state
|
|
|
|
|
* changes.
|
|
|
|
|
*
|
|
|
|
|
* This is a Observer class that can observe any JS values. The way it works
|
|
|
|
|
* can be summarized thusly:
|
|
|
|
|
* - primitive values are not observed at all
|
|
|
|
|
* - Objects are observed by replacing all their keys with getters/setters
|
|
|
|
|
* (recursively)
|
|
|
|
|
* - Arrays are observed by replacing their prototype with a customized version,
|
|
|
|
|
* which wrap some methods to allow the tracking of each state change.
|
|
|
|
|
*
|
|
|
|
|
* Note that this code is inspired by Vue.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-04-16 14:24:19 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2019-05-08 11:23:48 +02:00
|
|
|
// Modified Array prototype
|
2019-04-16 14:24:19 +02:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2019-05-08 11:23:48 +02:00
|
|
|
// we define here a new modified Array prototype, which basically override all
|
|
|
|
|
// Array methods that change some state to be able to track their changes
|
2019-04-16 15:17:36 +02:00
|
|
|
const methodsToPatch = [
|
|
|
|
|
"push",
|
|
|
|
|
"pop",
|
|
|
|
|
"shift",
|
|
|
|
|
"unshift",
|
|
|
|
|
"splice",
|
|
|
|
|
"sort",
|
|
|
|
|
"reverse"
|
|
|
|
|
];
|
2019-05-28 14:04:08 +02:00
|
|
|
const methodLen = methodsToPatch.length;
|
|
|
|
|
|
2019-04-16 15:17:36 +02:00
|
|
|
const ArrayProto = Array.prototype;
|
|
|
|
|
const ModifiedArrayProto = Object.create(ArrayProto);
|
|
|
|
|
|
2019-05-28 14:04:08 +02:00
|
|
|
for (let i = 0; i < methodLen; i++) {
|
|
|
|
|
const method = methodsToPatch[i];
|
2019-04-16 15:17:36 +02:00
|
|
|
const initialMethod = ArrayProto[method];
|
|
|
|
|
ModifiedArrayProto[method] = function(...args) {
|
2019-04-17 16:55:57 +02:00
|
|
|
if (!this.__observer__.allowMutations) {
|
|
|
|
|
throw new Error(`Array cannot be changed here")`);
|
|
|
|
|
}
|
2019-05-15 17:30:01 +02:00
|
|
|
this.__observer__.rev++;
|
2019-04-17 09:16:19 +02:00
|
|
|
this.__observer__.notifyChange();
|
2019-04-16 15:17:36 +02:00
|
|
|
this.__owl__.rev++;
|
|
|
|
|
let parent = this;
|
|
|
|
|
do {
|
|
|
|
|
parent.__owl__.deepRev++;
|
|
|
|
|
} while ((parent = parent.__owl__.parent));
|
|
|
|
|
let inserted;
|
|
|
|
|
switch (method) {
|
|
|
|
|
case "push":
|
|
|
|
|
case "unshift":
|
|
|
|
|
inserted = args;
|
|
|
|
|
break;
|
|
|
|
|
case "splice":
|
|
|
|
|
inserted = args.slice(2);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (inserted) {
|
2019-05-28 14:04:08 +02:00
|
|
|
for (let i = 0, iLen = inserted.length; i < iLen; i++) {
|
|
|
|
|
this.__observer__.observe(inserted[i], this);
|
2019-04-16 15:17:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return initialMethod.call(this, ...args);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 11:23:48 +02:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
// Observer
|
|
|
|
|
//------------------------------------------------------------------------------
|
2019-04-16 22:31:46 +02:00
|
|
|
export class Observer {
|
|
|
|
|
rev: number = 1;
|
|
|
|
|
allowMutations: boolean = true;
|
2019-04-17 09:16:19 +02:00
|
|
|
dirty: boolean = false;
|
|
|
|
|
|
|
|
|
|
notifyCB() {}
|
|
|
|
|
notifyChange() {
|
|
|
|
|
this.dirty = true;
|
|
|
|
|
Promise.resolve().then(() => {
|
|
|
|
|
if (this.dirty) {
|
|
|
|
|
this.dirty = false;
|
|
|
|
|
this.notifyCB();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-04-16 14:24:19 +02:00
|
|
|
|
2019-04-16 22:31:46 +02:00
|
|
|
observe(value: any, parent?: any) {
|
|
|
|
|
if (value === null) {
|
|
|
|
|
// fun fact: typeof null === 'object'
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (typeof value !== "object") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ("__owl__" in value) {
|
|
|
|
|
// already observed
|
2019-04-18 11:30:33 +02:00
|
|
|
value.__owl__.parent = parent;
|
2019-04-16 22:31:46 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
|
this._observeArr(value, parent);
|
|
|
|
|
} else {
|
|
|
|
|
this._observeObj(value, parent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set(target: any, key: number | string, value: any) {
|
2019-05-15 17:30:01 +02:00
|
|
|
this.rev++;
|
2019-04-16 22:31:46 +02:00
|
|
|
this._addProp(target, key, value);
|
2019-04-16 14:24:19 +02:00
|
|
|
target.__owl__.rev++;
|
2019-04-17 09:16:19 +02:00
|
|
|
this.notifyChange();
|
2019-04-16 22:31:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_observeObj<T extends { __owl__?: any }>(obj: T, parent?: any) {
|
2019-05-15 17:30:01 +02:00
|
|
|
obj.__owl__ = { rev: this.rev, deepRev: this.rev, parent };
|
2019-04-16 22:31:46 +02:00
|
|
|
Object.defineProperty(obj, "__owl__", { enumerable: false });
|
2019-05-28 14:04:08 +02:00
|
|
|
for (let key in obj) {
|
2019-04-16 22:31:46 +02:00
|
|
|
this._addProp(obj, key, obj[key]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_observeArr(arr: Array<any>, parent?: any) {
|
2019-05-15 17:30:01 +02:00
|
|
|
(<any>arr).__owl__ = { rev: this.rev, deepRev: this.rev, parent };
|
2019-04-16 22:31:46 +02:00
|
|
|
Object.defineProperty(arr, "__owl__", { enumerable: false });
|
|
|
|
|
(<any>arr).__proto__ = Object.create(ModifiedArrayProto);
|
|
|
|
|
(<any>arr).__proto__.__observer__ = this;
|
2019-05-28 14:04:08 +02:00
|
|
|
for (let i = 0, iLen = arr.length; i < iLen; i++) {
|
2019-04-16 22:31:46 +02:00
|
|
|
this.observe(arr[i], arr);
|
|
|
|
|
}
|
2019-04-16 14:24:19 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-16 22:31:46 +02:00
|
|
|
_addProp<T extends { __owl__?: any }>(
|
2019-04-16 14:24:19 +02:00
|
|
|
obj: T,
|
|
|
|
|
key: string | number,
|
|
|
|
|
value: any
|
|
|
|
|
) {
|
2019-04-16 22:31:46 +02:00
|
|
|
var self = this;
|
2019-04-16 14:24:19 +02:00
|
|
|
Object.defineProperty(obj, key, {
|
|
|
|
|
enumerable: true,
|
|
|
|
|
get() {
|
|
|
|
|
return value;
|
|
|
|
|
},
|
|
|
|
|
set(newVal) {
|
|
|
|
|
if (newVal !== value) {
|
2019-05-15 17:30:01 +02:00
|
|
|
self.rev++;
|
2019-04-17 16:55:57 +02:00
|
|
|
if (!self.allowMutations) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Observed state cannot be changed here! (key: "${key}", val: "${newVal}")`
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-04-16 14:24:19 +02:00
|
|
|
value = newVal;
|
2019-04-16 22:31:46 +02:00
|
|
|
self.observe(newVal, obj);
|
2019-04-16 14:24:19 +02:00
|
|
|
obj.__owl__.rev!++;
|
|
|
|
|
let parent = obj;
|
|
|
|
|
do {
|
|
|
|
|
parent.__owl__.deepRev++;
|
2019-04-18 11:30:33 +02:00
|
|
|
} while ((parent = parent.__owl__.parent) && parent !== obj);
|
2019-04-17 09:16:19 +02:00
|
|
|
self.notifyChange();
|
2019-04-16 14:24:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-04-16 22:31:46 +02:00
|
|
|
this.observe(value, obj);
|
2019-04-16 14:24:19 +02:00
|
|
|
}
|
|
|
|
|
}
|