writeInstanceManagerApi method
void
writeInstanceManagerApi(
- DartOptions generatorOptions,
- Root root,
- Indent indent, {
- required String dartPackageName,
override
Writes the implementation of the API for the InstanceManager
to
indent
.
Implementation
@override
void writeInstanceManagerApi(
DartOptions generatorOptions,
Root root,
Indent indent, {
required String dartPackageName,
}) {
final cb.Parameter binaryMessengerParameter = cb.Parameter(
(cb.ParameterBuilder builder) => builder
..name = 'binaryMessenger'
..type = cb.refer('BinaryMessenger?')
..named = true,
);
final cb.Field binaryMessengerField = cb.Field(
(cb.FieldBuilder builder) => builder
..name = '${varNamePrefix}binaryMessenger'
..type = cb.refer('BinaryMessenger?')
..modifier = cb.FieldModifier.final$,
);
final cb.Class instanceManagerApi = cb.Class(
(cb.ClassBuilder builder) => builder
..name = dartInstanceManagerApiClassName
..docs.add(
'/// Generated API for managing the Dart and native `$dartInstanceManagerClassName`s.',
)
..constructors.add(
cb.Constructor(
(cb.ConstructorBuilder builder) {
builder
..docs.add(
'/// Constructor for [$dartInstanceManagerApiClassName].')
..optionalParameters.add(binaryMessengerParameter)
..initializers.add(
cb.Code(
'${binaryMessengerField.name} = ${binaryMessengerParameter.name}',
),
);
},
),
)
..fields.addAll(
<cb.Field>[
binaryMessengerField,
cb.Field(
(cb.FieldBuilder builder) {
builder
..name = _pigeonChannelCodec
..type = cb.refer('MessageCodec<Object?>')
..static = true
..modifier = cb.FieldModifier.constant
..assignment = const cb.Code('$_pigeonCodec()');
},
)
],
)
..methods.add(
cb.Method(
(cb.MethodBuilder builder) {
builder
..name = 'setUpMessageHandlers'
..static = true
..returns = cb.refer('void')
..optionalParameters.addAll(<cb.Parameter>[
cb.Parameter(
(cb.ParameterBuilder builder) => builder
..name = '${classMemberNamePrefix}clearHandlers'
..type = cb.refer('bool')
..named = true
..defaultTo = const cb.Code('false'),
),
binaryMessengerParameter,
cb.Parameter(
(cb.ParameterBuilder builder) => builder
..name = 'instanceManager'
..named = true
..type = cb.refer('$dartInstanceManagerClassName?'),
),
])
..body = cb.Block.of(
cb.Block(
(cb.BlockBuilder builder) {
final StringBuffer messageHandlerSink = StringBuffer();
_writeFlutterMethodMessageHandler(
Indent(messageHandlerSink),
name: 'removeStrongReferenceName',
parameters: <Parameter>[
Parameter(
name: 'identifier',
type: const TypeDeclaration(
baseName: 'int',
isNullable: false,
),
)
],
returnType: const TypeDeclaration.voidDeclaration(),
channelName: makeRemoveStrongReferenceChannelName(
dartPackageName,
),
isMockHandler: false,
isAsynchronous: false,
nullHandlerExpression:
'${classMemberNamePrefix}clearHandlers',
onCreateApiCall: (
String methodName,
Iterable<Parameter> parameters,
Iterable<String> safeArgumentNames,
) {
return '(instanceManager ?? $dartInstanceManagerClassName.instance).remove(${safeArgumentNames.single})';
},
);
builder.statements.add(
cb.Code(messageHandlerSink.toString()),
);
},
).statements,
);
},
),
)
..methods.addAll(
<cb.Method>[
cb.Method(
(cb.MethodBuilder builder) {
builder
..name = 'removeStrongReference'
..returns = cb.refer('Future<void>')
..modifier = cb.MethodModifier.async
..requiredParameters.add(
cb.Parameter(
(cb.ParameterBuilder builder) => builder
..name = 'identifier'
..type = cb.refer('int'),
),
)
..body = cb.Block(
(cb.BlockBuilder builder) {
final StringBuffer messageCallSink = StringBuffer();
_writeHostMethodMessageCall(
Indent(messageCallSink),
addSuffixVariable: false,
channelName: makeRemoveStrongReferenceChannelName(
dartPackageName),
parameters: <Parameter>[
Parameter(
name: 'identifier',
type: const TypeDeclaration(
baseName: 'int',
isNullable: false,
),
),
],
returnType: const TypeDeclaration.voidDeclaration(),
);
builder.statements.addAll(<cb.Code>[
cb.Code(messageCallSink.toString()),
]);
},
);
},
),
cb.Method(
(cb.MethodBuilder builder) {
builder
..name = 'clear'
..returns = cb.refer('Future<void>')
..modifier = cb.MethodModifier.async
..docs.addAll(<String>[
'/// Clear the native `$dartInstanceManagerClassName`.',
'///',
'/// This is typically called after a hot restart.',
])
..body = cb.Block(
(cb.BlockBuilder builder) {
final StringBuffer messageCallSink = StringBuffer();
_writeHostMethodMessageCall(
Indent(messageCallSink),
addSuffixVariable: false,
channelName: makeClearChannelName(dartPackageName),
parameters: <Parameter>[],
returnType: const TypeDeclaration.voidDeclaration(),
);
builder.statements.addAll(<cb.Code>[
cb.Code(messageCallSink.toString()),
]);
},
);
},
),
],
),
);
final cb.DartEmitter emitter = cb.DartEmitter(useNullSafetySyntax: true);
indent.format(
DartFormatter().format('${instanceManagerApi.accept(emitter)}'),
);
}