-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpreload.js
More file actions
136 lines (117 loc) · 3.4 KB
/
preload.js
File metadata and controls
136 lines (117 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
function addPreload(p5, fn, lifecycles) {
const methods = {
loadImage: () => new p5.Image(1, 1),
loadModel: () => new p5.Geometry(),
loadJSON: () => {},
loadStrings: () => [],
loadFont: (pInst) => new p5.Font(pInst, new FontFace('default', 'default.woff'))
};
p5.isPreloadSupported = function() {
return true;
};
const promises = [];
const prevMethods = {};
// Override existing methods to return an object immediately,
// and keep track of all things being loaded
for (const method in methods) {
const prevMethod = fn[method];
prevMethods[method] = prevMethod;
fn[method] = function(...args) {
if (!this._isInPreload) {
return prevMethod.apply(this, args);
}
const obj = methods[method](this);
const promise = prevMethod.apply(this, args).then((result) => {
for (const key in result) {
obj[key] = result[key];
}
});
promises.push(promise);
return obj;
};
}
const prevLoadBytes = fn.loadBytes;
fn.loadBytes = function(...args) {
if (!this._isInPreload) return prevLoadBytes.apply(this, args);
const obj = {};
const promise = prevLoadBytes.apply(this, args).then((result) => {
obj.bytes = result;
});
promises.push(promise);
return obj;
};
const prevLoadTable = fn.loadTable;
fn.loadTable = function(...args) {
if (args.length > 1 && args[1] instanceof String) {
if (args[1] === 'csv') {
args[1] = ',';
} else if (args[1] === 'tsv') {
args[1] = '\t';
} else if (args[1] === 'ssv') {
args[1] = ';';
}
}
if (!this._isInPreload) return prevLoadTable.apply(this, args);
const obj = new p5.Table();
const promise = prevLoadTable.apply(this, args).then((result) => {
for (const key in result) {
obj[key] = result[key];
}
});
promises.push(promise);
return obj;
};
// Storage for registered method callbacks
const registeredMethods = {
init: [],
afterSetup: [],
pre: [],
post: [],
remove: []
};
// Implement registerMethod similar to p5.js v1
p5.prototype.registerMethod = function (methodName, callback) {
if (registeredMethods[methodName]) {
registeredMethods[methodName].push(callback);
} else {
console.warn(`p5.registerMethod: "${methodName}" is not a valid method name`);
}
};
lifecycles.presetup = async function() {
// Call init callbacks first
for (const callback of registeredMethods.init) {
callback.call(this);
}
if (!window.preload) return;
this._isInPreload = true;
window.preload();
this._isInPreload = false;
// Wait for everything to load before letting setup run
await Promise.all(promises);
};
// Hook into postsetup for afterSetup callbacks
lifecycles.postsetup = function () {
for (const callback of registeredMethods.afterSetup) {
callback.call(this);
}
};
// Hook into other lifecycle events
lifecycles.predraw = function () {
for (const callback of registeredMethods.pre) {
callback.call(this);
}
};
lifecycles.postdraw = function () {
for (const callback of registeredMethods.post) {
callback.call(this);
}
};
lifecycles.remove = function () {
for (const callback of registeredMethods.remove) {
callback.call(this);
}
};
}
if (typeof p5 !== undefined) {
p5.registerAddon(addPreload);
}