-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add before/after preload and setup #6433
Changes from 9 commits
2543102
50163a2
6b16199
25d9cb4
6bb0e6f
5514eaa
fa06c9c
ff32bbd
a9394b7
c0d6349
670cbda
f2aec47
b09e55a
e872ad6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
const { expect } = require('chai'); | ||
const { expect, assert } = require('chai'); | ||
|
||
suite('Core', function() { | ||
suite('p5.prototype.registerMethod', function() { | ||
|
@@ -31,6 +31,91 @@ suite('Core', function() { | |
p5.prototype._registeredMethods.init = originalInit; | ||
} | ||
}); | ||
test('should register and call before and after "preload" hooks', function() { | ||
return new Promise(resolve => { | ||
let beforePreloadCalled = false; | ||
let preloadCalled = false; | ||
let afterPreloadCalled = false; | ||
|
||
p5.prototype.registerMethod('beforePreload', () => { | ||
beforePreloadCalled = true; | ||
}); | ||
|
||
p5.prototype.registerMethod('preload', () => { | ||
assert.equal(beforePreloadCalled, true); | ||
preloadCalled = true; | ||
}); | ||
|
||
p5.prototype.registerMethod('afterPreload', () => { | ||
if(beforePreloadCalled && preloadCalled) | ||
afterPreloadCalled = true; | ||
}); | ||
|
||
myp5 = new p5(function(sketch) { | ||
sketch.preload = () => {}; | ||
sketch.setup = () => { | ||
assert.equal(afterPreloadCalled, true); | ||
}; | ||
resolve(); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently I think this will resolve before setup or preload gets run. Does it work to move the |
||
}); | ||
}); | ||
test('should register and call before and after "setup" hooks', function() { | ||
return new Promise(resolve => { | ||
let beforeSetupCalled = false; | ||
let setupCalled = false; | ||
let afterSetupCalled = false; | ||
|
||
p5.prototype.registerMethod('beforeSetup', () => { | ||
beforeSetupCalled = true; | ||
}); | ||
|
||
p5.prototype.registerMethod('setup', () => { | ||
assert.equal(beforeSetupCalled, true); | ||
setupCalled = true; | ||
}); | ||
|
||
p5.prototype.registerMethod('afterSetup', () => { | ||
if(beforeSetupCalled && setupCalled) | ||
afterSetupCalled = true; | ||
}); | ||
|
||
myp5 = new p5(function(sketch) { | ||
sketch.setup = () => {}; | ||
sketch.draw = () => { | ||
assert.equal(afterSetupCalled, true); | ||
resolve(); | ||
}; | ||
}); | ||
}); | ||
}); | ||
test('should register and call pre and post "draw" hooks', function() { | ||
return new Promise(resolve => { | ||
let preDrawCalled = false; | ||
let drawCalled = false; | ||
let postDrawCalled = false; | ||
|
||
p5.prototype.registerMethod('pre', () => { | ||
preDrawCalled = true; | ||
}); | ||
|
||
p5.prototype.registerMethod('draw', () => { | ||
assert.equal(preDrawCalled, true); | ||
drawCalled = true; | ||
}); | ||
|
||
p5.prototype.registerMethod('post', () => { | ||
if(preDrawCalled && drawCalled) | ||
postDrawCalled = true; | ||
}); | ||
|
||
myp5 = new p5(function(sketch) { | ||
sketch.draw = () => {}; | ||
}); | ||
assert.equal(postDrawCalled, true); | ||
resolve(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Getting a As an alternative: the first time sketch.draw = () => {
if (sketch.frameCount === 2) {
assert.equal(postDrawCalled, true);
resolve();
}
}; |
||
}); | ||
}); | ||
}); | ||
|
||
suite('new p5() / global mode', function() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, looks like tests are failing because
this
is undefined. You might need to make this a method onp5.prototype
and then callthis.callRegisteredHooksFor(...)
as a method instead of a function?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your help!