Skip to content

Commit e03e4d3

Browse files
authored
Merge pull request #5816 from davepagurek/fix/load-outside-preload
Prevent load* methods outside of preload from rerunning setup
2 parents 73f654c + a73e966 commit e03e4d3

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/core/main.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ class p5 {
167167
//////////////////////////////////////////////
168168

169169
this._setupDone = false;
170+
this._preloadDone = false;
170171
// for handling hidpi
171172
this._pixelDensity = Math.ceil(window.devicePixelRatio) || 1;
172173
this._userNode = node;
@@ -292,7 +293,7 @@ class p5 {
292293

293294
this._decrementPreload = function() {
294295
const context = this._isGlobal ? window : this;
295-
if (typeof context.preload === 'function') {
296+
if (!context._preloadDone && typeof context.preload === 'function') {
296297
context._setProperty('_preloadCount', context._preloadCount - 1);
297298
context._runIfPreloadsAreDone();
298299
}
@@ -309,6 +310,8 @@ class p5 {
309310

310311
this._incrementPreload = function() {
311312
const context = this._isGlobal ? window : this;
313+
// Do nothing if we tried to increment preloads outside of `preload`
314+
if (context._preloadDone) return;
312315
context._setProperty('_preloadCount', context._preloadCount + 1);
313316
};
314317

@@ -336,6 +339,8 @@ class p5 {
336339
// Record the time when sketch starts
337340
this._millisStart = window.performance.now();
338341

342+
context._preloadDone = true;
343+
339344
// Short-circuit on this, in case someone used the library in "global"
340345
// mode earlier
341346
if (typeof context.setup === 'function') {

test/unit/io/loadShader.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,15 @@ suite('loadShader', function() {
162162
});
163163
assert.instanceOf(model, p5.Shader);
164164
});
165+
166+
test('does not run setup after complete when called outside of preload', async function() {
167+
let setupCallCount = 0;
168+
await promisedSketch(function(sketch, resolve, reject) {
169+
sketch.setup = function() {
170+
setupCallCount++;
171+
sketch.loadShader(vertFile, fragFile, resolve, reject);
172+
};
173+
});
174+
assert.equal(setupCallCount, 1);
175+
});
165176
});

0 commit comments

Comments
 (0)