@@ -498,7 +498,47 @@ NAPI_METHOD(db_close) {
498
498
499
499
napi_value callback = argv[1 ];
500
500
CloseWorker* worker = new CloseWorker (env, database, callback);
501
- worker->Queue ();
501
+
502
+ if (database->iterators_ .empty ()) {
503
+ worker->Queue ();
504
+ NAPI_RETURN_UNDEFINED ();
505
+ }
506
+
507
+ // TODO fix me!
508
+
509
+ /*
510
+ // yikes, we still have iterators open! naughty naughty.
511
+ // we have to queue up a CloseWorker and manually close each of them.
512
+ // the CloseWorker will be invoked once they are all cleaned up
513
+ database->pendingCloseWorker = worker;
514
+
515
+ for (
516
+ std::map< uint32_t, leveldown::Iterator * >::iterator it
517
+ = database->iterators.begin()
518
+ ; it != database->iterators.end()
519
+ ; ++it) {
520
+
521
+ // for each iterator still open, first check if it's already in
522
+ // the process of ending (ended==true means an async End() is
523
+ // in progress), if not, then we call End() with an empty callback
524
+ // function and wait for it to hit ReleaseIterator() where our
525
+ // CloseWorker will be invoked
526
+
527
+ leveldown::Iterator *iterator = it->second;
528
+
529
+ if (!iterator->ended) {
530
+ v8::Local<v8::Function> end =
531
+ v8::Local<v8::Function>::Cast(iterator->handle()->Get(
532
+ Nan::New<v8::String>("end").ToLocalChecked()));
533
+ v8::Local<v8::Value> argv[] = {
534
+ Nan::New<v8::FunctionTemplate>(EmptyMethod)->GetFunction() // empty callback
535
+ };
536
+ Nan::AsyncResource ar("leveldown:iterator.end");
537
+ ar.runInAsyncScope(iterator->handle(), end, 1, argv);
538
+ }
539
+ }
540
+
541
+ */
502
542
503
543
NAPI_RETURN_UNDEFINED ();
504
544
}
0 commit comments