Skip to content

Commit

Permalink
fix: render error when pages changes quickly
Browse files Browse the repository at this point in the history
  • Loading branch information
Flyer3d committed Mar 11, 2024
1 parent 1403788 commit 2d1c994
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 24 deletions.
11 changes: 0 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
},
"dependencies": {
"classnames-es": "^2.2.6",
"d3-queue": "~3.0.7",
"debug": "^4.1.0",
"lodash": "^4.17.20",
"query-string": "^6.2.0",
Expand Down
11 changes: 11 additions & 0 deletions packages/drawing/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/drawing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
"build": "rm -rf ./es/* && BABEL_ENV=es rollup --config ./rollup.config.js index.js"
},
"dependencies": {
"abortcontroller-polyfill": "^1.7.5",
"classnames-es": "^2.2.6",
"hammerjs": "~2.0.8",
"lodash": "~4.17.21"
},
"peerDependencies": {
"react": "^16.14",
"fabric": "^5.3.0"
"fabric": "^5.3.0",
"react": "^16.14"
}
}
65 changes: 55 additions & 10 deletions packages/drawing/src/drawing.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import React from 'react'
import { fabric } from 'fabric/dist/fabric.min'
import Hammer from 'hammerjs'
import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'

import {
penToolModeEnum,
Expand Down Expand Up @@ -60,6 +61,9 @@ function clearExternalSelection () {
}
}

let abortController = null
let signal = null;

Check failure on line 65 in packages/drawing/src/drawing.jsx

View workflow job for this annotation

GitHub Actions / check / check

Extra semicolon

export class Drawing extends React.Component {
constructor (props) {
super(props)
Expand Down Expand Up @@ -1081,32 +1085,73 @@ export class Drawing extends React.Component {
}
}

createCanvasObjects = (pageObjects) => {
this.clearCanvasObjects()
_abortableCreateObjectsPromise = (pageObjects) => {

Check failure on line 1088 in packages/drawing/src/drawing.jsx

View workflow job for this annotation

GitHub Actions / check / check

Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`
return new Promise((resolve, reject) => {

Check warning on line 1089 in packages/drawing/src/drawing.jsx

View workflow job for this annotation

GitHub Actions / check / check

'reject' is defined but never used
let shouldAbort = false
signal.addEventListener('abort', () => {

Check failure on line 1091 in packages/drawing/src/drawing.jsx

View workflow job for this annotation

GitHub Actions / check / check

Expected blank line before this statement
shouldAbort = true
resolve()
});

Check failure on line 1094 in packages/drawing/src/drawing.jsx

View workflow job for this annotation

GitHub Actions / check / check

Extra semicolon

if (pageObjects.length) {
const normalizedObjects = pageObjects.map(_ => normalizeFields({ ..._, remote: true })).filter(_ => !_._removed)
const normalizedObjects = pageObjects.map(_ => normalizeFields({..._, remote: true})).filter(_ => !_._removed)

Check failure on line 1096 in packages/drawing/src/drawing.jsx

View workflow job for this annotation

GitHub Actions / check / check

A space is required after '{'

Check failure on line 1096 in packages/drawing/src/drawing.jsx

View workflow job for this annotation

GitHub Actions / check / check

A space is required before '}'

if(shouldAbort) return

Check failure on line 1098 in packages/drawing/src/drawing.jsx

View workflow job for this annotation

GitHub Actions / check / check

Expected space(s) after "if"
fabric.util.enlivenObjects(normalizedObjects, (enlivenedObjects) => {
// Есть ситуации, когда во время выполнения enlivenObjects this.canvas уже нет
if (!this.canvas) return
if(shouldAbort) return

Check failure on line 1101 in packages/drawing/src/drawing.jsx

View workflow job for this annotation

GitHub Actions / check / check

Expected space(s) after "if"

this.canvas.renderOnAddRemove = false
if (!this.canvas) {
resolve()
return

Check failure on line 1105 in packages/drawing/src/drawing.jsx

View workflow job for this annotation

GitHub Actions / check / check

Expected blank line before this statement
}

enlivenedObjects.forEach((object) => {
// Есть ситуации, когда во время выполнения enlivenObjects this.canvas уже нет
if (!this.canvas) return
if (!this.canvas) {
resolve()
return

Check failure on line 1112 in packages/drawing/src/drawing.jsx

View workflow job for this annotation

GitHub Actions / check / check

Expected blank line before this statement
}

this._fixObjectInteractivity(object)

this.canvas._objectsMap.set(object._id, object)
})

if(shouldAbort) return
this.canvas.renderOnAddRemove = false
this.canvas.add(...enlivenedObjects)

this.canvas.renderOnAddRemove = true
this.canvas.requestRenderAll()

if(shouldAbort) { // Если дошли до этого места и прервали загрузку объектов - надо удалить добавленные объекты
this.canvas.remove(...enlivenedObjects)
return
}

resolve()
})
})
}

createCanvasObjects = (pageObjects) => {
if (abortController) {
abortController.abort()
signal = null
abortController = null
}

abortController = new window.AbortController();
signal = abortController.signal;

this.clearCanvasObjects()
if (pageObjects.length) {
this._abortableCreateObjectsPromise(pageObjects)
.then(() => {
this.canvas.requestRenderAll()
}
)
.finally(() => {
signal = null
abortController = null
})
}
}

Expand Down

0 comments on commit 2d1c994

Please sign in to comment.