@@ -6,40 +6,42 @@ function addPreload(p5, fn, lifecycles) {
6
6
'loadStrings' : ( ) => [ ] ,
7
7
'loadFont' : ( pInst ) => new p5 . Font ( pInst , new FontFace ( 'default' , 'default.woff' ) ) ,
8
8
} ;
9
+
10
+ p5 . isPreloadSupported = function ( ) {
11
+ return true ;
12
+ } ;
9
13
10
14
const promises = [ ] ;
15
+ const prevMethods = { } ;
11
16
12
- lifecycles . presetup = async function ( ) {
13
- if ( ! window . preload ) return ;
14
-
15
- const promises = [ ] ;
16
- const prevMethods = { } ;
17
-
18
- // Override existing methods to return an object immediately,
19
- // and keep track of all things being loaded
20
- for ( const method in methods ) {
21
- const prevMethod = fn [ method ] ;
22
- prevMethods [ method ] = prevMethod ;
17
+ // Override existing methods to return an object immediately,
18
+ // and keep track of all things being loaded
19
+ for ( const method in methods ) {
20
+ const prevMethod = fn [ method ] ;
21
+ prevMethods [ method ] = prevMethod ;
23
22
24
- fn [ method ] = function ( ...args ) {
25
- const obj = methods [ method ] ( this ) ;
26
- const promise = prevMethod . apply ( this , args ) . then ( ( result ) => {
27
- for ( const key in result ) {
28
- obj [ key ] = result [ key ] ;
29
- }
30
- } ) ;
31
- promises . push ( promise ) ;
32
- return obj ;
23
+ fn [ method ] = function ( ...args ) {
24
+ if ( ! this . _isInPreload ) {
25
+ return prevMethod . apply ( this , args ) ;
33
26
}
27
+ const obj = methods [ method ] ( this ) ;
28
+ const promise = prevMethod . apply ( this , args ) . then ( ( result ) => {
29
+ for ( const key in result ) {
30
+ obj [ key ] = result [ key ] ;
31
+ }
32
+ } ) ;
33
+ promises . push ( promise ) ;
34
+ return obj ;
34
35
}
35
-
36
+ }
37
+
38
+ lifecycles . presetup = async function ( ) {
39
+ if ( ! window . preload ) return ;
40
+
41
+ this . _isInPreload = true ;
36
42
window . preload ( ) ;
37
-
38
- // Restore previous functionality
39
- for ( const method in prevMethods ) {
40
- fn [ method ] = prevMethods [ method ] ;
41
- }
42
-
43
+ this . _isInPreload = false ;
44
+
43
45
// Wait for everything to load before letting setup run
44
46
await Promise . all ( promises ) ;
45
47
}
0 commit comments