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

Refactor: move CheckEndCallback to Iterator #690

Merged
merged 1 commit into from
Dec 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -727,7 +735,7 @@ struct Iterator {
bool ended_;

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

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

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

Expand Down