-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reduce global redundant code (#1872)
* refactor: reduce global redundant code * chore: add changeset
- Loading branch information
Showing
22 changed files
with
348 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
'@antv/g-plugin-device-renderer': patch | ||
'@antv/g-plugin-image-loader': patch | ||
'@antv/g-plugin-svg-renderer': patch | ||
'@antv/g-image-exporter': patch | ||
'@antv/g-mobile-svg': patch | ||
'@antv/g-canvaskit': patch | ||
'@antv/g-canvas': patch | ||
'@antv/g-webgpu': patch | ||
'@antv/g-webgl': patch | ||
'@antv/g-lite': patch | ||
'@antv/g-svg': patch | ||
--- | ||
|
||
refactor: reduce global redundant code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { | ||
Canvas, | ||
ElementEvent, | ||
Rect, | ||
Group, | ||
CustomEvent, | ||
EventTarget, | ||
} from '@antv/g'; | ||
import * as tinybench from 'tinybench'; | ||
|
||
export async function event(context: { canvas: Canvas }) { | ||
const { canvas } = context; | ||
console.log(canvas); | ||
|
||
await canvas.ready; | ||
|
||
const { width, height } = canvas.getConfig(); | ||
const root = new Group(); | ||
let count = 2e4; | ||
let rects = []; | ||
let eventTargets = []; | ||
|
||
function render() { | ||
root.destroyChildren(); | ||
rects = []; | ||
|
||
for (let i = 0; i < count; i++) { | ||
const x = Math.random() * width; | ||
const y = Math.random() * height; | ||
const size = 10 + Math.random() * 40; | ||
const speed = 1 + Math.random(); | ||
|
||
const rect = new Rect({ | ||
style: { | ||
x, | ||
y, | ||
width: size, | ||
height: size, | ||
fill: 'white', | ||
stroke: '#000', | ||
lineWidth: 1, | ||
}, | ||
}); | ||
root.appendChild(rect); | ||
rects[i] = { x, y, size, speed, el: rect }; | ||
|
||
// --- | ||
const eventTarge = new EventTarget(); | ||
eventTargets.push(eventTarge); | ||
eventTarge.addEventListener(ElementEvent.BOUNDS_CHANGED, () => { | ||
// | ||
}); | ||
} | ||
} | ||
|
||
render(); | ||
canvas.appendChild(root); | ||
|
||
// benchmark | ||
// ---------- | ||
const boundsChangedEvent = new CustomEvent(ElementEvent.BOUNDS_CHANGED); | ||
boundsChangedEvent.detail = { affectChildren: true }; | ||
|
||
const bench = new tinybench.Bench({ name: 'event benchmark', time: 1e2 }); | ||
|
||
bench.add('Iterating trigger events', async () => { | ||
root.forEach((el) => { | ||
el.dispatchEvent(boundsChangedEvent); | ||
}); | ||
}); | ||
bench.add('Iterating trigger events without propagate', async () => { | ||
root.forEach((el) => { | ||
el.dispatchEvent(boundsChangedEvent, true); | ||
}); | ||
}); | ||
bench.add('Iterating over trigger events on empty objects', async () => { | ||
eventTargets.forEach((el) => { | ||
el.dispatchEvent(boundsChangedEvent); | ||
}); | ||
}); | ||
bench.add('Batch triggering events', async () => { | ||
canvas.dispatchEvent(boundsChangedEvent, true); | ||
}); | ||
|
||
await bench.run(); | ||
|
||
console.log(bench.name); | ||
console.table(bench.table()); | ||
console.log(bench.results); | ||
console.log(bench.tasks); | ||
|
||
// ---------- | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.