Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rate limit Viewport.refresh #445

Merged
merged 3 commits into from
Jan 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Viewport.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { assert } from 'chai';
import { Viewport } from './Viewport';

class MockWindow {
// Disable refreshLoop in test
public requestAnimationFrame() { }
}

describe('Viewport', () => {
var terminal;
var viewportElement;
Expand All @@ -11,6 +16,7 @@ describe('Viewport', () => {
const CHARACTER_HEIGHT = 10;

beforeEach(() => {
(<any>global).window = new MockWindow();
terminal = {
lines: [],
rows: 0,
Expand Down Expand Up @@ -73,10 +79,14 @@ describe('Viewport', () => {
terminal.rows = 1;
assert.equal(scrollAreaElement.style.height, 0 * CHARACTER_HEIGHT + 'px');
viewport.syncScrollArea();
assert.ok(viewport.isRefreshQueued);
viewport.refresh();
assert.equal(viewportElement.style.height, 1 * CHARACTER_HEIGHT + 'px');
assert.equal(scrollAreaElement.style.height, 1 * CHARACTER_HEIGHT + 'px');
terminal.lines.push('');
viewport.syncScrollArea();
assert.ok(viewport.isRefreshQueued);
viewport.refresh();
assert.equal(viewportElement.style.height, 1 * CHARACTER_HEIGHT + 'px');
assert.equal(scrollAreaElement.style.height, 2 * CHARACTER_HEIGHT + 'px');
});
Expand Down
20 changes: 17 additions & 3 deletions src/Viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class Viewport {
private currentRowHeight: number;
private lastRecordedBufferLength: number;
private lastRecordedViewportHeight: number;
private isRefreshQueued: boolean;

/**
* Creates a new Viewport.
Expand All @@ -29,12 +30,25 @@ export class Viewport {
this.currentRowHeight = 0;
this.lastRecordedBufferLength = 0;
this.lastRecordedViewportHeight = 0;
this.isRefreshQueued = false;

this.terminal.on('scroll', this.syncScrollArea.bind(this));
this.terminal.on('resize', this.syncScrollArea.bind(this));
this.viewportElement.addEventListener('scroll', this.onScroll.bind(this));

this.syncScrollArea();
this.refreshLoop();
}

/**
* Queues a refresh to be done on next animation frame.
*/
private refreshLoop(): void {
if (this.isRefreshQueued) {
this.refresh();
this.isRefreshQueued = false;
}
window.requestAnimationFrame(this.refreshLoop.bind(this));
}

/**
Expand Down Expand Up @@ -68,15 +82,15 @@ export class Viewport {
if (this.lastRecordedBufferLength !== this.terminal.lines.length) {
// If buffer height changed
this.lastRecordedBufferLength = this.terminal.lines.length;
this.refresh();
this.isRefreshQueued = true;
} else if (this.lastRecordedViewportHeight !== this.terminal.rows) {
// If viewport height changed
this.refresh();
this.isRefreshQueued = true;
} else {
// If size has changed, refresh viewport
var size = this.charMeasureElement.getBoundingClientRect();
if (size.height !== this.currentRowHeight) {
this.refresh(size);
this.isRefreshQueued = true;
}
}

Expand Down