From caa28a52f9e41fa86de2bf2eddf201147da6a651 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sat, 7 Dec 2019 11:39:39 +0200 Subject: [PATCH] Refactor: move CheckEndCallback to Iterator (cherry picked from commit Level/leveldown@d3453fbde4d2a8aa04d9091101c25c999649069b) --- binding.cc | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/binding.cc b/binding.cc index 31c17dd0..1dd81561 100644 --- a/binding.cc +++ b/binding.cc @@ -31,7 +31,6 @@ class NullLogger : public rocksdb::Logger { */ struct Database; struct Iterator; -struct EndWorker; static void iterator_end_do (napi_env env, Iterator* iterator, napi_value cb); /** @@ -573,6 +572,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; @@ -703,7 +711,7 @@ struct Iterator { bool ended_; leveldb::ReadOptions* options_; - EndWorker* endWorker_; + BaseWorker* endWorker_; private: napi_ref ref_; @@ -1449,30 +1457,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 () {} @@ -1513,8 +1507,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]); @@ -1526,8 +1519,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_; }; @@ -1548,8 +1539,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();