playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
38 lines (37 loc) • 852 B
JavaScript
import { http, Http } from "../../platform/net/http.js";
import { ResourceHandler } from "./handler.js";
class JsonHandler extends ResourceHandler {
decoder = null;
constructor(app) {
super(app, "json");
}
load(url, callback) {
if (typeof url === "string") {
url = {
load: url,
original: url
};
}
const options = {
retry: this.maxRetries > 0,
maxRetries: this.maxRetries
};
if (url.load.startsWith("blob:")) {
options.responseType = Http.ResponseType.JSON;
}
http.get(url.load, options, (err, response) => {
if (!err) {
callback(null, response);
} else {
callback(`Error loading JSON resource: ${url.original} [${err}]`);
}
});
}
openBinary(data) {
this.decoder ?? (this.decoder = new TextDecoder("utf-8"));
return JSON.parse(this.decoder.decode(data));
}
}
export {
JsonHandler
};