Use promise for external library loading.

This commit is contained in:
kovacsv 2021-07-28 09:03:38 +02:00
parent 2757d9ecb5
commit 8f4151fce5
5 changed files with 57 additions and 65 deletions

View File

@ -14,17 +14,14 @@ OV.Exporter3dm = class extends OV.ExporterBase
ExportContent (model, format, files, onFinish)
{
if (this.rhino === null) {
OV.LoadExternalLibrary ('rhino3dm.min.js', {
success : () => {
rhino3dm ().then ((rhino) => {
this.rhino = rhino;
this.ExportRhinoContent (model, files, onFinish);
});
},
error : () => {
onFinish ();
}
});
OV.LoadExternalLibrary ('rhino3dm.min.js').then (() => {
rhino3dm ().then ((rhino) => {
this.rhino = rhino;
this.ExportRhinoContent (model, files, onFinish);
});
}).catch (() => {
onFinish ();
});
} else {
this.ExportRhinoContent (model, files, onFinish);
}

View File

@ -38,18 +38,15 @@ OV.Importer3dm = class extends OV.ImporterBase
ImportContent (fileContent, onFinish)
{
if (this.rhino === null) {
OV.LoadExternalLibrary ('rhino3dm.min.js', {
success : () => {
rhino3dm ().then ((rhino) => {
this.rhino = rhino;
this.ImportRhinoContent (fileContent);
onFinish ();
});
},
error : () => {
OV.LoadExternalLibrary ('rhino3dm.min.js').then (() => {
rhino3dm ().then ((rhino) => {
this.rhino = rhino;
this.ImportRhinoContent (fileContent);
onFinish ();
}
});
});
}).catch (() => {
onFinish ();
});
} else {
this.ImportRhinoContent (fileContent);
onFinish ();

View File

@ -271,17 +271,14 @@ OV.GltfExtensions = class
return;
}
if (this.draco === null && extensionsRequired.indexOf ('KHR_draco_mesh_compression') !== -1) {
OV.LoadExternalLibrary ('draco_decoder.js', {
success : () => {
DracoDecoderModule ().then ((draco) => {
this.draco = draco;
callbacks.onSuccess ();
});
},
error : () => {
callbacks.onError ();
}
});
OV.LoadExternalLibrary ('draco_decoder.js').then (() => {
DracoDecoderModule ().then ((draco) => {
this.draco = draco;
callbacks.onSuccess ();
});
}).catch (() => {
callbacks.onError ();
});
} else {
callbacks.onSuccess ();
}

View File

@ -38,18 +38,15 @@ OV.ImporterIfc = class extends OV.ImporterBase
ImportContent (fileContent, onFinish)
{
if (this.ifc === null) {
OV.LoadExternalLibrary ('web-ifc-api.js', {
success : () => {
this.ifc = new IfcAPI ();
this.ifc.Init ().then (() => {
this.ImportIfcContent (fileContent);
onFinish ();
});
},
error : () => {
onFinish ();
}
});
OV.LoadExternalLibrary ('web-ifc-api.js').then (() => {
this.ifc = new IfcAPI ();
this.ifc.Init ().then (() => {
this.ImportIfcContent (fileContent);
onFinish ();
});
}).catch (() => {
onFinish ();
});
} else {
this.ImportIfcContent (fileContent);
onFinish ();

View File

@ -2,25 +2,29 @@ OV.ExternalLibLocation = null;
OV.LoadedExternalLibs = {};
OV.LoadExternalLibrary = function (libName, callbacks)
OV.LoadExternalLibrary = function (libName)
{
if (OV.ExternalLibLocation === null) {
callbacks.error ();
return;
}
if (OV.LoadedExternalLibs[libName] !== undefined) {
callbacks.success ();
return;
}
let scriptElement = document.createElement ('script');
scriptElement.type = 'text/javascript';
scriptElement.src = OV.ExternalLibLocation + '/' + libName;
scriptElement.onload = function () {
callbacks.success ();
OV.LoadedExternalLibs[libName] = true;
};
scriptElement.onerror = callbacks.error;
document.head.appendChild (scriptElement);
return new Promise ((resolve, reject) => {
if (OV.ExternalLibLocation === null) {
reject ();
return;
}
if (OV.LoadedExternalLibs[libName] !== undefined) {
resolve ();
return;
}
let scriptElement = document.createElement ('script');
scriptElement.type = 'text/javascript';
scriptElement.src = OV.ExternalLibLocation + '/' + libName;
scriptElement.onload = () => {
OV.LoadedExternalLibs[libName] = true;
resolve ();
};
scriptElement.onerror = () => {
reject ();
};
document.head.appendChild (scriptElement);
});
};