d-markey / squadron

Multithreading and worker thread pool for Dart / Flutter, to offload CPU-bound and heavy I/O tasks to Isolate or Web Worker threads.
https://pub.dev/packages/squadron
MIT License
79 stars 0 forks source link

[Feature Req] Add a binary reader/writer with nice API to marshal data #34

Closed AlexDochioiu closed 6 months ago

AlexDochioiu commented 6 months ago

Not sure if you're familiar with the hive flutter library. If you're not, they basically allow the user to create type adapters to marshal/unmarshal the data with a fairly nice API. See example here: https://docs.hivedb.dev/#/custom-objects/create_adapter_manually

The main code for the reader and writer is in those 2 files: https://github.com/isar/hive/blob/legacy/hive/lib/src/binary/binary_writer_impl.dart https://github.com/isar/hive/blob/legacy/hive/lib/src/binary/binary_reader_impl.dart

Replicating/cherrypicking the binary reader/writer (or even extracting it into a library) could make using SquadronMarshaler a lot easier.

P.s. I personally spent 10 mins to copy the code into my own repo and make use of it. I think everyone could benefit if this is directly part of squadron

P.p.s. While it may be nice, don't think there's a need to add handling for custom types and register adapters (the way hive does it). Just a simple binary reader/writer should be more than enough to improve usability.

d-markey commented 6 months ago

Hello @AlexDochioiu,

Thanks for the feedback, I have played with Hive a little bit in the past but am not too familiar. The marshaling part of squadron is the bare minimum to have something that maps a non-transferable object to/from something that is transferable. I did not want to be opionated about the way transformations are implemented. For instance JSON could be a good candidate too as I believe transfering strings should be just as efficient as byte arrays. I think the real problem lies in transformation/parsing + instantiations.

From my perspective, specific implementations are out of Squadron's scope, apart from the IdentityMarshaler. The base idea behind this implementation is that for cross-platform apps, marshalers could be conditionally imported with IdentityMarshalers on VM and proper marshallers on browser.

BTW you could release your code for Hive-like marshaling as a standalone package for everyone else to use? :)