Skip to content

Commit

Permalink
feat: Make client background service ready
Browse files Browse the repository at this point in the history
  • Loading branch information
krille-chan committed Jan 7, 2024
1 parent a753746 commit ecf27dc
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1271,21 +1271,6 @@ class Client extends MatrixApi {
Duration timeoutForServerRequests = const Duration(seconds: 8),
bool returnNullIfSeen = true,
}) async {
// Get access token if necessary:
final database = _database ??= await databaseBuilder?.call(this);
if (!isLogged()) {
if (database == null) {
throw Exception(
'Can not execute getEventByPushNotification() without a database');
}
final clientInfoMap = await database.getClient(clientName);
final token = clientInfoMap?.tryGet<String>('token');
if (token == null) {
throw Exception('Client is not logged in.');
}
accessToken = token;
}

// Check if the notification contains an event at all:
final eventId = notification.eventId;
final roomId = notification.roomId;
Expand All @@ -1298,6 +1283,7 @@ class Client extends MatrixApi {
id: roomId,
client: this,
);
await room.loadHeroUsers();
final roomName = notification.roomName;
final roomAlias = notification.roomAlias;
if (roomName != null) {
Expand Down Expand Up @@ -1414,7 +1400,7 @@ class Client extends MatrixApi {

if (storeInDatabase) {
await database?.transaction(() async {
await database.storeEventUpdate(
await database?.storeEventUpdate(
EventUpdate(
roomID: roomId,
type: EventUpdateType.timeline,
Expand Down Expand Up @@ -1453,6 +1439,7 @@ class Client extends MatrixApi {
String? newOlmAccount,
bool waitForFirstSync = true,
bool waitUntilLoadCompletedLoaded = true,
bool isBackgroundClient = false,

/// Will be called if the app performs a migration task from the [legacyDatabaseBuilder]
void Function()? onMigration,
Expand Down Expand Up @@ -1577,13 +1564,15 @@ class Client extends MatrixApi {
encryption?.pickledOlmAccount,
);
}
userDeviceKeysLoading = database
.getUserDeviceKeys(this)
.then((keys) => _userDeviceKeys = keys);
roomsLoading = database.getRoomList(this).then((rooms) {
_rooms = rooms;
_sortRooms();
});
if (!isBackgroundClient) {
userDeviceKeysLoading = database
.getUserDeviceKeys(this)
.then((keys) => _userDeviceKeys = keys);
roomsLoading = database.getRoomList(this).then((rooms) {
_rooms = rooms;
_sortRooms();
});
}
_accountDataLoading = database.getAccountData().then((data) {
_accountData = data;
_updatePushrules();
Expand All @@ -1603,7 +1592,10 @@ class Client extends MatrixApi {
);

/// Timeout of 0, so that we don't see a spinner for 30 seconds.
firstSyncReceived = _sync(timeout: Duration.zero);
if (!isBackgroundClient) {
firstSyncReceived = _sync(timeout: Duration.zero);
}

if (waitForFirstSync) {
await firstSyncReceived;
}
Expand Down Expand Up @@ -1673,7 +1665,7 @@ class Client extends MatrixApi {
/// Immediately start a sync and wait for completion.
/// If there is an active sync already, wait for the active sync instead.
Future<void> oneShotSync() {
return _sync();
return _currentSync ??= _innerSync();
}

/// Pass a timeout to set how long the server waits before sending an empty response.
Expand Down

0 comments on commit ecf27dc

Please sign in to comment.