Skip to content

Commit 734c7a1

Browse files
committed
update C-API to the latest dev version
1 parent 1005c52 commit 734c7a1

13 files changed

+1364
-714
lines changed

objectbox/CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## latest
22

33
* Add `Query.findUnique()` to find a single object matching the query.
4-
* Add data `Browser`, see [docs](https://docs.objectbox.io/data-browser) for more details.
4+
* Add ObjectBox Admin, see [docs](https://docs.objectbox.io/data-browser) for more details.
55
* Fix generator when mixing backlinks and "standard" relations in the same entity (generated code had a syntax error).
66

77
## 1.1.1 (2021-07-09)

objectbox/lib/objectbox.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ library objectbox;
66

77
export 'src/annotations.dart';
88
export 'src/box.dart' show Box, PutMode;
9-
export 'src/browser.dart' show Browser;
9+
export 'src/admin.dart' show Admin;
1010
export 'src/common.dart';
1111
export 'src/query.dart'
1212
show

objectbox/lib/src/admin.dart

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export 'native/admin.dart' if (dart.library.html) 'web/admin.dart';

objectbox/lib/src/browser.dart

-1
This file was deleted.

objectbox/lib/src/native/admin.dart

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import 'dart:ffi';
2+
3+
import 'bindings/bindings.dart';
4+
import 'bindings/helpers.dart';
5+
import 'store.dart';
6+
7+
/// ObjectBox Admin allows you to explore the database in a regular web browser.
8+
///
9+
/// Admin runs directly on your device or on your development machine.
10+
/// Behind the scenes this works by bundling a simple HTTP server into ObjectBox
11+
/// when building your app. If triggered, it will then provide a basic web
12+
/// interface to the data and schema.
13+
class Admin {
14+
late Pointer<OBX_admin> _cAdmin;
15+
late final Pointer<OBX_dart_finalizer> _cFinalizer;
16+
17+
@pragma('vm:prefer-inline')
18+
Pointer<OBX_admin> get _ptr =>
19+
isClosed() ? throw StateError('Admin already closed') : _cAdmin;
20+
21+
/// Whether the loaded ObjectBox native library supports Admin.
22+
static bool isAvailable() => C.has_feature(OBXFeature.Admin);
23+
24+
/// Creates an ObjectBox Admin associated with the given store and options.
25+
Admin(Store store, {String bindUri = 'http://127.0.0.1:8090'}) {
26+
if (!isAvailable()) {
27+
throw UnsupportedError(
28+
'Admin is not available in the loaded ObjectBox runtime library.');
29+
}
30+
initializeDartAPI();
31+
32+
final opt = checkObxPtr(C.admin_opt());
33+
try {
34+
checkObx(C.admin_opt_store(opt, InternalStoreAccess.ptr(store)));
35+
checkObx(C.admin_opt_user_management(opt, false));
36+
withNativeString(bindUri,
37+
(Pointer<Int8> cStr) => checkObx(C.admin_opt_bind(opt, cStr)));
38+
} catch (_) {
39+
C.admin_opt_free(opt);
40+
rethrow;
41+
}
42+
43+
_cAdmin = C.admin(opt);
44+
45+
// Keep the finalizer so we can detach it when close() is called manually.
46+
_cFinalizer = C.dartc_attach_finalizer(
47+
this, native_admin_close, _cAdmin.cast(), 1024 * 1024);
48+
if (_cFinalizer == nullptr) {
49+
close();
50+
throwLatestNativeError();
51+
}
52+
}
53+
54+
/// Closes and cleans up all resources used by this Admin.
55+
void close() {
56+
if (!isClosed()) {
57+
final errors = List.filled(2, 0);
58+
if (_cFinalizer != nullptr) {
59+
errors[0] = C.dartc_detach_finalizer(_cFinalizer, this);
60+
}
61+
errors[1] = C.admin_close(_cAdmin);
62+
_cAdmin = nullptr;
63+
errors.forEach(checkObx);
64+
}
65+
}
66+
67+
/// Returns if the admin is already closed and can no longer be used.
68+
bool isClosed() => _cAdmin.address == 0;
69+
70+
/// Port the admin listens on. This is especially useful if the port was
71+
/// assigned automatically (a "0" port was used in the [bindUri]).
72+
late final int port = () {
73+
final result = C.admin_port(_ptr);
74+
reachabilityFence(this);
75+
if (result == 0) throwLatestNativeError();
76+
return result;
77+
}();
78+
}

objectbox/lib/src/native/bindings/bindings.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ late final native_query_close =
122122
_lib!.lookup<NativeFunction<_native_close>>('obx_query_close');
123123
late final native_query_prop_close =
124124
_lib!.lookup<NativeFunction<_native_close>>('obx_query_prop_close');
125-
late final native_browser_close =
126-
_lib!.lookup<NativeFunction<_native_close>>('obx_browser_close');
125+
late final native_admin_close =
126+
_lib!.lookup<NativeFunction<_native_close>>('obx_admin_close');
127127

128128
/// Keeps `this` alive until this call, preventing finalizers to run.
129129
/// Necessary for objects with a finalizer attached because the optimizer may

0 commit comments

Comments
 (0)