summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/repo/lib/common/model_manager.js
blob: fd46225279561a1f8b7a4761c6c44a8c03ac4f67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import Disposable from './disposable';
import Model from './model';

export default class ModelManager {
  constructor(monaco) {
    this.monaco = monaco;
    this.disposable = new Disposable();
    this.models = new Map();
  }

  hasCachedModel(path) {
    return this.models.has(path);
  }

  addModel(file) {
    if (this.hasCachedModel(file.path)) {
      return this.models.get(file.path);
    }

    const model = new Model(this.monaco, file);
    this.models.set(model.path, model);
    this.disposable.add(model);

    return model;
  }

  dispose() {
    // dispose of all the models
    this.disposable.dispose();
    this.models.clear();
  }
}