OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Concurrent programming using _isolates_: | 6 * Concurrent programming using _isolates_: |
7 * independent workers that are similar to threads | 7 * independent workers that are similar to threads |
8 * but don't share memory, | 8 * but don't share memory, |
9 * communicating only via messages. | 9 * communicating only via messages. |
10 * | 10 * |
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 | 515 |
516 /** | 516 /** |
517 * Returns a broadcast stream of uncaught errors from the isolate. | 517 * Returns a broadcast stream of uncaught errors from the isolate. |
518 * | 518 * |
519 * Each error is provided as an error event on the stream. | 519 * Each error is provided as an error event on the stream. |
520 * | 520 * |
521 * The actual error object and stackTraces will not necessarily | 521 * The actual error object and stackTraces will not necessarily |
522 * be the same object types as in the actual isolate, but they will | 522 * be the same object types as in the actual isolate, but they will |
523 * always have the same [Object.toString] result. | 523 * always have the same [Object.toString] result. |
524 * | 524 * |
525 * The returned stream is done when the isolate terminates. | 525 * This stream is based on [addErrorListener] and [removeErrorListener]. |
526 * | |
527 * If the isolate has already terminated, the stream will never emit | |
528 * any events, not even a done event. | |
529 * If the isolate terminates while nobody is listening, the returned | |
530 * stream will also not emit a done event. | |
531 * | |
532 * This stream is based on [addErrorListener], [removeErrorListener], | |
533 * [addOnExitListener] and [removeOnExitListener]. | |
534 */ | 526 */ |
535 Stream get errors { | 527 Stream get errors { |
536 StreamController controller; | 528 StreamController controller; |
537 RawReceivePort port; | 529 RawReceivePort port; |
538 void handleError(message) { | 530 void handleError(message) { |
539 if (message != null) { | 531 String errorDescription = message[0]; |
540 String errorDescription = message[0]; | 532 String stackDescription = message[1]; |
541 String stackDescription = message[1]; | 533 var error = new RemoteError(errorDescription, stackDescription); |
542 var error = new RemoteError(errorDescription, stackDescription); | 534 controller.addError(error, error.stackTrace); |
543 controller.addError(error, error.stackTrace); | |
544 } else { | |
545 port.close(); | |
546 controller.close(); | |
547 } | |
548 } | 535 } |
549 controller = new StreamController.broadcast( | 536 controller = new StreamController.broadcast( |
550 sync: true, | 537 sync: true, |
551 onListen: () { | 538 onListen: () { |
552 port = new RawReceivePort(handleError); | 539 port = new RawReceivePort(handleError); |
553 this.addOnExitListener(port.sendPort); | |
554 this.addErrorListener(port.sendPort); | 540 this.addErrorListener(port.sendPort); |
555 }, | 541 }, |
556 onCancel: () { | 542 onCancel: () { |
557 this.removeErrorListener(port.sendPort); | 543 this.removeErrorListener(port.sendPort); |
558 this.removeOnExitListener(port.sendPort); | |
559 port.close(); | 544 port.close(); |
560 port = null; | 545 port = null; |
561 }); | 546 }); |
562 return controller.stream; | 547 return controller.stream; |
563 } | 548 } |
564 } | 549 } |
565 | 550 |
566 /** | 551 /** |
567 * Sends messages to its [ReceivePort]s. | 552 * Sends messages to its [ReceivePort]s. |
568 * | 553 * |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 * as the original error, but has no other features of the original error. | 697 * as the original error, but has no other features of the original error. |
713 */ | 698 */ |
714 class RemoteError implements Error { | 699 class RemoteError implements Error { |
715 final String _description; | 700 final String _description; |
716 final StackTrace stackTrace; | 701 final StackTrace stackTrace; |
717 RemoteError(String description, String stackDescription) | 702 RemoteError(String description, String stackDescription) |
718 : _description = description, | 703 : _description = description, |
719 stackTrace = new StackTrace.fromString(stackDescription); | 704 stackTrace = new StackTrace.fromString(stackDescription); |
720 String toString() => _description; | 705 String toString() => _description; |
721 } | 706 } |
OLD | NEW |