Skip to content
This repository was archived by the owner on May 29, 2023. It is now read-only.

Commit d293b55

Browse files
committed
feat(tokens): add MIDI_INPUTS and MIDI_OUTPUTS
1 parent 8694815 commit d293b55

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

projects/midi/src/public-api.ts

+2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ export * from './pipes/frequency/frequency.pipe';
3030
/* Tokens */
3131
export * from './tokens/midi-access';
3232
export * from './tokens/midi-input';
33+
export * from './tokens/midi-inputs';
3334
export * from './tokens/midi-messages';
3435
export * from './tokens/midi-output';
36+
export * from './tokens/midi-outputs';
3537
export * from './tokens/midi-support';
3638
export * from './tokens/sysex';
3739

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import MIDIInput = WebMidi.MIDIInput;
2+
3+
import {InjectionToken} from '@angular/core';
4+
import {Observable} from 'rxjs';
5+
import {getPortsStream} from '../utils/get-ports-stream';
6+
7+
export const MIDI_INPUTS = new InjectionToken<Observable<MIDIInput[]>>(
8+
'Array of MIDI inputs',
9+
{
10+
factory: () => getPortsStream('inputs'),
11+
},
12+
);
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import MIDIOutput = WebMidi.MIDIOutput;
2+
3+
import {InjectionToken} from '@angular/core';
4+
import {Observable} from 'rxjs';
5+
import {getPortsStream} from '../utils/get-ports-stream';
6+
7+
export const MIDI_OUTPUTS = new InjectionToken<Observable<MIDIOutput[]>>(
8+
'Array of MIDI inputs',
9+
{
10+
factory: () => getPortsStream('outputs'),
11+
},
12+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import MIDIInput = WebMidi.MIDIInput;
2+
import MIDIOutput = WebMidi.MIDIOutput;
3+
4+
import {inject} from '@angular/core';
5+
import {MIDI_ACCESS} from '@ng-web-apis/midi';
6+
import {from, fromEvent, Observable, of} from 'rxjs';
7+
import {FromEventTarget} from 'rxjs/internal/observable/fromEvent';
8+
import {map, shareReplay, startWith, switchMap} from 'rxjs/operators';
9+
10+
export function getPortsStream(ports: 'inputs'): Observable<MIDIInput[]>;
11+
export function getPortsStream(ports: 'outputs'): Observable<MIDIOutput[]>;
12+
export function getPortsStream(
13+
ports: 'inputs' | 'outputs',
14+
): Observable<(MIDIOutput | MIDIInput)[]> {
15+
return from(inject(MIDI_ACCESS).catch(() => null)).pipe(
16+
switchMap(access =>
17+
access
18+
? fromEvent(access as FromEventTarget<any>, 'statechange').pipe(
19+
map(() => [...access[ports].values()]),
20+
startWith([...access[ports].values()]),
21+
)
22+
: of([]),
23+
),
24+
shareReplay(1),
25+
);
26+
}

0 commit comments

Comments
 (0)