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 (#690)
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers authored Dec 7, 2019
1 parent b9513aa commit d3453fb
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 @@ -17,7 +17,6 @@
*/
struct Database;
struct Iterator;
struct EndWorker;
static void iterator_end_do (napi_env env, Iterator* iterator, napi_value cb);

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

Expand Down Expand Up @@ -724,7 +732,7 @@ struct Iterator {
bool ended_;

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

private:
napi_ref ref_;
Expand Down Expand Up @@ -1385,30 +1393,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 @@ -1449,8 +1443,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 @@ -1462,8 +1455,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 @@ -1484,8 +1475,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 d3453fb

Please sign in to comment.