@@ -5,15 +5,19 @@ import { FileSystem } from './FileSystem.js';
55
66interface FileSession {
77 readonly file : string ;
8- readonly sessionId : string ;
8+ readonly sessionId : SessionId ;
99 readonly escapedSessionId : string ;
10- readonly timestamp : number ;
1110}
1211
1312type SessionEvents = {
1413 unlocked : [ ] ;
1514} ;
1615
16+ export interface SessionId {
17+ readonly id : string ;
18+ readonly timestamp : number ;
19+ }
20+
1721const SESSION_MARKER_PREFIX = 'bt-session' ;
1822
1923const isDefined = < T > ( t : T | undefined ) : t is T => ! ! t ;
@@ -30,17 +34,16 @@ export class SessionFiles implements BacktraceModule {
3034 constructor (
3135 private readonly _fileSystem : FileSystem ,
3236 private readonly _directory : string ,
33- public readonly sessionId : string ,
37+ public readonly sessionId : SessionId ,
3438 private readonly _maxPreviousLockedSessions = 1 ,
35- public readonly timestamp = Date . now ( ) ,
3639 private readonly _lockable = true ,
3740 ) {
38- this . _escapedSessionId = this . escapeFileName ( sessionId ) ;
41+ this . _escapedSessionId = this . escapeFileName ( sessionId . id ) ;
3942 this . marker = this . getFileName ( SESSION_MARKER_PREFIX ) ;
4043 }
4144
4245 public initialize ( ) : void {
43- this . createSessionMarker ( ) ;
46+ return this . createSessionMarker ( ) ;
4447 }
4548
4649 public getPreviousSession ( ) {
@@ -57,12 +60,14 @@ export class SessionFiles implements BacktraceModule {
5760 . filter ( ( f ) => f . startsWith ( SESSION_MARKER_PREFIX ) )
5861 . map ( ( f ) => this . getFileSession ( f ) )
5962 . filter ( isDefined )
60- . sort ( ( a , b ) => b . timestamp - a . timestamp ) ;
63+ . sort ( ( a , b ) => b . sessionId . timestamp - a . sessionId . timestamp ) ;
6164
62- const currentSessionMarker = sessionMarkers . find ( ( s ) => s . sessionId === this . sessionId ) ;
65+ const currentSessionMarker = sessionMarkers . find ( ( s ) => this . sessionIdEquals ( s . sessionId , this . sessionId ) ) ;
6366
6467 const lastSessionMarker = currentSessionMarker
65- ? sessionMarkers . filter ( ( { timestamp } ) => currentSessionMarker . timestamp > timestamp ) [ 0 ]
68+ ? sessionMarkers . filter (
69+ ( { sessionId } ) => currentSessionMarker . sessionId . timestamp > sessionId . timestamp ,
70+ ) [ 0 ]
6671 : sessionMarkers [ 0 ] ;
6772
6873 if ( ! lastSessionMarker ) {
@@ -74,15 +79,14 @@ export class SessionFiles implements BacktraceModule {
7479 this . _directory ,
7580 lastSessionMarker . sessionId ,
7681 this . _maxPreviousLockedSessions - 1 ,
77- lastSessionMarker . timestamp ,
7882 this . _maxPreviousLockedSessions > 0 ,
7983 ) ) ;
8084 }
8185
82- public getSessionWithId ( sessionId : string ) {
86+ public getSessionWithId ( sessionId : SessionId ) {
8387 // eslint-disable-next-line @typescript-eslint/no-this-alias
8488 let session : SessionFiles | undefined = this ;
85- while ( session && session . sessionId !== sessionId ) {
89+ while ( session && this . sessionIdEquals ( session . sessionId , sessionId ) ) {
8690 session = session . getPreviousSession ( ) ;
8791 }
8892
@@ -124,7 +128,11 @@ export class SessionFiles implements BacktraceModule {
124128 public getFileName ( prefix : string ) {
125129 this . throwIfCleared ( ) ;
126130
127- return this . _directory + '/' + `${ this . escapeFileName ( prefix ) } _${ this . _escapedSessionId } _${ this . timestamp } ` ;
131+ return (
132+ this . _directory +
133+ '/' +
134+ `${ this . escapeFileName ( prefix ) } _${ this . _escapedSessionId } _${ this . sessionId . timestamp } `
135+ ) ;
128136 }
129137
130138 public getSessionFiles ( ) {
@@ -134,7 +142,7 @@ export class SessionFiles implements BacktraceModule {
134142 return files
135143 . map ( ( file ) => this . getFileSession ( file ) )
136144 . filter ( isDefined )
137- . filter ( ( { sessionId } ) => sessionId === this . sessionId )
145+ . filter ( ( { sessionId } ) => this . sessionIdEquals ( sessionId , this . sessionId ) )
138146 . map ( ( { file } ) => this . _directory + '/' + file ) ;
139147 }
140148
@@ -184,7 +192,14 @@ export class SessionFiles implements BacktraceModule {
184192 return undefined ;
185193 }
186194
187- return { file, escapedSessionId, timestamp, sessionId : this . unescapeFileName ( escapedSessionId ) } ;
195+ return {
196+ file,
197+ escapedSessionId,
198+ sessionId : {
199+ timestamp,
200+ id : this . unescapeFileName ( escapedSessionId ) ,
201+ } ,
202+ } ;
188203 }
189204
190205 private readDirectoryFiles ( ) {
@@ -232,4 +247,8 @@ export class SessionFiles implements BacktraceModule {
232247 throw new Error ( 'This session files are cleared.' ) ;
233248 }
234249 }
250+
251+ private sessionIdEquals ( a : SessionId , b : SessionId ) {
252+ return a . id === b . id && a . timestamp === b . timestamp ;
253+ }
235254}
0 commit comments