Skip to content

Commit

Permalink
Revert "Poll for live data only when page is visible"
Browse files Browse the repository at this point in the history
This reverts commit 8900bd5. We suspect
that this may be making more requests than it is meant to, and it's only
auxiliary to the cache in making the realtime endpoint more performant.
  • Loading branch information
phildarnowsky committed Mar 3, 2020
1 parent 8900bd5 commit 68c0c9c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 88 deletions.
88 changes: 5 additions & 83 deletions apps/site/assets/ts/schedule/__tests__/LineDiagramTest.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { mount, ReactWrapper, shallow, ShallowWrapper } from "enzyme";
import { mount, ReactWrapper } from "enzyme";
import { cloneDeep, merge } from "lodash";
import LineDiagram from "../components/line-diagram/LineDiagram";
import { EnhancedRoute, RouteType } from "../../__v3api";
Expand All @@ -16,21 +16,12 @@ import simpleLiveData from "./lineDiagramData/live-data.json";
const lineDiagram = (simpleLineDiagram as unknown) as LineDiagramStop[];
let lineDiagramBranchingOut = (outwardLineDiagram as unknown) as LineDiagramStop[];

// Mock useInterval to instead call the function immediately
jest.mock("use-interval", () => {
return { useInterval: jest.fn((fn: () => void) => fn()) };
});

// Mock useFetch to return fixture data with a spy-able reload function
const liveDataReload = jest.fn();
const useFetch = jest.fn().mockImplementation(() => ({
data: simpleLiveData,
isLoading: false,
reload: liveDataReload
}));
jest.mock("react-async", () => {
return { useFetch: () => useFetch() };
return {
useFetch: jest.fn(() => ({ data: simpleLiveData, reload: () => {} }))
};
});
jest.mock("use-interval");

const route = {
type: 3 as RouteType,
Expand Down Expand Up @@ -355,72 +346,3 @@ describe("LineDiagram for CR with branches going inward", () => {
expect(moreStops.exists()).toBeTruthy();
});
});

describe("LineDiagram live data", () => {
const animationFrameSpy = jest.spyOn(window, "requestAnimationFrame");
const documentHiddenSpy = jest.spyOn(document, "hidden", "get");

let wrapper: ShallowWrapper;

const mountComponent = () => {
wrapper = shallow(
<LineDiagram
lineDiagram={lineDiagram}
route={route as EnhancedRoute}
directionId={directionId}
routePatternsByDirection={
routePatternsByDirection as RoutePatternsByDirection
}
services={[]}
stops={{ 0: stops, 1: stops }}
today="2019-12-05"
scheduleNote={null}
/>
);
};

it("polls for live data using an animation frame request", () => {
animationFrameSpy.mockImplementation(fn => {
fn(0);
return 1;
});
mountComponent();

expect(liveDataReload).toHaveBeenCalledTimes(1);
});

it("does not poll for live data when no animation frame occurs", () => {
animationFrameSpy.mockImplementation(fn => {
return 1;
});
mountComponent();

expect(liveDataReload).not.toHaveBeenCalled();
});

it("does not poll for live data when the page is not visible", () => {
animationFrameSpy.mockImplementation(fn => {
fn(0);
return 1;
});
documentHiddenSpy.mockReturnValue(true);
mountComponent();

expect(liveDataReload).not.toHaveBeenCalled();
});

it("does not poll for live data when another request is in flight", () => {
animationFrameSpy.mockImplementation(fn => {
fn(0);
return 1;
});
useFetch.mockImplementationOnce(() => ({
data: {},
isLoading: true,
reload: liveDataReload
}));
mountComponent();

expect(liveDataReload).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { ReactElement, useState } from "react";
import { useInterval } from "use-interval";
import { useFetch } from "react-async";
import { useInterval } from "use-interval";

import {
LineDiagramStop,
Expand Down Expand Up @@ -105,11 +105,9 @@ const LineDiagram = ({
{ json: true, watch: directionId }
);
const liveData = (maybeLiveData || {}) as LiveDataByStop;

useInterval(() => {
requestAnimationFrame(() => {
if (!document.hidden && !liveDataIsLoading) reloadLiveData();
});
/* istanbul ignore next */
if (!liveDataIsLoading) reloadLiveData();
}, 15000);

const handleStopClick = (stop: RouteStop): void => {
Expand Down

0 comments on commit 68c0c9c

Please sign in to comment.