Debian Codegraph @

addons/point_of_sale/static/src/lazy_getter.js

raw
1 import { effect } from "@web/core/utils/reactive";
2 import { getDisabler } from "./proxy_trap";
3
4 function getAllGetters(proto) {
5 const getters = new Map();
6 while (proto !== null) {
7 const descriptors = Object.getOwnPropertyDescriptors(proto);
8 for (const [name, descriptor] of Object.entries(descriptors)) {
9 if (descriptor.get && !getters.has(name)) {
10 getters.set(name, descriptor.get);
11 }
12 }
13 proto = Object.getPrototypeOf(proto);
14 }
15 return getters;
16 }
17
18 const classGetters = new Map();
19
20 export function clearGettersCache() {
21 classGetters.clear();
22 }
23
24 function getLazyGetters(Class) {
25 if (!classGetters.has(Class)) {
26 const getters = new Map();
27 const excludedLazyGetters = Class.excludedLazyGetters || [];
28 for (const [name, func] of getAllGetters(Class.prototype)) {
29 if (
30 (name.startsWith("__") && name.endsWith("__")) ||
31 excludedLazyGetters.includes(name)
32 ) {
33 continue;
34 }
35 getters.set(name, _defineLazyGetter(name, func));
36 }
37 classGetters.set(Class, getters);
38 }
39 return classGetters.get(Class);
40 }
41
42 function _defineLazyGetter(name, func) {
43 return [`__lazy_${name}`, (obj) => func.call(obj)];
44 }
45
46 /**
47 /**
48 * Creates a lazy getter for an object instance, ensuring the value is (re)computed only when needed.
49 * @param {Object} object - The object on which to define the lazy getter.
50 * @param {string} name - The name of the getter
51 * @param {Function} func - The function that computes the property value when needed.
52 */
53 export function createLazyGetter(object, name, func) {
54 if (!(object instanceof WithLazyGetterTrap)) {
55 throw new Error("The object must be an instance of WithLazyGetterTrap");
56 }
57 const [lazyName, lazyMethod] = _defineLazyGetter(name, func);
58 lazyComputed(object, lazyName, lazyMethod);
59 const getter = function () {
60 const disabler = getDisabler(object, name);
61 if (disabler.isDisabled()) {
62 return func();
63 }
64 return object[lazyName];
65 };
66 Object.defineProperty(object, name, {
67 get: function () {
68 return getter();
69 },
70 });
71 }
72
73 function defineLazyGetterTrap(Class) {
74 const getters = getLazyGetters(Class);
75 return function get(target, prop, receiver) {
76 const disabler = getDisabler(target, prop);
77 if (disabler.isDisabled() || !getters.has(prop)) {
78 return Reflect.get(target, prop, receiver);
79 }
80 return disabler.call(() => {
81 const [lazyName] = getters.get(prop);
82 // For a getter, we should get the value from the receiver.
83 // Because the receiver is linked to the reactivity.
84 // We want to read the getter from it to make sure that the getter
85 // is part of the reactivity as well.
86 // To avoid infinite recursion, we disable this proxy trap
87 // during the time the lazy getter is accessed.
88 return receiver[lazyName];
89 });
90 };
91 }
92
93 function lazyComputed(obj, propName, compute) {
94 const key = Symbol(propName);
95 Object.defineProperty(obj, propName, {
96 get() {
97 return this[key]();
98 },
99 configurable: true,
100 });
101
102 /**
103 * - `recompute` depends on the dependencies of `compute`.
104 * - When one of the dependencies of `compute` changed, `recompute` invalidates the cache of the `compute`.
105 * - The cache of `compute` is saved in `value`.
106 */
107 effect(
108 function recompute(obj) {
109 const value = [];
110 obj[key] = () => {
111 if (!value.length) {
112 value.push(compute(obj));
113 }
114 return value[0];
115 };
116 },
117 [obj]
118 );
119 }
120
121 export class WithLazyGetterTrap {
122 constructor({ traps = {} }) {
123 const Class = this.constructor;
124 const instance = new Proxy(this, { get: defineLazyGetterTrap(Class), ...traps });
125 for (const [lazyName, func] of getLazyGetters(Class).values()) {
126 lazyComputed(instance, lazyName, func);
127 }
128 return instance;
129 }
130 }