@@ -2,17 +2,16 @@ import 'dart:ffi';
22import 'signatures.dart' ;
33import "package:ffi/ffi.dart" show allocate, free;
44
5- typedef bool dataVisitorCallback (Pointer <Uint8 > dataPtr, int length);
6-
7- int visitorId = 0 ;
8- final callbacks = < int , dataVisitorCallback> {};
5+ int _lastId = 0 ;
6+ final _callbacks = < int , bool Function (Pointer <Uint8 > dataPtr, int length)> {};
97
8+ // called from C, forwards calls to the actual callback registered at the given ID
109int _forwarder (Pointer <Void > callbackId, Pointer <Uint8 > dataPtr, int size) {
11- if (callbackId == null ) {
12- throw Exception ("Data-visitor callback issued with NULL user_data" );
10+ if (callbackId == null || callbackId.address == 0 ) {
11+ throw Exception ("Data-visitor callback issued with NULL user_data (callback ID) " );
1312 }
1413
15- return callbacks [callbackId.cast <Int64 >().value](dataPtr, size) ? 1 : 0 ;
14+ return _callbacks [callbackId.cast <Int64 >().value](dataPtr, size) ? 1 : 0 ;
1615}
1716
1817/// A data visitor wrapper/forwarder to be used where obx_data_visitor is expected.
@@ -24,28 +23,28 @@ class DataVisitor {
2423
2524 Pointer <Void > get userData => _idPtr.cast <Void >();
2625
27- DataVisitor (dataVisitorCallback callback) {
26+ DataVisitor (bool Function ( Pointer < Uint8 > dataPtr, int length) callback) {
2827 // cycle through ids until we find an empty slot
29- visitorId ++ ;
30- var initialId = visitorId ;
31- while (callbacks .containsKey (visitorId )) {
32- visitorId ++ ;
28+ _lastId ++ ;
29+ var initialId = _lastId ;
30+ while (_callbacks .containsKey (_lastId )) {
31+ _lastId ++ ;
3332
34- if (initialId == visitorId ) {
33+ if (initialId == _lastId ) {
3534 throw Exception ("Data-visitor callbacks queue full - can't allocate another" );
3635 }
3736 }
3837 // register the visitor
39- _id = visitorId ;
40- callbacks [_id] = callback;
38+ _id = _lastId ;
39+ _callbacks [_id] = callback;
4140
4241 _idPtr = allocate <Int64 >();
4342 _idPtr.value = _id;
4443 }
4544
4645 void close () {
4746 // unregister the visitor
48- callbacks .remove (_id);
47+ _callbacks .remove (_id);
4948 free (_idPtr);
5049 }
5150}
0 commit comments