Skip to content
Snippets Groups Projects
Commit dbeeb9b5 authored by netpro2k's avatar netpro2k
Browse files

Add media index support to media-loader/reolsveMedia

parent cfbe58c2
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ AFRAME.registerComponent("media-loader", { ...@@ -5,6 +5,7 @@ AFRAME.registerComponent("media-loader", {
schema: { schema: {
src: { type: "string" }, src: { type: "string" },
token: { type: "string" }, token: { type: "string" },
index: { type: "number" },
resize: { default: false } resize: { default: false }
}, },
...@@ -59,7 +60,7 @@ AFRAME.registerComponent("media-loader", { ...@@ -59,7 +60,7 @@ AFRAME.registerComponent("media-loader", {
// TODO: correctly handle case where src changes // TODO: correctly handle case where src changes
async update(oldData) { async update(oldData) {
try { try {
const { src, token } = this.data; const { src, token, index } = this.data;
if (src !== oldData.src && !this.showLoaderTimeout) { if (src !== oldData.src && !this.showLoaderTimeout) {
this.showLoaderTimeout = setTimeout(this.showLoader, 100); this.showLoaderTimeout = setTimeout(this.showLoader, 100);
...@@ -67,7 +68,7 @@ AFRAME.registerComponent("media-loader", { ...@@ -67,7 +68,7 @@ AFRAME.registerComponent("media-loader", {
if (!src) return; if (!src) return;
const { raw, images, contentType } = await resolveMedia(src, token); const { raw, images, contentType } = await resolveMedia(src, token, false, index);
if (token) { if (token) {
if (this.blobURL) { if (this.blobURL) {
......
...@@ -17,9 +17,10 @@ const fetchContentType = async (url, token) => { ...@@ -17,9 +17,10 @@ const fetchContentType = async (url, token) => {
}; };
const resolveMediaCache = new Map(); const resolveMediaCache = new Map();
export const resolveMedia = async (url, token, skipContentType) => { export const resolveMedia = async (url, token, skipContentType, index) => {
const parsedUrl = new URL(url); const parsedUrl = new URL(url);
if (resolveMediaCache.has(url)) return resolveMediaCache.get(url); const cacheKey = `${url}|${index}`;
if (resolveMediaCache.has(cacheKey)) return resolveMediaCache.get(cacheKey);
const isNotHttpOrHttps = parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:"; const isNotHttpOrHttps = parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:";
const resolved = const resolved =
...@@ -28,7 +29,7 @@ export const resolveMedia = async (url, token, skipContentType) => { ...@@ -28,7 +29,7 @@ export const resolveMedia = async (url, token, skipContentType) => {
: await fetch(mediaAPIEndpoint, { : await fetch(mediaAPIEndpoint, {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ media: { url } }) body: JSON.stringify({ media: { url, index } })
}).then(r => r.json()); }).then(r => r.json());
if (!isNotHttpOrHttps && !skipContentType) { if (!isNotHttpOrHttps && !skipContentType) {
...@@ -37,7 +38,7 @@ export const resolveMedia = async (url, token, skipContentType) => { ...@@ -37,7 +38,7 @@ export const resolveMedia = async (url, token, skipContentType) => {
resolved.contentType = contentType; resolved.contentType = contentType;
} }
resolveMediaCache.set(url, resolved); resolveMediaCache.set(cacheKey, resolved);
return resolved; return resolved;
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment