postEvent函数
向"Extension"事件流中发送名为eventKind
的事件,带有数据eventData
。
如果extensionStreamHasListener为false,则该方法不会执行任何操作。可以通过重写stream
来设置事件应发送到的目标流。`stream`的名称不能以下划线开头,也不能是核心虚拟机服务流。
实现
void postEvent(String eventKind, Map eventData,
{@Since('3.0 ') 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);
}