Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Commit

Permalink
Refactor: move CheckEndCallback to Iterator
Browse files Browse the repository at this point in the history
(cherry picked from commit Level/leveldown@d3453fb)
  • Loading branch information
vweevers committed Nov 27, 2021
1 parent 54fc5df commit caa28a5
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -703,7 +711,7 @@ struct Iterator {
bool ended_;

leveldb::ReadOptions* options_;
EndWorker* endWorker_;
BaseWorker* endWorker_;

private:
napi_ref ref_;
Expand Down Expand Up @@ -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 () {}

Expand Down Expand Up @@ -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]);
Expand All @@ -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<std::pair<std::string, std::string> > result_;
bool ok_;
};
Expand All @@ -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();

Expand Down

0 comments on commit caa28a5

Please sign in to comment.