Skip to content

Commit bcfa511

Browse files
George KaragkiaourisVadim Demedes
authored andcommitted
Create Renderer class (#64)
1 parent ceaf43e commit bcfa511

2 files changed

Lines changed: 55 additions & 29 deletions

File tree

index.js

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const renderToString = require('./lib/render-to-string');
88
const diff = require('./lib/diff');
99
const h = require('./lib/h');
1010
const Indent = require('./lib/components/indent');
11+
const Renderer = require('./lib/renderer');
1112
const Color = require('./lib/components/color');
1213
const Bold = require('./lib/components/bold');
1314
const Underline = require('./lib/components/underline');
@@ -16,6 +17,7 @@ exports.StringComponent = StringComponent;
1617
exports.Component = Component;
1718
exports.h = h;
1819
exports.Indent = Indent;
20+
exports.Renderer = Renderer;
1921
exports.Color = Color;
2022
exports.Underline = Underline;
2123
exports.Bold = Bold;
@@ -45,32 +47,15 @@ exports.render = (tree, options) => {
4547

4648
const log = logUpdate.create(stdout);
4749

48-
const context = {};
49-
let isUnmounted = false;
50-
let currentTree;
51-
5250
readline.emitKeypressEvents(stdin);
5351

5452
if (stdin.isTTY) {
5553
stdin.setRawMode(true);
5654
}
5755

58-
const update = () => {
59-
const nextTree = build(tree, currentTree, onUpdate, context); // eslint-disable-line no-use-before-define
60-
log(renderToString(nextTree));
61-
62-
currentTree = nextTree;
63-
};
64-
65-
const onUpdate = () => {
66-
if (isUnmounted) {
67-
return;
68-
}
69-
70-
update();
71-
};
72-
73-
update();
56+
const currentTree = new Renderer(tree);
57+
currentTree.on('update', log);
58+
currentTree.update();
7459

7560
const onKeyPress = (ch, key) => {
7661
if (key.name === 'escape' || (key.ctrl && key.name === 'c')) {
@@ -80,7 +65,7 @@ exports.render = (tree, options) => {
8065

8166
if (stdin.isTTY) {
8267
stdin.on('keypress', onKeyPress);
83-
stdout.on('resize', update);
68+
stdout.on('resize', currentTree.update);
8469
}
8570

8671
const consoleMethods = ['dir', 'log', 'info', 'warn', 'error'];
@@ -92,7 +77,7 @@ exports.render = (tree, options) => {
9277
log.clear();
9378
log.done();
9479
originalFn.apply(console, args);
95-
update();
80+
currentTree.update();
9681
};
9782

9883
console[method].restore = () => {
@@ -101,19 +86,14 @@ exports.render = (tree, options) => {
10186
});
10287

10388
const exit = () => {
104-
if (isUnmounted) {
105-
return;
106-
}
107-
10889
if (stdin.isTTY) {
10990
stdin.setRawMode(false);
11091
stdin.removeListener('keypress', onKeyPress);
11192
stdin.pause();
112-
stdout.removeListener('resize', update);
93+
stdout.removeListener('resize', currentTree.update);
11394
}
11495

115-
isUnmounted = true;
116-
build(null, currentTree, onUpdate, context); // eslint-disable-line no-use-before-define
96+
currentTree.unmount();
11797
log.done();
11898

11999
consoleMethods.forEach(method => console[method].restore());

lib/renderer.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
const {EventEmitter} = require('events');
4+
const renderToString = require('./render-to-string');
5+
const diff = require('./diff');
6+
7+
const noop = () => {};
8+
9+
const build = (nextTree, prevTree, onUpdate = noop, context = {}) => {
10+
return diff(prevTree, nextTree, onUpdate, context);
11+
};
12+
13+
class Renderer extends EventEmitter {
14+
constructor(tree) {
15+
super();
16+
17+
this.tree = tree;
18+
this.context = {};
19+
this.isUnmounted = false;
20+
this.update = this.update.bind(this);
21+
this.onUpdate = this.onUpdate.bind(this);
22+
}
23+
24+
update() {
25+
const nextTree = build(this.tree, this.currentTree, this.onUpdate, this.context);
26+
27+
this.emit('update', renderToString(nextTree));
28+
this.currentTree = nextTree;
29+
}
30+
31+
onUpdate() {
32+
if (!this.isUnmounted) {
33+
this.update();
34+
}
35+
}
36+
37+
unmount() {
38+
if (!this.isUnmounted) {
39+
this.isUnmounted = true;
40+
41+
build(null, this.currentTree);
42+
}
43+
}
44+
}
45+
46+
module.exports = Renderer;

0 commit comments

Comments
 (0)