@esotericsoftware/spine-phaser-v3 4.2.84 → 4.2.86

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.
@@ -45,6 +45,7 @@ var spine = (() => {
45
45
  AnimationState: () => AnimationState,
46
46
  AnimationStateAdapter: () => AnimationStateAdapter,
47
47
  AnimationStateData: () => AnimationStateData,
48
+ AssetCache: () => AssetCache,
48
49
  AssetManager: () => AssetManager,
49
50
  AssetManagerBase: () => AssetManagerBase,
50
51
  AtlasAttachmentLoader: () => AtlasAttachmentLoader,
@@ -5765,16 +5766,15 @@ var spine = (() => {
5765
5766
  pathPrefix = "";
5766
5767
  textureLoader;
5767
5768
  downloader;
5768
- assets = {};
5769
- assetsRefCount = {};
5770
- assetsLoaded = {};
5769
+ cache;
5771
5770
  errors = {};
5772
5771
  toLoad = 0;
5773
5772
  loaded = 0;
5774
- constructor(textureLoader, pathPrefix = "", downloader = new Downloader()) {
5773
+ constructor(textureLoader, pathPrefix = "", downloader = new Downloader(), cache = new AssetCache()) {
5775
5774
  this.textureLoader = textureLoader;
5776
5775
  this.pathPrefix = pathPrefix;
5777
5776
  this.downloader = downloader;
5777
+ this.cache = cache;
5778
5778
  }
5779
5779
  start(path) {
5780
5780
  this.toLoad++;
@@ -5783,8 +5783,8 @@ var spine = (() => {
5783
5783
  success(callback, path, asset) {
5784
5784
  this.toLoad--;
5785
5785
  this.loaded++;
5786
- this.assets[path] = asset;
5787
- this.assetsRefCount[path] = (this.assetsRefCount[path] || 0) + 1;
5786
+ this.cache.assets[path] = asset;
5787
+ this.cache.assetsRefCount[path] = (this.cache.assetsRefCount[path] || 0) + 1;
5788
5788
  if (callback) callback(path, asset);
5789
5789
  }
5790
5790
  error(callback, path, message) {
@@ -5815,7 +5815,7 @@ var spine = (() => {
5815
5815
  }) {
5816
5816
  path = this.start(path);
5817
5817
  if (this.reuseAssets(path, success, error)) return;
5818
- this.assetsLoaded[path] = new Promise((resolve, reject) => {
5818
+ this.cache.assetsLoaded[path] = new Promise((resolve, reject) => {
5819
5819
  this.downloader.downloadBinary(path, (data) => {
5820
5820
  this.success(success, path, data);
5821
5821
  resolve(data);
@@ -5841,7 +5841,7 @@ var spine = (() => {
5841
5841
  }) {
5842
5842
  path = this.start(path);
5843
5843
  if (this.reuseAssets(path, success, error)) return;
5844
- this.assetsLoaded[path] = new Promise((resolve, reject) => {
5844
+ this.cache.assetsLoaded[path] = new Promise((resolve, reject) => {
5845
5845
  this.downloader.downloadJson(path, (data) => {
5846
5846
  this.success(success, path, data);
5847
5847
  resolve(data);
@@ -5855,10 +5855,14 @@ var spine = (() => {
5855
5855
  reuseAssets(path, success = () => {
5856
5856
  }, error = () => {
5857
5857
  }) {
5858
- const loadedStatus = this.assetsLoaded[path];
5858
+ const loadedStatus = this.cache.assetsLoaded[path];
5859
5859
  const alreadyExistsOrLoading = loadedStatus !== void 0;
5860
5860
  if (alreadyExistsOrLoading) {
5861
- loadedStatus.then((data) => this.success(success, path, data)).catch((errorMsg) => this.error(error, path, errorMsg));
5861
+ this.cache.assetsLoaded[path] = loadedStatus.then((data) => {
5862
+ data = data instanceof Image || data instanceof ImageBitmap ? this.textureLoader(data) : data;
5863
+ this.success(success, path, data);
5864
+ return data;
5865
+ }).catch((errorMsg) => this.error(error, path, errorMsg));
5862
5866
  }
5863
5867
  return alreadyExistsOrLoading;
5864
5868
  }
@@ -5867,7 +5871,7 @@ var spine = (() => {
5867
5871
  }) {
5868
5872
  path = this.start(path);
5869
5873
  if (this.reuseAssets(path, success, error)) return;
5870
- this.assetsLoaded[path] = new Promise((resolve, reject) => {
5874
+ this.cache.assetsLoaded[path] = new Promise((resolve, reject) => {
5871
5875
  let isBrowser = !!(typeof window !== "undefined" && typeof navigator !== "undefined" && window.document);
5872
5876
  let isWebWorker = !isBrowser;
5873
5877
  if (isWebWorker) {
@@ -5911,7 +5915,7 @@ var spine = (() => {
5911
5915
  let parent = index >= 0 ? path.substring(0, index + 1) : "";
5912
5916
  path = this.start(path);
5913
5917
  if (this.reuseAssets(path, success, error)) return;
5914
- this.assetsLoaded[path] = new Promise((resolve, reject) => {
5918
+ this.cache.assetsLoaded[path] = new Promise((resolve, reject) => {
5915
5919
  this.downloader.downloadText(path, (atlasText) => {
5916
5920
  try {
5917
5921
  let atlas = new TextureAtlas(atlasText);
@@ -5930,7 +5934,7 @@ var spine = (() => {
5930
5934
  },
5931
5935
  (imagePath, message) => {
5932
5936
  if (!abort) {
5933
- const errorMsg = `Couldn't load texture atlas ${path} page image: ${imagePath}`;
5937
+ const errorMsg = `Couldn't load texture ${path} page image: ${imagePath}`;
5934
5938
  this.error(error, path, errorMsg);
5935
5939
  reject(errorMsg);
5936
5940
  }
@@ -5955,7 +5959,7 @@ var spine = (() => {
5955
5959
  }, fileAlias) {
5956
5960
  path = this.start(path);
5957
5961
  if (this.reuseAssets(path, success, error)) return;
5958
- this.assetsLoaded[path] = new Promise((resolve, reject) => {
5962
+ this.cache.assetsLoaded[path] = new Promise((resolve, reject) => {
5959
5963
  this.downloader.downloadText(path, (atlasText) => {
5960
5964
  try {
5961
5965
  const atlas = new TextureAtlas(atlasText);
@@ -6019,33 +6023,36 @@ var spine = (() => {
6019
6023
  );
6020
6024
  });
6021
6025
  }
6026
+ setCache(cache) {
6027
+ this.cache = cache;
6028
+ }
6022
6029
  get(path) {
6023
- return this.assets[this.pathPrefix + path];
6030
+ return this.cache.assets[this.pathPrefix + path];
6024
6031
  }
6025
6032
  require(path) {
6026
6033
  path = this.pathPrefix + path;
6027
- let asset = this.assets[path];
6034
+ let asset = this.cache.assets[path];
6028
6035
  if (asset) return asset;
6029
6036
  let error = this.errors[path];
6030
6037
  throw Error("Asset not found: " + path + (error ? "\n" + error : ""));
6031
6038
  }
6032
6039
  remove(path) {
6033
6040
  path = this.pathPrefix + path;
6034
- let asset = this.assets[path];
6041
+ let asset = this.cache.assets[path];
6035
6042
  if (asset.dispose) asset.dispose();
6036
- delete this.assets[path];
6037
- delete this.assetsRefCount[path];
6038
- delete this.assetsLoaded[path];
6043
+ delete this.cache.assets[path];
6044
+ delete this.cache.assetsRefCount[path];
6045
+ delete this.cache.assetsLoaded[path];
6039
6046
  return asset;
6040
6047
  }
6041
6048
  removeAll() {
6042
- for (let path in this.assets) {
6043
- let asset = this.assets[path];
6049
+ for (let path in this.cache.assets) {
6050
+ let asset = this.cache.assets[path];
6044
6051
  if (asset.dispose) asset.dispose();
6045
6052
  }
6046
- this.assets = {};
6047
- this.assetsLoaded = {};
6048
- this.assetsRefCount = {};
6053
+ this.cache.assets = {};
6054
+ this.cache.assetsLoaded = {};
6055
+ this.cache.assetsRefCount = {};
6049
6056
  }
6050
6057
  isLoadingComplete() {
6051
6058
  return this.toLoad == 0;
@@ -6061,7 +6068,7 @@ var spine = (() => {
6061
6068
  }
6062
6069
  // dispose asset only if it's not used by others
6063
6070
  disposeAsset(path) {
6064
- if (--this.assetsRefCount[path] === 0) {
6071
+ if (--this.cache.assetsRefCount[path] === 0) {
6065
6072
  this.remove(path);
6066
6073
  }
6067
6074
  }
@@ -6072,6 +6079,23 @@ var spine = (() => {
6072
6079
  return this.errors;
6073
6080
  }
6074
6081
  };
6082
+ var AssetCache = class _AssetCache {
6083
+ assets = {};
6084
+ assetsRefCount = {};
6085
+ assetsLoaded = {};
6086
+ static AVAILABLE_CACHES = /* @__PURE__ */ new Map();
6087
+ static getCache(id) {
6088
+ const cache = _AssetCache.AVAILABLE_CACHES.get(id);
6089
+ if (cache) return cache;
6090
+ const newCache = new _AssetCache();
6091
+ _AssetCache.AVAILABLE_CACHES.set(id, newCache);
6092
+ return newCache;
6093
+ }
6094
+ async addAsset(path, asset) {
6095
+ this.assetsLoaded[path] = Promise.resolve(asset);
6096
+ this.assets[path] = await asset;
6097
+ }
6098
+ };
6075
6099
  var Downloader = class {
6076
6100
  callbacks = {};
6077
6101
  rawDataUris = {};