Browse Source

mol-plugin-ui: support AssetManager in DownloadBlob

David Sehnal 5 years ago
parent
commit
0fd00ecab8
2 changed files with 16 additions and 3 deletions
  1. 7 1
      src/mol-plugin-state/transforms/data.ts
  2. 9 2
      src/mol-util/data-source.ts

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

@@ -88,7 +88,7 @@ const DownloadBlob = PluginStateTransform.BuiltIn({
     apply({ params }, plugin: PluginContext) {
         return Task.create('Download Blob', async ctx => {
             const entries: SO.Data.BlobEntry[] = [];
-            const data = await ajaxGetMany(ctx, params.sources, params.maxConcurrency || 4);
+            const data = await ajaxGetMany(ctx, params.sources, params.maxConcurrency || 4, plugin.managers.asset);
 
             for (let i = 0; i < data.length; i++) {
                 const r = data[i], src = params.sources[i];
@@ -102,6 +102,12 @@ const DownloadBlob = PluginStateTransform.BuiltIn({
             return new SO.Data.Blob(entries, { label: 'Data Blob', description: `${entries.length} ${entries.length === 1 ? 'entry' : 'entries'}` });
         });
     },
+    dispose({ params }, plugin: PluginContext) {
+        if (!params) return;
+        for (const s of params.sources) {
+            plugin.managers.asset.release({ url: s.url, body: s.body });
+        }
+    }
     // TODO: ??
     // update({ oldParams, newParams, b }) {
     //     return 0 as any;

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

@@ -10,6 +10,7 @@
 import { Task, RuntimeContext } from '../mol-task';
 import { unzip, ungzip } from './zip/zip';
 import { utf8Read } from '../mol-io/common/utf8';
+import { AssetManager } from './assets';
 
 // polyfill XMLHttpRequest in node.js
 const XHR = typeof document === 'undefined' ? require('xhr2') as {
@@ -281,7 +282,7 @@ function ajaxGetInternal<T extends DataType>(title: string | undefined, url: str
 }
 
 export type AjaxGetManyEntry<T> = { kind: 'ok', id: string, result: T } | { kind: 'error', id: string, error: any }
-export async function ajaxGetMany(ctx: RuntimeContext, sources: { id: string, url: string, isBinary?: boolean, body?: string, canFail?: boolean }[], maxConcurrency: number) {
+export async function ajaxGetMany(ctx: RuntimeContext, sources: { id: string, url: string, isBinary?: boolean, body?: string, canFail?: boolean }[], maxConcurrency: number, assetManager?: AssetManager) {
     const len = sources.length;
     const slots: AjaxGetManyEntry<string | Uint8Array>[] = new Array(sources.length);
 
@@ -290,7 +291,13 @@ export async function ajaxGetMany(ctx: RuntimeContext, sources: { id: string, ur
     let currentSrc = 0;
     for (let _i = Math.min(len, maxConcurrency); currentSrc < _i; currentSrc++) {
         const current = sources[currentSrc];
-        promises.push(wrapPromise(currentSrc, current.id, ajaxGet({ url: current.url, type: current.isBinary ? 'binary' : 'string' }).runAsChild(ctx)));
+
+        if (assetManager) {
+            promises.push(wrapPromise(currentSrc, current.id,
+                assetManager.resolve({ url: current.url, body: current.body }, current.isBinary ? 'binary' : 'string').runAsChild(ctx)));
+        } else {
+            promises.push(wrapPromise(currentSrc, current.id, ajaxGet({ url: current.url, type: current.isBinary ? 'binary' : 'string' }).runAsChild(ctx)));
+        }
         promiseKeys.push(currentSrc);
     }