-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc523cc
commit 673f691
Showing
10 changed files
with
112 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,27 @@ | ||
library dio_flutter_transformer; | ||
|
||
import 'dart:async'; | ||
import 'dart:convert'; | ||
|
||
import 'package:dio/dio.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
|
||
import 'package:dio/dio.dart'; | ||
/// Dio has already implemented a [SyncTransformer], and as the default | ||
/// [Transformer]. If you want to custom the transformation of | ||
/// request/response data, you can provide a [Transformer] by your self, and | ||
/// replace the [SyncTransformer] by setting the [dio.Transformer]. | ||
/// | ||
/// [FlutterTransformer] is especially for flutter, by which the json decoding | ||
/// will be in background with [compute] function. | ||
/// FlutterTransformer optimized for performance. | ||
/// JSON decoding/encoding will only be offloaded to a separate isolate | ||
/// if the data is above a certain size threshold. | ||
/// FlutterTransformer | ||
class FlutterTransformer extends SyncTransformer { | ||
static const int _computeThreshold = 200 * 1024; // 200 KB threshold | ||
|
||
FlutterTransformer() | ||
: super( | ||
jsonDecodeCallback: _parseJson, | ||
jsonEncodeCallback: _parseString, | ||
); | ||
} | ||
|
||
/// Determines if data should be processed in the main thread or offloaded. | ||
bool _shouldUseCompute(String text) { | ||
return text.length > FlutterTransformer._computeThreshold; | ||
} | ||
|
||
/// Parses and decodes a JSON string. Offloads to a separate isolate if data is large. | ||
Future<Map<String, dynamic>> _parseJson(String text) { | ||
if (_shouldUseCompute(text)) { | ||
return compute(_parseAndDecode, text); | ||
} else { | ||
return Future.value(_parseAndDecode(text)); | ||
} | ||
} | ||
|
||
/// Encodes an object to a JSON string. Offloads to a separate isolate if data is large. | ||
Future<String> _parseString(Object obj) { | ||
final String jsonString = _parseAndEncode(obj); | ||
if (jsonString.length > FlutterTransformer._computeThreshold) { | ||
return compute(_parseAndEncode, obj); | ||
} else { | ||
return Future.value(jsonString); | ||
} | ||
FlutterTransformer() : super(jsonDecodeCallback: _parseJson); | ||
} | ||
|
||
/// Decodes a JSON string into a Map. | ||
Map<String, dynamic> _parseAndDecode(String response) { | ||
// Must be top-level function | ||
_parseAndDecode(String response) { | ||
return jsonDecode(response); | ||
} | ||
|
||
/// Encodes an object into a JSON string. | ||
String _parseAndEncode(Object obj) { | ||
return jsonEncode(obj); | ||
_parseJson(String text) { | ||
return compute(_parseAndDecode, text); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters