Skip to content

Commit 482b728

Browse files
authored
feat: Migration to capability-based API pt3 (#52)
* feat: Add scripting and transport capabilities * Introduced ScriptingCapabilityDelegate with methods for script evaluation and execution. * Added TransportCapabilityDelegate for server communication and resource management. * Updated index.dart to export new capabilities. * Enhanced UIDriver to include new capability delegates. * wip * wip * wip * refactor: Update logging methods for consistency * Replaced instances of `driver.error` with `driver.logError` for improved logging consistency. * Updated logging method names in `LoggingManager` and `LoggingCapabilityDelegate` to include the `log` prefix. * Adjusted logging calls in `DuitRegistry` to reflect the new method names. * wip
1 parent b1348d3 commit 482b728

16 files changed

Lines changed: 467 additions & 40 deletions

lib/src/action_api/action_executor.dart

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import "package:duit_kernel/duit_kernel.dart";
22

33
/// The [ActionExecutor] is an abstract class responsible for executing actions.
44
///
5-
/// It relies on a [UIDriver] to perform actions and an [DebugLogger] to log messages.
5+
/// It relies on a [UIDriver] to perform actions and an DebugLogger to log messages.
66
/// This class serves as a base for concrete implementations that define how actions
77
/// should be executed.
88
///
99
/// Subclasses must implement the [executeAction] method to handle different types
1010
/// of [ServerAction]s.
1111
abstract class ActionExecutor {
1212
final UIDriver driver;
13+
@Deprecated("Use [LoggingCapabilityDelegate] instead")
1314
final DebugLogger? logger;
1415

1516
ActionExecutor({
@@ -58,10 +59,10 @@ final class DefaultActionExecutor extends ActionExecutor {
5859

5960
return null;
6061
} catch (e, s) {
61-
logger?.error(
62+
driver.logError(
6263
"[Error while executing transport action]",
63-
error: e,
64-
stackTrace: s,
64+
e,
65+
s,
6566
);
6667
}
6768

@@ -71,10 +72,10 @@ final class DefaultActionExecutor extends ActionExecutor {
7172
try {
7273
return action.event;
7374
} catch (e, s) {
74-
logger?.error(
75+
driver.logError(
7576
"[Error while executing local action]",
76-
error: e,
77-
stackTrace: s,
77+
e,
78+
s,
7879
);
7980
}
8081
break;
@@ -97,10 +98,10 @@ final class DefaultActionExecutor extends ActionExecutor {
9798

9899
return null;
99100
} catch (e, s) {
100-
logger?.error(
101+
driver.logError(
101102
"[Error while executing script action]",
102-
error: e,
103-
stackTrace: s,
103+
e,
104+
s,
104105
);
105106
}
106107
break;

lib/src/action_api/event_resolver.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "package:flutter/material.dart" show BuildContext;
55

66
abstract class EventResolver {
77
final UIDriver driver;
8+
@Deprecated("Use [LoggingCapabilityDelegate] instead")
89
final DebugLogger? logger;
910

1011
EventResolver({
@@ -32,8 +33,6 @@ final class DefaultEventResolver extends EventResolver {
3233
}
3334

3435
try {
35-
final driver = this.driver;
36-
3736
switch (event) {
3837
case UpdateEvent():
3938
event.updates.forEach((key, value) async {
@@ -46,7 +45,7 @@ final class DefaultEventResolver extends EventResolver {
4645
"ExternalEventHandler instance is not set",
4746
);
4847
if (driver.externalEventHandler != null) {
49-
logger?.error("ExternalEventHandler instance is not set");
48+
driver.logError("ExternalEventHandler instance is not set");
5049
throw StateError("ExternalEventHandler instance is not set");
5150
}
5251
await driver.externalEventHandler?.handleNavigation(
@@ -57,7 +56,7 @@ final class DefaultEventResolver extends EventResolver {
5756
break;
5857
case OpenUrlEvent():
5958
if (driver.externalEventHandler != null) {
60-
logger?.error("ExternalEventHandler instance is not set");
59+
driver.logError("ExternalEventHandler instance is not set");
6160
throw StateError("ExternalEventHandler instance is not set");
6261
}
6362
await driver.externalEventHandler?.handleOpenUrl(event.url);
@@ -106,10 +105,10 @@ final class DefaultEventResolver extends EventResolver {
106105
break;
107106
}
108107
} catch (e, s) {
109-
logger?.error(
108+
driver.logError(
110109
"Error while resolving ${event.type} event",
111-
error: e,
112-
stackTrace: s,
110+
e,
111+
s,
113112
);
114113
}
115114
}

lib/src/capabilities/index.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ export "actions_capability.dart";
22
export "focus_capability.dart";
33
export "ui_controller_capability.dart";
44
export "viewmodel_capability.dart";
5+
export "transport_capability.dart";
6+
export "scripting_capability.dart";
7+
export "logging/index.dart";
8+
export "native_module_capability.dart";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export "logging_capability.dart";
2+
export "io.dart" if (dart.library.js_interop) "web.dart";
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import "package:duit_kernel/src/capabilities/logging/logging_capability.dart";
2+
import "package:flutter/foundation.dart";
3+
4+
/// A class that provides logging capabilities for the Duit UI system.
5+
///
6+
/// This class implements the [LoggingCapabilityDelegate] mixin and provides
7+
/// concrete implementations for logging messages at different levels.
8+
final class LoggingManager with LoggingCapabilityDelegate {
9+
const LoggingManager();
10+
11+
@override
12+
void logCritical(message, [Object? exception, StackTrace? stackTrace]) {
13+
final buff = StringBuffer("[DUIT FRAMEWORK] CRITICAL: $message \nTime: ${DateTime.now().toUtc()}");
14+
if (exception != null) {
15+
buff.write("\nException: ${exception.toString()}");
16+
}
17+
if (stackTrace != null) {
18+
buff.write("\nStackTrace: ${stackTrace.toString()}");
19+
}
20+
debugPrint(buff.toString());
21+
}
22+
23+
@override
24+
void logDebug(message, [Object? exception, StackTrace? stackTrace]) {
25+
final buff = StringBuffer("[DUIT FRAMEWORK] DEBUG: $message \nTime: ${DateTime.now().toUtc()}");
26+
if (exception != null) {
27+
buff.write("\nException: ${exception.toString()}");
28+
}
29+
if (stackTrace != null) {
30+
buff.write("\nStackTrace: ${stackTrace.toString()}");
31+
}
32+
debugPrint(buff.toString());
33+
}
34+
35+
@override
36+
void logError(message, [Object? exception, StackTrace? stackTrace]) {
37+
final buff = StringBuffer("[DUIT FRAMEWORK] ERROR: $message \nTime: ${DateTime.now().toUtc()}");
38+
if (exception != null) {
39+
buff.write("\nException: ${exception.toString()}");
40+
}
41+
if (stackTrace != null) {
42+
buff.write("\nStackTrace: ${stackTrace.toString()}");
43+
}
44+
debugPrint(buff.toString());
45+
}
46+
47+
@override
48+
void logInfo(message, [Object? exception, StackTrace? stackTrace]) {
49+
final buff = StringBuffer("[DUIT FRAMEWORK] INFO: $message \nTime: ${DateTime.now().toUtc()}");
50+
if (exception != null) {
51+
buff.write("\nException: ${exception.toString()}");
52+
}
53+
if (stackTrace != null) {
54+
buff.write("\nStackTrace: ${stackTrace.toString()}");
55+
}
56+
debugPrint(buff.toString());
57+
}
58+
59+
@override
60+
void logVerbose(message, [Object? exception, StackTrace? stackTrace]) {
61+
final buff = StringBuffer("[DUIT FRAMEWORK] VERBOSE: $message \nTime: ${DateTime.now().toUtc()}");
62+
if (exception != null) {
63+
buff.write("\nException: ${exception.toString()}");
64+
}
65+
if (stackTrace != null) {
66+
buff.write("\nStackTrace: ${stackTrace.toString()}");
67+
}
68+
debugPrint(buff.toString());
69+
}
70+
71+
@override
72+
void logWarning(message, [Object? exception, StackTrace? stackTrace]) {
73+
final buff = StringBuffer("[DUIT FRAMEWORK] WARNING: $message \nTime: ${DateTime.now().toUtc()}");
74+
if (exception != null) {
75+
buff.write("\nException: ${exception.toString()}");
76+
}
77+
if (stackTrace != null) {
78+
buff.write("\nStackTrace: ${stackTrace.toString()}");
79+
}
80+
debugPrint(buff.toString());
81+
}
82+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import "package:duit_kernel/src/misc/error.dart";
2+
import "package:meta/meta.dart";
3+
4+
/// A mixin that provides logging capabilities compatible with Talker API.
5+
///
6+
/// This mixin defines methods for logging messages at different levels,
7+
/// handling exceptions, and logging custom log types. All methods must be
8+
/// overridden by implementing classes.
9+
mixin LoggingCapabilityDelegate {
10+
/// Logs an informational message.
11+
///
12+
/// Use this method to log general informational messages about the
13+
/// application's state or flow.
14+
@mustBeOverridden
15+
void logInfo(
16+
message, [
17+
Object? exception,
18+
StackTrace? stackTrace,
19+
]) =>
20+
throw const MissingCapabilityMethodImplementation(
21+
"info",
22+
"LoggingCapabilityDelegate",
23+
);
24+
25+
/// Logs a debug message.
26+
///
27+
/// Use this method to log detailed debugging information that is typically
28+
/// only useful during development.
29+
@mustBeOverridden
30+
void logDebug(
31+
message, [
32+
Object? exception,
33+
StackTrace? stackTrace,
34+
]) =>
35+
throw const MissingCapabilityMethodImplementation(
36+
"debug",
37+
"LoggingCapabilityDelegate",
38+
);
39+
40+
/// Logs a warning message.
41+
///
42+
/// Use this method to log warnings about potential issues or unexpected
43+
/// conditions that don't prevent the application from functioning.
44+
@mustBeOverridden
45+
void logWarning(
46+
message, [
47+
Object? exception,
48+
StackTrace? stackTrace,
49+
]) =>
50+
throw const MissingCapabilityMethodImplementation(
51+
"warning",
52+
"LoggingCapabilityDelegate",
53+
);
54+
55+
/// Logs an error message.
56+
///
57+
/// Use this method to log error messages that indicate problems or failures
58+
/// in the application.
59+
@mustBeOverridden
60+
void logError(
61+
message, [
62+
Object? exception,
63+
StackTrace? stackTrace,
64+
]) =>
65+
throw const MissingCapabilityMethodImplementation(
66+
"error",
67+
"LoggingCapabilityDelegate",
68+
);
69+
70+
/// Logs a critical error message.
71+
///
72+
/// Use this method to log critical errors that require immediate attention
73+
/// and may indicate serious problems in the application.
74+
@mustBeOverridden
75+
void logCritical(
76+
message, [
77+
Object? exception,
78+
StackTrace? stackTrace,
79+
]) =>
80+
throw const MissingCapabilityMethodImplementation(
81+
"critical",
82+
"LoggingCapabilityDelegate",
83+
);
84+
85+
/// Logs a verbose message.
86+
///
87+
/// Use this method to log very detailed information that is typically only
88+
/// useful for deep debugging or troubleshooting.
89+
@mustBeOverridden
90+
void logVerbose(
91+
message, [
92+
Object? exception,
93+
StackTrace? stackTrace,
94+
]) =>
95+
throw const MissingCapabilityMethodImplementation(
96+
"verbose",
97+
"LoggingCapabilityDelegate",
98+
);
99+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import "dart:js_interop";
2+
3+
import "package:duit_kernel/src/capabilities/logging/logging_capability.dart";
4+
import "package:web/web.dart";
5+
6+
/// A class that provides logging capabilities for the Duit UI system.
7+
///
8+
/// This class implements the [LoggingCapabilityDelegate] mixin and provides
9+
/// concrete implementations for logging messages at different levels.
10+
final class LoggingManager with LoggingCapabilityDelegate {
11+
const LoggingManager();
12+
13+
@override
14+
void logCritical(message, [Object? exception, StackTrace? stackTrace]) {
15+
final buff = StringBuffer("[DUIT FRAMEWORK] CRITICAL: $message \nTime: ${DateTime.now().toIso8601String()}");
16+
if (exception != null) {
17+
buff.write("\nException: ${exception.toString()}");
18+
}
19+
if (stackTrace != null) {
20+
buff.write("\nStackTrace: ${stackTrace.toString()}");
21+
}
22+
console.error(buff.toString().toJS);
23+
}
24+
25+
@override
26+
void logDebug(message, [Object? exception, StackTrace? stackTrace]) {
27+
final buff = StringBuffer("[DUIT FRAMEWORK] DEBUG: $message \nTime: ${DateTime.now().toIso8601String()}");
28+
if (exception != null) {
29+
buff.write("\nException: ${exception.toString()}");
30+
}
31+
if (stackTrace != null) {
32+
buff.write("\nStackTrace: ${stackTrace.toString()}");
33+
}
34+
console.debug(buff.toString().toJS);
35+
}
36+
37+
@override
38+
void logError(message, [Object? exception, StackTrace? stackTrace]) {
39+
final buff = StringBuffer("[DUIT FRAMEWORK] ERROR: $message \nTime: ${DateTime.now().toIso8601String()}");
40+
if (exception != null) {
41+
buff.write("\nException: ${exception.toString()}");
42+
}
43+
if (stackTrace != null) {
44+
buff.write("\nStackTrace: ${stackTrace.toString()}");
45+
}
46+
console.error(buff.toString().toJS);
47+
}
48+
49+
@override
50+
void logInfo(message, [Object? exception, StackTrace? stackTrace]) {
51+
final buff = StringBuffer("[DUIT FRAMEWORK] INFO: $message \nTime: ${DateTime.now().toIso8601String()}");
52+
if (exception != null) {
53+
buff.write("\nException: ${exception.toString()}");
54+
}
55+
if (stackTrace != null) {
56+
buff.write("\nStackTrace: ${stackTrace.toString()}");
57+
}
58+
console.info(buff.toString().toJS);
59+
}
60+
61+
@override
62+
void logVerbose(message, [Object? exception, StackTrace? stackTrace]) {
63+
final buff = StringBuffer("[DUIT FRAMEWORK] VERBOSE: $message \nTime: ${DateTime.now().toIso8601String()}");
64+
if (exception != null) {
65+
buff.write("\nException: ${exception.toString()}");
66+
}
67+
if (stackTrace != null) {
68+
buff.write("\nStackTrace: ${stackTrace.toString()}");
69+
}
70+
console.log(buff.toString().toJS);
71+
}
72+
73+
@override
74+
void logWarning(message, [Object? exception, StackTrace? stackTrace]) {
75+
final buff = StringBuffer("[DUIT FRAMEWORK] WARNING: $message \nTime: ${DateTime.now().toIso8601String()}");
76+
if (exception != null) {
77+
buff.write("\nException: ${exception.toString()}");
78+
}
79+
if (stackTrace != null) {
80+
buff.write("\nStackTrace: ${stackTrace.toString()}");
81+
}
82+
console.warn(buff.toString().toJS);
83+
}
84+
}

0 commit comments

Comments
 (0)