dart_algolia icon indicating copy to clipboard operation
dart_algolia copied to clipboard

Decoding JSON in an Isolate

Open techouse opened this issue 1 year ago • 0 comments

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.

techouse avatar Oct 05 '22 09:10 techouse