Skip to content

Commit a65d70c

Browse files
authored
Refactor preload plugin usage code. NFC (#16116)
Just moves code from library_fs into library_browser. The code belongs in that location anyhow - it would be good to move even more of it - and also this will help WasmFS which needs the same code, and this will allow us to avoid duplicating it.
1 parent 5a5bdaa commit a65d70c

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/library_browser.js

+17
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,23 @@ var LibraryBrowser = {
310310
}
311311
},
312312

313+
// Tries to handle an input byteArray using preload plugins. Returns true if
314+
// it was handled.
315+
handledByPreloadPlugin: function(byteArray, fullname, finish, onerror) {
316+
// Ensure plugins are ready.
317+
Browser.init();
318+
319+
var handled = false;
320+
Module['preloadPlugins'].forEach(function(plugin) {
321+
if (handled) return;
322+
if (plugin['canHandle'](fullname)) {
323+
plugin['handle'](byteArray, fullname, finish, onerror);
324+
handled = true;
325+
}
326+
});
327+
return handled;
328+
},
329+
313330
createContext: function(canvas, useWebGL, setInModule, webGLContextAttributes) {
314331
if (useWebGL && Module.ctx && canvas == Module.canvas) return Module.ctx; // no need to recreate GL context if it's already been created for this canvas.
315332

src/library_fs.js

+7-13
Original file line numberDiff line numberDiff line change
@@ -1864,7 +1864,6 @@ FS.staticInit();` +
18641864
// do preloading for the Image/Audio part, as if the typed array were the
18651865
// result of an XHR that you did manually.
18661866
createPreloadedFile: function(parent, name, url, canRead, canWrite, onload, onerror, dontCreateFile, canOwn, preFinish) {
1867-
Browser.init(); // XXX perhaps this method should move onto Browser?
18681867
// TODO we should allow people to just pass in a complete filename instead
18691868
// of parent and name being that we just join them anyways
18701869
var fullname = name ? PATH_FS.resolve(PATH.join2(parent, name)) : parent;
@@ -1878,18 +1877,13 @@ FS.staticInit();` +
18781877
if (onload) onload();
18791878
removeRunDependency(dep);
18801879
}
1881-
var handled = false;
1882-
Module['preloadPlugins'].forEach(function(plugin) {
1883-
if (handled) return;
1884-
if (plugin['canHandle'](fullname)) {
1885-
plugin['handle'](byteArray, fullname, finish, function() {
1886-
if (onerror) onerror();
1887-
removeRunDependency(dep);
1888-
});
1889-
handled = true;
1890-
}
1891-
});
1892-
if (!handled) finish(byteArray);
1880+
if (Browser.handledByPreloadPlugin(byteArray, fullname, finish, function() {
1881+
if (onerror) onerror();
1882+
removeRunDependency(dep);
1883+
})) {
1884+
return;
1885+
}
1886+
finish(byteArray);
18931887
}
18941888
addRunDependency(dep);
18951889
if (typeof url == 'string') {

0 commit comments

Comments
 (0)