1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
odoo.define("web/static/src/js/model.js", function (require) {
"use strict";
const { groupBy, partitionBy } = require("web.utils");
const Registry = require("web.Registry");
const { Component, core } = owl;
const { EventBus, Observer } = core;
const isNotNull = (val) => val !== null && val !== undefined;
/**
* Feature extension of the class Model.
* @see {Model}
*/
class ModelExtension {
/**
* @param {Object} config
* @param {Object} config.env
*/
constructor(config) {
this.config = config;
this.env = this.config.env;
this.shouldLoad = true;
this.state = {};
}
//---------------------------------------------------------------------
// Public
//---------------------------------------------------------------------
/**
* Used by the parent model to initiate a load action. The actual
* loading of the extension is determined by the "shouldLoad" property.
* @param {Object} params
*/
async callLoad(params) {
if (this.shouldLoad) {
this.shouldLoad = false;
await this.load(params);
}
}
/**
* @param {string} method
* @param {...any} args
*/
dispatch(method, ...args) {
if (method in this) {
this[method](...args);
}
}
/**
* Exports the current state of the extension.
* @returns {Object}
*/
exportState() {
return this.state;
}
/**
* Meant to return the result of the appropriate getter or do nothing
* if not concerned by the given property.
* @abstract
* @param {string} property
* @param {...any} args
* @returns {null}
*/
get() {
return null;
}
/**
* Imports the given state after parsing it. If no state is given the
* extension will prepare a new state and will need to be loaded.
* @param {Object} [state]
*/
importState(state) {
this.shouldLoad = !state;
if (this.shouldLoad) {
this.prepareState();
} else {
Object.assign(this.state, state);
}
}
/**
* Called and awaited on initial model load.
* @abstract
* @param {Object} params
* @returns {Promise}
*/
async load(/* params */) {
/* ... */
}
/**
* Called on initialization if no imported state for the extension is
* found.
* @abstract
*/
prepareState() {
/* ... */
}
}
/**
* The layer of an extension indicates with which other extensions this one
* will be loaded. This property must be overridden in case the model
* depends on other extensions to be loaded first.
*/
ModelExtension.layer = 0;
/**
* Model
*
* The purpose of the class Model and the associated hook useModel
* is to offer something similar to an owl store but with no automatic
* notification (and rendering) of components when the 'state' used in the
* model would change. Instead, one should call the "__notifyComponents"
* function whenever it is useful to alert registered component.
* Nevertheless, when calling a method through the 'dispatch' method, a
* notification does take place automatically, and registered components
* (via useModel) are rendered.
*
* It is highly expected that this class will change in a near future. We
* don't have the necessary hindsight to be sure its actual form is good.
*
* The following snippets show a typical use case of the model system: a
* search model with a control panel extension feature.
*
*-------------------------------------------------------------------------
* MODEL AND EXTENSIONS DEFINITION
*-------------------------------------------------------------------------
*
* 1. Definition of the main model
* @see Model
* ```
* class ActionModel extends Model {
* // ...
* }
* ```
*
* 2. Definition of the model extension
* @see ModelExtension
* ```
* class ControlPanelModelExtension extends ActionModel.Extension {
* // ...
* }
* ```
*
* 3. Registration of the extension into the main model
* @see Registry()
* ```
* ActionModel.registry.add("SearchPanel", ControlPanelModelExtension, 10);
* ```
*
*-------------------------------------------------------------------------
* ON VIEW/ACTION INIT
*-------------------------------------------------------------------------
*
* 4. Creation of the core model and its extensions
* @see Model.prototype.constructor()
* ```
* const extensions = {
* SearchPanel: {
* // ...
* }
* }
* const searchModelConfig = {
* // ...
* };
* const actionModel = new ActionModel(extensions, searchModelConfig);
* ```
*
* 5. Loading of all extensions' asynchronous data
* @see Model.prototype.load()
* ```
* await actionModel.load();
* ```
*
* 6. Subscribing to the model changes
* @see useModel()
* ```
* class ControlPanel extends Component {
* constructor() {
* super(...arguments);
* // env must contain the actionModel
* this.actionModel = useModel('actionModel');
* }
* }
* ```
*
*-------------------------------------------------------------------------
* MODEL USAGE ON RUNTIME
*-------------------------------------------------------------------------
*
* Case: dispatch an action
* @see Model.prototype.dispatch()
* ```
* actionModel.dispatch("updateProperty", value);
* ```
*
* Case: call a getter
* @see Model.prototype.get()
* ```
* const result = actionModel.get("property");
* ```
*
* @abstract
* @extends EventBus
*/
class Model extends EventBus {
/**
* Instantiated extensions are determined by the `extensions` argument:
* - keys are the extensions names as added in the registry
* - values are the local configurations given to each extension
* The extensions are grouped by the sequence number they where
* registered with in the registry. Extensions being on the same level
* will be loaded in parallel; this means that all extensions belonging
* to the same group are awaited before loading the next group.
* @param {Object<string, any>} [extensions={}]
* @param {Object} [globalConfig={}] global configuration: can be
* accessed by itself and each of the added extensions.
* @param {Object} [globalConfig.env]
* @param {string} [globalConfig.importedState]
*/
constructor(extensions = {}, globalConfig = {}) {
super();
this.config = globalConfig;
this.env = this.config.env;
this.dispatching = false;
this.extensions = [];
this.externalState = {};
this.mapping = {};
this.rev = 1;
const { name, registry } = this.constructor;
if (!registry || !(registry instanceof Registry)) {
throw new Error(`Unimplemented registry on model "${name}".`);
}
// Order, group and sequencially instantiate all extensions
const registryExtensions = Object.entries(registry.entries());
const extensionNameLayers = registryExtensions.map(
([name, { layer }]) => ({ name, layer })
);
const groupedNameLayers = groupBy(extensionNameLayers, "layer");
for (const groupNameLayers of Object.values(groupedNameLayers)) {
for (const { name } of groupNameLayers) {
if (name in extensions) {
this.addExtension(name, extensions[name]);
}
}
}
this.importState(this.config.importedState);
}
//---------------------------------------------------------------------
// Public
//---------------------------------------------------------------------
/**
* Method used internally to instantiate all extensions. Can also be
* called externally to add extensions after model instantiation.
* @param {string} extensionName
* @param {Object} extensionConfig
*/
addExtension(extensionName, extensionConfig) {
const { name, registry } = this.constructor;
const Extension = registry.get(extensionName);
if (!Extension) {
throw new Error(`Unknown model extension "${extensionName}" in model "${name}"`);
}
// Extension config = this.config ∪ extension.config
const get = this.__get.bind(this, Extension.name);
const trigger = this.trigger.bind(this);
const config = Object.assign({ get, trigger }, this.config, extensionConfig);
const extension = new Extension(config);
if (!(Extension.layer in this.extensions)) {
this.extensions[Extension.layer] = [];
}
this.extensions[Extension.layer].push(extension);
}
/**
* Returns the result of the first related method on any instantiated
* extension. This method must be overridden if multiple extensions
* return a value with a common method (and dispatchAll does not
* suffice). After the dispatch of the action, all models are partially
* reloaded and components are notified afterwards.
* @param {string} method
* @param {...any} args
*/
dispatch(method, ...args) {
const isInitialDispatch = !this.dispatching;
this.dispatching = true;
for (const extension of this.extensions.flat()) {
extension.dispatch(method, ...args);
}
if (!isInitialDispatch) {
return;
}
this.dispatching = false;
let rev = this.rev;
// Calls 'after dispatch' hooks
// Purpose: fetch updated data from the server. This is considered
// a loading action and is thus performed by groups instead of
// loading all extensions at once.
this._loadExtensions({ isInitialLoad: false }).then(() => {
// Notifies subscribed components
// Purpose: re-render components bound by 'useModel'
if (rev === this.rev) {
this._notifyComponents();
}
});
}
/**
* Stringifies and exports an object holding the exported state of each
* active extension.
* @returns {string}
*/
exportState() {
const exported = {};
for (const extension of this.extensions.flat()) {
exported[extension.constructor.name] = extension.exportState();
}
const fullState = Object.assign({}, this.externalState, exported);
return JSON.stringify(fullState);
}
/**
* Returns the result of the first related getter on any instantiated
* extension. This method must be overridden if multiple extensions
* share a common getter (and getAll does not make the job).
* @param {string} property
* @param {...any} args
* @returns {any}
*/
get(property, ...args) {
for (const extension of this.extensions.flat()) {
const result = extension.get(property, ...args);
if (isNotNull(result)) {
return result;
}
}
return null;
}
/**
* Parses the given stringified state object and imports each state
* part to its related extension.
* @param {string} [stringifiedState="null"]
*/
importState(stringifiedState = "null") {
const state = JSON.parse(stringifiedState) || {};
Object.assign(this.externalState, state);
for (const extension of this.extensions.flat()) {
extension.importState(state[extension.constructor.name]);
}
}
/**
* Must be called after construction and state preparation/import.
* Waits for all asynchronous work needed by the model extensions to be
* ready.
* /!\ The current model extensions do not require a smarter system at
* the moment (therefore using layers instead of dependencies). It
* should be changed if at some point an extension needs another
* specific extension to be loaded instead of a whole batch (with the
* current system some promises will be waited needlessly).
* @returns {Promise}
*/
async load() {
await this._loadExtensions({ isInitialLoad: true });
}
//---------------------------------------------------------------------
// Private
//---------------------------------------------------------------------
/**
* Returns the list of the results of all extensions providing a getter
* for the given property returning a non-null value, excluding the
* extension whose name is equal to "excluded". This method is given to
* each extension in the "config" object bound to the model scope and
* having the extension name bound as the first argument.
* @private
* @param {string} excluded
* @param {string} property
* @param {...any} args
* @returns {any[]}
*/
__get(excluded, property, ...args) {
const results = [];
for (const extension of this.extensions.flat()) {
if (extension.constructor.name !== excluded) {
const result = extension.get(property, ...args);
if (isNotNull(result)) {
results.push(result);
}
}
}
return results;
}
/**
* Private handler to loop over all extension layers sequencially and
* wait for a given callback to be completed on all extensions of a
* same layer.
* @private
* @param {Object} params
* @param {boolean} params.isInitialLoad whether this call comes
* from the initial load.
* @returns {Promise}
*/
async _loadExtensions(params) {
for (let layer = 0; layer < this.extensions.length; layer++) {
await Promise.all(this.extensions[layer].map(
(extension) => extension.callLoad(params)
));
}
}
/**
* @see Context.__notifyComponents() in owl.js for explanation
* @private
*/
async _notifyComponents() {
const rev = ++this.rev;
const subscriptions = this.subscriptions.update;
const groups = partitionBy(subscriptions, (s) =>
s.owner ? s.owner.__owl__.depth : -1
);
for (let group of groups) {
const proms = group.map((sub) =>
sub.callback.call(sub.owner, rev)
);
Component.scheduler.flush();
await Promise.all(proms);
}
}
}
Model.Extension = ModelExtension;
/**
* This is more or less the hook 'useContextWithCB' from owl only slightly
* simplified.
*
* @param {string} modelName
* @returns {model}
*/
function useModel(modelName) {
const component = Component.current;
const model = component.env[modelName];
if (!(model instanceof Model)) {
throw new Error(`No Model found when connecting '${
component.name
}'`);
}
const mapping = model.mapping;
const __owl__ = component.__owl__;
const componentId = __owl__.id;
if (!__owl__.observer) {
__owl__.observer = new Observer();
__owl__.observer.notifyCB = component.render.bind(component);
}
const currentCB = __owl__.observer.notifyCB;
__owl__.observer.notifyCB = function () {
if (model.rev > mapping[componentId]) {
return;
}
currentCB();
};
mapping[componentId] = 0;
const renderFn = __owl__.renderFn;
__owl__.renderFn = function (comp, params) {
mapping[componentId] = model.rev;
return renderFn(comp, params);
};
model.on("update", component, async (modelRev) => {
if (mapping[componentId] < modelRev) {
mapping[componentId] = modelRev;
await component.render();
}
});
const __destroy = component.__destroy;
component.__destroy = (parent) => {
model.off("update", component);
__destroy.call(component, parent);
};
return model;
}
return {
Model,
useModel,
};
});
|