From 8f4151fce585999d667266033dc6f0c1371f0b0d Mon Sep 17 00:00:00 2001 From: kovacsv Date: Wed, 28 Jul 2021 09:03:38 +0200 Subject: [PATCH] Use promise for external library loading. --- source/export/exporter3dm.js | 19 +++++++-------- source/import/importer3dm.js | 19 +++++++-------- source/import/importergltf.js | 19 +++++++-------- source/import/importerifc.js | 21 +++++++---------- source/io/externallibs.js | 44 +++++++++++++++++++---------------- 5 files changed, 57 insertions(+), 65 deletions(-) diff --git a/source/export/exporter3dm.js b/source/export/exporter3dm.js index 0cd82e6..ada4aca 100644 --- a/source/export/exporter3dm.js +++ b/source/export/exporter3dm.js @@ -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); } diff --git a/source/import/importer3dm.js b/source/import/importer3dm.js index b4b8e47..4582d91 100644 --- a/source/import/importer3dm.js +++ b/source/import/importer3dm.js @@ -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 (); diff --git a/source/import/importergltf.js b/source/import/importergltf.js index bc297d8..30db05e 100644 --- a/source/import/importergltf.js +++ b/source/import/importergltf.js @@ -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 (); } diff --git a/source/import/importerifc.js b/source/import/importerifc.js index c18602b..440bc7d 100644 --- a/source/import/importerifc.js +++ b/source/import/importerifc.js @@ -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 (); diff --git a/source/io/externallibs.js b/source/io/externallibs.js index c2a3344..9d4dab2 100644 --- a/source/io/externallibs.js +++ b/source/io/externallibs.js @@ -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); + }); };