• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

melchor629 / node-flac-bindings / 14597664676

22 Apr 2025 02:43PM UTC coverage: 91.38% (-0.02%) from 91.398%
14597664676

push

github

melchor629
chore: update deps

159 of 202 branches covered (78.71%)

Branch coverage included in aggregate %.

5025 of 5471 relevant lines covered (91.85%)

37329.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.88
/src/mappings/native_async_iterator.hpp
1
#pragma once
2

3
#include "../utils/async.hpp"
4
#include "../utils/converters.hpp"
5
#include <optional>
6

7
namespace flac_bindings {
8

9
  using namespace Napi;
10

11
  template<typename T>
12
  class NativeAsyncIterator: public ObjectWrap<NativeAsyncIterator<T>> {
13
  public:
14
    typedef std::optional<T> IterationReturnValue;
15
    typedef AsyncBackgroundTask<IterationReturnValue> AsyncTask;
16
    typedef std::function<IterationReturnValue(typename AsyncTask::ExecutionProgress&, uint64_t)>
17
      IteratorFunction;
18
    typedef std::function<Napi::Value(const Napi::Env&, T)> ToJsFunction;
19

20
    static Napi::Value newIterator(
1✔
21
      const Napi::Env& env,
22
      const char* name,
23
      const IteratorFunction& impl,
24
      const ToJsFunction& map) {
25
      EscapableHandleScope scope(env);
1✔
26

27
      Function constructor = NativeAsyncIterator::DefineClass(
1✔
28
        env,
29
        "NativeAsyncIterator",
30
        {
31
          NativeAsyncIterator::InstanceMethod("next", &NativeAsyncIterator::next),
1✔
32
        });
33

34
      auto obj = constructor.New({
3✔
35
        String::New(env, name),
1✔
36
        External<IteratorFunction>::New(env, new IteratorFunction(impl)),
1✔
37
        External<ToJsFunction>::New(env, new ToJsFunction(map)),
1✔
38
      });
39
      return scope.Escape(obj);
2✔
40
    }
1✔
41

42
    NativeAsyncIterator(const CallbackInfo& info): ObjectWrap<NativeAsyncIterator<T>>(info) {
1✔
43
      name = info[0].As<String>().Utf8Value();
1✔
44
      impl = info[1].As<External<IteratorFunction>>().Data();
1✔
45
      map = info[2].As<External<ToJsFunction>>().Data();
1✔
46
    }
1✔
47

48
    virtual ~NativeAsyncIterator() {
×
49
      delete impl;
×
50
      delete map;
×
51
    }
×
52

53
    Napi::Value next(const CallbackInfo& info) {
5✔
54
      auto worker = new AsyncBackgroundTask<IterationReturnValue>(
20✔
55
        info.Env(),
10✔
56
        [this](auto c) { c.resolve((*impl)(c, pos)); },
15✔
57
        nullptr,
58
        name.c_str(),
59
        [this](auto env, auto value) {
5✔
60
          auto returnObject = Object::New(env);
5✔
61

62
          if (value.has_value()) {
5✔
63
            returnObject["done"] = booleanToJs(env, false);
4✔
64
            returnObject["value"] = (*map)(env, value.value());
4✔
65
            pos += 1;
4✔
66
          } else {
67
            returnObject["done"] = booleanToJs(env, true);
1✔
68
          }
69

70
          return returnObject;
5✔
71
        });
72

73
      worker->Queue();
5✔
74
      return worker->getPromise();
5✔
75
    }
76

77
  private:
78
    std::string name;
79
    IteratorFunction* impl;
80
    ToJsFunction* map;
81
    uint64_t pos = 0;
82
  };
83

84
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc