Skip to content

Commit 3f05d2a

Browse files
[O2B-1024] Create stateful components (#1283)
1 parent a86f456 commit 3f05d2a

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @license
3+
* Copyright CERN and copyright holders of ALICE O2. This software is
4+
* distributed under the terms of the GNU General Public License v3 (GPL
5+
* Version 3), copied verbatim in the file "COPYING".
6+
*
7+
* See http://alice-o2.web.cern.ch/license for full licensing information.
8+
*
9+
* In applying this license CERN does not waive the privileges and immunities
10+
* granted to it by virtue of its status as an Intergovernmental Organization
11+
* or submit itself to any jurisdiction.
12+
*/
13+
import { Observable } from '/js/src/index.js';
14+
15+
// No static property in es2020, using module encapsulation instead
16+
let _renderer = null;
17+
18+
/**
19+
* Specific component that can triggers re-render on its own when its property changes
20+
*
21+
* Some components do not really require models, for example dropdowns. However, we need a way for these components to trigger re-render
22+
* without having to pass a model all the way along to it. This is the purpose of this component: calling their notify function will trigger
23+
* a re-render
24+
* At the application startup, a renderer should be provided in order for the component to trigger re-rendering
25+
*/
26+
export class StatefulComponent extends Observable {
27+
/**
28+
* Set the current renderer that the stateful components should notify when their state changed
29+
*
30+
* @param {{notify: function}} renderer the renderer to use
31+
* @return {void}
32+
*/
33+
static useRenderer(renderer) {
34+
_renderer = renderer;
35+
}
36+
37+
// eslint-disable-next-line valid-jsdoc
38+
/**
39+
* @inheritDoc
40+
*/
41+
notify() {
42+
super.notify();
43+
if (_renderer) {
44+
_renderer.notify();
45+
}
46+
}
47+
}

lib/public/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { mount } from '/js/src/index.js';
2222
import view from './view.js';
2323
import Model from './Model.js';
2424
import { markdownEngine } from './components/common/markdown/markdownEngine.js';
25+
import { StatefulComponent } from './components/common/StatefulComponent.js';
2526

2627
/* global HyperMD CompleteEmoji */
2728

@@ -34,3 +35,6 @@ mount(document.body, view, model, debug);
3435

3536
// Expose model to interact with it the browser's console
3637
window.model = model;
38+
39+
// Register the model for the stateful components
40+
StatefulComponent.useRenderer(model);

0 commit comments

Comments
 (0)