@@ -51,28 +51,15 @@ final class CompilationDispatcher {
51
51
/// This is used in outgoing messages.
52
52
late Uint8List _compilationIdVarint;
53
53
54
- /// Whether we detected a [ProtocolError] while parsing an incoming response.
55
- ///
56
- /// If we have, we don't want to send the final compilation result because
57
- /// it'll just be a wrapper around the error.
58
- var _requestError = false ;
59
-
60
54
/// Creates a [CompilationDispatcher] that receives encoded protocol buffers
61
55
/// through [_mailbox] and sends them through [_sendPort] .
62
56
CompilationDispatcher (this ._mailbox, this ._sendPort);
63
57
64
58
/// Listens for incoming `CompileRequests` and runs their compilations.
65
59
void listen () {
66
- do {
67
- Uint8List packet;
68
- try {
69
- packet = _mailbox.take ();
70
- } on StateError catch (_) {
71
- break ;
72
- }
73
-
60
+ while (true ) {
74
61
try {
75
- var (compilationId, messageBuffer) = parsePacket (packet );
62
+ var (compilationId, messageBuffer) = parsePacket (_receive () );
76
63
77
64
_compilationId = compilationId;
78
65
_compilationIdVarint = serializeVarint (compilationId);
@@ -88,9 +75,7 @@ final class CompilationDispatcher {
88
75
case InboundMessage_Message .compileRequest:
89
76
var request = message.compileRequest;
90
77
var response = _compile (request);
91
- if (! _requestError) {
92
- _send (OutboundMessage ()..compileResponse = response);
93
- }
78
+ _send (OutboundMessage ()..compileResponse = response);
94
79
95
80
case InboundMessage_Message .versionRequest:
96
81
throw paramsError ("VersionRequest must have compilation ID 0." );
@@ -113,7 +98,7 @@ final class CompilationDispatcher {
113
98
} catch (error, stackTrace) {
114
99
_handleError (error, stackTrace);
115
100
}
116
- } while ( ! _requestError);
101
+ }
117
102
}
118
103
119
104
OutboundMessage_CompileResponse _compile (
@@ -287,20 +272,13 @@ final class CompilationDispatcher {
287
272
void sendLog (OutboundMessage_LogEvent event) =>
288
273
_send (OutboundMessage ()..logEvent = event);
289
274
290
- /// Sends [error] to the host.
275
+ /// Sends [error] to the host and exit .
291
276
///
292
277
/// This is used during compilation by other classes like host callable.
293
- /// Therefore it must set _requestError = true to prevent sending a CompileFailure after
294
- /// sending a ProtocolError.
295
- void sendError (ProtocolError error) {
296
- _sendError (error);
297
- _requestError = true ;
278
+ Never sendError (ProtocolError error) {
279
+ Isolate .exit (_sendPort, _serializePacket (OutboundMessage ()..error = error));
298
280
}
299
281
300
- /// Sends [error] to the host.
301
- void _sendError (ProtocolError error) =>
302
- _send (OutboundMessage ()..error = error);
303
-
304
282
InboundMessage_CanonicalizeResponse sendCanonicalizeRequest (
305
283
OutboundMessage_CanonicalizeRequest request) =>
306
284
_sendRequest <InboundMessage_CanonicalizeResponse >(
@@ -326,19 +304,9 @@ final class CompilationDispatcher {
326
304
message.id = _outboundRequestId;
327
305
_send (message);
328
306
329
- Uint8List packet;
330
- try {
331
- packet = _mailbox.take ();
332
- } on StateError catch (_) {
333
- // Compiler is shutting down, throw without calling `_handleError` as we
334
- // don't want to report this as an actual error.
335
- _requestError = true ;
336
- rethrow ;
337
- }
338
-
339
307
try {
340
308
var messageBuffer =
341
- Uint8List .sublistView (packet , _compilationIdVarint.length);
309
+ Uint8List .sublistView (_receive () , _compilationIdVarint.length);
342
310
343
311
InboundMessage message;
344
312
try {
@@ -376,21 +344,24 @@ final class CompilationDispatcher {
376
344
return response;
377
345
} catch (error, stackTrace) {
378
346
_handleError (error, stackTrace);
379
- _requestError = true ;
380
- rethrow ;
381
347
}
382
348
}
383
349
384
350
/// Handles an error thrown by the dispatcher or code it dispatches to.
385
351
///
386
352
/// The [messageId] indicate the IDs of the message being responded to, if
387
353
/// available.
388
- void _handleError (Object error, StackTrace stackTrace, {int ? messageId}) {
389
- _sendError (handleError (error, stackTrace, messageId: messageId));
354
+ Never _handleError (Object error, StackTrace stackTrace, {int ? messageId}) {
355
+ sendError (handleError (error, stackTrace, messageId: messageId));
390
356
}
391
357
392
358
/// Sends [message] to the host with the given [wireId] .
393
359
void _send (OutboundMessage message) {
360
+ _sendPort.send (_serializePacket (message));
361
+ }
362
+
363
+ /// Serialize [message] to [Uint8List] .
364
+ Uint8List _serializePacket (OutboundMessage message) {
394
365
var protobufWriter = CodedBufferWriter ();
395
366
message.writeToCodedBufferWriter (protobufWriter);
396
367
@@ -407,6 +378,17 @@ final class CompilationDispatcher {
407
378
};
408
379
packet.setAll (1 , _compilationIdVarint);
409
380
protobufWriter.writeTo (packet, 1 + _compilationIdVarint.length);
410
- _sendPort.send (packet);
381
+ return packet;
382
+ }
383
+
384
+ /// Receive a packet from the host.
385
+ Uint8List _receive () {
386
+ try {
387
+ return _mailbox.take ();
388
+ } on StateError catch (_) {
389
+ // The [_mailbox] has been closed, exit the current isolate immediately
390
+ // to avoid bubble the error up as [SassException] during [_sendRequest].
391
+ Isolate .exit ();
392
+ }
411
393
}
412
394
}
0 commit comments