postEvent function
Post an event of eventKind
with payload of eventData
to the "Extension"
event stream.
If extensionStreamHasListener is false, this method is a no-op.
Override stream
to set the destination stream that the event should be
posted to. The stream
may not start with an underscore or be a core VM
Service stream.
Implementation
void postEvent(String eventKind, Map eventData, {String stream = 'Extension'}) {
const destinationStreamKey = '__destinationStream';
// Keep protected streams in sync with `streams_` in runtime/vm/service.cc
// `Extension` is the only stream that should not be protected here.
final protectedStreams = <String>[
'VM',
'Isolate',
'Debug',
'GC',
'_Echo',
'HeapSnapshot',
'Logging',
'Timeline',
'Profiler',
];
if (protectedStreams.contains(stream)) {
throw ArgumentError.value(
stream, 'stream', 'Cannot be a protected stream.');
} else if (stream.startsWith('_')) {
throw ArgumentError.value(
stream, 'stream', 'Cannot start with an underscore.');
}
if (!extensionStreamHasListener) {
return;
}
// TODO: When NNBD is complete, delete the following two lines.
checkNotNullable(eventKind, 'eventKind');
checkNotNullable(eventData, 'eventData');
checkNotNullable(stream, 'stream');
Map mutableEventData = Map.from(eventData); // Shallow copy.
mutableEventData[destinationStreamKey] = stream;
String eventDataAsString = json.encode(mutableEventData);
_postEvent(eventKind, eventDataAsString);
}