Skip to content

Commit 96c352c

Browse files
committed
update C-API to the latest dev version
1 parent 6b189aa commit 96c352c

13 files changed

+1366
-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

66
## 1.1.1 (2021-07-09)
77

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

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 => (_cAdmin.address != 0)
19+
? _cAdmin
20+
: throw StateError('Admin already closed');
21+
22+
/// Whether the loaded ObjectBox native library supports Admin.
23+
static bool isAvailable() => C.has_feature(OBXFeature.Admin);
24+
25+
/// Creates a sync client associated with the given store and options.
26+
/// This does not initiate any connection attempts yet: call start() to do so.
27+
Admin(Store store, {String bindUri = 'http://127.0.0.1:8090'}) {
28+
if (!isAvailable()) {
29+
throw UnsupportedError(
30+
'Admin is not available in the loaded ObjectBox runtime library.');
31+
}
32+
initializeDartAPI();
33+
34+
final opt = checkObxPtr(C.admin_opt());
35+
try {
36+
checkObx(C.admin_opt_store(opt, InternalStoreAccess.ptr(store)));
37+
checkObx(C.admin_opt_user_management(opt, false));
38+
withNativeString(bindUri,
39+
(Pointer<Int8> cStr) => checkObx(C.admin_opt_bind(opt, cStr)));
40+
} catch (_) {
41+
C.admin_opt_free(opt);
42+
rethrow;
43+
}
44+
45+
_cAdmin = C.admin(opt);
46+
47+
// Keep the finalizer so we can detach it when close() is called manually.
48+
_cFinalizer = C.dartc_attach_finalizer(
49+
this, native_admin_close, _cAdmin.cast(), 1024 * 1024);
50+
if (_cFinalizer == nullptr) {
51+
close();
52+
throwLatestNativeError();
53+
}
54+
}
55+
56+
/// Closes and cleans up all resources used by this Admin.
57+
void close() {
58+
if (_cAdmin.address != 0) {
59+
final errors = List.filled(2, 0);
60+
if (_cFinalizer != nullptr) {
61+
errors[0] = C.dartc_detach_finalizer(_cFinalizer, this);
62+
}
63+
errors[1] = C.admin_close(_cAdmin);
64+
_cAdmin = nullptr;
65+
errors.forEach(checkObx);
66+
}
67+
}
68+
69+
/// Returns if the admin is already closed and can no longer be used.
70+
bool isClosed() => _cAdmin.address == 0;
71+
72+
/// Port the admin listens on. This is especially useful if the port was
73+
/// assigned automatically (a "0" port was used in the [bindUri]).
74+
late final int port = () {
75+
final result = C.admin_port(_ptr);
76+
reachabilityFence(this);
77+
if (result == 0) throwLatestNativeError();
78+
return result;
79+
}();
80+
}

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)