From 8ec67ad5b2e9e37eacc94d0abffca1999e08424b Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Mon, 21 Oct 2019 23:14:44 +0200 Subject: [PATCH] Refactor: move CheckEndCallback to Iterator --- binding.cc | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/binding.cc b/binding.cc index 26de429e..fb28e4e6 100644 --- a/binding.cc +++ b/binding.cc @@ -17,7 +17,6 @@ */ struct Database; struct Iterator; -struct EndWorker; static void iterator_end_do (napi_env env, Iterator* iterator, napi_value cb); /** @@ -575,6 +574,15 @@ struct Iterator { database_->ReleaseSnapshot(options_->snapshot); } + void CheckEndCallback () { + nexting_ = false; + + if (endWorker_ != NULL) { + endWorker_->Queue(); + endWorker_ = NULL; + } + } + bool GetIterator () { if (dbIterator_ != NULL) return false; @@ -727,7 +735,7 @@ struct Iterator { bool ended_; leveldb::ReadOptions* options_; - EndWorker* endWorker_; + BaseWorker* endWorker_; private: napi_ref ref_; @@ -1389,30 +1397,16 @@ NAPI_METHOD(iterator_end) { NAPI_RETURN_UNDEFINED(); } -/** - * TODO Move this to Iterator. There isn't any reason - * for this function being a separate function pointer. - */ -void CheckEndCallback (Iterator* iterator) { - iterator->nexting_ = false; - if (iterator->endWorker_ != NULL) { - iterator->endWorker_->Queue(); - iterator->endWorker_ = NULL; - } -} - /** * Worker class for nexting an iterator. */ struct NextWorker final : public BaseWorker { NextWorker (napi_env env, Iterator* iterator, - napi_value callback, - void (*localCallback)(Iterator*)) + napi_value callback) : BaseWorker(env, iterator->database_, callback, "leveldown.iterator.next"), - iterator_(iterator), - localCallback_(localCallback) {} + iterator_(iterator) {} ~NextWorker () {} @@ -1453,8 +1447,7 @@ struct NextWorker final : public BaseWorker { } // clean up & handle the next/end state - // TODO this should just do iterator_->CheckEndCallback(); - localCallback_(iterator_); + iterator_->CheckEndCallback(); napi_value argv[3]; napi_get_null(env_, &argv[0]); @@ -1466,8 +1459,6 @@ struct NextWorker final : public BaseWorker { } Iterator* iterator_; - // TODO why do we need a function pointer for this? - void (*localCallback_)(Iterator*); std::vector > result_; bool ok_; }; @@ -1488,8 +1479,7 @@ NAPI_METHOD(iterator_next) { NAPI_RETURN_UNDEFINED(); } - NextWorker* worker = new NextWorker(env, iterator, callback, - CheckEndCallback); + NextWorker* worker = new NextWorker(env, iterator, callback); iterator->nexting_ = true; worker->Queue();