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

Convert MIDIManager to global singleton #200

Open
orchetect opened this issue Dec 27, 2023 · 0 comments
Open

Convert MIDIManager to global singleton #200

orchetect opened this issue Dec 27, 2023 · 0 comments
Labels
🎩 refinement An enhancement to an existing feature

Comments

@orchetect
Copy link
Owner

orchetect commented Dec 27, 2023

Proposal

There are numerous arguments in favor of making MIDIManager an implicit global singleton rather than something that has to be instanced.

The core benefits:

  • manager does not need to be stored in a variable by the consumer
  • manager is available globally and does not have to be injected into methods that require a reference to it
  • manager instance would no longer need to be propagated through SwiftUI environment

The conceivable use cases of creating more than one manager instance is almost zero. The only area where it becomes useful is in unit testing, where it could be possible to retain the ability to instance multiple managers internally. (For example, tests involving sending events to/from legacy Core MIDI API to a receiver using new API which requires two managers.)

Current Implementation

// manager instance which must be stored in a variable, typically at app-leveo scope
let midiManager = MIDIManager(
    clientName: "TestAppMIDIManager",
    model: "TestApp",
    manufacturer: "MyCompany"
)

try MIDIManager.start()

Suggested Implementation

// MIDIManager.shared will be a global static singleton instance
// the consumer must still set up the required parameters first before starting the manager,
// but instead of passing them as parameters to an initializer, a method on the singleton may be called instead:
MIDIManager.shared.setup(
    clientName: "TestAppMIDIManager",
    model: "TestApp",
    manufacturer: "MyCompany"
)

try MIDIManager.shared.start()

Or the two could be combined:

try MIDIManager.shared.start(
    clientName: "TestAppMIDIManager",
    model: "TestApp",
    manufacturer: "MyCompany"
)

Considerations

  • Currently there are two manager classes: MIDIManager and ObservableMIDIManager. These may need to be merged or the former deprecated.
  • It would need to be investigated how/if ObservableMIDIManager can be used as an observable object in SwiftUI views while it is a global singleton, without it being installed in an App or View struct.
@orchetect orchetect added the 🎩 refinement An enhancement to an existing feature label Dec 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎩 refinement An enhancement to an existing feature
Projects
None yet
Development

No branches or pull requests

1 participant