Skip to content

Commit

Permalink
fix(generator): Prevent possible inifinite loop when tab isn't visible
Browse files Browse the repository at this point in the history
Add bulletproof code, when tab isn't visible terminate the loop.

Ref naver#2606
  • Loading branch information
netil committed Mar 25, 2022
1 parent 1b63a75 commit 0e50645
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/module/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
import {d3Transition} from "../../types/types";
import {window} from "./browser";
import {isArray} from "./util";
import {isArray, isTabVisible} from "./util";

const {setTimeout, clearTimeout} = window;

Expand Down Expand Up @@ -66,6 +66,12 @@ export function generateWait() {
continue;
}

// when tab isn't visible exit loop
if (isTabVisible() === false) {
done = transitionsToWait.length;
break;
}

try {
t.transition();
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/module/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ function parseDate(date: Date | string | number | any): Date {
* @private
*/
function isTabVisible(): boolean {
return !document.hidden;
return document?.hidden === false || document?.visibilityState === "visible";
}

/**
Expand Down
52 changes: 52 additions & 0 deletions test/internals/generator-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (c) 2017 ~ present NAVER Corp.
* billboard.js project is licensed under the MIT license
*/
/* eslint-disable */
import {expect} from "chai";
import sinon from "sinon";
import util from "../assets/util";
import {window} from "../../src/module/browser";
import {isTabVisible} from "../../src/module/util";

describe("GENERATOR", function() {
let chart;
let args;

beforeEach(() => {
chart = util.generate(args);
});

describe("generateWait()", () => {
const spy = sinon.spy();

before(() => {
args = {
data: {
columns: [
["data1", 30, 200, 100, 400, 150, 250],
["data2", 50, 20, 10, 40, 15, 25],
["data3", 150, 120, 110, 140, 115, 125]
]
},
onrendered: spy
};
});

it("should onrendered callback called, when tab isn't visible", done => {
window.$$TEST$$.isTabVisible = false;

// when
chart.toggle(["data1", "data2"]);

setTimeout(() => {
expect(isTabVisible()).to.be.false;
expect(spy.called).to.be.true;

// restore
delete window.$$TEST$$.isTabVisible;
done();
}, 500);
});
});
});

0 comments on commit 0e50645

Please sign in to comment.