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

feat: packages/alice_objectbox #205

Merged
merged 62 commits into from
Jun 28, 2024
Merged

feat: packages/alice_objectbox #205

merged 62 commits into from
Jun 28, 2024

Conversation

techouse
Copy link
Collaborator

@techouse techouse commented Jun 26, 2024

This PR adds the ability to store all the AliceHttpCalls in an ObjectBox Box via a package.

How to get started

  1. Add it to your dependencies
dependencies:
  alice_objectbox: ^1.0.0
  1. Follow the ObjectBox example
Future<void> main() async {
  /// This is required so ObjectBox can get the application directory
  /// to store the database in.
  WidgetsFlutterBinding.ensureInitialized();

  /// Initialize [AliceObjectBoxStore] before running the app.
  final AliceObjectBoxStore store =
      await AliceObjectBoxStore.create(persistent: false);

  /// Pass [AliceObjectBoxStore] to the app
  runApp(MyApp(store: store));
}

class MyApp extends StatefulWidget {
  const MyApp({
    super.key,
    required this.store,
  });

  final AliceObjectBoxStore store;

  @override
  State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
  late final AliceHttpAdapter _aliceHttpAdapter = AliceHttpAdapter();

  late final Alice _alice = Alice(
    showNotification: true,
    showInspectorOnShake: true,
    maxCallsCount: 1000,
    aliceStorage: AliceObjectBox(
      store: widget.store,
      maxCallsCount: 1000,
    ),
  )..addAdapter(_aliceHttpAdapter);

  // your custom stuff...
}

Caveats

  • ObjectBox requires code generation, therefore ensure that you run code generation after every update to this particular package!
dart run build_runner build --delete-conflicting-outputs
  • Obviously the point of using ObjectBox is data persistence, so the calls will be preserved between app restarts and will have to be manually deleted. The default 1000 calls limit is still present.

Addresses #156

@jhomlala
Copy link
Owner

I've merged UI refactor PR. Please rebase :)

@jhomlala
Copy link
Owner

AliceCore from alice package will handle that. AliceStorage has void subscribeToCallChanges(Future<void> Function(List<AliceHttpCall>? calls) callback); which is being used in AliceCore to listen to the changes.

@techouse
Copy link
Collaborator Author

I'll try to implement all the changes from your fork into mine. ⏳

@jhomlala
Copy link
Owner

jhomlala commented Jun 28, 2024

Also, why is this now a method and mapped 3x instead of 1x?

@override
  Stream<List<AliceHttpCall>> getCallsStream() {
    return store.httpCalls
        .query()
        .order<int>(CachedAliceHttpCall_.createdTime, flags: Order.descending)
        .watch(triggerImmediately: true)
        .map((Query<CachedAliceHttpCall> query) => query.find())
        .map((calls) {
      return calls.map((call) {
        return call.getAliceHttpCall();
      }).toList();
    }).asBroadcastStream();
  }

Not sure why this is needed. What's wrong with this?

Stream<List<AliceHttpCall>> get callsStream => _store.httpCalls
      .query()
      .order<int>(CachedAliceHttpCall_.createdTime, flags: Order.descending)
      .watch(triggerImmediately: true)
      .map((Query<CachedAliceHttpCall> query) => query.find())
      .asBroadcastStream();

It's a stream after all.

Yeah it's dirty solution, just wanted to show some PoC. Feel free to remove duplicated code.

@techouse
Copy link
Collaborator Author

@jhomlala OK, I've merged all your stuff with mine and I think it now looks good.

Run over it once you can and let me know it you have more feedback.

@jhomlala
Copy link
Owner

jhomlala commented Jun 28, 2024

I don't want to block this PR, it looks good, but I'm thinking about unit tests (at least for storages) and updating docs. What do you think? Thank you so much for this hard work.

@techouse
Copy link
Collaborator Author

Unit testing storage will be quite labor intensive, as you have to create a local ObjectBox on the runner, i.e. a macOS binary etc. It's all well documented on ObjectBox's website ... but still a PITA to set up. 🙈

I'd rather make driver tests, but then you have to run simulators, compile the thing and play it on a sim in Github Actions + mock an HTTP server because you don't wanna DDoS anyone 🤪 #fun

The documentation is for the most part there in the example. Not sure if there should be any more readmes etc. I can add some more stuff, if needed.

@jhomlala
Copy link
Owner

jhomlala commented Jun 28, 2024

Sure, I understand. We can do it in separate PR. I'm thinking about doing unit tests for things that we can do, for example memory storage. For hard stuff - we can do integration tests.

For documentation I was thinking about adding info about storage here: https://github.com/jhomlala/alice/blob/master/docs/config.md.

Well I think we can merge it, but I keep looking for some tests - we're getting more and more logic.

@techouse
Copy link
Collaborator Author

I added some more docs.

@jhomlala
Copy link
Owner

@techouse Are you still working on this PR?

@techouse
Copy link
Collaborator Author

techouse commented Jun 28, 2024

Just fixed a bug 🐛 should be all good now

@techouse
Copy link
Collaborator Author

techouse commented Jun 28, 2024

Hmm, I was just thinking whether all the add / remove etc methods should be async. Waiting for them synchronously is quite a pain, i.e.

FutureOr<void> addCall(AliceHttpCall call);

FutureOr<void> addError(AliceHttpError error, int requestId);

FutureOr<void> addResponse(AliceHttpResponse response, int requestId);

FutureOr<void> addHttpCall(AliceHttpCall aliceHttpCall);

FutureOr<void> removeCalls();

Maybe in a new PR. This one is too big already :D

@techouse
Copy link
Collaborator Author

@jhomlala let me know if you're happy with this PR. Then we can start tackling other stuff.

@jhomlala jhomlala merged commit 4dc9928 into jhomlala:master Jun 28, 2024
2 checks passed
@techouse techouse deleted the feat/objectbox branch June 28, 2024 19:32
@techouse techouse linked an issue Jun 28, 2024 that may be closed by this pull request
@techouse techouse added the enhancement New feature or request label Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Alice didn't use database and store all data in RAM
2 participants