1
1
import 'dart:async' ;
2
2
import 'dart:isolate' ;
3
+ import 'dart:math' ;
3
4
import 'dart:ui' ;
4
5
5
- import 'package:uuid/uuid.dart' ;
6
6
import 'package:flutter/material.dart' ;
7
7
import 'package:flutter/services.dart' ;
8
8
@@ -22,22 +22,18 @@ class FlutterIsolate {
22
22
/// as the current isolate. The spawned isolate will be able to use flutter
23
23
/// plugins. T can be any type that can be normally be passed through to
24
24
/// regular isolate's entry point.
25
- static Future <FlutterIsolate > spawn <T >(
26
- void entryPoint (T message), T message) async {
27
- final userEntryPointId =
28
- PluginUtilities .getCallbackHandle (entryPoint)! .toRawHandle ();
29
- final isolateId = const Uuid ().v4 ();
25
+ static Future <FlutterIsolate > spawn <T >(void entryPoint (T message), T message) async {
26
+ final userEntryPointId = PluginUtilities .getCallbackHandle (entryPoint)! .toRawHandle ();
27
+ final isolateId = uuid ();
30
28
final isolateResult = Completer <FlutterIsolate >();
31
29
final setupReceivePort = ReceivePort ();
32
30
33
- IsolateNameServer .registerPortWithName (
34
- setupReceivePort.sendPort, isolateId);
31
+ IsolateNameServer .registerPortWithName (setupReceivePort.sendPort, isolateId);
35
32
late StreamSubscription setupSubscription;
36
33
setupSubscription = setupReceivePort.listen ((data) {
37
34
final portSetup = (data as List <dynamic >);
38
35
final setupPort = portSetup[0 ] as SendPort ;
39
- final remoteIsolate = FlutterIsolate ._(
40
- isolateId, portSetup[1 ] as SendPort ? , portSetup[2 ], portSetup[3 ]);
36
+ final remoteIsolate = FlutterIsolate ._(isolateId, portSetup[1 ] as SendPort ? , portSetup[2 ], portSetup[3 ]);
41
37
42
38
setupPort.send (< Object ? > [userEntryPointId, message]);
43
39
@@ -46,26 +42,20 @@ class FlutterIsolate {
46
42
isolateResult.complete (remoteIsolate);
47
43
});
48
44
_control.invokeMethod ("spawn_isolate" , {
49
- "entry_point" :
50
- PluginUtilities .getCallbackHandle (_flutterIsolateEntryPoint)!
51
- .toRawHandle (),
45
+ "entry_point" : PluginUtilities .getCallbackHandle (_flutterIsolateEntryPoint)! .toRawHandle (),
52
46
"isolate_id" : isolateId
53
47
});
54
48
return isolateResult.future;
55
49
}
56
50
57
- bool get _isCurrentIsolate =>
58
- _isolateId == null ||
59
- _current != null && _current! ._isolateId == _isolateId;
51
+ bool get _isCurrentIsolate => _isolateId == null || _current != null && _current! ._isolateId == _isolateId;
60
52
61
53
/// Requests the isolate to pause. This uses the underlying isolates pause
62
54
/// implementation to pause the isolate from with the pausing isolate
63
55
/// otherwises uses a SendPort to pass through a pause requres to the target
64
56
void pause () => _isCurrentIsolate
65
57
? Isolate .current.pause ()
66
- : Isolate (controlPort! ,
67
- pauseCapability: pauseCapability,
68
- terminateCapability: terminateCapability)
58
+ : Isolate (controlPort! , pauseCapability: pauseCapability, terminateCapability: terminateCapability)
69
59
.pause (pauseCapability);
70
60
71
61
/// Requests the isolate to resume. This uses the underlying isolates resume
@@ -74,9 +64,7 @@ class FlutterIsolate {
74
64
/// ports will not be serviced when an isolate is paused.
75
65
void resume () => _isCurrentIsolate
76
66
? Isolate .current.resume (Capability ())
77
- : Isolate (controlPort! ,
78
- pauseCapability: pauseCapability,
79
- terminateCapability: terminateCapability)
67
+ : Isolate (controlPort! , pauseCapability: pauseCapability, terminateCapability: terminateCapability)
80
68
.resume (pauseCapability! );
81
69
82
70
/// Requestes to terminate the flutter isolate. As the isolate that is
@@ -92,9 +80,8 @@ class FlutterIsolate {
92
80
/// Will return a List of UUIDs representing all running isolates.
93
81
/// NOTE: You cannot kill them individually. This information
94
82
/// is only useful if you want to count the number of isolates
95
- static Future <List <String >> get runningIsolates => _control
96
- .invokeMethod <List <Object ?>>("get_isolate_list" )
97
- .then ((value) => value? .cast <String >() ?? []);
83
+ static Future <List <String >> get runningIsolates =>
84
+ _control.invokeMethod <List <Object ?>>("get_isolate_list" ).then ((value) => value? .cast <String >() ?? []);
98
85
99
86
/// Will kill all running isolates and wait for it to complete.
100
87
/// It's useful if you want to hot reload an application.
@@ -105,19 +92,14 @@ class FlutterIsolate {
105
92
static final _control = MethodChannel ("com.rmawatson.flutterisolate/control" );
106
93
static final _event = EventChannel ("com.rmawatson.flutterisolate/event" );
107
94
108
- FlutterIsolate ._(
109
- [this ._isolateId,
110
- this .controlPort,
111
- this .pauseCapability,
112
- this .terminateCapability]);
95
+ FlutterIsolate ._([this ._isolateId, this .controlPort, this .pauseCapability, this .terminateCapability]);
113
96
114
97
@pragma ('vm:entry-point' )
115
98
static macosIsolateInitialize () {
116
99
_isolateInitialize ();
117
100
}
118
101
119
- static FlutterIsolate get current =>
120
- _current != null ? _current! : FlutterIsolate ._();
102
+ static FlutterIsolate get current => _current != null ? _current! : FlutterIsolate ._();
121
103
122
104
@pragma ('vm:entry-point' )
123
105
static void _isolateInitialize () {
@@ -126,8 +108,7 @@ class FlutterIsolate {
126
108
late StreamSubscription eventSubscription;
127
109
eventSubscription = _event.receiveBroadcastStream ().listen ((isolateId) {
128
110
_current = FlutterIsolate ._(isolateId, null , null );
129
- final sendPort =
130
- IsolateNameServer .lookupPortByName (_current! ._isolateId! )! ;
111
+ final sendPort = IsolateNameServer .lookupPortByName (_current! ._isolateId! )! ;
131
112
final setupReceivePort = ReceivePort ();
132
113
IsolateNameServer .removePortNameMapping (_current! ._isolateId! );
133
114
sendPort.send (< dynamic > [
@@ -143,14 +124,36 @@ class FlutterIsolate {
143
124
final args = data as List <Object ?>;
144
125
final userEntryPointHandle = args[0 ] as int ;
145
126
final userMessage = args[1 ];
146
- Function userEntryPoint = PluginUtilities . getCallbackFromHandle (
147
- CallbackHandle .fromRawHandle (userEntryPointHandle))! ;
127
+ Function userEntryPoint =
128
+ PluginUtilities . getCallbackFromHandle ( CallbackHandle .fromRawHandle (userEntryPointHandle))! ;
148
129
setupSubscription.cancel ();
149
130
setupReceivePort.close ();
150
131
userEntryPoint (userMessage);
151
132
});
152
133
});
153
134
}
135
+
136
+ static String uuid () {
137
+ var random = Random ();
138
+ Uint8List bytes = Uint8List (16 );
139
+ for (int i = 0 ; i < 16 ; i++ ) {
140
+ bytes[i] = random.nextInt (256 );
141
+ }
142
+
143
+ // Set the version and the variant
144
+ bytes[6 ] = (bytes[6 ] & 0x0f ) | 0x40 ; // Version 4
145
+ bytes[8 ] = (bytes[8 ] & 0x3f ) | 0x80 ; // Variant 10
146
+
147
+ var uuid = bytes.map ((byte) => byte.toRadixString (16 ).padLeft (2 , '0' )).join ();
148
+
149
+ var a = uuid.substring (0 , 8 );
150
+ var b = uuid.substring (8 , 12 );
151
+ var c = uuid.substring (12 , 16 );
152
+ var d = uuid.substring (16 , 20 );
153
+ var e = uuid.substring (20 );
154
+
155
+ return '$a -$b -$c -$d -$e }' ;
156
+ }
154
157
}
155
158
156
159
@pragma ('vm:entry-point' )
0 commit comments