OLD | NEW |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dartino 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 | 4 |
5 import 'dart:fletch._system' as fletch; | 5 import 'dart:dartino._system' as dartino; |
6 import 'dart:fletch._system' show patch; | 6 import 'dart:dartino._system' show patch; |
7 import 'dart:fletch'; | 7 import 'dart:dartino'; |
8 import 'dart:fletch.os' as os; | 8 import 'dart:dartino.os' as os; |
9 import 'dart:math'; | 9 import 'dart:math'; |
10 | 10 |
11 Channel _eventQueue; | 11 Channel _eventQueue; |
12 int _numberOfEvents = 0; | 12 int _numberOfEvents = 0; |
13 | 13 |
14 void _handleEvents() { | 14 void _handleEvents() { |
15 while (_numberOfEvents > 0) { | 15 while (_numberOfEvents > 0) { |
16 var event = _eventQueue.receive(); | 16 var event = _eventQueue.receive(); |
17 _numberOfEvents--; | 17 _numberOfEvents--; |
18 event(); | 18 event(); |
(...skipping 15 matching lines...) Expand all Loading... |
34 _ensureEventQueue().send(callback); | 34 _ensureEventQueue().send(callback); |
35 } | 35 } |
36 } | 36 } |
37 | 37 |
38 int get _currentTimestamp { | 38 int get _currentTimestamp { |
39 return new DateTime.now().millisecondsSinceEpoch; | 39 return new DateTime.now().millisecondsSinceEpoch; |
40 } | 40 } |
41 | 41 |
42 // TODO(ajohnsen): We should create a heap-like structure in Dart, so we only | 42 // TODO(ajohnsen): We should create a heap-like structure in Dart, so we only |
43 // have one active port/channel per process. | 43 // have one active port/channel per process. |
44 class _FletchTimer implements Timer { | 44 class _DartinoTimer implements Timer { |
45 final int _milliseconds; | 45 final int _milliseconds; |
46 var _callback; | 46 var _callback; |
47 int _timestamp = 0; | 47 int _timestamp = 0; |
48 _FletchTimer _next; | 48 _DartinoTimer _next; |
49 bool _isActive = true; | 49 bool _isActive = true; |
50 Channel _channel; | 50 Channel _channel; |
51 Port _port; | 51 Port _port; |
52 | 52 |
53 bool get _isPeriodic => _milliseconds >= 0; | 53 bool get _isPeriodic => _milliseconds >= 0; |
54 | 54 |
55 _FletchTimer(this._timestamp, this._callback) | 55 _DartinoTimer(this._timestamp, this._callback) |
56 : _milliseconds = -1 { | 56 : _milliseconds = -1 { |
57 _channel = new Channel(); | 57 _channel = new Channel(); |
58 _port = new Port(_channel); | 58 _port = new Port(_channel); |
59 _schedule(); | 59 _schedule(); |
60 } | 60 } |
61 | 61 |
62 _FletchTimer.periodic(this._timestamp, | 62 _DartinoTimer.periodic(this._timestamp, |
63 void callback(Timer timer), | 63 void callback(Timer timer), |
64 this._milliseconds) { | 64 this._milliseconds) { |
65 _callback = () { callback(this); }; | 65 _callback = () { callback(this); }; |
66 _channel = new Channel(); | 66 _channel = new Channel(); |
67 _port = new Port(_channel); | 67 _port = new Port(_channel); |
68 _schedule(); | 68 _schedule(); |
69 } | 69 } |
70 | 70 |
71 void _schedule() { | 71 void _schedule() { |
72 Fiber.fork(() { | 72 Fiber.fork(() { |
(...skipping 17 matching lines...) Expand all Loading... |
90 } | 90 } |
91 | 91 |
92 void cancel() { | 92 void cancel() { |
93 _scheduleTimeout(-1, _port); | 93 _scheduleTimeout(-1, _port); |
94 _port.send(-1); | 94 _port.send(-1); |
95 _isActive = false; | 95 _isActive = false; |
96 } | 96 } |
97 | 97 |
98 bool get isActive => _isActive; | 98 bool get isActive => _isActive; |
99 | 99 |
100 @fletch.native external static void _scheduleTimeout(int timeout, Port port); | 100 @dartino.native external static void _scheduleTimeout(int timeout, Port port); |
101 } | 101 } |
102 | 102 |
103 @patch class Timer { | 103 @patch class Timer { |
104 @patch static Timer _createTimer(Duration duration, void callback()) { | 104 @patch static Timer _createTimer(Duration duration, void callback()) { |
105 int milliseconds = max(0, duration.inMilliseconds); | 105 int milliseconds = max(0, duration.inMilliseconds); |
106 return new _FletchTimer(_currentTimestamp + milliseconds, callback); | 106 return new _DartinoTimer(_currentTimestamp + milliseconds, callback); |
107 } | 107 } |
108 | 108 |
109 @patch static Timer _createPeriodicTimer(Duration duration, | 109 @patch static Timer _createPeriodicTimer(Duration duration, |
110 void callback(Timer timer)) { | 110 void callback(Timer timer)) { |
111 int milliseconds = max(0, duration.inMilliseconds); | 111 int milliseconds = max(0, duration.inMilliseconds); |
112 return new _FletchTimer.periodic(_currentTimestamp + milliseconds, | 112 return new _DartinoTimer.periodic(_currentTimestamp + milliseconds, |
113 callback, | 113 callback, |
114 milliseconds); | 114 milliseconds); |
115 } | 115 } |
116 } | 116 } |
117 | 117 |
118 @patch void _rethrow(Object error, StackTrace stackTrace) { | 118 @patch void _rethrow(Object error, StackTrace stackTrace) { |
119 throw new AsyncError(error, stackTrace); | 119 throw new AsyncError(error, stackTrace); |
120 } | 120 } |
OLD | NEW |