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

Fix showing file contents for record item #2455

Merged
merged 6 commits into from
Jun 19, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 33 additions & 9 deletions frontend/src/components/worksheets/items/RecordItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import * as React from 'react';
import * as Mousetrap from '../../../util/ws_mousetrap_fork';
import BundleDetail from '../BundleDetail';
import NewRun from '../NewRun';
import { worksheetItemPropsChanged } from '../../../util/worksheet_utils';
import { useEffect } from 'react';
import { fetchAsyncBundleContents } from '../../../util/async_loading_utils';
import { FETCH_STATUS_SCHEMA } from '../../../constants';

class RecordItem extends React.Component {
/** Constructor. */
Expand All @@ -28,13 +30,6 @@ class RecordItem extends React.Component {
this.setState({ bundleInfoUpdates: { ...bundleInfoUpdates, ...update } });
};

shouldComponentUpdate(nextProps, nextState) {
return (
worksheetItemPropsChanged(this.props, nextProps) ||
yipenghe marked this conversation as resolved.
Show resolved Hide resolved
this.state.showDetail !== nextState.showDetail
);
}

rerunItem = (runProp) => {
this.setState({
showDetail: false,
Expand Down Expand Up @@ -130,4 +125,33 @@ class RecordItem extends React.Component {
}
}

export default RecordItem;
const RecordWrapper = (props) => {
const { item, onAsyncItemLoad } = props;
useEffect(() => {
(async function() {
if (item.status.code === FETCH_STATUS_SCHEMA.BRIEFLY_LOADED) {
try {
const { contents } = await fetchAsyncBundleContents({ contents: item.rows });
onAsyncItemLoad({
...item,
rows: contents,
status: {
code: FETCH_STATUS_SCHEMA.READY,
error_message: '',
},
});
} catch (e) {
console.error(e);
// TODO: better error message handling here.
}
}
})();
// TODO: see how we can add onAsyncItemLoad as a dependency, if needed.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [item.rows, item.status]);
return <RecordItem {...props} />;
};

export default RecordWrapper;

// export default RecordItem;
24 changes: 2 additions & 22 deletions frontend/src/components/worksheets/items/TableItem/TableItem.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
// @flow
import React, { useEffect } from 'react';
import $ from 'jquery';
import { withStyles } from '@material-ui/core';
import Table from '@material-ui/core/Table';
import TableHead from '@material-ui/core/TableHead';
import TableCell from './TableCell';
import TableRow from '@material-ui/core/TableRow';
import BundleRow from './BundleRow';
import Checkbox from '@material-ui/core/Checkbox';
import CheckBoxOutlineBlankIcon from '@material-ui/icons/CheckBoxOutlineBlank';
import SvgIcon from '@material-ui/core/SvgIcon';
import CheckBoxIcon from '@material-ui/icons/CheckBox';
import { getIds } from '../../../../util/worksheet_utils';
import { semaphore } from '../../../../util/async_loading_utils';
import { fetchAsyncBundleContents } from '../../../../util/async_loading_utils';
import { FETCH_STATUS_SCHEMA } from '../../../../constants';

class TableItem extends React.Component<{
Expand Down Expand Up @@ -233,28 +228,13 @@ const styles = (theme) => ({

const TableContainer = withStyles(styles)(_TableContainer);

async function fetchAsyncTableContents({ contents }) {
return semaphore.use(async () => {
const response = await $.ajax({
type: 'POST',
contentType: 'application/json',
url: '/rest/interpret/genpath-table-contents',
async: true,
data: JSON.stringify({ contents }),
dataType: 'json',
cache: false,
});
return response;
});
}

const TableWrapper = (props) => {
const { item, onAsyncItemLoad } = props;
useEffect(() => {
(async function() {
if (item.status.code === FETCH_STATUS_SCHEMA.BRIEFLY_LOADED) {
try {
const { contents } = await fetchAsyncTableContents({ contents: item.rows });
const { contents } = await fetchAsyncBundleContents({ contents: item.rows });
onAsyncItemLoad({
...item,
rows: contents,
Expand Down
17 changes: 17 additions & 0 deletions frontend/src/util/async_loading_utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import { Semaphore } from 'await-semaphore';
import $ from 'jquery';

// Limit concurrent requests for async resolving items
const MAX_CONCURRENT_REQUESTS = 3;
export const semaphore = new Semaphore(MAX_CONCURRENT_REQUESTS);

export async function fetchAsyncBundleContents({ contents }) {
// used in table and record items
return semaphore.use(async () => {
const response = await $.ajax({
type: 'POST',
contentType: 'application/json',
url: '/rest/interpret/genpath-table-contents',
async: true,
data: JSON.stringify({ contents }),
dataType: 'json',
cache: false,
});
return response;
});
}