reflector_dispatcher:使用dart的rpc_dispatcher.Dispatcher的具体实现

  • r1_215869
    了解作者
  • 11.8KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-14 02:41
    上传日期
ReflectorDispatcher通过使用[package:reflectable]内省一个类实例来实现[Dispatcher]。 package:reflectable允许您通过实例的字符串名称来调用它们的方法。 使用类实例及其在根据Reflectable中的指示进行创建时创建的预建自省数据来构造[ReflectorDispatcher]。 ReflectorDispatcher.dispatch(“ someMethod”)将返回一个Future,无论它返回什么值。 这是针对JSON-RPC的,因此存在一些限制。 只能使用位置或命名参数来调用方法,不能同时使用这两个方法。 可选参数应按预期工作。 ReflectorDispatcher主要是package:reflectable的包装,替代dart:mirrors,与Flutter不兼容。 对于分派的任何方法,Dispatch
reflector_dispatcher-master.zip
  • reflector_dispatcher-master
  • example
  • reflector_dispatcher_example.dart
    2.2KB
  • reflector_dispatcher_example.reflectable.dart
    4KB
  • analysis_options.yaml
    434B
  • test
  • reflector_dispatcher_test.reflectable.dart
    7.5KB
  • reflector_dispatcher_test.dart
    6.5KB
  • LICENSE
    1KB
  • .gitignore
    369B
  • pubspec.yaml
    459B
  • CHANGELOG.md
    85B
  • lib
  • src
  • reflector_dispatcher_base.dart
    3.5KB
  • reflector_dispatcher.dart
    146B
  • README.md
    3.4KB
内容介绍
ReflectorDispatcher implements [Dispatcher] by introspecting a class instance using [package:reflectable]. package:reflectable allows you to invoke an instance's methods by their string names. Construct a [ReflectorDispatcher] with a class instance and its pre-built introspection data created when you build according to the directions in Reflectable. ReflectorDispatcher.dispatch("someMethod") will return a Future of whatever value it returns. This is for JSON-RPC, so there are some restrictions. A method can only be invoked with positional or named parameters, not both. Optional parameters should work as expected. ReflectorDispatcher is mainly a wrapper around package:reflectable, as a replacement for dart:mirrors, which is incompatible with Flutter. For any method dispatched, Dispatcher returns either the return value of the method or instances of one of three "Exception" classes. Most errors will be corralled into these objects, so that runtime exceptions don't get thrown, instead are returned in an orderly manner. ## Usage A simple usage example: ```dart import 'package:reflector_dispatcher/reflector_dispatcher.dart'; import 'package:reflectable/reflectable.dart'; /// this import does not exist until you run the builder /// (presuming this is in an 'example' folder) /// > pub run build_runner build example /// or > flutter pub run build_runner build example import 'reflector_dispatcher_example.reflectable.dart'; // Annotate with this class to enable reflection. class Reflector extends Reflectable { const Reflector() : super( invokingCapability, // Request the capability to invoke methods. declarationsCapability); // needed for introspecting methods. } const reflectable = Reflector(); /// Look in test/reflector_dispatcher_test.dart for additional examples. /// Make a class with the API we want. Something silly but instructive. /// /// Hold count in Foo. @reflectable class Foo { num _count = 0; /// initialize with a count Foo(this._count); /// increment with an optional positional parameter void increment([num aNumber = 1]) => _count += aNumber; /// decrement with a required named parameter void decrement({required num aNumber}) => _count -= aNumber; /// get the current value of count num getCount() => _count; /// we decrement by 2 a lot. num goTwoLess() { _count -= 2; return _count; } } void main() async { // do this before using the ReflectorDispatcher initializeReflectable(); /// make a dispatcher with an initialized instance of Foo class and its /// introspected data var dispatcher = ReflectorDispatcher(Foo(29), reflectable); /// dispatch a method with a parameter await dispatcher.dispatch('increment', 4); /// get the new value var c = await (dispatcher.dispatch('getCount')); printCount(c); // 33 /// dispatch a method with a named parameter await dispatcher.dispatch('decrement', {'aNumber': 18}); var d = await (dispatcher.dispatch('getCount')); printCount(d); // 15 /// dispatch a method without a parameter await dispatcher.dispatch('increment'); var e = await (dispatcher.dispatch('getCount')); printCount(e); // 16 var f = await dispatcher.dispatch('goTwoLess'); printCount(f); // 14 } void printCount(num aCount) { print('the currentCount is $aCount'); } ``` ## Features and bugs Please file feature requests and bugs at the [issue tracker][tracker]. [tracker]: https://github.com/jwashin/reflector_dispatcher
评论
    相关推荐