debian-mirror-gitlab/app/assets/javascripts/ide/lib/common/model_manager.js

48 lines
994 B
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import eventHub from '../../eventhub';
import Disposable from './disposable';
import Model from './model';
export default class ModelManager {
2018-11-08 19:23:39 +05:30
constructor() {
2018-05-09 12:01:36 +05:30
this.disposable = new Disposable();
this.models = new Map();
}
hasCachedModel(key) {
return this.models.has(key);
}
getModel(key) {
return this.models.get(key);
}
2018-10-15 14:42:47 +05:30
addModel(file, head = null) {
2018-05-09 12:01:36 +05:30
if (this.hasCachedModel(file.key)) {
return this.getModel(file.key);
}
2018-11-08 19:23:39 +05:30
const model = new Model(file, head);
2018-05-09 12:01:36 +05:30
this.models.set(model.path, model);
this.disposable.add(model);
eventHub.$on(
`editor.update.model.dispose.${file.key}`,
this.removeCachedModel.bind(this, file),
);
return model;
}
removeCachedModel(file) {
this.models.delete(file.key);
eventHub.$off(`editor.update.model.dispose.${file.key}`, this.removeCachedModel);
}
dispose() {
// dispose of all the models
this.disposable.dispose();
this.models.clear();
}
}