Skip to content

Commit 24e8a8a

Browse files
committedJun 17, 2022
plugin: implementing the example with the class extension.
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent cad8e6e commit 24e8a8a

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ docker/sandbox/lightning_dir_one/lightningd-regtest.pid
2121
*.info
2222

2323
packages/cln_plugin/cln_dart
24+
25+
packages/cln_plugin/cln_class_dart

‎packages/cln_plugin/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fmt:
1414

1515
examples:
1616
$(CC) compile exe example/cln_plugin_example.dart -o cln_dart
17+
$(CC) compile exe example/cln_plugin_class_example.dart -o cln_class_dart
1718

1819
gen:
1920
$(CC) run build_runner build
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'package:cln_plugin/cln_plugin.dart';
2+
3+
/// N.B. This style of developing a plugin is the suggested one
4+
/// for middle and big plugin.
5+
///
6+
/// This involve the possibility to develop in a more OOP style the
7+
/// code, but also to keep the code clean a simple to read.
8+
9+
class MyPlugin extends Plugin {
10+
Future<Map<String, Object>> firstMethod(
11+
Plugin plugin, Map<String, Object> request) {
12+
return Future.value({
13+
"foo_opt": getOpt(key: "foo_opt"),
14+
});
15+
}
16+
17+
@override
18+
void configurePlugin() {
19+
// This is the standard way to register a plugin
20+
registerOption(
21+
name: "foo_opt",
22+
type: "string",
23+
def: "hello",
24+
description: "This is an example of how the option looks like");
25+
26+
/// This is the standard way to register a custom rpc method
27+
registerRPCMethod(
28+
name: "first_method",
29+
usage: "",
30+
description: "This is an example of custom rpc method",
31+
callback: (plugin, request) => firstMethod(plugin, request));
32+
}
33+
}
34+
35+
void main() {
36+
var plugin = MyPlugin();
37+
plugin.start();
38+
}

‎packages/cln_plugin/lib/src/cln_plugin_base.dart

+8-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Plugin implements CLNPlugin {
4242
/// that core lightning send to us.
4343
Map<String, Object> configuration = {};
4444

45-
Plugin({this.dynamic = false});
45+
Plugin({this.dynamic = true});
4646

4747
@override
4848
void registerFeature({required String name, required String value}) {
@@ -55,7 +55,7 @@ class Plugin implements CLNPlugin {
5555
required String type,
5656
required String def,
5757
required String description,
58-
required bool deprecated}) {
58+
bool deprecated = false}) {
5959
options[name] = Option(
6060
name: name,
6161
type: type,
@@ -125,16 +125,20 @@ class Plugin implements CLNPlugin {
125125
return Future.value({});
126126
}
127127

128+
/// configurePlugin is used to configure the plugin that extend this class
129+
void configurePlugin() {}
130+
128131
// init plugin used to register the rpc method required by the plugin
129132
// life cycle
130-
void configurePlugin() {
133+
void _defaultPluginConfiguration() {
131134
rpcMethods["getmanifest"] =
132135
GetManifest(callback: (Plugin plugin, Map<String, Object> request) {
133136
return getManifest(plugin, request);
134137
});
135138
rpcMethods["init"] = InitMethod(
136139
callback: (Plugin plugin, Map<String, Object> request) =>
137140
init(plugin, request));
141+
configurePlugin();
138142
}
139143

140144
Future<Map<String, Object>> _call(
@@ -160,7 +164,7 @@ class Plugin implements CLNPlugin {
160164

161165
@override
162166
void start() async {
163-
configurePlugin();
167+
_defaultPluginConfiguration();
164168
try {
165169
String? messageSocket;
166170

0 commit comments

Comments
 (0)
Please sign in to comment.