Skip to content

Commit 13335a9

Browse files
committed
Commented
1 parent 1837007 commit 13335a9

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## Introduction
22
This is a library for using a long-lived isolate to encode and decode JSONs.
33

4+
When in a VM it uses isolates. This package is able to support web by falling back to encoding/decoding without isolates on web. This is so that an application using this package does not have to do that check itself.
5+
46
## Usage
57

68
A simple usage example:

lib/src/isolate_json.dart

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,42 @@ import 'dart:isolate';
44
class JsonIsolate {
55
static final JsonIsolate _singleton = JsonIsolate._internal();
66

7-
SendPort sendPort;
8-
Isolate isolate;
7+
SendPort _sendPort;
8+
Isolate _isolate;
99

1010
JsonIsolate._internal();
1111

12+
/// Singleton for using JSONs in isolates.
1213
factory JsonIsolate() {
1314
return _singleton;
1415
}
1516

17+
/// Decodes a JSON with an isolate.
1618
Future<dynamic> decodeJson(String json) async {
17-
if (isolate == null) {
19+
if (_isolate == null) {
1820
await _makeIsolate();
1921
}
2022

21-
return _sendReceive(sendPort, json, _JsonAction.decode);
23+
return _sendReceive(_sendPort, json, _JsonAction.decode);
2224
}
2325

26+
/// Encodes a JSON with an isolate.
2427
Future<dynamic> encodeJson(dynamic toEncode) async {
25-
if (isolate == null) {
28+
if (_isolate == null) {
2629
await _makeIsolate();
2730
}
2831

29-
return _sendReceive(sendPort, toEncode, _JsonAction.encode);
32+
return _sendReceive(_sendPort, toEncode, _JsonAction.encode);
3033
}
3134

3235
Future<void> _makeIsolate() async {
3336
final receivePort = ReceivePort();
34-
isolate = await Isolate.spawn(
37+
_isolate = await Isolate.spawn(
3538
_isolateDecode,
3639
receivePort.sendPort,
3740
debugName: 'json_isolate',
3841
);
39-
sendPort = await receivePort.first;
42+
_sendPort = await receivePort.first;
4043
receivePort.close();
4144
}
4245

lib/src/norm_json.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@ class JsonIsolate {
55

66
JsonIsolate._internal();
77

8+
/// Singleton for using JSONs in isolates.
89
factory JsonIsolate() {
910
return _singleton;
1011
}
1112

13+
/// Decodes a JSON without an isolate.
14+
///
15+
/// Intended only when using the package on web.
1216
Future<dynamic> decodeJson(String json) async {
1317
return jsonDecode(json);
1418
}
1519

20+
/// Decodes a JSON without an isolate.
21+
///
22+
/// Intended only when using the package on web.
1623
Future<dynamic> encodeJson(dynamic toEncode) async {
1724
return jsonEncode(toEncode);
1825
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: isolate_json
22
description: Library for using a long-lived isolate for JSON encoding and decoding.
33
version: 1.0.0
4-
homepage: https://www.example.com
4+
homepage: https://github.com/chmoore889/isolate_json
55

66
environment:
77
sdk: '>=2.10.0 <3.0.0'

0 commit comments

Comments
 (0)