Skip to content
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

Add optional callback functions when opening database #182

Merged
merged 13 commits into from
Sep 1, 2019
Merged

Add optional callback functions when opening database #182

merged 13 commits into from
Sep 1, 2019

Conversation

noordawod
Copy link
Contributor

@noordawod noordawod commented Aug 27, 2019

This is backward-compatible.

@noordawod noordawod changed the title Add optional callback functions when opening database (backward-compatible). Add optional callback functions when opening database Aug 27, 2019
@dkaera
Copy link
Collaborator

dkaera commented Aug 27, 2019

Please, add floor_generator/lib/generated/i18n.dart (126) to .gitignore . It's an autogenerated file.

@noordawod
Copy link
Contributor Author

Please, add floor_generator/lib/generated/i18n.dart (126) to .gitignore . It's an autogenerated file.

Done.

@noordawod
Copy link
Contributor Author

Travis seems to be down or something...

@noordawod
Copy link
Contributor Author

The command "./tool/travis.sh dartfmt" exited with 1.

Travis is not feeling well :/ but the code now passes tests if you do it offline: pub run test

@dkaera
Copy link
Collaborator

dkaera commented Aug 28, 2019

The command "./tool/travis.sh dartfmt" exited with 1.

Travis is not feeling well :/ but the code now passes tests if you do it offline: pub run test

As I see in CI log, you need to run flutter format ./floor_generator/ or navigate to floor_generator directory and run flutter format . from Terminal to apply Dart format for floor_generator module.

@noordawod
Copy link
Contributor Author

The command "./tool/travis.sh dartfmt" exited with 1.
Travis is not feeling well :/ but the code now passes tests if you do it offline: pub run test

As I see in CI log, you need to run flutter format ./floor_generator/ or navigate to floor_generator directory and run flutter format . from Terminal to apply Dart format for floor_generator module.

Ugh, thanks for the tip. Retrying...

@noordawod
Copy link
Contributor Author

Travis stopped?

travis-ci/travis-ci#10204

@codecov
Copy link

codecov bot commented Aug 28, 2019

Codecov Report

Merging #182 into develop will increase coverage by 0.24%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #182      +/-   ##
===========================================
+ Coverage    77.52%   77.76%   +0.24%     
===========================================
  Files           49       49              
  Lines         1366     1381      +15     
===========================================
+ Hits          1059     1074      +15     
  Misses         307      307
Impacted Files Coverage Δ
floor_generator/lib/writer/database_writer.dart 79.41% <100%> (+1.28%) ⬆️
..._generator/lib/writer/database_builder_writer.dart 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cc331e4...ef0a589. Read the comment docs.

@vitusortner
Copy link
Collaborator

Hi @noordawod thanks for your contribution! What's the reasoning for allowing these additional functions? I see some problems with opening the possibility of hooking into the migration (onUpgrade) and table creation (onCreate) process.

@noordawod
Copy link
Contributor Author

noordawod commented Aug 29, 2019

Hi @noordawod thanks for your contribution! What's the reasoning for allowing these additional functions? I see some problems with opening the possibility of hooking into the migration (onUpgrade) and table creation (onCreate) process.

Hi @vitusortner.

My use case: I needed to know when the database is first created (so show an onboarding screen), then when an upgrade is going to happen (show a progress indicator), and when the database is up to speed -- and then rush the user to the main screen. I see many people wanting this use case, but maybe it's just me :)

About the callbacks: exposing the database instance could potentially be a problem if it isn't exposed in the first place, but I believe it is. If you do think this may cause trouble, then maybe just allow callbacks without the database instance? (new typedefs)

Let me know what you think.

PS: I wonder what problems do you see with this?

@noordawod
Copy link
Contributor Author

@vitusortner

Would you like having callbacks without access to the SQLite database? I can do this modification if it fits better with the architecture you want.

@vitusortner
Copy link
Collaborator

I gave it a second thought and now think, that there is completely valid use cases for exposing the database.

Implementation

I would prefer passing the callbacks via the generated DatabaseBuilder (similar to how migrations are forwarded) instead of using the DatabaseBuilder.build function. An example usage is shown in the following snippet.

The callback parameters onCreate, onOpen, onUpgrade should be named arguments.

final callback = Callback(
   onCreate: (database, version) {},
   onOpen: (database) {},
   onUpgrade: (database, startVersion, endVersion) {},
);

final database = await $FloorTestDatabase
    .addCallback(callback)
    .build();

Callback could be as simple as the following and should be located at floor/lib/src/callback.dart.

class Callback {
  final FutureOr<void> Function(Database database, int version) onCreate;
  final FutureOr<void> Function(Database database) onOpen;
  final FutureOr<void> Function(
    Database database,
    int startVersion,
    int endVersion,
  ) onUpgrade;

  Callback({this.onCreate, this.onOpen, this.onUpgrade});
}

Using FutureOr opens up the possibility of either using Dart's asynchronous capabilities in case they are required or to just execute synchronous code.

What made you decide on executing the passed functions before the migrations and table creations happen?

@noordawod
Copy link
Contributor Author

noordawod commented Sep 1, 2019

@vitusortner

This should work nicely too! What's your eta for this?

The decision stems from old habbits (from the JavaScript/jQuery world): the handler may return a false to cancel an event (click for example). But in our use case, it could be beneficial that these callbacks happen after Floor have done its work. Up to you.

@vitusortner
Copy link
Collaborator

If you'd find time for the implementation, I'd welcome your contribution! Based on the changes you've done, you know which places of the code base to touch.

Let's try with putting the callbacks after the initialization has happened.

@noordawod
Copy link
Contributor Author

On it. I'll push to this pr.

@noordawod
Copy link
Contributor Author

@vitusortner
Working nicely in my project!

floor/lib/src/callback.dart Outdated Show resolved Hide resolved
floor_generator/lib/writer/database_builder_writer.dart Outdated Show resolved Hide resolved
floor_generator/lib/writer/database_builder_writer.dart Outdated Show resolved Hide resolved
floor_generator/lib/writer/database_writer.dart Outdated Show resolved Hide resolved
floor_generator/lib/writer/database_writer.dart Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
Copy link
Contributor Author

@noordawod noordawod left a comment

Choose a reason for hiding this comment

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

Changes deployed.

@vitusortner
Copy link
Collaborator

Looking good so far. I'll add some documentation to the project's README files on the new callbacks and merge the PR after that. Thanks for your assistance!

@noordawod
Copy link
Contributor Author

Sounds good! Maybe also add a test for the new functionality?

@vitusortner
Copy link
Collaborator

Yes, it would be beneficial to verify that each callback is invoked when it should be. Unluckily, I currently see no simple way of doing that.

@vitusortner vitusortner self-requested a review September 1, 2019 17:53
@vitusortner vitusortner merged commit a6473d1 into pinchbv:develop Sep 1, 2019
@noordawod noordawod deleted the feature/open-callbacks branch September 1, 2019 23:07
@noordawod
Copy link
Contributor Author

Coming to pub.dev anytime soon? :)

@vitusortner
Copy link
Collaborator

Is now released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants