Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add destroy method #10

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions midi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export type MidiCallback = (deltaTime: number, message: MidiMessage) => void;
export class Input extends EventEmitter {
/** Close the midi port */
closePort(): void;
/** Close and dispose of the midi port */
destroy(): void;
/** Count the available input ports */
getPortCount(): number;
/** Get the name of a specified input port */
Expand Down Expand Up @@ -42,6 +44,8 @@ export class Input extends EventEmitter {
export class Output {
/** Close the midi port */
closePort(): void;
/** Close and dispose of the midi port */
destroy(): void;
/** Count the available output ports */
getPortCount(): number;
/** Get the name of a specified output port */
Expand Down
6 changes: 6 additions & 0 deletions midi.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Input extends EventEmitter {
closePort() {
return this.input.closePort()
}
destroy() {
return this.input.destroy()
}
getPortCount() {
return this.input.getPortCount()
}
Expand Down Expand Up @@ -47,6 +50,9 @@ class Output {
closePort() {
return this.output.closePort()
}
destroy() {
return this.output.destroy()
}
getPortCount() {
return this.output.getPortCount()
}
Expand Down
26 changes: 22 additions & 4 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ std::unique_ptr<Napi::FunctionReference> NodeMidiInput::Init(const Napi::Env &en
InstanceMethod<&NodeMidiInput::OpenPort>("openPort", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&NodeMidiInput::OpenVirtualPort>("openVirtualPort", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&NodeMidiInput::ClosePort>("closePort", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&NodeMidiInput::Destroy>("destroy", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&NodeMidiInput::IsPortOpen>("isPortOpen", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),

InstanceMethod<&NodeMidiInput::IgnoreTypes>("ignoreTypes", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
Expand Down Expand Up @@ -58,7 +59,8 @@ NodeMidiInput::NodeMidiInput(const Napi::CallbackInfo &info) : Napi::ObjectWrap<

NodeMidiInput::~NodeMidiInput()
{
cleanup();
closePortAndRemoveCallback();
handle.reset();
}

void NodeMidiInput::setupCallback(const Napi::Env &env)
Expand All @@ -76,14 +78,14 @@ void NodeMidiInput::setupCallback(const Napi::Env &env)
this,
[](Napi::Env, void *, NodeMidiInput *ctx) { // Finalizer used to clean threads up
// This TSFN can be destroyed when the worker_thread is destroyed, well before the NodeMidiInput is.
ctx->cleanup();
ctx->closePortAndRemoveCallback();
});

handle->setCallback(&NodeMidiInput::Callback, this);
}
}

void NodeMidiInput::cleanup()
void NodeMidiInput::closePortAndRemoveCallback()
{
if (handle != nullptr)
{
Expand Down Expand Up @@ -260,7 +262,23 @@ Napi::Value NodeMidiInput::ClosePort(const Napi::CallbackInfo &info)
return env.Null();
}

cleanup();
closePortAndRemoveCallback();
return env.Null();
}

Napi::Value NodeMidiInput::Destroy(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

if (!handle)
{
return env.Null();
}

closePortAndRemoveCallback();
handle.reset();

return env.Null();
}

Expand Down
3 changes: 2 additions & 1 deletion src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class NodeMidiInput : public Napi::ObjectWrap<NodeMidiInput>
bool configured = false;

void setupCallback(const Napi::Env &env);
void cleanup();
void closePortAndRemoveCallback();

public:
static std::unique_ptr<Napi::FunctionReference>
Expand All @@ -43,6 +43,7 @@ class NodeMidiInput : public Napi::ObjectWrap<NodeMidiInput>
Napi::Value OpenPort(const Napi::CallbackInfo &info);
Napi::Value OpenVirtualPort(const Napi::CallbackInfo &info);
Napi::Value ClosePort(const Napi::CallbackInfo &info);
Napi::Value Destroy(const Napi::CallbackInfo &info);
Napi::Value IsPortOpen(const Napi::CallbackInfo &info);

Napi::Value IgnoreTypes(const Napi::CallbackInfo &info);
Expand Down
18 changes: 18 additions & 0 deletions src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ std::unique_ptr<Napi::FunctionReference> NodeMidiOutput::Init(const Napi::Env &e
InstanceMethod<&NodeMidiOutput::OpenPort>("openPort", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&NodeMidiOutput::OpenVirtualPort>("openVirtualPort", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&NodeMidiOutput::ClosePort>("closePort", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&NodeMidiOutput::Destroy>("destroy", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&NodeMidiOutput::IsPortOpen>("isPortOpen", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),

InstanceMethod<&NodeMidiOutput::Send>("sendMessage", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
Expand Down Expand Up @@ -47,6 +48,7 @@ NodeMidiOutput::~NodeMidiOutput()
if (handle)
{
handle->closePort();
handle.reset();
}
}

Expand Down Expand Up @@ -175,6 +177,22 @@ Napi::Value NodeMidiOutput::ClosePort(const Napi::CallbackInfo &info)
return env.Null();
}

Napi::Value NodeMidiOutput::Destroy(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

if (!handle)
{
return env.Null();
}

handle->closePort();
handle.reset();

return env.Null();
}

Napi::Value NodeMidiOutput::IsPortOpen(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
Expand Down
1 change: 1 addition & 0 deletions src/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class NodeMidiOutput : public Napi::ObjectWrap<NodeMidiOutput>
Napi::Value OpenPort(const Napi::CallbackInfo &info);
Napi::Value OpenVirtualPort(const Napi::CallbackInfo &info);
Napi::Value ClosePort(const Napi::CallbackInfo &info);
Napi::Value Destroy(const Napi::CallbackInfo &info);
Napi::Value IsPortOpen(const Napi::CallbackInfo &info);

Napi::Value Send(const Napi::CallbackInfo &info);
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,10 @@ picomatch@^2.0.4, picomatch@^2.2.1:
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42"
integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==

pkg-prebuilds@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/pkg-prebuilds/-/pkg-prebuilds-0.1.0.tgz#7ac38674146708e763a4581a0759d5e8594f61b6"
integrity sha512-ALsGSiwO6EDvjrrFRiv7Q6HZPrqCgTpNxQMFs3P4Ic25cP94DmLy0iGvZDlJmQBbq2IS8xkZrifwkoOHIetY9Q==
pkg-prebuilds@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/pkg-prebuilds/-/pkg-prebuilds-0.2.1.tgz#4b91f410ab600df4eb657634d623549cc188c5ed"
integrity sha512-FdOlDiRqRL7i9aYzQflhGWCoiJf/8u6Qgzq48gKsRDYejtfjvGb1U5QGSzllcqpNg2a8Swx/9fMgtuVefwU+zw==
dependencies:
yargs "^17.5.1"

Expand Down