mirror_dispatcher 1.0.1 copy "mirror_dispatcher: ^1.0.1" to clipboard
mirror_dispatcher: ^1.0.1 copied to clipboard

Method Dispatcher using dart:mirrors. Facilitates invoking an API for JSON-RPC server side implementation.

example/mirror_dispatcher_example.dart

import 'package:mirror_dispatcher/mirror_dispatcher.dart';

/// Look in test/mirror_dispatcher_test.dart for additional examples.

/// Make a class with the API we want. Something silly but instructive.
///
/// Hold count in Foo.
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 {
  /// make a dispatcher with an initialized instance of Foo class
  var dispatcher = MirrorDispatcher(Foo(29));

  /// 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');
}
1
likes
160
points
45
downloads

Publisher

unverified uploader

Weekly Downloads

Method Dispatcher using dart:mirrors. Facilitates invoking an API for JSON-RPC server side implementation.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

rpc_dispatcher, rpc_exceptions

More

Packages that depend on mirror_dispatcher