|
| 1 | +import 'package:dart_eval/dart_eval.dart'; |
| 2 | +import 'package:dart_eval/dart_eval_bridge.dart'; |
| 3 | +import 'package:dart_eval/stdlib/core.dart'; |
| 4 | +import 'package:dart_eval/stdlib/typed_data.dart'; |
| 5 | +import 'package:flutter/services.dart'; |
| 6 | + |
| 7 | +/// dart_eval wrapper for [BinaryMessenger] |
| 8 | +class $BinaryMessenger implements $Instance { |
| 9 | + /// Compile-type type reference for [BinaryMessenger] |
| 10 | + static const $type = BridgeTypeRef(BridgeTypeSpec( |
| 11 | + 'package:flutter/src/services/binary_messenger.dart', 'BinaryMessenger')); |
| 12 | + |
| 13 | + /// Compile-type class declaration for [BinaryMessenger] |
| 14 | + static const $declaration = BridgeClassDef( |
| 15 | + BridgeClassType($type, isAbstract: true), |
| 16 | + constructors: {}, |
| 17 | + methods: { |
| 18 | + 'send': BridgeMethodDef(BridgeFunctionDef( |
| 19 | + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.future)), |
| 20 | + params: [ |
| 21 | + BridgeParameter('channel', |
| 22 | + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), false), |
| 23 | + BridgeParameter( |
| 24 | + 'data', |
| 25 | + BridgeTypeAnnotation(BridgeTypeRef(TypedDataTypes.byteData)), |
| 26 | + false) |
| 27 | + ])), |
| 28 | + 'setMessageHandler': BridgeMethodDef(BridgeFunctionDef( |
| 29 | + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.voidType)), |
| 30 | + params: [ |
| 31 | + BridgeParameter('channel', |
| 32 | + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)), false), |
| 33 | + BridgeParameter('handler', |
| 34 | + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.function)), true) |
| 35 | + ])), |
| 36 | + }, |
| 37 | + wrap: true); |
| 38 | + |
| 39 | + final $Instance _superclass; |
| 40 | + |
| 41 | + /// Wrap a [BinaryMessenger] in a [$BinaryMessenger] |
| 42 | + $BinaryMessenger.wrap(this.$value) : _superclass = $Object($value); |
| 43 | + |
| 44 | + @override |
| 45 | + final BinaryMessenger $value; |
| 46 | + |
| 47 | + @override |
| 48 | + get $reified => $value; |
| 49 | + |
| 50 | + @override |
| 51 | + $Value? $getProperty(Runtime runtime, String identifier) { |
| 52 | + switch (identifier) { |
| 53 | + case 'send': |
| 54 | + return __send; |
| 55 | + case 'setMessageHandler': |
| 56 | + return __setMessageHandler; |
| 57 | + default: |
| 58 | + return _superclass.$getProperty(runtime, identifier); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + static const $Function __send = $Function(_send); |
| 63 | + static $Value? _send(Runtime runtime, $Value? target, List<$Value?> args) { |
| 64 | + final self = target as $BinaryMessenger; |
| 65 | + final channel = args[0] as $String; |
| 66 | + runtime.assertPermission('method_channel', channel); |
| 67 | + final data = args[1] as $ByteData?; |
| 68 | + final result = self.$value.send(channel.$value, data?.$value); |
| 69 | + if (result == null) return const $null(); |
| 70 | + return $Future.wrap(result.then( |
| 71 | + (value) => value == null ? const $null() : $ByteData.wrap(value))); |
| 72 | + } |
| 73 | + |
| 74 | + static const $Function __setMessageHandler = $Function(_setMessageHandler); |
| 75 | + static $Value? _setMessageHandler( |
| 76 | + Runtime runtime, $Value? target, List<$Value?> args) { |
| 77 | + final self = target as $BinaryMessenger; |
| 78 | + final channel = args[0] as $String; |
| 79 | + runtime.assertPermission('method_channel', channel); |
| 80 | + final handler = args[1] as EvalCallable?; |
| 81 | + self.$value.setMessageHandler( |
| 82 | + channel.$value, |
| 83 | + handler == null |
| 84 | + ? null |
| 85 | + : (data) => handler.call(runtime, null, |
| 86 | + [data == null ? const $null() : $ByteData.wrap(data)])!.$value); |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + @override |
| 91 | + int $getRuntimeType(Runtime runtime) => runtime.lookupType($type.spec!); |
| 92 | + |
| 93 | + @override |
| 94 | + void $setProperty(Runtime runtime, String identifier, $Value value) { |
| 95 | + return _superclass.$setProperty(runtime, identifier, value); |
| 96 | + } |
| 97 | +} |
0 commit comments