Skip to content

Commit 4ddfb72

Browse files
committed
Add two events: init and destroy
1 parent 97568c9 commit 4ddfb72

4 files changed

Lines changed: 39 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## v0.18.2
44

5+
- Add two events: `init` and `destroy`
56
- Fix an issue with normalizing RGBA values
67

78
## v0.18.1

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,8 @@ scatterplot.set({ showReticle: true, reticleColor: [1, 0, 0, 0.66] });
572572

573573
| Name | Trigger | Payload |
574574
| -------------------- | ------------------------------------------ | ---------------------------------- |
575+
| init | when the scatter plot is initialized | `undefined` |
576+
| destroy | when the scatter plot is destroyed | `undefined` |
575577
| backgroundImageReady | when the background image was loaded | `undefined` |
576578
| pointOver | when the mouse cursor is over a point | pointIndex |
577579
| pointOut | when the mouse cursor moves out of a point | pointIndex |

src/index.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,13 +2573,16 @@ const createScatterplot = (initialProperties = {}) => {
25732573
// setWidth and setHeight can be async when width or height are set to
25742574
// 'auto'. And since draw() would have anyway been async we can just make
25752575
// all calls async.
2576-
window.requestAnimationFrame(() => {
2577-
if (!canvas) return; // Instance was destroyed in between
2578-
updateViewAspectRatio();
2579-
refresh();
2580-
draw();
2581-
drawHandler();
2582-
});
2576+
return new Promise((resolve) =>
2577+
window.requestAnimationFrame(() => {
2578+
if (!canvas) return; // Instance was destroyed in between
2579+
updateViewAspectRatio();
2580+
refresh();
2581+
draw();
2582+
drawHandler();
2583+
resolve();
2584+
})
2585+
);
25832586
};
25842587

25852588
const hover = (point, showReticleOnce = false) => {
@@ -2744,7 +2747,7 @@ const createScatterplot = (initialProperties = {}) => {
27442747
encodingTex = createEncodingTexture();
27452748

27462749
// Set dimensions
2747-
set({
2750+
const whenSet = set({
27482751
backgroundImage,
27492752
width,
27502753
height,
@@ -2764,6 +2767,10 @@ const createScatterplot = (initialProperties = {}) => {
27642767
canvas.addEventListener('mouseleave', mouseLeaveCanvasHandler, false);
27652768
canvas.addEventListener('click', mouseClickHandler, false);
27662769
canvas.addEventListener('dblclick', mouseDblClickHandler, false);
2770+
2771+
whenSet.then(() => {
2772+
pubSub.publish('init');
2773+
});
27672774
};
27682775

27692776
const destroy = () => {
@@ -2786,6 +2793,7 @@ const createScatterplot = (initialProperties = {}) => {
27862793
pointConnections.destroy();
27872794
reticleHLine.destroy();
27882795
reticleVLine.destroy();
2796+
pubSub.publish('destroy');
27892797
pubSub.clear();
27902798
};
27912799

tests/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,26 @@ test('set({ showReticle, reticleColor })', async (t) => {
975975

976976
/* ---------------------------------- events -------------------------------- */
977977

978+
test('init and destroy events', async (t) => {
979+
const canvas = createCanvas(200, 200);
980+
const scatterplot = createScatterplot({ canvas, width: 200, height: 200 });
981+
982+
const whenInit = new Promise((resolve) =>
983+
scatterplot.subscribe('init', resolve, 1)
984+
);
985+
const whenDestroy = new Promise((resolve) =>
986+
scatterplot.subscribe('destroy', resolve, 1)
987+
);
988+
989+
await whenInit;
990+
991+
scatterplot.destroy();
992+
993+
await whenDestroy;
994+
995+
t.equal(true, true, '"init" and "destroy" event fired');
996+
});
997+
978998
test('tests involving mouse events', async (t2) => {
979999
await t2.test('draw(), clear(), publish("select")', async (t) => {
9801000
const dim = 200;

0 commit comments

Comments
 (0)