Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
c5ad1067
Unverified
Commit
c5ad1067
authored
6 years ago
by
Ian Hickson
Committed by
GitHub
6 years ago
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Handle errors in `compute()` by propagating them to the Future. (#24848)
parent
273364e2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
5 deletions
+45
-5
isolates.dart
packages/flutter/lib/src/foundation/isolates.dart
+23
-5
isolates_test.dart
packages/flutter/test/foundation/isolates_test.dart
+22
-0
No files found.
packages/flutter/lib/src/foundation/isolates.dart
View file @
c5ad1067
...
...
@@ -49,6 +49,7 @@ Future<R> compute<Q, R>(ComputeCallback<Q, R> callback, Q message, { String debu
final
Flow
flow
=
Flow
.
begin
();
Timeline
.
startSync
(
'
$debugLabel
: start'
,
flow:
flow
);
final
ReceivePort
resultPort
=
ReceivePort
();
final
ReceivePort
errorPort
=
ReceivePort
();
Timeline
.
finishSync
();
final
Isolate
isolate
=
await
Isolate
.
spawn
<
_IsolateConfiguration
<
Q
,
R
>>(
_spawn
,
...
...
@@ -61,13 +62,32 @@ Future<R> compute<Q, R>(ComputeCallback<Q, R> callback, Q message, { String debu
),
errorsAreFatal:
true
,
onExit:
resultPort
.
sendPort
,
onError:
errorPort
.
sendPort
,
);
final
R
result
=
await
resultPort
.
first
;
final
Completer
<
R
>
result
=
Completer
<
R
>();
errorPort
.
listen
((
dynamic
errorData
)
{
assert
(
errorData
is
List
<
dynamic
>);
assert
(
errorData
.
length
==
2
);
final
Exception
exception
=
Exception
(
errorData
[
0
]);
final
StackTrace
stack
=
StackTrace
.
fromString
(
errorData
[
1
]);
if
(
result
.
isCompleted
)
{
Zone
.
current
.
handleUncaughtError
(
exception
,
stack
);
}
else
{
result
.
completeError
(
exception
,
stack
);
}
});
resultPort
.
listen
((
dynamic
resultData
)
{
assert
(
resultData
==
null
||
resultData
is
R
);
if
(!
result
.
isCompleted
)
result
.
complete
(
resultData
);
});
await
result
.
future
;
Timeline
.
startSync
(
'
$debugLabel
: end'
,
flow:
Flow
.
end
(
flow
.
id
));
resultPort
.
close
();
errorPort
.
close
();
isolate
.
kill
();
Timeline
.
finishSync
();
return
result
;
return
result
.
future
;
}
@immutable
...
...
@@ -92,9 +112,7 @@ void _spawn<Q, R>(_IsolateConfiguration<Q, R> configuration) {
R
result
;
Timeline
.
timeSync
(
'
${configuration.debugLabel}
'
,
()
{
result
=
configuration
.
apply
();
},
()
{
result
=
configuration
.
apply
();
},
flow:
Flow
.
step
(
configuration
.
flowId
),
);
Timeline
.
timeSync
(
...
...
This diff is collapsed.
Click to expand it.
packages/flutter/test/foundation/isolates_test.dart
0 → 100644
View file @
c5ad1067
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import
'package:flutter/foundation.dart'
;
import
'../flutter_test_alternative.dart'
;
int
test1
(
int
value
)
{
return
value
+
1
;
}
int
test2
(
int
value
)
{
throw
2
;
}
void
main
(
)
{
test
(
'compute()'
,
()
async
{
expect
(
await
compute
(
test1
,
0
),
1
);
expect
(
compute
(
test2
,
0
),
throwsA
(
isInstanceOf
<
Exception
>()));
});
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment