Skip to content

Commit

Permalink
feat/dart-rid-api: rid object combining Rid API and config (#28)
Browse files Browse the repository at this point in the history
* rid-macro: more consistent use of rid config globals names

* rid-macro: rid.debugReply exposes _RID_DEBUG_REPLY

* rid-macro: rid.debugLock exposes _RID_DEBUG_LOCK

* rid-macro: rid.msgTimeout instead of separate global

* rid-macro: exposing limited rid.replyChannel API

* rid-build: expose limited rid.messageChannel API

* rid-build: rename ReplyChannel to RidReplyChannel

* rid-build: correcting rid message toString

* rid-build: renaming msgTimeout to replyTimeout

* test: adapting tests to API changes

* examples: adapting to latest API
  • Loading branch information
thlorenz authored Sep 15, 2021
1 parent 852d5d6 commit ce4dd17
Show file tree
Hide file tree
Showing 22 changed files with 243 additions and 200 deletions.
2 changes: 1 addition & 1 deletion examples/dart/clock/lib/keyboard_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion examples/dart/todo/lib/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
46 changes: 25 additions & 21 deletions rid-build/dart/_message_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,47 @@ 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);
}
}

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, '');
}

@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 &&
Expand All @@ -56,14 +56,18 @@ class RidMsg {
int get hashCode => type.hashCode ^ message.hashCode ^ details.hashCode;
}

class RidMsgChannel {
abstract class RidMessageChannel {
Stream<RidMessage> get stream;
}

class RidMessageChannelInternal implements RidMessageChannel {
final _zone = Zone.current;
final StreamController<RidMsg> _sink;
final StreamController<RidMessage> _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');
Expand All @@ -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);
Expand All @@ -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<RidMsg> get stream => _sink.stream;
Stream<RidMessage> get stream => _sink.stream;

int get nativePort {
return _receivePort.sendPort.nativePort;
Expand All @@ -111,7 +115,7 @@ class RidMsgChannel {
}

static bool _initialized = false;
static RidMsgChannel instance(
static RidMessageChannelInternal instance(
DynamicLibrary dl,
bool isDebugMode,
) {
Expand All @@ -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);
}
}
13 changes: 9 additions & 4 deletions rid-build/dart/_reply_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ abstract class IReply {

typedef Decode<TReply> = TReply Function(int packedBase, String? data);

abstract class RidReplyChannel<TReply extends IReply> {
Stream<TReply> get stream;
}

// TODO: error handling (could be part of Post data)
class ReplyChannel<TReply extends IReply> {
class RidReplyChannelInternal<TReply extends IReply>
implements RidReplyChannel<TReply> {
final _zone = Zone.current;
final StreamController<TReply> _sink;
final Decode<TReply> _decode;
Expand All @@ -22,7 +27,7 @@ class ReplyChannel<TReply extends IReply> {
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',
Expand Down Expand Up @@ -77,7 +82,7 @@ class ReplyChannel<TReply extends IReply> {
}

static bool _initialized = false;
static ReplyChannel<TReply> instance<TReply extends IReply>(
static RidReplyChannelInternal<TReply> instance<TReply extends IReply>(
DynamicLibrary dl,
Decode<TReply> decode,
bool isDebugMode,
Expand All @@ -87,6 +92,6 @@ class ReplyChannel<TReply extends IReply> {
"The reply channel can only be initialized once unless running in debug mode");
}
_initialized = true;
return ReplyChannel<TReply>._(dl, decode, isDebugMode);
return RidReplyChannelInternal<TReply>._(dl, decode, isDebugMode);
}
}
4 changes: 2 additions & 2 deletions rid-build/dart/_reply_channel_stub.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ReplyChannelStub {
class RidReplyChannelStub {
Future<void> dispose() => Future.value();
}

/// Stubbing reply channel until we declare a #[rid:reply] enum.
final replyChannel = ReplyChannelStub();
final _replyChannel = RidReplyChannelStub();
9 changes: 6 additions & 3 deletions rid-build/dart/_rid_rid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion rid-build/src/dart_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down
14 changes: 10 additions & 4 deletions rid-common/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Loading

0 comments on commit ce4dd17

Please sign in to comment.