Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(797)

Side by Side Diff: sdk/lib/io/string_stream.dart

Issue 12213092: Rework Timer interface. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/io/stream_util.dart ('k') | sdk/lib/io/websocket_impl.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 part of dart.io; 5 part of dart.io;
6 6
7 // Interface for decoders decoding binary data into string data. The 7 // Interface for decoders decoding binary data into string data. The
8 // decoder keeps track of line breaks during decoding. 8 // decoder keeps track of line breaks during decoding.
9 abstract class _StringDecoder { 9 abstract class _StringDecoder {
10 // Add more binary data to be decoded. The ownership of the buffer 10 // Add more binary data to be decoded. The ownership of the buffer
(...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 _input.onData = _onData; 518 _input.onData = _onData;
519 } else { 519 } else {
520 _input.onData = null; 520 _input.onData = null;
521 } 521 }
522 } 522 }
523 } 523 }
524 524
525 // TODO(sgjesse): Find a better way of scheduling callbacks from 525 // TODO(sgjesse): Find a better way of scheduling callbacks from
526 // the event loop. 526 // the event loop.
527 void _checkScheduleCallback() { 527 void _checkScheduleCallback() {
528 void issueDataCallback(Timer timer) { 528 void issueDataCallback() {
529 _scheduledDataCallback = null; 529 _scheduledDataCallback = null;
530 if (_clientDataHandler != null) { 530 if (_clientDataHandler != null) {
531 _clientDataHandler(); 531 _clientDataHandler();
532 _checkScheduleCallback(); 532 _checkScheduleCallback();
533 } 533 }
534 } 534 }
535 535
536 void issueLineCallback(Timer timer) { 536 void issueLineCallback() {
537 _scheduledLineCallback = null; 537 _scheduledLineCallback = null;
538 if (_clientLineHandler != null) { 538 if (_clientLineHandler != null) {
539 _clientLineHandler(); 539 _clientLineHandler();
540 _checkScheduleCallback(); 540 _checkScheduleCallback();
541 } 541 }
542 } 542 }
543 543
544 void issueCloseCallback(Timer timer) { 544 void issueCloseCallback() {
545 _scheduledCloseCallback = null; 545 _scheduledCloseCallback = null;
546 if (!_closed) { 546 if (!_closed) {
547 if (_clientCloseHandler != null) _clientCloseHandler(); 547 if (_clientCloseHandler != null) _clientCloseHandler();
548 _closed = true; 548 _closed = true;
549 } 549 }
550 } 550 }
551 551
552 if (!_closed) { 552 if (!_closed) {
553 // Schedule data callback if string data available. 553 // Schedule data callback if string data available.
554 if (_clientDataHandler != null && 554 if (_clientDataHandler != null &&
555 !_decoder.isEmpty && 555 !_decoder.isEmpty &&
556 _scheduledDataCallback == null) { 556 _scheduledDataCallback == null) {
557 if (_scheduledLineCallback != null) { 557 if (_scheduledLineCallback != null) {
558 _scheduledLineCallback.cancel(); 558 _scheduledLineCallback.cancel();
559 } 559 }
560 _scheduledDataCallback = new Timer(0, issueDataCallback); 560 _scheduledDataCallback = Timer.run(issueDataCallback);
561 } 561 }
562 562
563 // Schedule line callback if a line is available. 563 // Schedule line callback if a line is available.
564 if (_clientLineHandler != null && 564 if (_clientLineHandler != null &&
565 (_decoder.lineBreaks > 0 || (!_decoder.isEmpty && _inputClosed)) && 565 (_decoder.lineBreaks > 0 || (!_decoder.isEmpty && _inputClosed)) &&
566 _scheduledLineCallback == null) { 566 _scheduledLineCallback == null) {
567 if (_scheduledDataCallback != null) { 567 if (_scheduledDataCallback != null) {
568 _scheduledDataCallback.cancel(); 568 _scheduledDataCallback.cancel();
569 } 569 }
570 _scheduledLineCallback = new Timer(0, issueLineCallback); 570 _scheduledLineCallback = Timer.run(issueLineCallback);
571 } 571 }
572 572
573 // Schedule close callback if no more data and input is closed. 573 // Schedule close callback if no more data and input is closed.
574 if (_decoder.isEmpty && 574 if (_decoder.isEmpty &&
575 _inputClosed && 575 _inputClosed &&
576 _scheduledCloseCallback == null) { 576 _scheduledCloseCallback == null) {
577 _scheduledCloseCallback = new Timer(0, issueCloseCallback); 577 _scheduledCloseCallback = Timer.run(issueCloseCallback);
578 } 578 }
579 } 579 }
580 } 580 }
581 581
582 InputStream _input; 582 InputStream _input;
583 Encoding _encoding; 583 Encoding _encoding;
584 _StringDecoder _decoder; 584 _StringDecoder _decoder;
585 bool _inputClosed = false; // Is the underlying input stream closed? 585 bool _inputClosed = false; // Is the underlying input stream closed?
586 bool _closed = false; // Is this stream closed. 586 bool _closed = false; // Is this stream closed.
587 bool _eof = false; // Has all data been read from the decoder? 587 bool _eof = false; // Has all data been read from the decoder?
588 Timer _scheduledDataCallback; 588 Timer _scheduledDataCallback;
589 Timer _scheduledLineCallback; 589 Timer _scheduledLineCallback;
590 Timer _scheduledCloseCallback; 590 Timer _scheduledCloseCallback;
591 Function _clientDataHandler; 591 Function _clientDataHandler;
592 Function _clientLineHandler; 592 Function _clientLineHandler;
593 Function _clientCloseHandler; 593 Function _clientCloseHandler;
594 } 594 }
OLDNEW
« no previous file with comments | « sdk/lib/io/stream_util.dart ('k') | sdk/lib/io/websocket_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698