knoxpo / dart_algolia

[Unofficial] Algolia is a pure dart SDK, wrapped around Algolia REST API for easy implementation for your Flutter or Dart projects.
Other
117 stars 112 forks source link

Decoding JSON in an Isolate #103

Open techouse opened 2 years ago

techouse commented 2 years ago

Hi,

Since the JSON payloads can be quite large, using json.decode() on the main thread could cause janking.

One solution would be to expose a method that does the decoding which defaults to json.decode but allow it to be overridden in order to use compute() or any other Isolate method.

Internally, all references in the package to json.decode(response.body) would have to be replaced with something like await algolia.decodeJson(response.body),

Example:

class Algolia {
  // ... stuff

  /// The method that one can override
  FutureOr<dynamic> decodeJson(String payload) => json.decode(payload);
}

This can then be overridden by the user using compute(), for example

class MyAlgolia extends Algolia {
  // ... stuff

  @override
  FutureOr<dynamic> decodeJson(String payload) => compute(json.decode, payload);
}

If you're happy with this approach, I can write up something and submit a PR.