diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index fb69a56..d9c7c9d 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -21,7 +21,7 @@ See [Effective Dart: Style](https://dart.dev/effective-dart/style) for general r Controllers live in `lib/controllers/` and provide a source that exposes data and logic via Riverpod providers, allowing other parts of the code to watch state changes with ref.watch (`ref.watch(MyController.provider)`), access the current value with ref.read (`ref.read(MyController.provider)`), and run helper methods on those classes using the notifier: ```dart -ref.watch(MyController.provider.notifier).helperMethod() +ref.read(MyController.provider.notifier).helperMethod() ``` We use an object oriented style for controllers, where `provider` is a static member on the controller class. E.g. diff --git a/lib/controllers/client.dart b/lib/controllers/client.dart index 8cada03..c783f0f 100644 --- a/lib/controllers/client.dart +++ b/lib/controllers/client.dart @@ -91,19 +91,25 @@ class ClientController extends AsyncNotifier { callback, ) async { final bufferPointer = data.toGomuksBufferPtr(); - final handle = await future; - final response = await Isolate.run( - () => callback(handle, bufferPointer.ref), - ); - calloc.free(bufferPointer); + try { + final handle = await future; - final json = response.buf.toJson(); - if (response.command.cast().toDartString() == "error") { - throw json; + final response = await Isolate.run( + () => callback(handle, bufferPointer.ref), + ); + + final json = response.buf.toJson(); + + if (response.command.cast().toDartString() == "error") { + throw json; + } + + return json; + } finally { + calloc.free(bufferPointer.ref.base); + calloc.free(bufferPointer); } - - return json; } Future<(Event, RoomMetadata)> handlePush(Map data) async { @@ -250,9 +256,11 @@ class ClientController extends AsyncNotifier { Future logout() => _sendCommand("logout"); Future markRead(Room room) async { + if (room.timeline.isEmpty || room.metadata == null) return; final eventRowId = room.timeline[room.timeline.keys.reduce(max)]; final event = eventRowId == null ? null : room.events[eventRowId]; - if (event == null || room.metadata == null) return; + + if (event == null) return; await _sendCommand("mark_read", { "room_id": room.metadata!.id, diff --git a/lib/controllers/room_chat.dart b/lib/controllers/room_chat.dart index aeeb7f2..7b076f7 100644 --- a/lib/controllers/room_chat.dart +++ b/lib/controllers/room_chat.dart @@ -70,6 +70,7 @@ class RoomChatController(final String roomId) if (state.isLoading) return; state = .loading(); + final timelineKeys = ref .read(RoomsController.provider.select((value) => value[roomId])) ?.timeline