Skip to content

add channels and methods#20

Merged
bytemaster merged 7 commits intoEOSIO:masterfrom
wanderingbort:feature/add-channels-and-methods
Apr 30, 2018
Merged

add channels and methods#20
bytemaster merged 7 commits intoEOSIO:masterfrom
wanderingbort:feature/add-channels-and-methods

Conversation

@wanderingbort
Copy link

This PR adds the concept of Channels and Methods to appbase.

Channels

Channels are a means to publish data from one plugin that is consumed by many plugins without having to tightly couple the publisher plugin to the consumer plugin. This is a generic utility that encapsulates a common use case we were seeing in appbase applications where one plugin would directly expose boost signals for other plugins to subscribe to.

struct foo {
   int bar; 
};
using foo_channel_decl = channel_decl<struct my_tag, foo>;

// subscribe
auto subscription = app().get_channel<foo_channel_decl>().subscribe([](const foo& f){
   std::cout << "bar: " << f.bar << std::endl;
});

// send some foos
auto& foo_channel = app().get_channel<foo_channel_decl>();
foo_channel.publish(foo{5});  /// prints "bar: 5"
foo_channel.publish(foo{9});  /// prints "bar: 9"

Methods

Methods are a means to fetch values from or run a given command on provider plugin(s) without having to tightly couple the calling plugin to the provider plugin.

using add_method_decl = method_decl(struct my_tag, double(double,double));

// provide
auto provider = app().get_method<add_method_decl>().register_provider([](double a, double b) -> double {
   return a + b;
});

// call the method
double res;
auto& add_method = app().get_method<add_method_decl>();
res = add_method(1.0, 2.0);  /// res ~== 3.0
res = add_method(5.0, 4.0); /// res ~== 9.0

@bytemaster bytemaster merged commit 5c51b3c into EOSIO:master Apr 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants