Skip to content

Commit

Permalink
Update session.dart (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasyishak authored Mar 30, 2023
1 parent 4c8641c commit 1e37e1c
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions pkgs/unified_analytics/lib/src/session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:convert';
import 'dart:io';

import 'package:clock/clock.dart';
import 'package:file/file.dart';
Expand Down Expand Up @@ -76,18 +77,24 @@ class Session {
/// to date incase another tool is also calling this package and
/// making updates to the session file
void _refreshSessionData() {
try {
/// Using a nested function here to reduce verbosity
void parseContents() {
final String sessionFileContents = _sessionFile.readAsStringSync();
final Map<String, Object?> sessionObj = jsonDecode(sessionFileContents);
_sessionId = sessionObj['session_id'] as int;
_lastPing = sessionObj['last_ping'] as int;
}

try {
parseContents();
} on FormatException {
Initializer.createSessionFile(sessionFile: _sessionFile);

final String sessionFileContents = _sessionFile.readAsStringSync();
final Map<String, Object?> sessionObj = jsonDecode(sessionFileContents);
_sessionId = sessionObj['session_id'] as int;
_lastPing = sessionObj['last_ping'] as int;
parseContents();
} on PathNotFoundException {
Initializer.createSessionFile(sessionFile: _sessionFile);

parseContents();
}
}
}

0 comments on commit 1e37e1c

Please sign in to comment.