Skip to content

Commit

Permalink
fix(print): prevent flickering by using debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Mollweide committed May 5, 2018
1 parent 1e2226e commit d552720
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"cross-spawn": "6.0.5",
"glob": "7.1.2",
"keypress": "0.2.1",
"lodash.debounce": "4.0.8",
"string-length": "2.0.0"
},
"scripts": {
Expand Down
18 changes: 12 additions & 6 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
'use strict';
/* eslint require-jsdoc: 0*/
const debounce = require('lodash.debounce');
const { program } = require('../commander');

const cmdPrefix = cmd => `lerna-terminal${cmd ? `/${cmd}` : ''}~$ `;

const print = debounce((value, stdin) => {
/* istanbul ignore next */
if (process.env.NODE_ENV !== 'ci' && process.env.NODE_ENV !== 'test') {
process.stdout.write('\x1Bc');
}
process.stdout.write(value);
process.stdout.write(stdin);
}, 17);

const state = {};
const uiState = {
prefix: cmdPrefix(''),
Expand All @@ -20,12 +30,8 @@ const uiState = {
this.prefix = cmdPrefix('');
}
this.buffer = value;
/* istanbul ignore next */
if (process.env.NODE_ENV !== 'ci' && process.env.NODE_ENV !== 'test') {
process.stdout.write('\x1Bc');
}
process.stdout.write(value);
process.stdout.write(this.prefix + this.entered);

print(value, this.prefix + this.entered);
},
clearBuffer() {
this.buffer = '';
Expand Down
33 changes: 21 additions & 12 deletions src/store/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,37 @@ describe('provideStore', () => {
uiState.onChange('lala');
expect(uiState.entered).toBe('lala');
});
it('getUiState().print', () => {
it('getUiState().print', done => {
const uiState = Object.assign({}, getUiState());
uiState.onChange('focus');
uiState.print('printValue');
expect(uiState.buffer).toBe('printValue');
expect(uiState.entered).toBe('focus');
expect(uiState.prefix).toBe('lerna-terminal~$ ');
setTimeout(() => {
expect(uiState.buffer).toBe('printValue');
expect(uiState.entered).toBe('focus');
expect(uiState.prefix).toBe('lerna-terminal~$ ');
done();
}, 20);
});
it('getUiState().print -> focused', () => {
it('getUiState().print -> focused', done => {
const uiState = Object.assign({}, getUiState(), {
focus: 'focusedPackage',
});
uiState.print('printValue');
expect(uiState.buffer).toBe('printValue');
expect(uiState.entered).toBe('');
expect(uiState.prefix).toBe('lerna-terminal/focusedPackage~$ ');
setTimeout(() => {
expect(uiState.buffer).toBe('printValue');
expect(uiState.entered).toBe('');
expect(uiState.prefix).toBe('lerna-terminal/focusedPackage~$ ');
done();
}, 20);
});
it('getUiState().clearBuffer', () => {
it('getUiState().clearBuffer', done => {
const uiState = Object.assign({}, getUiState());
uiState.print('printValue');
expect(uiState.buffer).toBe('printValue');
uiState.clearBuffer();
expect(uiState.buffer).toBe('');
setTimeout(() => {
expect(uiState.buffer).toBe('printValue');
uiState.clearBuffer();
expect(uiState.buffer).toBe('');
done();
}, 20);
});
});

0 comments on commit d552720

Please sign in to comment.