@preference-sl/pref-viewer 2.10.0-beta.18 → 2.10.0-beta.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preference-sl/pref-viewer",
3
- "version": "2.10.0-beta.18",
3
+ "version": "2.10.0-beta.19",
4
4
  "description": "Web Component to preview GLTF models with Babylon.js",
5
5
  "author": "Alex Moreno Palacio <amoreno@preference.es>",
6
6
  "scripts": {
@@ -12,25 +12,52 @@ function _getDbOrThrow() {
12
12
  }
13
13
 
14
14
  // --- internal helpers -------------------------------------------------------
15
+
16
+ function _createStoreIfMissing(db, storeName) {
17
+ if (!db.objectStoreNames.contains(storeName)) {
18
+ const store = db.createObjectStore(storeName, { keyPath: "id" });
19
+ store.createIndex("expirationTimeStamp", "expirationTimeStamp", { unique: false });
20
+ }
21
+ }
22
+
23
+ /**
24
+ * Opens a DB without specifying a version. If the required object store doesn't
25
+ * exist, it re-opens with (currentVersion + 1) to create it.
26
+ */
15
27
  function _openEnsuringStore(dbName, storeName) {
16
28
  return new Promise((resolve, reject) => {
17
- const open = indexedDB.open(dbName, 5);
29
+ const open = indexedDB.open(dbName);
18
30
 
19
31
  open.onblocked = () => reject(new Error("Open blocked by another tab or worker"));
20
32
  open.onerror = () => reject(open.error);
33
+
34
+ open.onupgradeneeded = (e) => {
35
+ // First creation path (brand-new DB)
36
+ const db = e.target.result;
37
+ _createStoreIfMissing(db, storeName);
38
+ };
39
+
21
40
  open.onsuccess = (e) => {
22
- resolve(e.target.result);
23
- }
24
- open.onupgradeneeded = (ev) => {
25
- const upgradeDb = ev.target.result;
41
+ const db = e.target.result;
26
42
 
27
- // Eliminar la object store si ya existe
28
- if (upgradeDb.objectStoreNames.contains(storeName)) {
29
- upgradeDb.deleteObjectStore(storeName);
43
+ // If the store exists, we're done.
44
+ if (db.objectStoreNames.contains(storeName)) {
45
+ resolve(db);
46
+ return;
30
47
  }
31
48
 
32
- const store = upgradeDb.createObjectStore(storeName, { keyPath: 'id' });
33
- store.createIndex('expirationTimeStamp', 'expirationTimeStamp', { unique: false });
49
+ // Otherwise bump version just enough to add the store.
50
+ const nextVersion = db.version + 1;
51
+ db.close();
52
+
53
+ const upgrade = indexedDB.open(dbName, nextVersion);
54
+ upgrade.onblocked = () => reject(new Error("Upgrade blocked by another tab or worker"));
55
+ upgrade.onerror = () => reject(upgrade.error);
56
+ upgrade.onupgradeneeded = (ev) => {
57
+ const upDb = ev.target.result;
58
+ _createStoreIfMissing(upDb, storeName);
59
+ };
60
+ upgrade.onsuccess = (ev) => resolve(ev.target.result);
34
61
  };
35
62
  });
36
63
  }
package/src/index.js CHANGED
@@ -253,7 +253,7 @@ class PrefViewer extends HTMLElement {
253
253
  container_environment: !!this.#data.containers.environment.changed,
254
254
  container_materials: !!this.#data.containers.materials.changed,
255
255
  options_camera: !!this.#data.options.camera.changed,
256
- options_inneWallMaterial: !!this.#data.options.materials.innerWall.changed,
256
+ options_innerWallMaterial: !!this.#data.options.materials.innerWall.changed,
257
257
  options_outerWallMaterial: !!this.#data.options.materials.outerWall.changed,
258
258
  options_innerFloorMaterial: !!this.#data.options.materials.innerFloor.changed,
259
259
  options_outerFloorMaterial: !!this.#data.options.materials.outerFloor.changed,
@@ -263,7 +263,7 @@ class PrefViewer extends HTMLElement {
263
263
  container_environment: !!this.#data.containers.environment.changed?.success,
264
264
  container_materials: !!this.#data.containers.materials.changed?.success,
265
265
  options_camera: !!this.#data.options.camera.changed?.success,
266
- options_inneWallMaterial: !!this.#data.options.materials.innerWall.changed?.success,
266
+ options_innerWallMaterial: !!this.#data.options.materials.innerWall.changed?.success,
267
267
  options_outerWallMaterial: !!this.#data.options.materials.outerWall.changed?.success,
268
268
  options_innerFloorMaterial: !!this.#data.options.materials.innerFloor.changed?.success,
269
269
  options_outerFloorMaterial: !!this.#data.options.materials.outerFloor.changed?.success,
@@ -300,13 +300,13 @@ class PrefViewer extends HTMLElement {
300
300
 
301
301
  #setStatusOptionsLoaded() {
302
302
  const toLoadDetail = {
303
- inneWallMaterial: !!this.#data.options.materials.innerWall.changed,
303
+ innerWallMaterial: !!this.#data.options.materials.innerWall.changed,
304
304
  outerWallMaterial: !!this.#data.options.materials.outerWall.changed,
305
305
  innerFloorMaterial: !!this.#data.options.materials.innerFloor.changed,
306
306
  outerFloorMaterial: !!this.#data.options.materials.outerFloor.changed,
307
307
  };
308
308
  const loadedDetail = {
309
- inneWallMaterial: !!this.#data.options.materials.innerWall.changed?.success,
309
+ innerWallMaterial: !!this.#data.options.materials.innerWall.changed?.success,
310
310
  outerWallMaterial: !!this.#data.options.materials.outerWall.changed?.success,
311
311
  innerFloorMaterial: !!this.#data.options.materials.innerFloor.changed?.success,
312
312
  outerFloorMaterial: !!this.#data.options.materials.outerFloor.changed?.success,
@@ -353,17 +353,10 @@ class PrefViewer extends HTMLElement {
353
353
  return someChanged;
354
354
  }
355
355
 
356
- #storeChangedFlagsForContainer(container, success) {
357
- if (success) {
358
- container.timeStamp = container.changed.timeStamp;
359
- container.size = container.changed.size;
360
- container.changed.success = success;
361
- } else if(container.changed) {
362
- container.source = container.changed.source;
363
- container.changed.success = success;
364
- } else {
365
- container.changed = { success: success };
366
- }
356
+ #storeChangedFlagsForContainer(container) {
357
+ container.timeStamp = container.changed.timeStamp;
358
+ container.size = container.changed.size;
359
+ container.changed.success = true;
367
360
  }
368
361
 
369
362
  #resetChangedFlags() {
@@ -457,7 +450,6 @@ class PrefViewer extends HTMLElement {
457
450
  }
458
451
 
459
452
  #createLights() {
460
-
461
453
  // 1) Stronger ambient fill
462
454
  this.#hemiLight = new HemisphericLight("hemiLight", new Vector3(-10, 10, -10), this.#scene);
463
455
  this.#hemiLight.intensity = 0.6;
@@ -689,7 +681,7 @@ class PrefViewer extends HTMLElement {
689
681
  if (object.timeStamp === container.timeStamp) {
690
682
  return false;
691
683
  } else {
692
- Object.assign(container.changed, { timeStamp: object.timeStamp, size: object.size, success: false });
684
+ container.changed = { timeStamp: object.timeStamp, size: object.size, success: false };
693
685
  }
694
686
  }
695
687
 
@@ -708,17 +700,17 @@ class PrefViewer extends HTMLElement {
708
700
  if (container.timeStamp === null && container.size === size) {
709
701
  return false;
710
702
  } else {
711
- Object.assign(container.changed, { timeStamp: null, size: size, success: false });
703
+ container.changed = { timeStamp: null, size: size, success: false };
712
704
  }
713
705
  }
714
706
  } else {
715
707
  const extMatch = source.match(/\.(gltf|glb)(\?|#|$)/i);
716
708
  extension = extMatch ? `.${extMatch[1].toLowerCase()}` : ".gltf";
717
- const [fileSize, fileTimestamp ] = await this.#getServerFileDataHeader(source);
718
- if (container.size === fileSize && container.timeStamp === fileTimestamp) {
709
+ const [fileSize, fileTimeStamp ] = await this.#getServerFileDataHeader(source);
710
+ if (container.size === fileSize && container.timeStamp === fileTimeStamp) {
719
711
  return false;
720
712
  } else {
721
- Object.assign(container.changed, { timeStamp: fileTimestamp, size: fileSize, success: false });
713
+ container.changed = { timeStamp: fileTimeStamp, size: fileSize, success: false };
722
714
  }
723
715
  }
724
716
 
@@ -736,13 +728,13 @@ class PrefViewer extends HTMLElement {
736
728
  }
737
729
 
738
730
  async #loadContainers(loadModel = true, loadEnvironment = true, loadMaterials = true) {
739
- this.#setStatusSceneLoading();
740
-
741
731
  const promiseArray = [];
742
732
  promiseArray.push(loadModel ? this.#loadAssetContainer(this.#data.containers.model) : false);
743
733
  promiseArray.push(loadEnvironment ? this.#loadAssetContainer(this.#data.containers.environment) : false);
744
734
  promiseArray.push(loadMaterials ? this.#loadAssetContainer(this.#data.containers.materials) : false);
745
735
 
736
+ this.#setStatusSceneLoading();
737
+
746
738
  Promise.allSettled(promiseArray)
747
739
  .then(async (values) => {
748
740
  const modelContainer = values[0];
@@ -751,25 +743,21 @@ class PrefViewer extends HTMLElement {
751
743
 
752
744
  if (modelContainer.status === "fulfilled" && modelContainer.value) {
753
745
  this.#replaceContainer(this.#data.containers.model, modelContainer.value);
754
- this.#storeChangedFlagsForContainer(this.#data.containers.model, true);
746
+ this.#storeChangedFlagsForContainer(this.#data.containers.model);
755
747
  } else {
756
748
  this.#data.containers.model.show ? this.#addContainer(this.#data.containers.model) : this.#removeContainer(this.#data.containers.model);
757
- this.#storeChangedFlagsForContainer(this.#data.containers.model, false);
758
749
  }
759
750
 
760
751
  if (environmentContainer.status === "fulfilled" && environmentContainer.value) {
761
752
  this.#replaceContainer(this.#data.containers.environment, environmentContainer.value);
762
- this.#storeChangedFlagsForContainer(this.#data.containers.environment, true);
753
+ this.#storeChangedFlagsForContainer(this.#data.containers.environment);
763
754
  } else {
764
755
  this.#data.containers.environment.show ? this.#addContainer(this.#data.containers.environment) : this.#removeContainer(this.#data.containers.environment);
765
- this.#storeChangedFlagsForContainer(this.#data.containers.environment, false);
766
756
  }
767
757
 
768
758
  if (materialsContainer.status === "fulfilled" && materialsContainer.value) {
769
759
  this.#replaceContainer(this.#data.containers.materials, materialsContainer.value);
770
- this.#storeChangedFlagsForContainer(this.#data.containers.materials, true);
771
- } else {
772
- this.#storeChangedFlagsForContainer(this.#data.containers.materials, false);
760
+ this.#storeChangedFlagsForContainer(this.#data.containers.materials);
773
761
  }
774
762
 
775
763
  this.#setOptionsMaterials();
@@ -800,13 +788,10 @@ class PrefViewer extends HTMLElement {
800
788
  }
801
789
 
802
790
  // Containers
803
- this.#data.containers.model.changed = { storage: this.#data.containers.model.storage || null };
804
791
  this.#data.containers.model.storage = config.model?.storage || null;
805
792
  this.#data.containers.model.show = config.model?.visible !== undefined ? config.model.visible : this.#data.containers.model.show;
806
- this.#data.containers.environment.changed = { storage: this.#data.containers.environment.storage || null };
807
793
  this.#data.containers.environment.storage = config.scene?.storage || null;
808
794
  this.#data.containers.environment.show = config.scene?.visible !== undefined ? config.scene.visible : this.#data.containers.environment.show;
809
- this.#data.containers.materials.changed = { storage: this.#data.containers.materials.storage || null };
810
795
  this.#data.containers.materials.storage = config.materials?.storage || null;
811
796
 
812
797
  // Options
@@ -844,7 +829,6 @@ class PrefViewer extends HTMLElement {
844
829
  if (!model) {
845
830
  return false;
846
831
  }
847
- this.#data.containers.model.changed = { storage: this.#data.containers.model.storage || null };
848
832
  this.#data.containers.model.storage = model.storage || null;
849
833
  this.#data.containers.model.show = model.visible !== undefined ? model.visible : this.#data.containers.model.show;
850
834
  this.initialized && this.#loadContainers(true, false, false);
@@ -855,22 +839,11 @@ class PrefViewer extends HTMLElement {
855
839
  if (!scene) {
856
840
  return false;
857
841
  }
858
- this.#data.containers.environment.changed = { storage: this.#data.containers.environment.storage || null };
859
842
  this.#data.containers.environment.storage = scene.storage || null;
860
843
  this.#data.containers.environment.show = scene.visible !== undefined ? scene.visible : this.#data.containers.environment.show;
861
844
  this.initialized && this.#loadContainers(false, true, false);
862
845
  }
863
846
 
864
- loadMaterials(materials) {
865
- materials = typeof materials === "string" ? JSON.parse(materials) : materials;
866
- if (!materials) {
867
- return false;
868
- }
869
- this.#data.containers.materials.changed = { storage: this.#data.containers.materials.storage || null };
870
- this.#data.containers.materials.storage = materials.storage || null;
871
- this.initialized && this.#loadContainers(false, false, true);
872
- }
873
-
874
847
  showModel() {
875
848
  this.#data.containers.model.show = true;
876
849
  this.#addContainer(this.#data.containers.model);