Skip to content

Commit 2f7052a

Browse files
committed
Fix some more async issues
1 parent d1887a2 commit 2f7052a

File tree

4 files changed

+34
-39
lines changed

4 files changed

+34
-39
lines changed

src/io/files.js

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ import Renderer from '../core/p5.Renderer';
240240
* </code>
241241
* </div>
242242
*/
243-
p5.prototype.loadJSON = function (...args) {
243+
p5.prototype.loadJSON = async function (...args) {
244244
p5._validateParameters('loadJSON', args);
245245
const path = args[0];
246246
let callback;
@@ -266,8 +266,7 @@ p5.prototype.loadJSON = function (...args) {
266266
}
267267
}
268268

269-
const self = this;
270-
this.httpDo(
269+
await new Promise(resolve => this.httpDo(
271270
path,
272271
'GET',
273272
options,
@@ -280,7 +279,7 @@ p5.prototype.loadJSON = function (...args) {
280279
callback(resp);
281280
}
282281

283-
self._decrementPreload();
282+
resolve()
284283
},
285284
err => {
286285
// Error handling
@@ -292,7 +291,7 @@ p5.prototype.loadJSON = function (...args) {
292291
throw err;
293292
}
294293
}
295-
);
294+
));
296295

297296
return ret;
298297
};
@@ -431,7 +430,7 @@ p5.prototype.loadJSON = function (...args) {
431430
* </code>
432431
* </div>
433432
*/
434-
p5.prototype.loadStrings = function (...args) {
433+
p5.prototype.loadStrings = async function (...args) {
435434
p5._validateParameters('loadStrings', args);
436435

437436
const ret = [];
@@ -448,8 +447,7 @@ p5.prototype.loadStrings = function (...args) {
448447
}
449448
}
450449

451-
const self = this;
452-
p5.prototype.httpDo.call(
450+
await new Promise(resolve => p5.prototype.httpDo.call(
453451
this,
454452
args[0],
455453
'GET',
@@ -476,7 +474,7 @@ p5.prototype.loadStrings = function (...args) {
476474
callback(ret);
477475
}
478476

479-
self._decrementPreload();
477+
resolve()
480478
},
481479
function (err) {
482480
// Error handling
@@ -488,7 +486,7 @@ p5.prototype.loadStrings = function (...args) {
488486
throw err;
489487
}
490488
}
491-
);
489+
));
492490

493491
return ret;
494492
};
@@ -563,7 +561,7 @@ p5.prototype.loadStrings = function (...args) {
563561
* </code>
564562
* </div>
565563
*/
566-
p5.prototype.loadTable = function (path) {
564+
p5.prototype.loadTable = async function (path) {
567565
// p5._validateParameters('loadTable', arguments);
568566
let callback;
569567
let errorCallback;
@@ -604,8 +602,7 @@ p5.prototype.loadTable = function (path) {
604602

605603
const t = new p5.Table();
606604

607-
const self = this;
608-
this.httpDo(
605+
await new Promise(resolve => this.httpDo(
609606
path,
610607
'GET',
611608
'table',
@@ -737,7 +734,7 @@ p5.prototype.loadTable = function (path) {
737734
callback(t);
738735
}
739736

740-
self._decrementPreload();
737+
resolve()
741738
},
742739
err => {
743740
// Error handling
@@ -749,7 +746,7 @@ p5.prototype.loadTable = function (path) {
749746
console.error(err);
750747
}
751748
}
752-
);
749+
));
753750

754751
return t;
755752
};
@@ -929,7 +926,7 @@ function makeObject(row, headers) {
929926
* </code>
930927
* </div>
931928
*/
932-
p5.prototype.loadXML = function (...args) {
929+
p5.prototype.loadXML = async function (...args) {
933930
const ret = new p5.XML();
934931
let callback, errorCallback;
935932

@@ -944,8 +941,7 @@ p5.prototype.loadXML = function (...args) {
944941
}
945942
}
946943

947-
const self = this;
948-
this.httpDo(
944+
await new Promise(resolve => this.httpDo(
949945
args[0],
950946
'GET',
951947
'xml',
@@ -957,7 +953,7 @@ p5.prototype.loadXML = function (...args) {
957953
callback(ret);
958954
}
959955

960-
self._decrementPreload();
956+
resolve()
961957
},
962958
function (err) {
963959
// Error handling
@@ -969,7 +965,7 @@ p5.prototype.loadXML = function (...args) {
969965
throw err;
970966
}
971967
}
972-
);
968+
));
973969

974970
return ret;
975971
};
@@ -1000,11 +996,10 @@ p5.prototype.loadXML = function (...args) {
1000996
* }
1001997
* </code></div>
1002998
*/
1003-
p5.prototype.loadBytes = function (file, callback, errorCallback) {
999+
p5.prototype.loadBytes = async function (file, callback, errorCallback) {
10041000
const ret = {};
10051001

1006-
const self = this;
1007-
this.httpDo(
1002+
await new Promise(resolve => this.httpDo(
10081003
file,
10091004
'GET',
10101005
'arrayBuffer',
@@ -1015,7 +1010,7 @@ p5.prototype.loadBytes = function (file, callback, errorCallback) {
10151010
callback(ret);
10161011
}
10171012

1018-
self._decrementPreload();
1013+
resolve();
10191014
},
10201015
err => {
10211016
// Error handling
@@ -1027,7 +1022,7 @@ p5.prototype.loadBytes = function (file, callback, errorCallback) {
10271022
throw err;
10281023
}
10291024
}
1030-
);
1025+
));
10311026
return ret;
10321027
};
10331028

src/webgl/loading.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ import './p5.Geometry';
335335
* @param {Boolean} [options.flipV]
336336
* @return {p5.Geometry} new <a href="#/p5.Geometry">p5.Geometry</a> object.
337337
*/
338-
p5.prototype.loadModel = function (path, options) {
338+
p5.prototype.loadModel = async function (path, options) {
339339
p5._validateParameters('loadModel', arguments);
340340
let normalize = false;
341341
let successCallback;
@@ -420,7 +420,7 @@ p5.prototype.loadModel = function (path, options) {
420420
}
421421
}
422422
if (fileType.match(/\.stl$/i)) {
423-
this.httpDo(
423+
await new Promise(resolve => this.httpDo(
424424
path,
425425
'GET',
426426
'arrayBuffer',
@@ -439,15 +439,15 @@ p5.prototype.loadModel = function (path, options) {
439439
model.flipV();
440440
}
441441

442-
self._decrementPreload();
442+
resolve();
443443
if (typeof successCallback === 'function') {
444444
successCallback(model);
445445
}
446446
},
447447
failureCallback
448-
);
448+
));
449449
} else if (fileType.match(/\.obj$/i)) {
450-
this.loadStrings(
450+
await new Promise(resolve => this.loadStrings(
451451
path,
452452
async lines => {
453453
try {
@@ -474,14 +474,14 @@ p5.prototype.loadModel = function (path, options) {
474474
model.flipV();
475475
}
476476

477-
self._decrementPreload();
477+
resolve();
478478
if (typeof successCallback === 'function') {
479479
successCallback(model);
480480
}
481481
}
482482
},
483483
failureCallback
484-
);
484+
));
485485
} else {
486486
p5._friendlyFileLoadError(3, path);
487487
if (failureCallback) {

test/unit/visual/cases/webgl.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ visualSuite('WebGL', function() {
8787
visualTest('OBJ model with MTL file displays diffuse colors correctly', function(p5, screenshot) {
8888
return new Promise(resolve => {
8989
p5.createCanvas(50, 50, p5.WEBGL);
90-
p5.loadModel('unit/assets/octa-color.obj', model => {
90+
p5.loadModel('/unit/assets/octa-color.obj', model => {
9191
p5.background(255);
9292
p5.rotateX(10 * 0.01);
9393
p5.rotateY(10 * 0.01);
@@ -101,7 +101,7 @@ visualSuite('WebGL', function() {
101101
visualTest('Object with no colors takes on fill color', function(p5, screenshot) {
102102
return new Promise(resolve => {
103103
p5.createCanvas(50, 50, p5.WEBGL);
104-
p5.loadModel('unit/assets/cube.obj', model => {
104+
p5.loadModel('/unit/assets/cube.obj', model => {
105105
p5.background(255);
106106
p5.fill('blue'); // Setting a fill color
107107
p5.rotateX(p5.frameCount * 0.01);
@@ -117,8 +117,8 @@ visualSuite('WebGL', function() {
117117
'Object with different texture coordinates per use of vertex keeps the coordinates intact',
118118
async function(p5, screenshot) {
119119
p5.createCanvas(50, 50, p5.WEBGL);
120-
const tex = await new Promise(resolve => p5.loadImage('unit/assets/cat.jpg', resolve));
121-
const cube = await new Promise(resolve => p5.loadModel('unit/assets/cube-textures.obj', resolve));
120+
const tex = await new Promise(resolve => p5.loadImage('/unit/assets/cat.jpg', resolve));
121+
const cube = await new Promise(resolve => p5.loadModel('/unit/assets/cube-textures.obj', resolve));
122122
cube.normalize();
123123
p5.background(255);
124124
p5.texture(tex);

vitest.workspace.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const plugins = [
1010
];
1111

1212
export default defineWorkspace([
13-
{
13+
/*{
1414
plugins,
1515
publicDir: './test',
1616
test: {
@@ -21,7 +21,7 @@ export default defineWorkspace([
2121
],
2222
environment: 'node'
2323
}
24-
},
24+
},*/
2525
{
2626
plugins,
2727
publicDir: './test',
@@ -34,7 +34,7 @@ export default defineWorkspace([
3434
exclude: [
3535
'./test/unit/spec.js',
3636
'./test/unit/assets/**/*',
37-
'./test/unit/visual/**/*'
37+
'./test/unit/visual/visualTest.js',
3838
],
3939
testTimeout: 1000,
4040
globals: true,

0 commit comments

Comments
 (0)