Skip to content

Commit 1316afd

Browse files
committed
Make draw() return a promise
1 parent 9362e51 commit 1316afd

3 files changed

Lines changed: 37 additions & 19 deletions

File tree

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,13 @@ _DEPRECATED! Use [`scatterplot.createTextureFromUrl()`](#scatterplot.createTextu
113113

114114
<a name="scatterplot.draw" href="#scatterplot.draw">#</a> scatterplot.<b>draw</b>(<i>points</i>)
115115

116-
Sets and draws `points`, which must be an array of points where a point is interpreted as a quadruple of form `[x, y, category, value]`.
116+
Sets and draws `points`. Note that repeatedly calling this method without specifying `points` will not clear previously set points. To clear points use [`scatterplot.clear()`](#scatterplot.clear)
117+
118+
**Arguments:**
119+
120+
- `points` is an array of quadruples defining the point data. Each quadruple must be of the form `[x, y, category, value]` where `category` and `value` are optional and can be used for [coloring the points](#color-by-value-or-category).
121+
122+
**Returns:** a Promise object that resolves once the points have been drawn.
117123

118124
**Examples:**
119125

@@ -140,17 +146,24 @@ scatterplot.draw(points);
140146
scatterplot.draw();
141147

142148
// Lets actively unset the points. Since `draw()` assumes that you want to
143-
// redraw existing points you have to actively pass in an empty array
149+
// redraw existing points you have to actively pass in an empty array.
150+
// Alternatively, call `scatterplot.clear()`
144151
scatterplot.draw([]);
145152
```
146153

154+
<a name="scatterplot.clear" href="#scatterplot.clear">#</a> scatterplot.<b>clear</b>()
155+
156+
Clears previously drawn points.
157+
147158
<a name="scatterplot.get" href="#scatterplot.set">#</a> scatterplot.<b>get</b>(<i>property</i>)
148159

149160
**Returns:** one of the properties documented in [`set()`](#scatterplotset)
150161

151162
<a name="scatterplot.set" href="#scatterplot.set">#</a> scatterplot.<b>set</b>(<i>properties = {}</i>)
152163

153-
**Arguments:** `properties` is an object of key-value pairs. The list of all understood properties is given below.
164+
**Arguments:**
165+
166+
- `properties` is an object of key-value pairs. The list of all understood properties is given below.
154167

155168
**Properties:**
156169

src/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -879,12 +879,16 @@ const createScatterplot = (initialProperties = {}) => {
879879
if (isViewChanged) pubSub.publish('view', camera.view);
880880
};
881881

882-
const drawRaf = withRaf(draw);
882+
const drawHandler = () => pubSub.publish('draw');
883883

884-
const publicDraw = (newPoints, showRecticleOnce = false) => {
885-
if (newPoints) setPoints(newPoints);
886-
drawRaf(showRecticleOnce);
887-
};
884+
const drawRaf = withRaf(draw, drawHandler);
885+
886+
const publicDraw = (newPoints, showRecticleOnce = false) =>
887+
new Promise((resolve) => {
888+
if (newPoints) setPoints(newPoints);
889+
pubSub.subscribe('draw', resolve, 1);
890+
drawRaf(showRecticleOnce);
891+
});
888892

889893
const withDraw = (f) => (...args) => {
890894
const out = f(...args);
@@ -1150,7 +1154,7 @@ const createScatterplot = (initialProperties = {}) => {
11501154
if (!camera) camera = createDom2dCamera(canvas);
11511155

11521156
if (initialCameraView || initialView) {
1153-
camera.set(mat4.clone(initialCameraView || initialView));
1157+
camera.setView(mat4.clone(initialCameraView || initialView));
11541158
} else if (
11551159
initialCameraTarget ||
11561160
initialTarget ||
@@ -1165,7 +1169,7 @@ const createScatterplot = (initialProperties = {}) => {
11651169
initialCameraRotation || initialRotation || DEFAULT_ROTATION
11661170
);
11671171
} else {
1168-
camera.set(mat4.clone(DEFAULT_VIEW));
1172+
camera.setView(mat4.clone(DEFAULT_VIEW));
11691173
}
11701174
};
11711175

tests/index.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -717,15 +717,15 @@ test('draw(), clear(), publish("select")', async (t) => {
717717
[-1, -1],
718718
[-1, 1],
719719
];
720-
scatterplot.draw(points);
720+
await scatterplot.draw(points);
721721
// The second draw call should not block the drawing of the points!
722722
// This test is related to a previous issue caused by `drawRaf` as `withRaf`
723723
// overwrites previous arguments. While that is normally expected, this
724724
// should not overwrite the points from above
725-
scatterplot.draw();
725+
await scatterplot.draw();
726726

727727
// TODO: fix this!
728-
await wait(250);
728+
// await wait(250);
729729

730730
let selectedPoints = [];
731731
const selectHandler = ({ points: newSelectedPoints }) => {
@@ -781,10 +781,10 @@ test('lasso selection with publish("select")', async (t) => {
781781
[-1, -1],
782782
[-1, 1],
783783
];
784-
scatterplot.draw(points);
784+
await scatterplot.draw(points);
785785

786786
// TODO: fix this!
787-
await wait(250);
787+
// await wait(250);
788788

789789
let selectedPoints = [];
790790
const selectHandler = ({ points: newSelectedPoints }) => {
@@ -843,10 +843,10 @@ test('point hover with publish("pointover") and publish("pointout")', async (t)
843843
[-1, -1],
844844
[-1, 1],
845845
];
846-
scatterplot.draw(points);
846+
await scatterplot.draw(points);
847847

848848
// TODO: fix this!
849-
await wait(250);
849+
// await wait(250);
850850

851851
let hoveredPoint = null;
852852
const pointoverHandler = (point) => {
@@ -913,10 +913,11 @@ test('select()', async (t) => {
913913
[-1, -1],
914914
[-1, 1],
915915
];
916-
scatterplot.draw(points);
916+
917+
await scatterplot.draw(points);
917918

918919
// TODO: fix this!
919-
await wait(250);
920+
// await wait(250);
920921

921922
let selectedPoints = [];
922923
const selectHandler = ({ points: newSelectedPoints }) => {

0 commit comments

Comments
 (0)