-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathversion-control.service.ts
212 lines (188 loc) · 6.24 KB
/
version-control.service.ts
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import {Injectable, OnDestroy} from "@angular/core";
import {
VariantControllerBackendService,
VariantDto,
VersionControllerBackendService,
VersionDto,
} from "../../api/generated";
import {filter, map, takeUntil} from "rxjs/operators";
import {BehaviorSubject, Observable, Subject} from "rxjs";
import {DataService} from "./data.service";
import {NetzgrafikDto} from "../../data-structures/business.data.structures";
import {AutoSaveService} from "./auto-save.service";
import {LogService} from "../../logger/log.service";
import {VersionId} from "../../view/variant/variant-view/variant-history/model";
import {UndoService} from "./undo.service";
import {environment} from "../../../environments/environment";
@Injectable({
providedIn: "root",
})
export class VersionControlService implements OnDestroy {
readonly variantSubject = new BehaviorSubject<VariantDto | null>(null);
readonly variant$ = this.variantSubject.asObservable();
readonly versionCreatedSubject = new Subject<VersionId>();
readonly versionCreated$ = this.versionCreatedSubject.asObservable();
private readonly destroyed = new Subject<void>();
private previousVariantDto: VariantDto = undefined;
private variantChanged = false;
constructor(
private readonly variantsBackendService: VariantControllerBackendService,
private readonly versionsBackendService: VersionControllerBackendService,
private readonly dataService: DataService,
private readonly autoSaveService: AutoSaveService,
private readonly undoService: UndoService,
private readonly logService: LogService,
) {
if (!environment.disableBackend) {
autoSaveService.autosaveTrigger$
.pipe(
takeUntil(this.destroyed),
filter(() => this.variant.isWritable),
)
.subscribe(() => {
logService.debug("auto saving changes");
this.createSnapshot();
});
}
}
get variant(): VariantDto {
return this.variantSubject.value;
}
ngOnDestroy(): void {
this.destroyed.next();
this.destroyed.complete();
}
load(variantId: number, loadModel: boolean): void {
this.variantsBackendService
.getVariant(variantId)
.pipe(takeUntil(this.destroyed))
.subscribe((variant) => {
this.updateVarianteChangedInfo(variant);
this.variantSubject.next(variant);
if (loadModel) {
this.loadModel(variant.latestVersion);
} else {
this.autoSaveService.reset();
this.undoService.reset(variantId);
}
});
}
loadNetzgrafikDTO(netzgrafik: NetzgrafikDto) {
this.dataService.loadNetzgrafikDto(netzgrafik);
this.autoSaveService.reset();
this.undoService.reset(this.undoService.getCurrentVariantId() + 1);
}
reload(loadModel = false): void {
this.load(this.variant.id, loadModel);
}
getVariantIsWritable(): boolean {
if (this.variant === null) {
return true;
}
return this.variant.isWritable;
}
createSnapshot(newName?: string): void {
const name = newName || this.variant.latestVersion.name;
this.versionsBackendService
.createSnapshotVersion(this.variant.latestVersion.id, {
name,
comment: newName ? `Neuer Name: ${newName}` : "",
model: JSON.stringify(this.dataService.getNetzgrafikDto()),
})
.pipe(takeUntil(this.destroyed))
.subscribe((versionId) => {
this.versionCreatedSubject.next(versionId);
this.reload();
});
}
createRelease(comment: string): void {
this.versionsBackendService
.createReleaseVersion(this.variant.latestVersion.id, {
comment,
})
.pipe(takeUntil(this.destroyed))
.subscribe((versionId) => {
this.versionCreatedSubject.next(versionId);
this.reload();
});
}
restoreVersion(versionId: number): void {
this.versionsBackendService
.restoreVersion(versionId)
.pipe(takeUntil(this.destroyed))
.subscribe((newVersionId) => {
this.versionCreatedSubject.next(newVersionId);
this.reload(true);
});
}
loadModel(version: VersionDto): void {
this.versionsBackendService
.getVersionModel(this.variant.latestVersion.id)
.pipe(
takeUntil(this.destroyed),
map((model) => model as NetzgrafikDto),
)
.subscribe((netzgrafik) => {
this.dataService.loadNetzgrafikDto(netzgrafik);
this.autoSaveService.reset();
this.undoService.reset(version.variantId);
});
}
dropChanges(): void {
this.variantsBackendService
.dropSnapshots(this.variant.id)
.pipe(takeUntil(this.destroyed))
.subscribe(() => this.reload(true));
}
raiseSnapshotsToNewestReleaseVersion(): void {
this.variantsBackendService
.raiseSnapshotsToNewestReleaseVersion(this.variant.id)
.pipe(takeUntil(this.destroyed))
.subscribe(() => this.reload());
}
archiveVariant(): void {
this.variantsBackendService
.archiveVariant(this.variant.id)
.pipe(takeUntil(this.destroyed))
.subscribe(() => this.reload());
}
unarchiveVariant(): void {
this.variantsBackendService
.unarchiveVariant(this.variant.id)
.pipe(takeUntil(this.destroyed))
.subscribe(() => this.reload());
}
archiveVariantWithId(variantId: number): Observable<any> {
return this.variantsBackendService
.archiveVariant(variantId)
.pipe(takeUntil(this.destroyed));
}
unarchiveVariantWithId(variantId: number): Observable<any> {
return this.variantsBackendService
.unarchiveVariant(variantId)
.pipe(takeUntil(this.destroyed));
}
deleteVariant(): Observable<void> {
return this.variantsBackendService
.deleteVariant(this.variant.id)
.pipe(map(() => null));
}
getAndClearVarianteChangedSignal(): boolean {
const retVal = this.variantChanged;
this.variantChanged = false;
return retVal;
}
private updateVarianteChangedInfo(variant: VariantDto) {
if (variant === null) {
return;
}
if (this.previousVariantDto === undefined) {
this.previousVariantDto = variant;
this.variantChanged = true;
return;
}
this.variantChanged = this.previousVariantDto.projectId !== variant.projectId &&
this.previousVariantDto.id !== variant.id;
this.previousVariantDto = variant;
}
}