Skip to content

Commit

Permalink
refactor(rpc): ♻️ Use archethic_wallet_client data structures
Browse files Browse the repository at this point in the history
  • Loading branch information
Chralu committed Jun 18, 2024
1 parent d50d1c2 commit d389b57
Show file tree
Hide file tree
Showing 73 changed files with 385 additions and 3,965 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
.svn/
dist/
*.bak
pubspec_overrides.yaml

# Test
test/tmp_data
Expand Down
2 changes: 1 addition & 1 deletion lib/application/authentication/authentication.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/application/check_transaction_worker/provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/application/oracle/provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/application/verified_tokens.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 36 additions & 28 deletions lib/domain/rpc/command_dispatcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:async';
import 'dart:collection';

import 'package:aewallet/domain/models/core/result.dart';
import 'package:aewallet/domain/rpc/commands/failure.dart';
import 'package:archethic_wallet_client/archethic_wallet_client.dart' as awc;
import 'package:collection/collection.dart';

/// [CommandDispatcher] is a [Command] queue.
Expand All @@ -21,30 +21,34 @@ import 'package:collection/collection.dart';
/// => eventually command is processed, and result is returned to the [CommandDispatcher]
/// - RPC receives the [Command] result. It returns it to the DApp.
///
class Command<C> {
class Command<CommandDataT, SuccessDataT> {
Command(
this.completer,
this.command,
);

final Completer completer;
final C command;
final Completer<Result<SuccessDataT, awc.Failure>> completer;
final CommandDataT command;
}

class CommandHandler<C, S> {
class CommandHandler<CommandDataT, SuccessDataT> {
const CommandHandler({
required this.handle,
required this.canHandle,
});

final Future<Result<S, RPCFailure>> Function(C command) handle;
final Future<Result<SuccessDataT, awc.Failure>> Function(
dynamic command,
) handle;

final bool Function(dynamic commandData) canHandle;
}

// Executed before Handlers.
// If an [RPCFailure] is returned, processing stops here.
typedef CommandGuard<C> = FutureOr<RPCFailure?> Function(C command);
// If an [awc.Failure] is returned, processing stops here.
typedef CommandGuard<CommandDataT> = FutureOr<awc.Failure?> Function(
CommandDataT command,
);

class CommandDispatcher {
final _waitingCommands = Queue<Command>();
Expand All @@ -63,14 +67,14 @@ class CommandDispatcher {

/// Add a guard.
/// [CommandGuard]s are executed before processing any command. If the guard's
/// result is an [RPCFailure], then processing stops returning that failure.
/// result is an [awc.Failure], then processing stops returning that failure.
void addGuard(CommandGuard guard) {
_guards.add(guard);
}

/// Add a [CommandHandler].
void addHandler(
CommandHandler commandHandler,
void addHandler<CommandDataT, SuccessDataT>(
CommandHandler<CommandDataT, SuccessDataT> commandHandler,
) {
_handlers.add(commandHandler);

Expand All @@ -79,12 +83,12 @@ class CommandDispatcher {
}
}

Future<Result<S, RPCFailure>> add<C, S>(
C command,
Future<Result<SuccessDataT, awc.Failure>> add<CommandDataT, SuccessDataT>(
CommandDataT command,
) async {
final completer = Completer<Result<S, RPCFailure>>();
final completer = Completer<Result<SuccessDataT, awc.Failure>>();
_waitingCommands.add(
Command(
Command<CommandDataT, SuccessDataT>(
completer,
command,
),
Expand All @@ -94,17 +98,20 @@ class CommandDispatcher {
return completer.future;
}

CommandHandler? _findHandler<C, S>(
C commandData,
CommandHandler<CommandDataT, SuccessDataT>?
_findHandler<CommandDataT, SuccessDataT>(
Command<CommandDataT, SuccessDataT> command,
) {
return _handlers.firstWhereOrNull(
return _handlers
.whereType<CommandHandler<CommandDataT, SuccessDataT>>()
.firstWhereOrNull(
(handler) {
return handler.canHandle(commandData);
return handler.canHandle(command.command);
},
);
}

Future<RPCFailure?> _guard<C>(C command) async {
Future<awc.Failure?> _guard<CommandDataT>(CommandDataT command) async {
for (final guard in _guards) {
final failure = await guard(command);
if (failure != null) return failure;
Expand All @@ -116,28 +123,29 @@ class CommandDispatcher {
if (_handlers.isEmpty) return;
if (_waitingCommands.isEmpty) return;

final command = _waitingCommands.first;
_waitingCommands.removeFirst();
final handler = _findHandler(command.command);
final command = _waitingCommands.removeFirst();
final handler = _findHandler(command);

if (handler == null) {
command.completer.completeError(
RPCFailure.unsupportedMethod(),
command.completer.complete(
const Result.failure(
awc.Failure.unsupportedMethod,
),
);
return;
}

final guardFailure = await _guard(command.command);
if (guardFailure != null) {
command.completer.completeError(guardFailure);
command.completer.complete(Result.failure(guardFailure));
return;
}

await handler
handler
.handle(command.command)
.then(
command.completer.complete,
onError: command.completer.completeError,
onError: (error) => command.completer.complete(Result.failure(error)),
)
.whenComplete(_process);
}
Expand Down
12 changes: 0 additions & 12 deletions lib/domain/rpc/commands/add_service.dart

This file was deleted.

143 changes: 0 additions & 143 deletions lib/domain/rpc/commands/add_service.freezed.dart

This file was deleted.

Loading

0 comments on commit d389b57

Please sign in to comment.