Skip to content

Commit

Permalink
override notifier to notify supabase subscriptions by default
Browse files Browse the repository at this point in the history
  • Loading branch information
tshedor committed Oct 29, 2024
1 parent ff3efaf commit bdb9eaa
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ abstract class OfflineFirstRepository<RepositoryModel extends OfflineFirstModel>
/// Iterate through subscriptions after an upsert and notify any [subscribe] listeners.
@protected
@visibleForTesting
@visibleForOverriding
Future<void> notifySubscriptionsWithLocalData<TModel extends RepositoryModel>({
bool notifyWhenEmpty = true,
Map<Query?, StreamController<List<RepositoryModel>>>? subscriptionsByQuery,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ abstract class OfflineFirstWithSupabaseRepository
await offlineRequestQueue.client.requestManager.migrate();
}

@override
Future<void> notifySubscriptionsWithLocalData<TModel extends OfflineFirstWithSupabaseModel>({
bool notifyWhenEmpty = true,
Map<Query?, StreamController<List<OfflineFirstWithSupabaseModel>>>? subscriptionsByQuery,
}) async {
final supabaseControllers = supabaseRealtimeSubscriptions[TModel]
?.values
.fold(<Query, StreamController<List<OfflineFirstWithSupabaseModel>>>{}, (acc, eventMap) {
acc.addEntries(eventMap.entries);
return acc;
});
final subs = {
...?subscriptionsByQuery,
...?subscriptions[TModel],
...?supabaseControllers,
};
await super.notifySubscriptionsWithLocalData<TModel>(
notifyWhenEmpty: notifyWhenEmpty,
subscriptionsByQuery: subs,
);
}

/// Supabase's realtime payload only returns unique columns;
/// the instance must be discovered from these values so it
/// can be deleted by all providers.
Expand Down Expand Up @@ -303,9 +325,7 @@ abstract class OfflineFirstWithSupabaseRepository
memoryCacheProvider.upsert<TModel>(instance, repository: this);
}

await notifySubscriptionsWithLocalData<TModel>(
subscriptionsByQuery: supabaseRealtimeSubscriptions[TModel]![eventType]!,
);
await notifySubscriptionsWithLocalData<TModel>();
},
)
.subscribe();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class Customer extends OfflineFirstWithSupabaseModel {
other.id == id &&
other.firstName == firstName &&
other.lastName == lastName;

@override
String toString() {
return 'Customer(id: $id, firstName: $firstName, lastName: $lastName, pizzas: $pizzas)';
}
}

@ConnectOfflineFirstWithSupabase(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ void main() async {
expect(
customers,
emitsInOrder([
[],
[],
[customer],
]),
Expand Down Expand Up @@ -473,6 +474,7 @@ void main() async {
expect(
customers,
emitsInOrder([
[customer],
[customer],
[],
]),
Expand Down Expand Up @@ -522,6 +524,7 @@ void main() async {
expect(
customers,
emitsInOrder([
[customer1],
[customer1],
[customer2],
]),
Expand Down Expand Up @@ -557,6 +560,7 @@ void main() async {
expect(
customers,
emitsInOrder([
[],
[],
[customer],
]),
Expand Down Expand Up @@ -597,6 +601,7 @@ void main() async {
expect(
customers,
emitsInOrder([
[customer],
[customer],
[],
]),
Expand Down Expand Up @@ -645,6 +650,7 @@ void main() async {
expect(
customers,
emitsInOrder([
[customer1],
[customer1],
[customer2],
]),
Expand Down Expand Up @@ -683,6 +689,7 @@ void main() async {
expect(
customers,
emitsInOrder([
[],
[],
[customer1],
[customer2],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ class SupabaseResponse {
this.data, {
this.headers,
this.realtimeSubsequentReplies = const [],
this.realtimeSubsequentReplyDelay = const Duration(milliseconds: 10),
this.realtimeSubsequentReplyDelay = const Duration(milliseconds: 30),
});
}

0 comments on commit bdb9eaa

Please sign in to comment.