diff --git a/examples/dart/clock/lib/keyboard_handler.dart b/examples/dart/clock/lib/keyboard_handler.dart index 459c746..ba147e8 100644 --- a/examples/dart/clock/lib/keyboard_handler.dart +++ b/examples/dart/clock/lib/keyboard_handler.dart @@ -50,7 +50,7 @@ class KeyboardHandler { void start() async { resetScreen(); - replyChannel.stream.where((res) => res.type == Reply.Tick).listen((_) { + rid.replyChannel.stream.where((res) => res.type == Reply.Tick).listen((_) { ridStoreLock(); resetScreen(); ridStoreUnlock(); diff --git a/examples/dart/todo/lib/messages.dart b/examples/dart/todo/lib/messages.dart index 2024716..c3f08f6 100644 --- a/examples/dart/todo/lib/messages.dart +++ b/examples/dart/todo/lib/messages.dart @@ -4,7 +4,7 @@ import 'dart:async'; import 'log.dart'; messages() async { - RID_DEBUG_REPLY = (reply) => log.d('$reply'); + rid.debugReply = (reply) => log.d('$reply'); final store = Store.instance; await store.msgAddTodo("Hello"); diff --git a/rid-build/dart/_message_channel.dart b/rid-build/dart/_message_channel.dart index 925a7e4..fd16b4d 100644 --- a/rid-build/dart/_message_channel.dart +++ b/rid-build/dart/_message_channel.dart @@ -6,20 +6,20 @@ import '_isolate_binding.dart' show initIsolate; const String _MSG_SEPARATOR = '^'; -enum RidMsgType { Severe, Error, LogWarn, LogInfo, LogDebug } +enum RidMessageType { Severe, Error, LogWarn, LogInfo, LogDebug } -RidMsgType _ridMsgTypeFromString(String s) { +RidMessageType _ridMsgTypeFromString(String s) { switch (s.toLowerCase()) { case "err_severe": - return RidMsgType.Severe; + return RidMessageType.Severe; case "err_error": - return RidMsgType.Error; + return RidMessageType.Error; case "log_warn": - return RidMsgType.LogWarn; + return RidMessageType.LogWarn; case "log_info": - return RidMsgType.LogInfo; + return RidMessageType.LogInfo; case "log_debug": - return RidMsgType.LogDebug; + return RidMessageType.LogDebug; default: throw ArgumentError.value(s); } @@ -27,12 +27,12 @@ RidMsgType _ridMsgTypeFromString(String s) { final _REMOVE_QUOTE_RX = RegExp(r'(^"|"$)'); -class RidMsg { - final RidMsgType type; +class RidMessage { + final RidMessageType type; late final String message; late final String? details; - RidMsg._(this.type, String message, String? details) { + RidMessage._(this.type, String message, String? details) { this.message = message.replaceAll(_REMOVE_QUOTE_RX, ''); this.details = details?.replaceAll(_REMOVE_QUOTE_RX, ''); } @@ -40,13 +40,13 @@ class RidMsg { @override String toString() { final detailsString = details == null ? '' : ', details: "$details"'; - return 'RidMsg{ type: $type, message: "$message"$detailsString }'; + return 'RidMessage{ type: $type, message: "$message"$detailsString }'; } @override bool operator ==(Object other) => identical(this, other) || - other is RidMsg && + other is RidMessage && runtimeType == other.runtimeType && type == other.type && message == other.message && @@ -56,14 +56,18 @@ class RidMsg { int get hashCode => type.hashCode ^ message.hashCode ^ details.hashCode; } -class RidMsgChannel { +abstract class RidMessageChannel { + Stream get stream; +} + +class RidMessageChannelInternal implements RidMessageChannel { final _zone = Zone.current; - final StreamController _sink; + final StreamController _sink; final DynamicLibrary _dl; late final RawReceivePort _receivePort; late final _zonedAdd; - RidMsgChannel._(this._dl, bool isDebugMode) + RidMessageChannelInternal._(this._dl, bool isDebugMode) : _sink = StreamController.broadcast() { _receivePort = RawReceivePort(_onReceivedMsg, 'rid::messaging_channel::port'); @@ -82,7 +86,7 @@ class RidMsgChannel { } } - RidMsg _decode(String data) { + RidMessage _decode(String data) { int sepIdx = data.indexOf(_MSG_SEPARATOR); final type = data.substring(0, sepIdx); final msgType = _ridMsgTypeFromString(type); @@ -91,15 +95,15 @@ class RidMsgChannel { sepIdx = msg.indexOf(_MSG_SEPARATOR); if (sepIdx < 0) { // No details - return RidMsg._(msgType, msg, null); + return RidMessage._(msgType, msg, null); } else { final message = msg.substring(0, sepIdx); final details = msg.substring(sepIdx + 1); - return RidMsg._(msgType, message, details); + return RidMessage._(msgType, message, details); } } - Stream get stream => _sink.stream; + Stream get stream => _sink.stream; int get nativePort { return _receivePort.sendPort.nativePort; @@ -111,7 +115,7 @@ class RidMsgChannel { } static bool _initialized = false; - static RidMsgChannel instance( + static RidMessageChannelInternal instance( DynamicLibrary dl, bool isDebugMode, ) { @@ -120,6 +124,6 @@ class RidMsgChannel { "The message channel can only be initialized once unless running in debug mode"); } _initialized = true; - return RidMsgChannel._(dl, isDebugMode); + return RidMessageChannelInternal._(dl, isDebugMode); } } diff --git a/rid-build/dart/_reply_channel.dart b/rid-build/dart/_reply_channel.dart index 55962b8..9317cca 100644 --- a/rid-build/dart/_reply_channel.dart +++ b/rid-build/dart/_reply_channel.dart @@ -12,8 +12,13 @@ abstract class IReply { typedef Decode = TReply Function(int packedBase, String? data); +abstract class RidReplyChannel { + Stream get stream; +} + // TODO: error handling (could be part of Post data) -class ReplyChannel { +class RidReplyChannelInternal + implements RidReplyChannel { final _zone = Zone.current; final StreamController _sink; final Decode _decode; @@ -22,7 +27,7 @@ class ReplyChannel { late final _zonedAdd; int _lastReqId = 0; - ReplyChannel._(this._dl, this._decode, bool isDebugMode) + RidReplyChannelInternal._(this._dl, this._decode, bool isDebugMode) : _sink = StreamController.broadcast() { _receivePort = RawReceivePort(_onReceivedReply, 'rid::reply_channel::port'); initIsolate(this._dl, 'rid_init_reply_isolate', @@ -77,7 +82,7 @@ class ReplyChannel { } static bool _initialized = false; - static ReplyChannel instance( + static RidReplyChannelInternal instance( DynamicLibrary dl, Decode decode, bool isDebugMode, @@ -87,6 +92,6 @@ class ReplyChannel { "The reply channel can only be initialized once unless running in debug mode"); } _initialized = true; - return ReplyChannel._(dl, decode, isDebugMode); + return RidReplyChannelInternal._(dl, decode, isDebugMode); } } diff --git a/rid-build/dart/_reply_channel_stub.dart b/rid-build/dart/_reply_channel_stub.dart index 9bed166..ec53a42 100644 --- a/rid-build/dart/_reply_channel_stub.dart +++ b/rid-build/dart/_reply_channel_stub.dart @@ -1,6 +1,6 @@ -class ReplyChannelStub { +class RidReplyChannelStub { Future dispose() => Future.value(); } /// Stubbing reply channel until we declare a #[rid:reply] enum. -final replyChannel = ReplyChannelStub(); +final _replyChannel = RidReplyChannelStub(); diff --git a/rid-build/dart/_rid_rid.dart b/rid-build/dart/_rid_rid.dart index fd49ab5..bae642a 100644 --- a/rid-build/dart/_rid_rid.dart +++ b/rid-build/dart/_rid_rid.dart @@ -7,11 +7,14 @@ final dart_ffi.DynamicLibrary _dl = _open(); // Expose a rid instance which initializes and provides access to various features facilitating Dart/Rust interaction // class Rid { - final RidMsgChannel _messageChannel; + final RidMessageChannelInternal _messageChannel; + Duration? replyTimeout; + Rid._(dart_ffi.DynamicLibrary dl, bool isDebugMode) - : _messageChannel = RidMsgChannel.instance(dl, isDebugMode); + : _messageChannel = RidMessageChannelInternal.instance(dl, isDebugMode), + replyTimeout = const Duration(milliseconds: 200); - RidMsgChannel get messageChannel => _messageChannel; + RidMessageChannel get messageChannel => _messageChannel; } final rid = Rid._(_dl, _isDebugMode); diff --git a/rid-build/src/dart_generator.rs b/rid-build/src/dart_generator.rs index 87a3558..b1e8e39 100644 --- a/rid-build/src/dart_generator.rs +++ b/rid-build/src/dart_generator.rs @@ -58,7 +58,7 @@ fn dart_ffi_reexports() -> String { fn message_channel_reexports(message_channel: &str) -> String { format!( - "export '{message_channel}' show RidMsgChannel, RidMsg, RidMsgType;\n", + "export '{message_channel}' show RidMessageChannel, RidMessage, RidMessageType;\n", message_channel = message_channel ) } diff --git a/rid-common/src/constants.rs b/rid-common/src/constants.rs index 301eefb..670417b 100644 --- a/rid-common/src/constants.rs +++ b/rid-common/src/constants.rs @@ -29,13 +29,19 @@ pub const STRING_REF_ACCESS: &str = "rid_access_string_ref"; pub const UTILS_MODULE: &str = "__rid_utils_module"; /// Function set to debug rid store locking -pub const RID_DEBUG_LOCK: &str = "RID_DEBUG_LOCK"; +pub const RID_DEBUG_LOCK: &str = "rid.debugLock"; /// Function set to debug posted replies -pub const RID_DEBUG_REPLY: &str = "RID_DEBUG_REPLY"; +pub const RID_DEBUG_REPLY: &str = "rid.debugReply"; -/// Duration set to specify default message timeout -pub const RID_MSG_TIMEOUT: &str = "RID_MSG_TIMEOUT"; +/// Duration set to specify default reply timeout +pub const RID_MSG_TIMEOUT: &str = "rid.replyTimeout"; + +/// Access to reply channel user facing API +pub const RID_REPLY_CHANNEL: &str = "rid.replyChannel"; + +/// Access to internal reply channel API +pub const _RID_REPLY_CHANNEL: &str = "_replyChannel"; /// Dart method name to create the Rust store pub const RID_CREATE_STORE: &str = "_createStore"; diff --git a/rid-macro-impl/src/message/message_test.rs b/rid-macro-impl/src/message/message_test.rs index 8d8df3f..b72eeca 100644 --- a/rid-macro-impl/src/message/message_test.rs +++ b/rid-macro-impl/src/message/message_test.rs @@ -85,33 +85,33 @@ mod msg_variants_without_fields { } }; let expected_dart = r###" - extension Rid_Message_ExtOnPointerStoreForMsg on dart_ffi.Pointer { + extension Rid_Message_ExtOnPointerStoreForMsg on dart_ffi.Pointer { - Future msgInit({Duration? timeout}) { - final reqId = replyChannel.reqId; - rid_ffi.rid_msg_Init(reqId, ); + Future msgInit({Duration? timeout}) { + final reqId = _replyChannel.reqId; + rid_ffi.rid_msg_Init(reqId, ); - final reply = _isDebugMode && RID_DEBUG_REPLY != null - ? replyChannel.reply(reqId).then((PostedReply reply) { - if (RID_DEBUG_REPLY != null) RID_DEBUG_REPLY!(reply); - return reply; - }) - : replyChannel.reply(reqId); + final reply = _isDebugMode && rid.debugReply != null + ? _replyChannel.reply(reqId).then((PostedReply reply) { + if (rid.debugReply != null) rid.debugReply!(reply); + return reply; + }) + : _replyChannel.reply(reqId); - if (!_isDebugMode) return reply; + if (!_isDebugMode) return reply; - timeout ??= RID_MSG_TIMEOUT; - if (timeout == null) return reply; - final msgCall = 'msgInit() with reqId: $reqId'; - return _replyWithTimeout(reply, msgCall, StackTrace.current, timeout); - } + timeout ??= rid.replyTimeout; + if (timeout == null) return reply; + final msgCall = 'msgInit() with reqId: $reqId'; + return _replyWithTimeout(reply, msgCall, StackTrace.current, timeout); } + } - extension MsgApiFor_Store on Store { - Future msgInit({Duration? timeout}) { - return _store.msgInit(timeout: timeout); - } + extension MsgApiFor_Store on Store { + Future msgInit({Duration? timeout}) { + return _store.msgInit(timeout: timeout); } + } "###; let rust = render_rust(&msg); let dart = render_dart(&msg); @@ -143,55 +143,55 @@ mod msg_variants_without_fields { } }; let expected_dart = r###" - extension Rid_Message_ExtOnPointerStoreForMsg on dart_ffi.Pointer { + extension Rid_Message_ExtOnPointerStoreForMsg on dart_ffi.Pointer { - Future msgInit({Duration? timeout}) { - final reqId = replyChannel.reqId; - rid_ffi.rid_msg_Init(reqId, ); + Future msgInit({Duration? timeout}) { + final reqId = _replyChannel.reqId; + rid_ffi.rid_msg_Init(reqId, ); - final reply = _isDebugMode && RID_DEBUG_REPLY != null - ? replyChannel.reply(reqId).then((PostedReply reply) { - if (RID_DEBUG_REPLY != null) RID_DEBUG_REPLY!(reply); - return reply; - }) - : replyChannel.reply(reqId); + final reply = _isDebugMode && rid.debugReply != null + ? _replyChannel.reply(reqId).then((PostedReply reply) { + if (rid.debugReply != null) rid.debugReply!(reply); + return reply; + }) + : _replyChannel.reply(reqId); - if (!_isDebugMode) return reply; + if (!_isDebugMode) return reply; - timeout ??= RID_MSG_TIMEOUT; - if (timeout == null) return reply; - final msgCall = 'msgInit() with reqId: $reqId'; - return _replyWithTimeout(reply, msgCall, StackTrace.current, timeout); - } + timeout ??= rid.replyTimeout; + if (timeout == null) return reply; + final msgCall = 'msgInit() with reqId: $reqId'; + return _replyWithTimeout(reply, msgCall, StackTrace.current, timeout); + } - Future msgDeinit({Duration? timeout}) { - final reqId = replyChannel.reqId; - rid_ffi.rid_msg_Deinit(reqId, ); + Future msgDeinit({Duration? timeout}) { + final reqId = _replyChannel.reqId; + rid_ffi.rid_msg_Deinit(reqId, ); - final reply = _isDebugMode && RID_DEBUG_REPLY != null - ? replyChannel.reply(reqId).then((PostedReply reply) { - if (RID_DEBUG_REPLY != null) RID_DEBUG_REPLY!(reply); - return reply; - }) - : replyChannel.reply(reqId); + final reply = _isDebugMode && rid.debugReply != null + ? _replyChannel.reply(reqId).then((PostedReply reply) { + if (rid.debugReply != null) rid.debugReply!(reply); + return reply; + }) + : _replyChannel.reply(reqId); - if (!_isDebugMode) return reply; + if (!_isDebugMode) return reply; - timeout ??= RID_MSG_TIMEOUT; - if (timeout == null) return reply; - final msgCall = 'msgDeinit() with reqId: $reqId'; - return _replyWithTimeout(reply, msgCall, StackTrace.current, timeout); - } + timeout ??= rid.replyTimeout; + if (timeout == null) return reply; + final msgCall = 'msgDeinit() with reqId: $reqId'; + return _replyWithTimeout(reply, msgCall, StackTrace.current, timeout); } + } - extension MsgApiFor_Store on Store { - Future msgInit({Duration? timeout}) { - return _store.msgInit(timeout: timeout); - } - Future msgDeinit({Duration? timeout}) { - return _store.msgDeinit(timeout: timeout); - } + extension MsgApiFor_Store on Store { + Future msgInit({Duration? timeout}) { + return _store.msgInit(timeout: timeout); + } + Future msgDeinit({Duration? timeout}) { + return _store.msgDeinit(timeout: timeout); } + } "###; let rust = render_rust(&msg); let dart = render_dart(&msg); @@ -228,34 +228,34 @@ mod msg_variants_with_primitive_fields { } }; let expected_dart = r###" - extension Rid_Message_ExtOnPointerStoreForMsg on dart_ffi.Pointer { + extension Rid_Message_ExtOnPointerStoreForMsg on dart_ffi.Pointer { - Future msgAdd(@dart_ffi.Int32() int arg0, {Duration? timeout}) { - final reqId = replyChannel.reqId; - rid_ffi.rid_msg_Add(reqId, arg0); + Future msgAdd(@dart_ffi.Int32() int arg0, {Duration? timeout}) { + final reqId = _replyChannel.reqId; + rid_ffi.rid_msg_Add(reqId, arg0); - final reply = _isDebugMode && RID_DEBUG_REPLY != null - ? replyChannel.reply(reqId).then((PostedReply reply) { - if (RID_DEBUG_REPLY != null) RID_DEBUG_REPLY!(reply); - return reply; - }) - : replyChannel.reply(reqId); + final reply = _isDebugMode && rid.debugReply != null + ? _replyChannel.reply(reqId).then((PostedReply reply) { + if (rid.debugReply != null) rid.debugReply!(reply); + return reply; + }) + : _replyChannel.reply(reqId); - if (!_isDebugMode) return reply; + if (!_isDebugMode) return reply; - timeout ??= RID_MSG_TIMEOUT; - if (timeout == null) return reply; - final msgCall = 'msgAdd($arg0) with reqId: $reqId'; - return _replyWithTimeout(reply, msgCall, StackTrace.current, timeout); - } + timeout ??= rid.replyTimeout; + if (timeout == null) return reply; + final msgCall = 'msgAdd($arg0) with reqId: $reqId'; + return _replyWithTimeout(reply, msgCall, StackTrace.current, timeout); } + } - extension MsgApiFor_Store on Store { - Future msgAdd(@dart_ffi.Int32() int arg0, {Duration? timeout}) { - return _store.msgAdd(arg0, timeout: timeout); - } + extension MsgApiFor_Store on Store { + Future msgAdd(@dart_ffi.Int32() int arg0, {Duration? timeout}) { + return _store.msgAdd(arg0, timeout: timeout); } - "###; + } + "###; let rust = render_rust(&msg); let dart = render_dart(&msg); @@ -289,33 +289,33 @@ mod msg_variants_with_primitive_fields { } }; let expected_dart = r###" - extension Rid_Message_ExtOnPointerStoreForMsg on dart_ffi.Pointer { - - Future msgAdd(@dart_ffi.Int32() int arg0, String arg1, {Duration? timeout}) { - final reqId = replyChannel.reqId; - rid_ffi.rid_msg_Add(reqId, arg0, arg1.toNativeInt8()); - - final reply = _isDebugMode && RID_DEBUG_REPLY != null - ? replyChannel.reply(reqId).then((PostedReply reply) { - if (RID_DEBUG_REPLY != null) RID_DEBUG_REPLY!(reply); - return reply; - }) - : replyChannel.reply(reqId); - - if (!_isDebugMode) return reply; - - timeout ??= RID_MSG_TIMEOUT; - if (timeout == null) return reply; - final msgCall = 'msgAdd($arg0, $arg1) with reqId: $reqId'; - return _replyWithTimeout(reply, msgCall, StackTrace.current, timeout); - } - } - - extension MsgApiFor_Store on Store { - Future msgAdd(@dart_ffi.Int32() int arg0, String arg1, {Duration? timeout}) { - return _store.msgAdd(arg0, arg1, timeout: timeout); - } - } + extension Rid_Message_ExtOnPointerStoreForMsg on dart_ffi.Pointer { + + Future msgAdd(@dart_ffi.Int32() int arg0, String arg1, {Duration? timeout}) { + final reqId = _replyChannel.reqId; + rid_ffi.rid_msg_Add(reqId, arg0, arg1.toNativeInt8()); + + final reply = _isDebugMode && rid.debugReply != null + ? _replyChannel.reply(reqId).then((PostedReply reply) { + if (rid.debugReply != null) rid.debugReply!(reply); + return reply; + }) + : _replyChannel.reply(reqId); + + if (!_isDebugMode) return reply; + + timeout ??= rid.replyTimeout; + if (timeout == null) return reply; + final msgCall = 'msgAdd($arg0, $arg1) with reqId: $reqId'; + return _replyWithTimeout(reply, msgCall, StackTrace.current, timeout); + } + } + + extension MsgApiFor_Store on Store { + Future msgAdd(@dart_ffi.Int32() int arg0, String arg1, {Duration? timeout}) { + return _store.msgAdd(arg0, arg1, timeout: timeout); + } + } "###; let rust = render_rust(&msg); @@ -352,30 +352,31 @@ mod msg_variants_with_enum_fields { }; let expected_dart = r###" extension Rid_Message_ExtOnPointerStoreForMsg on dart_ffi.Pointer { - Future msgSetFilter(int arg0, {Duration? timeout}) { - final reqId = replyChannel.reqId; - rid_ffi.rid_msg_SetFilter(reqId, arg0); - - final reply = _isDebugMode && RID_DEBUG_REPLY != null - ? replyChannel.reply(reqId).then((PostedReply reply) { - if (RID_DEBUG_REPLY != null) RID_DEBUG_REPLY!(reply); - return reply; - }) - : replyChannel.reply(reqId); - - if (!_isDebugMode) return reply; - - timeout ??= RID_MSG_TIMEOUT; - if (timeout == null) return reply; - final msgCall = 'msgSetFilter($arg0) with reqId: $reqId'; - return _replyWithTimeout(reply, msgCall, StackTrace.current, timeout); - } + + Future msgSetFilter(int arg0, {Duration? timeout}) { + final reqId = _replyChannel.reqId; + rid_ffi.rid_msg_SetFilter(reqId, arg0); + + final reply = _isDebugMode && rid.debugReply != null + ? _replyChannel.reply(reqId).then((PostedReply reply) { + if (rid.debugReply != null) rid.debugReply!(reply); + return reply; + }) + : _replyChannel.reply(reqId); + + if (!_isDebugMode) return reply; + + timeout ??= rid.replyTimeout; + if (timeout == null) return reply; + final msgCall = 'msgSetFilter($arg0) with reqId: $reqId'; + return _replyWithTimeout(reply, msgCall, StackTrace.current, timeout); + } } extension MsgApiFor_Store on Store { - Future msgSetFilter(Filter arg0, {Duration? timeout}) { + Future msgSetFilter(Filter arg0, {Duration? timeout}) { return _store.msgSetFilter(arg0.index, timeout: timeout); - } + } } "###; diff --git a/rid-macro-impl/src/message/render_message_enum.rs b/rid-macro-impl/src/message/render_message_enum.rs index 5235909..1b9ca2a 100644 --- a/rid-macro-impl/src/message/render_message_enum.rs +++ b/rid-macro-impl/src/message/render_message_enum.rs @@ -2,7 +2,7 @@ use proc_macro2::TokenStream; use quote::{format_ident, quote, quote_spanned, IdentFragment}; use rid_common::{ DART_ASYNC, DART_FFI, FFI_GEN_BIND, RID_DEBUG_REPLY, RID_FFI, - RID_MSG_TIMEOUT, STRING_TO_NATIVE_INT8, + RID_MSG_TIMEOUT, STRING_TO_NATIVE_INT8, _RID_REPLY_CHANNEL, }; use syn::Ident; @@ -229,8 +229,6 @@ impl ParsedMessageEnum { } else { format!( r###"{comment} -{comment} final Duration? RID_MSG_TIMEOUT = const Duration(milliseconds: 200); -{comment} {comment} Future<{class_name}> _replyWithTimeout( {comment} Future<{class_name}> reply, {comment} String msgCall, @@ -359,15 +357,15 @@ impl ParsedMessageEnum { format!( r###" {comment} Future<{class_name}> {dart_method_name}({args_decl}{{Duration? timeout}}) {{ -{comment} final reqId = replyChannel.reqId; +{comment} final reqId = {_RID_REPLY_CHANNEL}.reqId; {comment} {rid_ffi}.{method_name}(reqId, {args_call}); {comment} {comment} final reply = _isDebugMode && {rid_debug_reply} != null -{comment} ? replyChannel.reply(reqId).then(({class_name} reply) {{ +{comment} ? {_RID_REPLY_CHANNEL}.reply(reqId).then(({class_name} reply) {{ {comment} if ({rid_debug_reply} != null) {rid_debug_reply}!(reply); {comment} return reply; {comment} }}) -{comment} : replyChannel.reply(reqId); +{comment} : {_RID_REPLY_CHANNEL}.reply(reqId); {comment} {comment} if (!_isDebugMode) return reply; {comment} @@ -383,6 +381,7 @@ impl ParsedMessageEnum { args_call = args_call, args_string = args_string, rid_ffi = RID_FFI, + _RID_REPLY_CHANNEL = _RID_REPLY_CHANNEL, rid_debug_reply = RID_DEBUG_REPLY, rid_msg_timeout = RID_MSG_TIMEOUT, comment = comment diff --git a/rid-macro-impl/src/model/store/store_module.rs b/rid-macro-impl/src/model/store/store_module.rs index 9a014e2..2fd96af 100644 --- a/rid-macro-impl/src/model/store/store_module.rs +++ b/rid-macro-impl/src/model/store/store_module.rs @@ -10,7 +10,7 @@ use crate::{ }; use rid_common::{ DART_FFI, FFI_GEN_BIND, RID_CREATE_STORE, RID_DEBUG_LOCK, RID_DEBUG_REPLY, - RID_FFI, RID_MSG_TIMEOUT, + RID_FFI, RID_MSG_TIMEOUT, _RID_REPLY_CHANNEL, }; pub fn render_store_module(store_ident: &syn::Ident) -> TokenStream { if &store_ident.to_string() != "Store" { @@ -38,11 +38,12 @@ pub fn render_store_module(store_ident: &syn::Ident) -> TokenStream { /// /// Disposes the store and closes the Rust reply channel in order to allow the app /// /// to exit properly. This needs to be called when exiting a Dart application. /// Future dispose() {{ -/// return replyChannel.dispose(); +/// return {_RID_REPLY_CHANNEL}.dispose(); /// }} /// }} /// ``` "###, + _RID_REPLY_CHANNEL = _RID_REPLY_CHANNEL, dart_ffi = DART_FFI, ffigen_bind = FFI_GEN_BIND, RawStore = raw_store_ident @@ -55,7 +56,7 @@ pub fn render_store_module(store_ident: &syn::Ident) -> TokenStream { /// ```dart /// int _locks = 0; /// -/// void Function(bool, int, {{String? request}})? RID_DEBUG_LOCK = (bool locking, int locks, {{String? request}}) {{ +/// void Function(bool, int, {{String? request}})? _RID_DEBUG_LOCK = (bool locking, int locks, {{String? request}}) {{ /// if (locking) {{ /// if (locks == 1) print('🔐 {{'); /// if (request != null) print(' $request'); @@ -64,19 +65,26 @@ pub fn render_store_module(store_ident: &syn::Ident) -> TokenStream { /// }} /// }}; /// +/// extension DebugLockConfig on Rid {{ +/// void Function(bool, int, {{String? request}})? get debugLock => _RID_DEBUG_LOCK; +/// void set debugLock(void Function(bool, int, {{String? request}})? val) => +/// _RID_DEBUG_LOCK = val; +/// }} +/// /// void ridStoreLock({{String? request}}) {{ /// if (_locks == 0) {rid_ffi}.rid_store_lock(); /// _locks++; -/// if (RID_DEBUG_LOCK != null) RID_DEBUG_LOCK!(true, _locks, request: request); +/// if ({RID_DEBUG_LOCK} != null) {RID_DEBUG_LOCK}!(true, _locks, request: request); /// }} /// /// void ridStoreUnlock() {{ /// _locks--; -/// if (RID_DEBUG_LOCK != null) RID_DEBUG_LOCK!(false, _locks); +/// if ({RID_DEBUG_LOCK} != null) {RID_DEBUG_LOCK}!(false, _locks); /// if (_locks == 0) {rid_ffi}.rid_store_unlock(); /// }} /// ``` "###, + RID_DEBUG_LOCK = RID_DEBUG_LOCK, rid_ffi = RID_FFI, ) .parse() @@ -86,9 +94,9 @@ pub fn render_store_module(store_ident: &syn::Ident) -> TokenStream { r###" /// ```dart /// void _initRid() {{ -/// print('Set {rid_debug_lock} to change if/how locking the rid store is logged'); -/// print('Set {rid_debug_reply} to change if/how posted replies are logged'); -/// print('Set {rid_msg_timeout} to change the default for if/when messages without reply time out'); +/// print('Set {RID_DEBUG_LOCK} to change if/how locking the rid store is logged'); +/// print('Set {RID_DEBUG_REPLY} to change if/how posted replies are logged'); +/// print('Set {RID_MSG_TIMEOUT} to change the default for if/when messages without reply time out'); /// }} /// /// {dart_ffi}.Pointer<{ffigen_bind}.{RawStore}> {createStore}() {{ @@ -98,9 +106,9 @@ pub fn render_store_module(store_ident: &syn::Ident) -> TokenStream { /// ``` "###, RawStore = raw_store_ident, - rid_debug_lock = RID_DEBUG_LOCK, - rid_debug_reply = RID_DEBUG_REPLY, - rid_msg_timeout = RID_MSG_TIMEOUT, + RID_DEBUG_LOCK = RID_DEBUG_LOCK, + RID_DEBUG_REPLY = RID_DEBUG_REPLY, + RID_MSG_TIMEOUT = RID_MSG_TIMEOUT, createStore = RID_CREATE_STORE, rid_ffi = RID_FFI, ffigen_bind = FFI_GEN_BIND, diff --git a/rid-macro-impl/src/reply/render_reply_dart.rs b/rid-macro-impl/src/reply/render_reply_dart.rs index 186ab87..215b1b0 100644 --- a/rid-macro-impl/src/reply/render_reply_dart.rs +++ b/rid-macro-impl/src/reply/render_reply_dart.rs @@ -1,4 +1,5 @@ use proc_macro2::TokenStream; +use rid_common::{RID_DEBUG_REPLY, _RID_REPLY_CHANNEL}; use syn::{punctuated::Punctuated, ItemEnum, Token, Variant}; use crate::{ @@ -42,10 +43,16 @@ pub fn render_reply_dart( {comment} }} {comment} }} {comment} -{comment} void Function({PostedReply})? RID_DEBUG_REPLY = (PostedReply reply) {{ +{comment} void Function({PostedReply})? _RID_DEBUG_REPLY = ({PostedReply} reply) {{ {comment} print('$reply'); {comment} }}; {comment} +{comment} +{comment} extension PostedReplyConfig on Rid {{ +{comment} void Function({PostedReply})? get debugReply => _RID_DEBUG_REPLY; +{comment} void set debugReply(void Function({PostedReply})? val) => _RID_DEBUG_REPLY = val; +{comment} }} +{comment} {comment} const int _TYPE_MASK= 0x000000000000ffff; {comment} const int _I64_MIN = -9223372036854775808; {comment} @@ -58,10 +65,15 @@ pub fn render_reply_dart( {comment} return {class_name}._(type, reqId, data); {comment} }} {comment} -{comment} final ReplyChannel<{class_name}> replyChannel = ReplyChannel.instance(_dl, decode, _isDebugMode); +{comment} final RidReplyChannelInternal<{class_name}> _replyChannel = RidReplyChannelInternal.instance(_dl, decode, _isDebugMode); +{comment} +{comment} extension ExposeRidReplyChannel on Rid {{ +{comment} RidReplyChannel<{PostedReply}> get replyChannel => {_RID_REPLY_CHANNEL}; +{comment} }} {comment} ``` "###, PostedReply = posted_reply_type, + _RID_REPLY_CHANNEL = _RID_REPLY_CHANNEL, comment = comment, enum = dart_enum_name, class_name = class_name, diff --git a/tests/dart/apps/test/todo.dart b/tests/dart/apps/test/todo.dart index 2b75db1..aa62807 100644 --- a/tests/dart/apps/test/todo.dart +++ b/tests/dart/apps/test/todo.dart @@ -3,8 +3,8 @@ import 'package:tests_apps/generated/rid_api.dart'; void main() { test('field_access: enums', () async { - RID_DEBUG_LOCK = null; - RID_DEBUG_REPLY = null; + rid.debugLock = null; + rid.debugReply = null; final store = Store.instance; await store.msgAddTodo("Hello"); diff --git a/tests/dart/export/test/enums.dart b/tests/dart/export/test/enums.dart index 4b8e3ab..94801e8 100644 --- a/tests/dart/export/test/enums.dart +++ b/tests/dart/export/test/enums.dart @@ -3,6 +3,7 @@ import 'package:test/test.dart'; import '../lib/generated/rid_api.dart'; void main() { + rid.debugLock = null; test('field_access: enums', () { final store = Store.instance; diff --git a/tests/dart/export/test/primitives.dart b/tests/dart/export/test/primitives.dart index 8870d18..75e9f3f 100644 --- a/tests/dart/export/test/primitives.dart +++ b/tests/dart/export/test/primitives.dart @@ -3,8 +3,7 @@ import 'package:test/test.dart'; import '../lib/generated/rid_api.dart'; void main() { - RID_DEBUG_LOCK = null; - RID_DEBUG_REPLY = null; + rid.debugLock = null; test('export: impl methods primitives owned', () { final store = Store.instance; diff --git a/tests/dart/export/test/strings.dart b/tests/dart/export/test/strings.dart index b914922..f604a58 100644 --- a/tests/dart/export/test/strings.dart +++ b/tests/dart/export/test/strings.dart @@ -3,6 +3,7 @@ import 'package:test/test.dart'; import '../lib/generated/rid_api.dart'; void main() { + rid.debugLock = null; test('export: strings owned', () { final store = Store.instance; // owned diff --git a/tests/dart/export/test/vecs.dart b/tests/dart/export/test/vecs.dart index 22efaee..0ea7bff 100644 --- a/tests/dart/export/test/vecs.dart +++ b/tests/dart/export/test/vecs.dart @@ -3,6 +3,7 @@ import 'package:test/test.dart'; import '../lib/generated/rid_api.dart'; void main() { + rid.debugLock = null; final store = Store.instance; test('export: Vec<&Todo>', () { diff --git a/tests/dart/field_access/test/enums.dart b/tests/dart/field_access/test/enums.dart index ff8f8c9..7ba1655 100644 --- a/tests/dart/field_access/test/enums.dart +++ b/tests/dart/field_access/test/enums.dart @@ -3,8 +3,10 @@ import 'package:test/test.dart'; import '../lib/generated/rid_api.dart'; void main() { + rid.debugLock = null; test('field_access: enums', () { final store = Store.instance; + expect(store.raw.filter, 1, reason: 'raw enum: filter'); expect(store.filter, Filter.Completed, reason: 'enum: filter'); }); diff --git a/tests/dart/field_access/test/hash_maps.dart b/tests/dart/field_access/test/hash_maps.dart index dce3511..111d504 100644 --- a/tests/dart/field_access/test/hash_maps.dart +++ b/tests/dart/field_access/test/hash_maps.dart @@ -3,8 +3,7 @@ import 'package:test/test.dart'; import '../lib/generated/rid_api.dart'; void main() { - RID_DEBUG_LOCK = null; - RID_DEBUG_REPLY = null; + rid.debugLock = null; // ----------------- // Primitives key/val same type diff --git a/tests/dart/field_access/test/strings.dart b/tests/dart/field_access/test/strings.dart index 118402b..f6d8eae 100644 --- a/tests/dart/field_access/test/strings.dart +++ b/tests/dart/field_access/test/strings.dart @@ -3,6 +3,7 @@ import 'package:test/test.dart'; import '../lib/generated/rid_api.dart'; void main() { + rid.debugLock = null; test('field_access: strings', () { final store = Store.instance; expect(store.title, "T-shirt Store", reason: 'String'); diff --git a/tests/dart/field_access/test/vecs.dart b/tests/dart/field_access/test/vecs.dart index a69e4b6..97799a8 100644 --- a/tests/dart/field_access/test/vecs.dart +++ b/tests/dart/field_access/test/vecs.dart @@ -4,6 +4,7 @@ import '../lib/generated/rid_api.dart'; void main() { final store = Store.instance; + rid.debugLock = null; test('field_access: Vec', () { expect(store.raw.todos.length, 2, reason: 'raw todos len'); diff --git a/tests/dart/framework/test/messaging.dart b/tests/dart/framework/test/messaging.dart index 1e71b89..a2a14e2 100644 --- a/tests/dart/framework/test/messaging.dart +++ b/tests/dart/framework/test/messaging.dart @@ -4,48 +4,48 @@ import '../lib/generated/rid_api.dart'; void main() { test('messaging: isolate', () async { rid_ffi.rid_export_send_log_warn_message(0); - RidMsg msg = await rid.messageChannel.stream.first; + RidMessage msg = await rid.messageChannel.stream.first; expect(msg.toString(), - 'RidMsg{ type: RidMsgType.LogWarn, message: "Warn 0 from Rust" }'); + 'RidMessage{ type: RidMessageType.LogWarn, message: "Warn 0 from Rust" }'); }); test('messaging: log warn/info/debug which have no details', () async { rid_ffi.rid_export_send_log_warn_message(0); - RidMsg msg = await rid.messageChannel.stream.first; + RidMessage msg = await rid.messageChannel.stream.first; expect(msg.toString(), - 'RidMsg{ type: RidMsgType.LogWarn, message: "Warn 0 from Rust" }'); + 'RidMessage{ type: RidMessageType.LogWarn, message: "Warn 0 from Rust" }'); rid_ffi.rid_export_send_log_info_message(1); msg = await rid.messageChannel.stream.first; expect(msg.toString(), - 'RidMsg{ type: RidMsgType.LogInfo, message: "Info 1 from Rust" }'); + 'RidMessage{ type: RidMessageType.LogInfo, message: "Info 1 from Rust" }'); rid_ffi.rid_export_send_log_debug_message(2); msg = await rid.messageChannel.stream.first; expect(msg.toString(), - 'RidMsg{ type: RidMsgType.LogDebug, message: "Debug 2 from Rust" }'); + 'RidMessage{ type: RidMessageType.LogDebug, message: "Debug 2 from Rust" }'); }); test('messaging: error/severe with details', () async { rid_ffi.rid_export_send_error_message_with_details(1); - RidMsg msg = await rid.messageChannel.stream.first; + RidMessage msg = await rid.messageChannel.stream.first; expect(msg.toString(), - 'RidMsg{ type: RidMsgType.Error, message: "Error 1 from Rust", details: "Some Error Details" }'); + 'RidMessage{ type: RidMessageType.Error, message: "Error 1 from Rust", details: "Some Error Details" }'); rid_ffi.rid_export_send_severe_error_message_with_details(2); msg = await rid.messageChannel.stream.first; expect(msg.toString(), - 'RidMsg{ type: RidMsgType.Severe, message: "Severe Error 2 from Rust", details: "Some Severe Error Details" }'); + 'RidMessage{ type: RidMessageType.Severe, message: "Severe Error 2 from Rust", details: "Some Severe Error Details" }'); }); test('messaging: error/severe without details', () async { rid_ffi.rid_export_send_error_message_without_details(1); - RidMsg msg = await rid.messageChannel.stream.first; + RidMessage msg = await rid.messageChannel.stream.first; expect(msg.toString(), - 'RidMsg{ type: RidMsgType.Error, message: "Error 1 from Rust" }'); + 'RidMessage{ type: RidMessageType.Error, message: "Error 1 from Rust" }'); rid_ffi.rid_export_send_severe_error_message_without_details(2); msg = await rid.messageChannel.stream.first; expect(msg.toString(), - 'RidMsg{ type: RidMsgType.Severe, message: "Severe Error 2 from Rust" }'); + 'RidMessage{ type: RidMessageType.Severe, message: "Severe Error 2 from Rust" }'); }); }