Skip to content

Commit

Permalink
finish writing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tshedor committed Oct 28, 2024
1 parent ce6d6de commit 74a3b10
Show file tree
Hide file tree
Showing 8 changed files with 406 additions and 169 deletions.
1 change: 1 addition & 0 deletions packages/brick_offline_first/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Added `subscriptionByQuery` to `OfflineFirstRepository#notifySubscriptionsWithLocalData` to pass a custom map of `StreamControllers`
- Add `GetFirstMixin` for convenient retrieval of the first results of `OfflineFirstRepository#get`
- Close all controllers in `OfflineFirstRepository#subscriptions` and clear the map on `OfflineFirstRepository#reset`

## 3.2.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,9 @@ abstract class OfflineFirstRepository<RepositoryModel extends OfflineFirstModel>
@visibleForTesting
Future<void> notifySubscriptionsWithLocalData<TModel extends RepositoryModel>({
bool notifyWhenEmpty = true,
Map<Query?, StreamController<List<RepositoryModel>>>? subscriptionByQuery,
Map<Query?, StreamController<List<RepositoryModel>>>? subscriptionsByQuery,
}) async {
final queriesControllers = (subscriptionByQuery ?? subscriptions[TModel])?.entries;
final queriesControllers = (subscriptionsByQuery ?? subscriptions[TModel])?.entries;
if (queriesControllers?.isEmpty ?? true) return;

// create a copy of the controllers to avoid concurrent modification while looping
Expand Down Expand Up @@ -337,6 +337,12 @@ abstract class OfflineFirstRepository<RepositoryModel extends OfflineFirstModel>
Future<void> reset() async {
await sqliteProvider.resetDb();
memoryCacheProvider.reset();
for (final subscription in subscriptions.values) {
for (final controller in subscription.values) {
await controller.close();
}
}
subscriptions.clear();
}

/// Listen for streaming changes when the [sqliteProvider].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,19 @@ abstract class OfflineFirstWithSupabaseRepository
);
}

@override
Future<void> reset() async {
await super.reset();
for (final subscription in supabaseRealtimeSubscriptions.values) {
for (final eventType in subscription.values) {
for (final controller in eventType.values) {
await controller.close();
}
}
}
supabaseRealtimeSubscriptions.clear();
}

/// Subscribes to realtime updates using
/// [Supabase channels](https://supabase.com/docs/guides/realtime?queryGroups=language&language=dart).
/// **This will only work if your Supabase table has realtime enabled.**
Expand All @@ -182,10 +195,7 @@ abstract class OfflineFirstWithSupabaseRepository
///
/// See [subscribe] for reactivity without using realtime.
///
/// `eventType` is the triggering remote event. It is **strongly recommended to not use
/// `PostgresChangeEvent.all`**. This event type requires manual reconcilliation
/// of local and remote data and can be extremely expensive. Instead, use individual
/// events like `PostgresChangeEvent.insert` or `PostgresChangeEvent.delete`.
/// `eventType` is the triggering remote event.
///
/// `policy` determines how data is fetched (local or remote). When `.localOnly`,
/// Supabase channels will not be used.
Expand Down Expand Up @@ -221,6 +231,11 @@ abstract class OfflineFirstWithSupabaseRepository
filter: queryToPostgresChangeFilter<TModel>(query),
callback: (payload) async {
switch (payload.eventType) {
// This code path is likely never hit; `PostgresChangeEvent.all` is used
// to listen to changes but as far as can be determined is not delivered within
// the payload of the callback.
//
// It's handled just in case this behavior changes.
case PostgresChangeEvent.all:
final localResults = await sqliteProvider.get<TModel>(repository: this);
final remoteResults =
Expand Down Expand Up @@ -263,12 +278,15 @@ abstract class OfflineFirstWithSupabaseRepository
provider: remoteProvider,
repository: this,
);
await sqliteProvider.delete<TModel>(instance as TModel, repository: this);
final primaryKey =
await sqliteProvider.primaryKeyByUniqueColumns<TModel>(instance as TModel);
instance.primaryKey = primaryKey;
await sqliteProvider.delete<TModel>(instance, repository: this);
memoryCacheProvider.delete<TModel>(instance, repository: this);
}

await notifySubscriptionsWithLocalData<TModel>(
subscriptionByQuery: supabaseRealtimeSubscriptions[TModel]![eventType],
subscriptionsByQuery: supabaseRealtimeSubscriptions[TModel]![eventType],
);
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ class Customer extends OfflineFirstWithSupabaseModel {
});

@override
int get hashCode => id.hashCode;
int get hashCode => id.hashCode ^ firstName.hashCode ^ lastName.hashCode;

@override
bool operator ==(Object other) => other is Customer && other.id == id;
bool operator ==(Object other) =>
other is Customer &&
other.id == id &&
other.firstName == firstName &&
other.lastName == lastName;
}

@ConnectOfflineFirstWithSupabase(
Expand Down
Loading

0 comments on commit 74a3b10

Please sign in to comment.