@@ -9,33 +9,17 @@ import '_isolates_io.dart'
9
9
10
10
/// Signature for the callback passed to [compute] .
11
11
///
12
- /// {@macro flutter.foundation.compute.types}
13
- ///
14
- /// Instances of [ComputeCallback] must be functions that can be sent to an
15
- /// isolate.
16
12
/// {@macro flutter.foundation.compute.callback}
17
13
///
18
- /// {@macro flutter.foundation.compute.types}
19
- typedef ComputeCallback <Q , R > = FutureOr <R > Function (Q message);
14
+ typedef ComputeCallback <M , R > = FutureOr <R > Function (M message);
20
15
21
16
/// The signature of [compute] , which spawns an isolate, runs `callback` on
22
17
/// that isolate, passes it `message` , and (eventually) returns the value
23
18
/// returned by `callback` .
24
- ///
25
- /// {@macro flutter.foundation.compute.usecase}
26
- ///
27
- /// The function used as `callback` must be one that can be sent to an isolate.
28
- /// {@macro flutter.foundation.compute.callback}
29
- ///
30
- /// {@macro flutter.foundation.compute.types}
31
- ///
32
- /// The `debugLabel` argument can be specified to provide a name to add to the
33
- /// [Timeline] . This is useful when profiling an application.
34
- typedef ComputeImpl = Future <R > Function <Q , R >(ComputeCallback <Q , R > callback, Q message, { String ? debugLabel });
19
+ typedef ComputeImpl = Future <R > Function <M , R >(ComputeCallback <M , R > callback, M message, { String ? debugLabel });
35
20
36
- /// A function that spawns an isolate and runs the provided `callback` on that
37
- /// isolate, passes it the provided `message` , and (eventually) returns the
38
- /// value returned by `callback` .
21
+ /// Asynchronously runs the given [callback] - with the provided [message] -
22
+ /// in the background and completes with the result.
39
23
///
40
24
/// {@template flutter.foundation.compute.usecase}
41
25
/// This is useful for operations that take longer than a few milliseconds, and
@@ -68,34 +52,26 @@ typedef ComputeImpl = Future<R> Function<Q, R>(ComputeCallback<Q, R> callback, Q
68
52
/// ```
69
53
/// {@end-tool}
70
54
///
71
- /// The function used as `callback` must be one that can be sent to an isolate.
72
- /// {@template flutter.foundation.compute.callback}
73
- /// Qualifying functions include:
74
- ///
75
- /// * top-level functions
76
- /// * static methods
77
- /// * closures that only capture objects that can be sent to an isolate
55
+ /// On web platforms this will run [callback] on the current eventloop.
56
+ /// On native platforms this will run [callback] in a separate isolate.
78
57
///
79
- /// Using closures must be done with care. Due to
80
- /// [dart-lang/sdk#36983] (https://github.com/dart-lang/sdk/issues/36983) a
81
- /// closure may capture objects that, while not directly used in the closure
82
- /// itself, may prevent it from being sent to an isolate.
83
- /// {@endtemplate}
84
- ///
85
- /// {@template flutter.foundation.compute.types}
86
- /// The [compute] method accepts the following parameters:
58
+ /// {@template flutter.foundation.compute.callback}
87
59
///
88
- /// * `Q` is the type of the message that kicks off the computation.
89
- /// * `R` is the type of the value returned.
60
+ /// The `callback` , the `message` given to it as well as the result have to be
61
+ /// objects that can be sent across isolates (as they may be transitively copied
62
+ /// if needed). The majority of objects can be sent across isolates.
90
63
///
91
- /// There are limitations on the values that can be sent and received to and
92
- /// from isolates. These limitations constrain the values of `Q` and `R` that
93
- /// are possible. See the discussion at [SendPort.send] .
64
+ /// See [SendPort.send] for more information about exceptions as well as a note
65
+ /// of warning about sending closures, which can capture more state than needed.
94
66
///
95
- /// The same limitations apply to any errors generated by the computation.
96
67
/// {@endtemplate}
97
68
///
98
- /// See also:
69
+ /// On native platforms `await compute(fun, message)` is equivalent to
70
+ /// `await Isolate.run(() => fun(message))` . See also [Isolate.run] .
99
71
///
100
- /// * [ComputeImpl] , for the [compute] function's signature.
101
- const ComputeImpl compute = isolates.compute;
72
+ /// The `debugLabel` - if provided - is used as name for the isolate that
73
+ /// executes `callback` . [Timeline] events produced by that isolate will have
74
+ /// the name associated with them. This is useful when profiling an application.
75
+ Future <R > compute <M , R >(ComputeCallback <M , R > callback, M message, {String ? debugLabel}) {
76
+ return isolates.compute <M , R >(callback, message, debugLabel: debugLabel);
77
+ }
0 commit comments