Debian Codegraph @

addons/point_of_sale/static/src/app/utils/debug-formatter.js

raw
1 import { Base } from "../models/related_models";
2 import { RAW_SYMBOL } from "../models/related_models/utils";
3 import { WithLazyGetterTrap } from "../../lazy_getter";
4 import { toRaw } from "@odoo/owl";
5
6 // https://www.mattzeunert.com/2016/02/19/custom-chrome-devtools-object-formatters.html
7
8 /**
9 * Custom Chrome DevTools Object Formatters
10 * This module registers custom formatters for Chrome DevTools console to improve
11 * the debugging experience by formatting some POS objects (ModelRecords,lazyGetter, ...) in a more readable way.
12 */
13
14 export function init() {
15 const formatters = [
16 baseObjectFormatter(),
17 modelFormatter(),
18 modelsFormatter(),
19 lazyGetterFormatter(),
20 immutableFormatter(),
21 reactiveArrayFormatter(),
22 ];
23 window.devtoolsFormatters = window.devtoolsFormatters
24 ? [...window.devtoolsFormatters, ...formatters]
25 : formatters;
26 }
27
28 function baseObjectFormatter() {
29 return {
30 header(obj) {
31 if (!(obj instanceof Base)) {
32 return null;
33 }
34 const name =
35 obj.constructor.name === "ModelRecord" ? obj.model.name : obj.constructor.name;
36 return ["div", {}, name + " #" + obj.id];
37 },
38 hasBody: () => true,
39 body: (obj) => ["div", {}, ...formatBaseInstance(obj)],
40 };
41 }
42
43 function modelsFormatter() {
44 return {
45 header(obj) {
46 return obj.constructor?.name === "Models" && obj._loadData
47 ? createSubObjectBlock(`Models`, getDebugObject(toRaw(obj)), "")
48 : null;
49 },
50 hasBody: () => false,
51 };
52 }
53
54 function modelFormatter() {
55 return {
56 header(obj) {
57 return obj.constructor?.name === "Model" && obj.models
58 ? createSubObjectBlock(
59 `Model(${obj.name})`,
60 { name: obj.name, fields: toRaw(obj.fields) },
61 ""
62 )
63 : null;
64 },
65 hasBody: () => false,
66 };
67 }
68
69 function immutableFormatter() {
70 return {
71 header(obj) {
72 return obj.__deepImmutable
73 ? createSubObjectBlock("ImmutableObject", getDebugObject(toRaw(obj)), "")
74 : null;
75 },
76 hasBody: () => false,
77 };
78 }
79
80 function reactiveArrayFormatter() {
81 return {
82 header(obj) {
83 return Array.isArray(obj) && toRaw(obj) !== obj ? ["div", {}, formatValue(obj)] : null;
84 },
85 hasBody: () => false,
86 };
87 }
88
89 function lazyGetterFormatter() {
90 return {
91 header(obj) {
92 if (!(obj instanceof WithLazyGetterTrap)) {
93 return null;
94 }
95 return createSubObjectBlock(obj.constructor.name, getDebugObject(toRaw(obj)), "");
96 },
97 hasBody: () => false,
98 };
99 }
100
101 function formatBaseInstance(obj) {
102 const blocks = [];
103 if (obj[RAW_SYMBOL]) {
104 blocks.push(createSubObjectBlock("[raw]", toRaw(obj[RAW_SYMBOL])));
105 }
106 const relations = getRelations(obj);
107 if (Object.keys(relations).length) {
108 blocks.push(createSubObjectBlock("[relations]", relations));
109 }
110 if (obj.uiState) {
111 blocks.push(createSubObjectBlock("[uiState]", toRaw(obj.uiState)));
112 }
113 const dismissFields = new Set([
114 "_dirty",
115 "model",
116 "models",
117 "uiState",
118 "raw",
119 ...Object.keys(obj.model.fields),
120 ]);
121
122 const debugObject = getDebugObject(obj, dismissFields);
123 if (debugObject) {
124 blocks.push(createSubObjectBlock("[other props]", debugObject));
125 }
126 // blocks.push(createSubBlock("(dirty)", obj._dirty));
127 // blocks.push(createSubObjectBlock("[model]", obj.model));
128 return blocks;
129 }
130
131 function createSubObjectBlock(name, obj, style = "color:#1E90FF") {
132 return ["div", {}, ["div", { style: style }, name], ["object", { object: obj }]];
133 }
134
135 function getRelations(obj) {
136 return Object.fromEntries(
137 Object.entries(obj.model.fields)
138 .filter(([_, value]) => value.relation && !value.dummy)
139 .map(([field]) => [field, obj[field]])
140 );
141 }
142
143 function formatValue(val) {
144 if (typeof val === "number" || typeof val === "boolean") {
145 return ["span", {}, val];
146 }
147 if (typeof val === "string") {
148 return ["span", {}, JSON.stringify(val)];
149 }
150 if (val instanceof Set || Array.isArray(val)) {
151 return ["object", { object: toRaw([...val]) }];
152 }
153 if (typeof val === "object") {
154 return ["object", { object: toRaw(val) }];
155 }
156 return ["span", {}, "" + val];
157 }
158
159 function getDebugObject(obj, dismissFields = new Set()) {
160 const debugObject = {};
161 let hasValue = false;
162
163 while (obj && obj !== Object.prototype) {
164 Object.entries(Object.getOwnPropertyDescriptors(obj)).forEach(([key, descriptor]) => {
165 if (dismissFields.has(key) || key.startsWith("__lazy")) {
166 return;
167 }
168
169 if ("value" in descriptor && typeof descriptor.value !== "function") {
170 debugObject[key] = descriptor.value;
171 hasValue = true;
172 } else if (typeof descriptor.get === "function") {
173 Object.defineProperty(debugObject, key, { get: () => obj[key] });
174 hasValue = true;
175 }
176 });
177 obj = Object.getPrototypeOf(obj);
178 }
179 return hasValue ? debugObject : null;
180 }