djipco / webmidi

Tame the Web MIDI API. Send and receive MIDI messages with ease. Control instruments with user-friendly functions (playNote, sendPitchBend, etc.). React to MIDI input with simple event listeners (noteon, pitchbend, controlchange, etc.).
Apache License 2.0
1.52k stars 115 forks source link

Listener class is not being exported #301

Closed master30f closed 1 year ago

master30f commented 1 year ago

Description The Listener class (and maybe some others) are not being exposed to the user, so it's impossible to use them in type definitions. Example:

import WebMidi from "webmidi"

class MidiManager {
    private activeListeners: WebMidi.Listener[] // Not possible -- Listener class is not exported

    ...
}

Environment:

Details None

djipco commented 1 year ago

The EventEmitter and Listener classes are actually part of the djipevents package which is separate from WEBMIDI.js. In such a case, I'm not sure what the proper way to do things is. To be honest, I'm not very familiar with TypeScript. Ideas?

master30f commented 1 year ago

I found that adding export before the class declaration in webmidi.cjs.d.ts like so:

export declare class Listener {
    ...
}

works as a very hackish work-around, but I'm not familiar with your build system (or any build systems in general as I've never made a ts library before) so I have no idea how to actually fix it.

Edit: I found out that in your case the declaration file is not auto generated, so applying the above fix to webmidi.d.ts might work.

djipco commented 1 year ago

As you noted, the Listener and EventEmitter classes are already declared in webmidi.d.ts but they are not exported. It was done this way because these two classes are actually part of a separate package called djipevents. If there is no harm in exporting them from webmidi.d.ts, I could easily do it. However, not being too familiar with TypeScript, I'm not sure of the implications. Could it cause unforeseen issues?

master30f commented 1 year ago

As far as I know it should not cause any harm, as the declarations in webmidi.d.ts are just typings -- they only tell the typescript compiler how the data structures "look" and will not collide with / override the javascript definitions.

djipco commented 1 year ago

This has been pushed in release v3.0.22. Thanks for suggesting this enhancement.