registerExtension 函数
- String method,
- ServiceExtensionHandler handler
注册一个 ServiceExtensionHandler,它将在当前 isolate 中调用 method
。 注意:服务协议扩展必须在每个 isolate 中注册。
注意:method
必须以 'ext.' 开头,并且您应使用以下结构以避免与其他包冲突:'ext.package.command'。也就是说,在 'ext.' 前缀之后,应该是注册的包名,然后是一个点 ('.'),然后是命令名。例如:'ext.dart.io.getOpenFiles'。
因为服务扩展是隔离特定的,使用扩展的客户端必须在每个 RPC 中包含一个 'isolateId' 参数。
实现
void registerExtension(String method, ServiceExtensionHandler handler) {
// TODO: When NNBD is complete, delete the following line.
checkNotNullable(method, 'method');
if (!method.startsWith('ext.')) {
throw new ArgumentError.value(method, 'method', 'Must begin with ext.');
}
if (_lookupExtension(method) != null) {
throw new ArgumentError('Extension already registered: $method');
}
// TODO: When NNBD is complete, delete the following line.
checkNotNullable(handler, 'handler');
final zoneHandler = Zone.current.bindBinaryCallback(handler);
_registerExtension(method, zoneHandler);
}