UNPKG

playcanvas

Version:

Open-source WebGL/WebGPU 3D engine for the web

53 lines (52 loc) 1.54 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { http, Http } from "../../platform/net/http.js"; import { ResourceHandler } from "./handler.js"; class JsonHandler extends ResourceHandler { constructor(app) { super(app, "json"); /** * TextDecoder for decoding binary data. * * @type {TextDecoder|null} * @private */ __publicField(this, "decoder", null); } 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}]`); } }); } /** * Parses raw DataView and returns string. * * @param {DataView} data - The raw data as a DataView * @returns {object} The parsed resource data. */ openBinary(data) { this.decoder ?? (this.decoder = new TextDecoder("utf-8")); return JSON.parse(this.decoder.decode(data)); } } export { JsonHandler };