Explorar el Código

Asset.getUrlAsset support body

Alexander Rose hace 5 años
padre
commit
dafa946937
Se han modificado 3 ficheros con 22 adiciones y 18 borrados
  1. 1 1
      src/mol-plugin-state/transforms/data.ts
  2. 19 15
      src/mol-util/assets.ts
  3. 2 2
      src/mol-util/data-source.ts

+ 1 - 1
src/mol-plugin-state/transforms/data.ts

@@ -47,7 +47,7 @@ const Download = PluginStateTransform.BuiltIn({
 })({
     apply({ params: p, cache }, plugin: PluginContext) {
         return Task.create('Download', async ctx => {
-            let url = Asset.getUrlAsset(p.url, plugin.managers.asset);
+            let url = Asset.getUrlAsset(plugin.managers.asset, p.url);
             const asset = await plugin.managers.asset.resolve(url, p.isBinary ? 'binary' : 'string').runInContext(ctx);
             (cache as any).asset = asset;
             return p.isBinary

+ 19 - 15
src/mol-util/assets.ts

@@ -35,24 +35,28 @@ namespace Asset {
         return x?.kind === 'file';
     }
 
-    export class Wrapper<T extends DataType = DataType> {
-        dispose() {
-            this.manager.release(this.asset);
-        }
-
-        constructor(public readonly data: DataResponse<T>, private asset: Asset, private manager: AssetManager) {
+    export interface Wrapper<T extends DataType = DataType> {
+        readonly data: DataResponse<T>
+        dispose: () => void
+    }
 
-        }
+    export function Wrapper<T extends DataType = DataType>(data: DataResponse<T>, asset: Asset, manager: AssetManager) {
+        return {
+            data,
+            dispose: () => {
+                manager.release(asset);
+            }
+        };
     }
 
     export function getUrl(url: string | Url) {
         return typeof url === 'string' ? url : url.url;
     }
 
-    export function getUrlAsset(url: string | Url, manager: AssetManager) {
+    export function getUrlAsset(manager: AssetManager, url: string | Url, body?: string) {
         if (typeof url === 'string') {
-            const asset = manager.tryFindUrl(url);
-            return asset || Url(url);
+            const asset = manager.tryFindUrl(url, body);
+            return asset || Url(url, { body });
         }
         return url;
     }
@@ -88,24 +92,24 @@ class AssetManager {
                 if (this._assets.has(asset.id)) {
                     const entry = this._assets.get(asset.id)!;
                     entry.refCount++;
-                    return new Asset.Wrapper(await readFromFile(entry.file, type).runInContext(ctx), asset, this);
+                    return Asset.Wrapper(await readFromFile(entry.file, type).runInContext(ctx), asset, this);
                 }
 
                 if (!store) {
-                    return new Asset.Wrapper(await ajaxGet({ ...asset, type }).runInContext(ctx), asset, this);
+                    return Asset.Wrapper(await ajaxGet({ ...asset, type }).runInContext(ctx), asset, this);
                 }
 
                 const data = await ajaxGet({ ...asset, type: 'binary' }).runInContext(ctx);
                 const file = new File([data], 'raw-data');
                 this._assets.set(asset.id, { asset, file, refCount: 1 });
-                return new Asset.Wrapper(await readFromFile(file, type).runInContext(ctx), asset, this);
+                return Asset.Wrapper(await readFromFile(file, type).runInContext(ctx), asset, this);
             });
         } else {
             return Task.create(`Read ${asset.name}`, async ctx => {
                 if (this._assets.has(asset.id)) {
                     const entry = this._assets.get(asset.id)!;
                     entry.refCount++;
-                    return new Asset.Wrapper(await readFromFile(entry.file, type).runInContext(ctx), asset, this);
+                    return Asset.Wrapper(await readFromFile(entry.file, type).runInContext(ctx), asset, this);
                 }
                 if (!(asset.file instanceof File)) {
                     throw new Error(`Cannot resolve file asset '${asset.name}' (${asset.id})`);
@@ -113,7 +117,7 @@ class AssetManager {
                 if (store) {
                     this._assets.set(asset.id, { asset, file: asset.file, refCount: 1 });
                 }
-                return new Asset.Wrapper(await readFromFile(asset.file, type).runInContext(ctx), asset, this);
+                return Asset.Wrapper(await readFromFile(asset.file, type).runInContext(ctx), asset, this);
             });
         }
     }

+ 2 - 2
src/mol-util/data-source.ts

@@ -293,7 +293,7 @@ export async function ajaxGetMany(ctx: RuntimeContext, assetManager: AssetManage
         const current = sources[currentSrc];
 
         promises.push(wrapPromise(currentSrc, current.id,
-            assetManager.resolve(Asset.getUrlAsset(current.url, assetManager), current.isBinary ? 'binary' : 'string').runAsChild(ctx)));
+            assetManager.resolve(Asset.getUrlAsset(assetManager, current.url), current.isBinary ? 'binary' : 'string').runAsChild(ctx)));
         promiseKeys.push(currentSrc);
     }
 
@@ -315,7 +315,7 @@ export async function ajaxGetMany(ctx: RuntimeContext, assetManager: AssetManage
         promiseKeys = promiseKeys.filter(_filterRemoveIndex, idx);
         if (currentSrc < len) {
             const current = sources[currentSrc];
-            const asset = assetManager.resolve(Asset.getUrlAsset(current.url, assetManager), current.isBinary ? 'binary' : 'string').runAsChild(ctx);
+            const asset = assetManager.resolve(Asset.getUrlAsset(assetManager, current.url), current.isBinary ? 'binary' : 'string').runAsChild(ctx);
             promises.push(wrapPromise(currentSrc, current.id, asset));
             promiseKeys.push(currentSrc);
             currentSrc++;