Skip to content

feat(offline_first): set policy for associations #569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ abstract class OfflineFirstRepository<TRepositoryModel extends OfflineFirstModel
/// is ignorable (e.g. eager loading). Defaults to `false`.
@override
Future<List<TModel>> get<TModel extends TRepositoryModel>({
OfflineFirstGetPolicy policy = OfflineFirstGetPolicy.awaitRemoteWhenNoneExist,
OfflineFirstGetPolicy policy =
OfflineFirstGetPolicy.awaitRemoteWhenNoneExist,
OfflineFirstGetPolicy? associationPolicy,
Query? query,
bool seedOnly = false,
}) async {
Expand All @@ -189,7 +191,7 @@ abstract class OfflineFirstRepository<TRepositoryModel extends OfflineFirstModel
final alwaysHydrate = policy == OfflineFirstGetPolicy.alwaysHydrate;

try {
_latestGetPolicy = policy;
_latestGetPolicy = associationPolicy ?? policy;

if (memoryCacheProvider.canFind<TModel>(query) && !requireRemote) {
final memoryCacheResults = memoryCacheProvider.get<TModel>(query: query, repository: this);
Expand Down Expand Up @@ -246,6 +248,7 @@ abstract class OfflineFirstRepository<TRepositoryModel extends OfflineFirstModel
Future<List<TModel>> getBatched<TModel extends TRepositoryModel>({
int batchSize = 50,
OfflineFirstGetPolicy policy = OfflineFirstGetPolicy.awaitRemoteWhenNoneExist,
OfflineFirstGetPolicy? associationPolicy,
Query? query,
bool seedOnly = false,
}) async {
Expand All @@ -267,6 +270,7 @@ abstract class OfflineFirstRepository<TRepositoryModel extends OfflineFirstModel
final results = await get<TModel>(
query: recursiveQuery,
policy: policy,
associationPolicy: associationPolicy,
seedOnly: seedOnly,
);
total.addAll(results);
Expand Down Expand Up @@ -363,6 +367,7 @@ abstract class OfflineFirstRepository<TRepositoryModel extends OfflineFirstModel
/// The stream will not close naturally.
Stream<List<TModel>> subscribe<TModel extends TRepositoryModel>({
OfflineFirstGetPolicy policy = OfflineFirstGetPolicy.localOnly,
OfflineFirstGetPolicy? associationPolicy,
Query? query,
}) {
query ??= const Query();
Expand All @@ -384,7 +389,10 @@ abstract class OfflineFirstRepository<TRepositoryModel extends OfflineFirstModel
subscriptions[TModel]?[query] = controller;

// ignore: discarded_futures
get<TModel>(query: query, policy: policy).then(
get<TModel>(
query: query,
policy: policy,
associationPolicy: associationPolicy,).then(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it's missing a formatting check

(results) {
if (!controller.isClosed) controller.add(results);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,16 @@ abstract class OfflineFirstWithSupabaseRepository<

@override
Future<List<TModel>> get<TModel extends TRepositoryModel>({
OfflineFirstGetPolicy policy = OfflineFirstGetPolicy.awaitRemoteWhenNoneExist,
OfflineFirstGetPolicy? associationPolicy,
OfflineFirstGetPolicy policy =
OfflineFirstGetPolicy.awaitRemoteWhenNoneExist,
Query? query,
bool seedOnly = false,
}) async {
try {
return await super.get<TModel>(
policy: policy,
associationPolicy: associationPolicy,
query: query,
seedOnly: seedOnly,
);
Expand Down Expand Up @@ -257,6 +260,7 @@ abstract class OfflineFirstWithSupabaseRepository<
/// is valid. The [Compare] operator is limited to a [PostgresChangeFilterType] equivalent.
/// See [_compareToFilterParam] for a precise breakdown.
Stream<List<TModel>> subscribeToRealtime<TModel extends TRepositoryModel>({
OfflineFirstGetPolicy? associationPolicy,
PostgresChangeEvent eventType = PostgresChangeEvent.all,
OfflineFirstGetPolicy policy = OfflineFirstGetPolicy.alwaysHydrate,
Query? query,
Expand Down Expand Up @@ -291,7 +295,7 @@ abstract class OfflineFirstWithSupabaseRepository<
case PostgresChangeEvent.all:
final localResults = await sqliteProvider.get<TModel>(repository: this);
final remoteResults =
await get<TModel>(query: query, policy: OfflineFirstGetPolicy.awaitRemote);
await get<TModel>(query: query, policy: OfflineFirstGetPolicy.awaitRemote, associationPolicy: associationPolicy);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both OfflineFirstWithRestRepository and OfflineFirstWithGraphqlRepository need to be updated

final toDelete = localResults.where((r) => !remoteResults.contains(r));

for (final deletableModel in toDelete) {
Expand All @@ -310,6 +314,7 @@ abstract class OfflineFirstWithSupabaseRepository<
final results = await get<TModel>(
query: query,
policy: OfflineFirstGetPolicy.localOnly,
associationPolicy: associationPolicy,
seedOnly: true,
);
if (results.isEmpty) return;
Expand All @@ -328,6 +333,7 @@ abstract class OfflineFirstWithSupabaseRepository<
await get<TModel>(
query: query,
policy: OfflineFirstGetPolicy.alwaysHydrate,
associationPolicy: associationPolicy,
seedOnly: true,
);

Expand Down Expand Up @@ -370,7 +376,9 @@ abstract class OfflineFirstWithSupabaseRepository<

// Fetch initial data
// ignore: discarded_futures
get<TModel>(query: query, policy: policy).then((results) {
get<TModel>(query: query,
policy: policy,
associationPolicy: associationPolicy,).then((results) {
if (!controller.isClosed) controller.add(results);
});

Expand Down