Skip to content

Commit

Permalink
feat: mock mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Tienisto committed Aug 15, 2024
1 parent 4beab7f commit a14791c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ fn generate_boilerplate(
);
}}
/// Initialize flutter_rust_bridge in mock mode.
/// No libraries for FFI are loaded.
static void initMock({{
required {api_class_name} api,
}}) {{
instance.initMock(
api: api,
);
}}
/// Dispose flutter_rust_bridge
///
/// The call to this function is optional, since flutter_rust_bridge (and everything else)
Expand Down
28 changes: 28 additions & 0 deletions frb_dart/lib/src/main_components/entrypoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ abstract class BaseEntrypoint<A extends BaseApi, AI extends BaseApiImpl,
await executeRustInitializers();
}

/// {@macro flutter_rust_bridge.only_for_generated_code}
@protected
void initMockImpl({
required A api,
}) {
if (__state != null) {
throw StateError('Should not initialize flutter_rust_bridge twice');
}

__state = _FakeEntrypointState(api: api);
}

/// {@macro flutter_rust_bridge.only_for_generated_code}
@protected
void disposeImpl() {
Expand Down Expand Up @@ -162,6 +174,22 @@ class _EntrypointState<A extends BaseApi> {
}
}

class _FakeEntrypointState<A extends BaseApi> implements _EntrypointState<A> {
final A api;

_FakeEntrypointState({
required this.api,
});

@override
void dispose() {}

@override
dynamic noSuchMethod(Invocation invocation) {
throw UnimplementedError('');
}
}

void _setUpRustToDartCommunication(
GeneralizedFrbRustBinding generalizedFrbRustBinding) {
generalizedFrbRustBinding.storeDartPostCObject();
Expand Down

0 comments on commit a14791c

Please sign in to comment.