Skip to content

Commit 53a806a

Browse files
Fix used counter in presets view (#2432)
* Add fix for counter issue * Change applied collection from array to set * Clear the session id before adding one * Fix linting issue * Change function name * Add check to verify session ids in set are string
1 parent e24b3bc commit 53a806a

File tree

5 files changed

+35
-23
lines changed

5 files changed

+35
-23
lines changed

application/client/src/app/service/history/collections.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface ICollection {
2626
last: number;
2727
used: number;
2828
uuid: string;
29+
applied_sessions: Set<string>;
2930
preset: boolean;
3031
relations: string[];
3132
entries: JsonSet;
@@ -47,6 +48,7 @@ export class Collections implements EntryConvertable, Equal<Collections>, Empty
4748
created: Date.now(),
4849
used: 1,
4950
uuid: smth.uuid(),
51+
applied_sessions: new Set([smth.uuid()]),
5052
preset: false,
5153
relations: [],
5254
origin: undefined,
@@ -98,6 +100,7 @@ export class Collections implements EntryConvertable, Equal<Collections>, Empty
98100
created: Date.now(),
99101
used: 1,
100102
uuid: uuid,
103+
applied_sessions: new Set(),
101104
preset: false,
102105
relations: [],
103106
origin: undefined,
@@ -117,6 +120,7 @@ export class Collections implements EntryConvertable, Equal<Collections>, Empty
117120
last: obj.getAsValidNumber(src, 'l'),
118121
preset: obj.getAsBool(src, 'p'),
119122
uuid: obj.getAsNotEmptyString(src, 'uu'),
123+
applied_sessions: obj.getAsSetOfStringsOrEmpty(src, 'as'),
120124
relations: obj.getAsArray(src, 'r'),
121125
origin: obj.getAsNotEmptyStringOrAsUndefined(src, 'o'),
122126
entries: obj.getAsObj(src, 'e'),
@@ -130,6 +134,7 @@ export class Collections implements EntryConvertable, Equal<Collections>, Empty
130134
public used: number;
131135
public last: number;
132136
public uuid: string;
137+
public applied_sessions: Set<string>;
133138
public preset: boolean;
134139
public relations: string[];
135140
public origin: string | undefined;
@@ -156,6 +161,7 @@ export class Collections implements EntryConvertable, Equal<Collections>, Empty
156161
this.last = definition.last;
157162
this.created = definition.created;
158163
this.uuid = definition.uuid;
164+
this.applied_sessions = definition.applied_sessions;
159165
this.relations = definition.relations;
160166
this.origin = definition.origin;
161167
this.preset = definition.preset;
@@ -182,6 +188,7 @@ export class Collections implements EntryConvertable, Equal<Collections>, Empty
182188
used: this.used,
183189
created: this.created,
184190
uuid: this.uuid,
191+
applied_sessions: this.applied_sessions,
185192
relations: this.relations,
186193
preset: this.preset,
187194
last: this.last,
@@ -194,27 +201,6 @@ export class Collections implements EntryConvertable, Equal<Collections>, Empty
194201
);
195202
}
196203

197-
public copy(): Collections {
198-
const uuid = unique();
199-
return new Collections(
200-
`Collections:${uuid}`,
201-
{
202-
name: this.name,
203-
last: Date.now(),
204-
created: Date.now(),
205-
used: 1,
206-
uuid,
207-
preset: false,
208-
relations: [],
209-
origin: undefined,
210-
entries: this.asCollectionsArray()
211-
.map((c) => c.as().jsonSet())
212-
.flat(),
213-
},
214-
this.storage,
215-
);
216-
}
217-
218204
public delete() {
219205
this.storage.delete(this);
220206
}
@@ -290,6 +276,7 @@ export class Collections implements EntryConvertable, Equal<Collections>, Empty
290276
this.last = definition.last;
291277
this.created = definition.created;
292278
this.uuid = definition.uuid;
279+
this.applied_sessions = definition.applied_sessions;
293280
this.relations = definition.relations;
294281
this.origin = definition.origin;
295282
this.preset = definition.preset;

application/client/src/app/service/history/session.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ export class HistorySession extends Subscriber {
153153
this.unsubscribe();
154154
this.collections = collections;
155155
this.collections.subscribe(this, this.session);
156+
this.storage.collections.clearSession(this.session.uuid());
157+
this.collections.applied_sessions.add(this.session.uuid());
156158
this.register(
157159
this.collections.updated.subscribe(() => {
158160
this.collections.updateUuid(this.storage.collections.update(this.collections));
@@ -163,6 +165,7 @@ export class HistorySession extends Subscriber {
163165
}
164166

165167
public destroy() {
168+
this.collections.applied_sessions.delete(this.session.uuid());
166169
this.unsubscribe();
167170
this.globals.unsubscribe();
168171
this.subjects.destroy();

application/client/src/app/service/history/storage.collections.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ export class StorageCollections {
6464
await this.save();
6565
}
6666

67+
public clearSession(session_id: string): void {
68+
this.collections.forEach((collection, _key, _map) => {
69+
collection.applied_sessions.delete(session_id);
70+
});
71+
}
72+
6773
public update(collections: Collections): string {
6874
const existed = Array.from(this.collections.values()).find((c) => c.isSame(collections));
6975
if (this.collections.has(collections.uuid) || existed === undefined) {

application/client/src/app/ui/views/toolbar/history/preset/component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ export class Preset extends ChangesDetector implements AfterContentInit {
8181
public getName(): string {
8282
if (this.collections.name === '-') {
8383
return `${new Date(this.collections.last).toLocaleDateString('en-US')} (${
84-
this.collections.used
84+
this.collections.applied_sessions.size
8585
})`;
8686
} else {
87-
return `${this.collections.name}(${this.collections.used})`;
87+
return `${this.collections.name}(${this.collections.applied_sessions.size})`;
8888
}
8989
}
9090

application/platform/env/obj.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,19 @@ export function objToStringMap(obj: {
416416
});
417417
return output;
418418
}
419+
420+
export function getAsSetOfStringsOrEmpty(src: any, key: string): Set<string> {
421+
const target = src[key];
422+
423+
if (!(target instanceof Set)) {
424+
return new Set();
425+
}
426+
427+
target.forEach((el: any) => {
428+
if (typeof el !== 'string') {
429+
throw new Error(`Parameter "${key}" must be a Set of strings. Found a non-string value.`);
430+
}
431+
});
432+
433+
return target as Set<string>;
434+
}

0 commit comments

Comments
 (0)