@esotericsoftware/spine-core 4.2.84 → 4.2.85

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.
@@ -26,6 +26,7 @@ var spine = (() => {
26
26
  AnimationState: () => AnimationState,
27
27
  AnimationStateAdapter: () => AnimationStateAdapter,
28
28
  AnimationStateData: () => AnimationStateData,
29
+ AssetCache: () => AssetCache,
29
30
  AssetManagerBase: () => AssetManagerBase,
30
31
  AtlasAttachmentLoader: () => AtlasAttachmentLoader,
31
32
  Attachment: () => Attachment,
@@ -5665,16 +5666,15 @@ var spine = (() => {
5665
5666
  pathPrefix = "";
5666
5667
  textureLoader;
5667
5668
  downloader;
5668
- assets = {};
5669
- assetsRefCount = {};
5670
- assetsLoaded = {};
5669
+ cache;
5671
5670
  errors = {};
5672
5671
  toLoad = 0;
5673
5672
  loaded = 0;
5674
- constructor(textureLoader, pathPrefix = "", downloader = new Downloader()) {
5673
+ constructor(textureLoader, pathPrefix = "", downloader = new Downloader(), cache = new AssetCache()) {
5675
5674
  this.textureLoader = textureLoader;
5676
5675
  this.pathPrefix = pathPrefix;
5677
5676
  this.downloader = downloader;
5677
+ this.cache = cache;
5678
5678
  }
5679
5679
  start(path) {
5680
5680
  this.toLoad++;
@@ -5683,8 +5683,8 @@ var spine = (() => {
5683
5683
  success(callback, path, asset) {
5684
5684
  this.toLoad--;
5685
5685
  this.loaded++;
5686
- this.assets[path] = asset;
5687
- this.assetsRefCount[path] = (this.assetsRefCount[path] || 0) + 1;
5686
+ this.cache.assets[path] = asset;
5687
+ this.cache.assetsRefCount[path] = (this.cache.assetsRefCount[path] || 0) + 1;
5688
5688
  if (callback) callback(path, asset);
5689
5689
  }
5690
5690
  error(callback, path, message) {
@@ -5715,7 +5715,7 @@ var spine = (() => {
5715
5715
  }) {
5716
5716
  path = this.start(path);
5717
5717
  if (this.reuseAssets(path, success, error)) return;
5718
- this.assetsLoaded[path] = new Promise((resolve, reject) => {
5718
+ this.cache.assetsLoaded[path] = new Promise((resolve, reject) => {
5719
5719
  this.downloader.downloadBinary(path, (data) => {
5720
5720
  this.success(success, path, data);
5721
5721
  resolve(data);
@@ -5741,7 +5741,7 @@ var spine = (() => {
5741
5741
  }) {
5742
5742
  path = this.start(path);
5743
5743
  if (this.reuseAssets(path, success, error)) return;
5744
- this.assetsLoaded[path] = new Promise((resolve, reject) => {
5744
+ this.cache.assetsLoaded[path] = new Promise((resolve, reject) => {
5745
5745
  this.downloader.downloadJson(path, (data) => {
5746
5746
  this.success(success, path, data);
5747
5747
  resolve(data);
@@ -5755,10 +5755,14 @@ var spine = (() => {
5755
5755
  reuseAssets(path, success = () => {
5756
5756
  }, error = () => {
5757
5757
  }) {
5758
- const loadedStatus = this.assetsLoaded[path];
5758
+ const loadedStatus = this.cache.assetsLoaded[path];
5759
5759
  const alreadyExistsOrLoading = loadedStatus !== void 0;
5760
5760
  if (alreadyExistsOrLoading) {
5761
- loadedStatus.then((data) => this.success(success, path, data)).catch((errorMsg) => this.error(error, path, errorMsg));
5761
+ this.cache.assetsLoaded[path] = loadedStatus.then((data) => {
5762
+ data = data instanceof Image || data instanceof ImageBitmap ? this.textureLoader(data) : data;
5763
+ this.success(success, path, data);
5764
+ return data;
5765
+ }).catch((errorMsg) => this.error(error, path, errorMsg));
5762
5766
  }
5763
5767
  return alreadyExistsOrLoading;
5764
5768
  }
@@ -5767,7 +5771,7 @@ var spine = (() => {
5767
5771
  }) {
5768
5772
  path = this.start(path);
5769
5773
  if (this.reuseAssets(path, success, error)) return;
5770
- this.assetsLoaded[path] = new Promise((resolve, reject) => {
5774
+ this.cache.assetsLoaded[path] = new Promise((resolve, reject) => {
5771
5775
  let isBrowser = !!(typeof window !== "undefined" && typeof navigator !== "undefined" && window.document);
5772
5776
  let isWebWorker = !isBrowser;
5773
5777
  if (isWebWorker) {
@@ -5811,7 +5815,7 @@ var spine = (() => {
5811
5815
  let parent = index >= 0 ? path.substring(0, index + 1) : "";
5812
5816
  path = this.start(path);
5813
5817
  if (this.reuseAssets(path, success, error)) return;
5814
- this.assetsLoaded[path] = new Promise((resolve, reject) => {
5818
+ this.cache.assetsLoaded[path] = new Promise((resolve, reject) => {
5815
5819
  this.downloader.downloadText(path, (atlasText) => {
5816
5820
  try {
5817
5821
  let atlas = new TextureAtlas(atlasText);
@@ -5830,7 +5834,7 @@ var spine = (() => {
5830
5834
  },
5831
5835
  (imagePath, message) => {
5832
5836
  if (!abort) {
5833
- const errorMsg = `Couldn't load texture atlas ${path} page image: ${imagePath}`;
5837
+ const errorMsg = `Couldn't load texture ${path} page image: ${imagePath}`;
5834
5838
  this.error(error, path, errorMsg);
5835
5839
  reject(errorMsg);
5836
5840
  }
@@ -5855,7 +5859,7 @@ var spine = (() => {
5855
5859
  }, fileAlias) {
5856
5860
  path = this.start(path);
5857
5861
  if (this.reuseAssets(path, success, error)) return;
5858
- this.assetsLoaded[path] = new Promise((resolve, reject) => {
5862
+ this.cache.assetsLoaded[path] = new Promise((resolve, reject) => {
5859
5863
  this.downloader.downloadText(path, (atlasText) => {
5860
5864
  try {
5861
5865
  const atlas = new TextureAtlas(atlasText);
@@ -5919,33 +5923,36 @@ var spine = (() => {
5919
5923
  );
5920
5924
  });
5921
5925
  }
5926
+ setCache(cache) {
5927
+ this.cache = cache;
5928
+ }
5922
5929
  get(path) {
5923
- return this.assets[this.pathPrefix + path];
5930
+ return this.cache.assets[this.pathPrefix + path];
5924
5931
  }
5925
5932
  require(path) {
5926
5933
  path = this.pathPrefix + path;
5927
- let asset = this.assets[path];
5934
+ let asset = this.cache.assets[path];
5928
5935
  if (asset) return asset;
5929
5936
  let error = this.errors[path];
5930
5937
  throw Error("Asset not found: " + path + (error ? "\n" + error : ""));
5931
5938
  }
5932
5939
  remove(path) {
5933
5940
  path = this.pathPrefix + path;
5934
- let asset = this.assets[path];
5941
+ let asset = this.cache.assets[path];
5935
5942
  if (asset.dispose) asset.dispose();
5936
- delete this.assets[path];
5937
- delete this.assetsRefCount[path];
5938
- delete this.assetsLoaded[path];
5943
+ delete this.cache.assets[path];
5944
+ delete this.cache.assetsRefCount[path];
5945
+ delete this.cache.assetsLoaded[path];
5939
5946
  return asset;
5940
5947
  }
5941
5948
  removeAll() {
5942
- for (let path in this.assets) {
5943
- let asset = this.assets[path];
5949
+ for (let path in this.cache.assets) {
5950
+ let asset = this.cache.assets[path];
5944
5951
  if (asset.dispose) asset.dispose();
5945
5952
  }
5946
- this.assets = {};
5947
- this.assetsLoaded = {};
5948
- this.assetsRefCount = {};
5953
+ this.cache.assets = {};
5954
+ this.cache.assetsLoaded = {};
5955
+ this.cache.assetsRefCount = {};
5949
5956
  }
5950
5957
  isLoadingComplete() {
5951
5958
  return this.toLoad == 0;
@@ -5961,7 +5968,7 @@ var spine = (() => {
5961
5968
  }
5962
5969
  // dispose asset only if it's not used by others
5963
5970
  disposeAsset(path) {
5964
- if (--this.assetsRefCount[path] === 0) {
5971
+ if (--this.cache.assetsRefCount[path] === 0) {
5965
5972
  this.remove(path);
5966
5973
  }
5967
5974
  }
@@ -5972,6 +5979,23 @@ var spine = (() => {
5972
5979
  return this.errors;
5973
5980
  }
5974
5981
  };
5982
+ var AssetCache = class _AssetCache {
5983
+ assets = {};
5984
+ assetsRefCount = {};
5985
+ assetsLoaded = {};
5986
+ static AVAILABLE_CACHES = /* @__PURE__ */ new Map();
5987
+ static getCache(id) {
5988
+ const cache = _AssetCache.AVAILABLE_CACHES.get(id);
5989
+ if (cache) return cache;
5990
+ const newCache = new _AssetCache();
5991
+ _AssetCache.AVAILABLE_CACHES.set(id, newCache);
5992
+ return newCache;
5993
+ }
5994
+ async addAsset(path, asset) {
5995
+ this.assetsLoaded[path] = Promise.resolve(asset);
5996
+ this.assets[path] = await asset;
5997
+ }
5998
+ };
5975
5999
  var Downloader = class {
5976
6000
  callbacks = {};
5977
6001
  rawDataUris = {};