Skip to content

Commit

Permalink
🆒 Parse json in a seperate isolate with compute
Browse files Browse the repository at this point in the history
  • Loading branch information
toureholder committed Jul 7, 2019
1 parent c9c237c commit f42e14c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 50 deletions.
10 changes: 10 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
coverage:
precision: 2
round: down
range: "70...100"

status:
project: no
patch: no
changes: no

ignore:
- "**/*.g.dart"
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
[![codecov](https://codecov.io/gh/toureh/flutter_workshop/branch/master/graph/badge.svg)](https://codecov.io/gh/toureh/flutter_workshop)
[![Lint](https://img.shields.io/badge/style-pedantic-blue.svg)](https://github.com/dart-lang/pedantic)

This repository contains a sample app that I use to give workshops geared towards beginners to Flutter. I've made an effort to keep the git history clean by doing cohesive commits that each represent a teachable building block in the app.
This repository contains a sample app that I use to give workshops geared towards beginners to Flutter.

The commit history contains multiple ways of doing the same thing. For example, in one commit we manually spawn an isolate to do work in the background and in a later commit we use use Flutter's handy `compute` function to do the job. In one commit we manually parse and serialize json and in a later commit we do it with code generation.

I've made an effort to squash changes into cohesive commits that each represent a teachable building block in the app.


## Contains examples of:
Expand All @@ -14,6 +18,7 @@ This repository contains a sample app that I use to give workshops geared toward
* Navigation & routing
* Networking
* JSON parsing and serialization
* Moving work to a background isolate
* BLoC pattern
* Storing key-value data on disk
* Dependency injection with InheritedWidget
Expand Down
23 changes: 4 additions & 19 deletions lib/model/donation/donation_api.dart
Original file line number Diff line number Diff line change
@@ -1,38 +1,23 @@
import 'dart:convert';
import 'dart:isolate';

import 'package:flutter/foundation.dart';
import 'package:flutter_workshop/base/base_api.dart';
import 'package:flutter_workshop/model/donation/donation.dart';
import 'package:flutter_workshop/model/donation/donation_api_response.dart';
import 'package:flutter_workshop/util/concurrency.dart';
import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';

DonationApiResponse parseDonations(Map<String, dynamic> json) =>
DonationApiResponse.fromJson(json);

class DonationApi extends BaseApi {
DonationApi({@required http.Client client}) : super(client: client);

Future<List<Donation>> getDonations() async {
final String url = '${baseUrl}listings/categories/33';
final http.Response response = await get(url);
final Map<String, dynamic> json = jsonDecode(response.body);
final dto =
await computeInIsolate<DonationApiResponse>(parseDonations, json);
final DonationApiResponse dto = await compute(parseDonations, json);
return dto.donations;
}
}

void parseDonations(SendPort sendPort) async {
final port = ReceivePort();
sendPort.send(port.sendPort);

await for (var message in port) {
if (message is RespondableMessage) {
final json = message.data;
final result = DonationApiResponse.fromJson(json);
message.sendPort.send(result);
}

port.close();
}
}
30 changes: 0 additions & 30 deletions lib/util/concurrency.dart

This file was deleted.

0 comments on commit f42e14c

Please sign in to comment.