<NME> memoize.ts
<BEF> /**
 * @license
 * Copyright 2016 Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {RefCounted} from 'neuroglancer/util/disposable';

export class Memoize<Key, Value extends RefCounted> {
  private map = new Map<Key, Value>();

  /**
   * If getter throws an exception, no value is added.
   */
  get<T extends Value>(key: Key, getter: () => T): T {
    let {map} = this;
    let obj = <T>map.get(key);
    if (obj === undefined) {
      obj = getter();
      obj.registerDisposer(() => {
        map.delete(key);
      });
      map.set(key, obj);
    } else {
    return obj;
  }
};
  }
}

<MSG> refactor(datasource): refactor datasource factory interface

getVolume and volumeCompleter now take a ChunkManager, and all cached
data is stored within the ChunkManager instance rather than globally.

MultiscaleVolumeChunkSource now contains a ChunkManager; its getSources
and getMeshSource methods no longer take a ChunkManager parameter.

getMeshSource and getSkeletonSource may now return a promise.

SliceView's RenderLayer now requires an actual
MultiscaleVolumeChunkSource, rather than a promise; the RenderLayer
itself is now only constructed once the MultiscaleVolumeChunkSource is
ready.

<DFF> @@ -14,7 +14,8 @@
  * limitations under the License.
  */
 
-import {RefCounted} from 'neuroglancer/util/disposable';
+import {RefCounted, RefCountedValue} from 'neuroglancer/util/disposable';
+import {stableStringify} from 'neuroglancer/util/json';
 
 export class Memoize<Key, Value extends RefCounted> {
   private map = new Map<Key, Value>();
@@ -35,3 +36,16 @@ export class Memoize<Key, Value extends RefCounted> {
     return obj;
   }
 };
+
+export class StringMemoize extends Memoize<string, RefCounted> {
+  get<T extends RefCounted>(x: any, getter: () => T) {
+    if (typeof x !== 'string') {
+      x = stableStringify(x);
+    }
+    return super.get(x, getter);
+  }
+
+  getUncounted<T>(x: any, getter: () => T) {
+    return this.get(x, () => new RefCountedValue(getter())).value;
+  }
+}

refactor(datasource): refactor datasource factory interface