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

Bug(#111): fix initial render #112

Merged
merged 2 commits into from
Jun 26, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-gojs",
"version": "4.5.0",
"version": "4.6.0",
"description": "GoJS React integration",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
9 changes: 5 additions & 4 deletions src/GojsDiagram.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ModelChangeEventType } from './modelChangeEvent';

const groupName = 'myGroup';
const singleNode = 'singleNode';

jest.useFakeTimers();
describe('<GojsDiagram />', () => {
const portFrom = 'R';
const portTo = 'L';
Expand Down Expand Up @@ -96,6 +96,8 @@ describe('<GojsDiagram />', () => {
let keyIndex = 0;

beforeEach(() => {
Object.defineProperty(Element.prototype, 'clientWidth', { value: 100 });
Object.defineProperty(Element.prototype, 'clientHeight', { value: 100 });
keyIndex = 0;
const dom = document.body;
modelChangeCallback = jest.fn();
Expand Down Expand Up @@ -133,6 +135,7 @@ describe('<GojsDiagram />', () => {
/>,
{ attachTo: dom }
);
jest.runAllTimers();
});

it('should default to "category" for nodeCategoryProperty', () => {
Expand Down Expand Up @@ -223,9 +226,7 @@ describe('<GojsDiagram />', () => {

testCases.forEach(test => {
// tslint:disable-next-line:max-line-length
it(`should update the render of the diagram (nodes and links) based on the new model provided as prop - case: ${
test.description
}`, () => {
it(`should update the render of the diagram (nodes and links) based on the new model provided as prop - case: ${test.description}`, () => {
checkIfDiagramRendersModel(model, diagram);
wrapper.setProps({ model: test.updatedModel });
checkIfDiagramRendersModel(test.updatedModel, diagram);
Expand Down
29 changes: 25 additions & 4 deletions src/GojsDiagram.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export interface GojsModel extends go.Model {
class GojsDiagram<N extends BaseNodeModel, L extends LinkModel> extends React.PureComponent<GojsDiagramProps<N, L>>
implements DiagramNotificationDelegate<N, L> {
private myDiagram: Diagram;
private divRef: React.RefObject<HTMLDivElement>;

private mountInterval;

private eventsToDispatch: ModelChangeEvent<N, L>[];
private modelChangedHandlers = [
new AddNodeModelChangedHandler<N, L>(),
Expand All @@ -55,19 +59,36 @@ class GojsDiagram<N extends BaseNodeModel, L extends LinkModel> extends React.Pu

constructor(props: GojsDiagramProps<N, L>) {
super(props);
this.divRef = React.createRef();
this.eventsToDispatch = [];
this.modelChangedHandler = this.modelChangedHandler.bind(this);
}

componentDidMount() {
this.init();
let intervalCount = 0;
this.mountInterval = setInterval(() => {
if (this.divRef.current && this.divRef.current.clientWidth && this.divRef.current.clientHeight) {
this.init();
clearInterval(this.mountInterval);
} else {
if (intervalCount > 10) {
clearInterval(this.mountInterval);
}
intervalCount++;
}
}, 10);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if the machine is slow? (big graph to display)
maybe the worst case should be around ~10s.

}

componentWillUnmount() {
if (this.props.onModelChange) {
if (this.myDiagram && this.props.onModelChange) {
this.myDiagram.removeModelChangedListener(this.modelChangedHandler);
}
this.myDiagram.clear();
if (this.myDiagram) {
this.myDiagram.clear();
}
if (this.mountInterval) {
clearInterval(this.mountInterval);
}
}

componentDidUpdate() {
Expand Down Expand Up @@ -109,7 +130,7 @@ class GojsDiagram<N extends BaseNodeModel, L extends LinkModel> extends React.Pu
}
}
render() {
return <div id={this.props.diagramId} className={this.props.className} />;
return <div ref={this.divRef} id={this.props.diagramId} className={this.props.className} />;
}

enqueueEvent(event: ModelChangeEvent<N, L>) {
Expand Down