Skip to content

Commit f957e1c

Browse files
committed
Fix Mouse.postData error if called before project loads
1 parent 4eb9117 commit f957e1c

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/io/mouse.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,19 @@ class Mouse {
101101
if (!(data.x > 0 && data.x < data.canvasWidth &&
102102
data.y > 0 && data.y < data.canvasHeight)) return;
103103

104+
// target will not exist if project is still loading
104105
const target = this._pickTarget(data.x, data.y);
105-
const isNewMouseDown = !previousDownState && this._isDown;
106-
const isNewMouseUp = previousDownState && !this._isDown;
107-
108-
// Draggable targets start click hats on mouse up.
109-
// Non-draggable targets start click hats on mouse down.
110-
if (target.draggable && isNewMouseUp) {
111-
this._activateClickHats(target);
112-
} else if (!target.draggable && isNewMouseDown) {
113-
this._activateClickHats(target);
106+
if (target) {
107+
const isNewMouseDown = !previousDownState && this._isDown;
108+
const isNewMouseUp = previousDownState && !this._isDown;
109+
110+
// Draggable targets start click hats on mouse up.
111+
// Non-draggable targets start click hats on mouse down.
112+
if (target.draggable && isNewMouseUp) {
113+
this._activateClickHats(target);
114+
} else if (!target.draggable && isNewMouseDown) {
115+
this._activateClickHats(target);
116+
}
114117
}
115118
}
116119
}

test/unit/tw_mouse.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const {test} = require('tap');
2+
const Runtime = require('../../src/engine/runtime');
3+
4+
test('isDown does not error before project loads', t => {
5+
const rt = new Runtime();
6+
rt.ioDevices.mouse.postData({
7+
isDown: true,
8+
x: 20,
9+
y: 20,
10+
canvasWidth: 100,
11+
canvasHeight: 100
12+
});
13+
t.end();
14+
});

0 commit comments

Comments
 (0)