|
| 1 | +import { App } from "../common/app"; |
| 2 | +import { DisposableObject } from "../common/disposable-object"; |
| 3 | +import { AppEvent, AppEventEmitter } from "../common/events"; |
| 4 | +import { DatabaseItem } from "../databases/local-databases"; |
| 5 | + |
| 6 | +interface DbModelingState { |
| 7 | + // Currently empty but will soon contain information about methods, etc. |
| 8 | +} |
| 9 | + |
| 10 | +export class ModelingStore extends DisposableObject { |
| 11 | + public readonly onActiveDbChanged: AppEvent<void>; |
| 12 | + public readonly onDbClosed: AppEvent<string>; |
| 13 | + |
| 14 | + private state: Map<string, DbModelingState>; |
| 15 | + private activeDb: string | undefined; |
| 16 | + |
| 17 | + private readonly onActiveDbChangedEventEmitter: AppEventEmitter<void>; |
| 18 | + private readonly onDbClosedEventEmitter: AppEventEmitter<string>; |
| 19 | + |
| 20 | + constructor(app: App) { |
| 21 | + super(); |
| 22 | + |
| 23 | + // State initialization |
| 24 | + this.activeDb = undefined; |
| 25 | + this.state = new Map<string, DbModelingState>(); |
| 26 | + |
| 27 | + // Event initialization |
| 28 | + this.onActiveDbChangedEventEmitter = this.push( |
| 29 | + app.createEventEmitter<void>(), |
| 30 | + ); |
| 31 | + this.onActiveDbChanged = this.onActiveDbChangedEventEmitter.event; |
| 32 | + |
| 33 | + this.onDbClosedEventEmitter = this.push(app.createEventEmitter<string>()); |
| 34 | + this.onDbClosed = this.onDbClosedEventEmitter.event; |
| 35 | + } |
| 36 | + |
| 37 | + public initializeStateForDb(databaseItem: DatabaseItem) { |
| 38 | + const dbUri = databaseItem.databaseUri.toString(); |
| 39 | + this.state.set(dbUri, { |
| 40 | + databaseItem, |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + public setActiveDb(databaseItem: DatabaseItem) { |
| 45 | + this.activeDb = databaseItem.databaseUri.toString(); |
| 46 | + this.onActiveDbChangedEventEmitter.fire(); |
| 47 | + } |
| 48 | + |
| 49 | + public removeDb(databaseItem: DatabaseItem) { |
| 50 | + const dbUri = databaseItem.databaseUri.toString(); |
| 51 | + |
| 52 | + if (!this.state.has(dbUri)) { |
| 53 | + throw Error("Cannot remove a database that has not been initialized"); |
| 54 | + } |
| 55 | + |
| 56 | + if (this.activeDb === dbUri) { |
| 57 | + this.activeDb = undefined; |
| 58 | + this.onActiveDbChangedEventEmitter.fire(); |
| 59 | + } |
| 60 | + |
| 61 | + this.state.delete(dbUri); |
| 62 | + this.onDbClosedEventEmitter.fire(dbUri); |
| 63 | + } |
| 64 | +} |
0 commit comments