Skip to content

Commit dc7f426

Browse files
[Feature] remove uuid dependency
1 parent edaa37a commit dc7f426

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

lib/flutter_isolate.dart

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import 'dart:async';
22
import 'dart:isolate';
3+
import 'dart:math';
34
import 'dart:ui';
45

5-
import 'package:uuid/uuid.dart';
66
import 'package:flutter/material.dart';
77
import 'package:flutter/services.dart';
88

@@ -22,22 +22,18 @@ class FlutterIsolate {
2222
/// as the current isolate. The spawned isolate will be able to use flutter
2323
/// plugins. T can be any type that can be normally be passed through to
2424
/// 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();
3028
final isolateResult = Completer<FlutterIsolate>();
3129
final setupReceivePort = ReceivePort();
3230

33-
IsolateNameServer.registerPortWithName(
34-
setupReceivePort.sendPort, isolateId);
31+
IsolateNameServer.registerPortWithName(setupReceivePort.sendPort, isolateId);
3532
late StreamSubscription setupSubscription;
3633
setupSubscription = setupReceivePort.listen((data) {
3734
final portSetup = (data as List<dynamic>);
3835
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]);
4137

4238
setupPort.send(<Object?>[userEntryPointId, message]);
4339

@@ -46,26 +42,20 @@ class FlutterIsolate {
4642
isolateResult.complete(remoteIsolate);
4743
});
4844
_control.invokeMethod("spawn_isolate", {
49-
"entry_point":
50-
PluginUtilities.getCallbackHandle(_flutterIsolateEntryPoint)!
51-
.toRawHandle(),
45+
"entry_point": PluginUtilities.getCallbackHandle(_flutterIsolateEntryPoint)!.toRawHandle(),
5246
"isolate_id": isolateId
5347
});
5448
return isolateResult.future;
5549
}
5650

57-
bool get _isCurrentIsolate =>
58-
_isolateId == null ||
59-
_current != null && _current!._isolateId == _isolateId;
51+
bool get _isCurrentIsolate => _isolateId == null || _current != null && _current!._isolateId == _isolateId;
6052

6153
/// Requests the isolate to pause. This uses the underlying isolates pause
6254
/// implementation to pause the isolate from with the pausing isolate
6355
/// otherwises uses a SendPort to pass through a pause requres to the target
6456
void pause() => _isCurrentIsolate
6557
? Isolate.current.pause()
66-
: Isolate(controlPort!,
67-
pauseCapability: pauseCapability,
68-
terminateCapability: terminateCapability)
58+
: Isolate(controlPort!, pauseCapability: pauseCapability, terminateCapability: terminateCapability)
6959
.pause(pauseCapability);
7060

7161
/// Requests the isolate to resume. This uses the underlying isolates resume
@@ -74,9 +64,7 @@ class FlutterIsolate {
7464
/// ports will not be serviced when an isolate is paused.
7565
void resume() => _isCurrentIsolate
7666
? Isolate.current.resume(Capability())
77-
: Isolate(controlPort!,
78-
pauseCapability: pauseCapability,
79-
terminateCapability: terminateCapability)
67+
: Isolate(controlPort!, pauseCapability: pauseCapability, terminateCapability: terminateCapability)
8068
.resume(pauseCapability!);
8169

8270
/// Requestes to terminate the flutter isolate. As the isolate that is
@@ -92,9 +80,8 @@ class FlutterIsolate {
9280
/// Will return a List of UUIDs representing all running isolates.
9381
/// NOTE: You cannot kill them individually. This information
9482
/// 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>() ?? []);
9885

9986
/// Will kill all running isolates and wait for it to complete.
10087
/// It's useful if you want to hot reload an application.
@@ -105,19 +92,14 @@ class FlutterIsolate {
10592
static final _control = MethodChannel("com.rmawatson.flutterisolate/control");
10693
static final _event = EventChannel("com.rmawatson.flutterisolate/event");
10794

108-
FlutterIsolate._(
109-
[this._isolateId,
110-
this.controlPort,
111-
this.pauseCapability,
112-
this.terminateCapability]);
95+
FlutterIsolate._([this._isolateId, this.controlPort, this.pauseCapability, this.terminateCapability]);
11396

11497
@pragma('vm:entry-point')
11598
static macosIsolateInitialize() {
11699
_isolateInitialize();
117100
}
118101

119-
static FlutterIsolate get current =>
120-
_current != null ? _current! : FlutterIsolate._();
102+
static FlutterIsolate get current => _current != null ? _current! : FlutterIsolate._();
121103

122104
@pragma('vm:entry-point')
123105
static void _isolateInitialize() {
@@ -126,8 +108,7 @@ class FlutterIsolate {
126108
late StreamSubscription eventSubscription;
127109
eventSubscription = _event.receiveBroadcastStream().listen((isolateId) {
128110
_current = FlutterIsolate._(isolateId, null, null);
129-
final sendPort =
130-
IsolateNameServer.lookupPortByName(_current!._isolateId!)!;
111+
final sendPort = IsolateNameServer.lookupPortByName(_current!._isolateId!)!;
131112
final setupReceivePort = ReceivePort();
132113
IsolateNameServer.removePortNameMapping(_current!._isolateId!);
133114
sendPort.send(<dynamic>[
@@ -143,14 +124,36 @@ class FlutterIsolate {
143124
final args = data as List<Object?>;
144125
final userEntryPointHandle = args[0] as int;
145126
final userMessage = args[1];
146-
Function userEntryPoint = PluginUtilities.getCallbackFromHandle(
147-
CallbackHandle.fromRawHandle(userEntryPointHandle))!;
127+
Function userEntryPoint =
128+
PluginUtilities.getCallbackFromHandle(CallbackHandle.fromRawHandle(userEntryPointHandle))!;
148129
setupSubscription.cancel();
149130
setupReceivePort.close();
150131
userEntryPoint(userMessage);
151132
});
152133
});
153134
}
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+
}
154157
}
155158

156159
@pragma('vm:entry-point')

pubspec.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ environment:
1010
dependencies:
1111
flutter:
1212
sdk: flutter
13-
uuid: ">=3.0.3 <5.0.0"
1413

1514
flutter:
1615
plugin:

0 commit comments

Comments
 (0)