From 480cafd99f65dfc5e92014c0d6daee4db1fed51f Mon Sep 17 00:00:00 2001 From: fuwa Date: Fri, 26 Jul 2019 13:37:30 +0000 Subject: [PATCH] use `compute` for background json parsing --- cyberwow/lib/controller/rpc/rpc.dart | 5 ++++- cyberwow/lib/controller/rpc/rpc2.dart | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/cyberwow/lib/controller/rpc/rpc.dart b/cyberwow/lib/controller/rpc/rpc.dart index ede1dfa..5b4479a 100644 --- a/cyberwow/lib/controller/rpc/rpc.dart +++ b/cyberwow/lib/controller/rpc/rpc.dart @@ -58,6 +58,8 @@ Future rpcHTTP(String method) async { } } +dynamic jsonDecode(String responseBody) => json.decode(responseBody); + Future rpc(String method, {String field}) async { final response = await rpcHTTP(method); @@ -66,7 +68,8 @@ Future rpc(String method, {String field}) async { if (response.statusCode != 200) { return null; } else { - final _result = json.decode(response.body)['result']; + final _body = await compute(jsonDecode, response.body); + final _result = _body['result']; final _field = field == null ? _result : _result[field]; return _field; diff --git a/cyberwow/lib/controller/rpc/rpc2.dart b/cyberwow/lib/controller/rpc/rpc2.dart index 312db2b..19d178a 100644 --- a/cyberwow/lib/controller/rpc/rpc2.dart +++ b/cyberwow/lib/controller/rpc/rpc2.dart @@ -44,6 +44,8 @@ Future rpc2(String method) async { } } +dynamic jsonDecode(String responseBody) => json.decode(responseBody); + Future rpc2String(String method, {String field}) async { final response = await rpc2(method); @@ -52,7 +54,7 @@ Future rpc2String(String method, {String field}) async { if (response.statusCode != 200) { return ''; } else { - final _body= json.decode(response.body); + final _body = await compute(jsonDecode, response.body); final _field = field == null ? _body: _body[field]; return pretty(_field);