Skip to content

Commit b08c3ad

Browse files
Yuuno, Hibikijoeyklee
authored andcommitted
fix preload bugs (#305)
1 parent cf08abc commit b08c3ad

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/utils/p5PreloadHelper.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,20 @@ export default function registerPreload(obj) {
2020
const fn = obj[key];
2121

2222
preloadFn[key] = function preloads(...args) {
23-
return fn.apply(obj, [...args, function doingPreloads() {
23+
let originCallback = null;
24+
let argLen = args.length;
25+
if (typeof args[argLen - 1] === 'function') {
26+
// find callback function attached
27+
originCallback = args[argLen - 1];
28+
argLen -= 1;
29+
}
30+
return fn.apply(obj, [...args.slice(0, argLen), function doingPreloads() {
2431
const targetPreloadFn = '_decrementPreload';
32+
try {
33+
if (originCallback) originCallback();
34+
} catch (err) {
35+
console.error(err);
36+
}
2537
if (window[targetPreloadFn]) return window[targetPreloadFn]();
2638
return null;
2739
}]);

webpack.test.babel.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import { existsSync, mkdirSync, writeFileSync, lstatSync } from 'fs';
77
import { join } from 'path';
8+
import assert from 'assert';
89
import merge from 'webpack-merge';
910
import common from './webpack.common.babel';
1011

0 commit comments

Comments
 (0)