Skip to content

Commit

Permalink
release: 0.1.5 - adding once method that allows you to listen to a sp…
Browse files Browse the repository at this point in the history
…ecific event once
  • Loading branch information
kulak-at committed Feb 2, 2024
1 parent 26177e5 commit 11fe56e
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## [0.1.5]
- Adding `.once` method that allows to listen to an event once (as a callback or a promise)

## [0.1.4] 2024-01-29
- Upgrading `Omnibus` dependency to 0.1.2
- `.on` callbacks can be now unsubscribed with Explicit Resource Management (`using` keyword)
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@midival/core",
"version": "0.1.4",
"version": "0.1.5",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "MIT",
Expand All @@ -9,8 +9,8 @@
"./mpe": "./dist/mpe.js"
},
"devDependencies": {
"@types/jest": "^29.5.11",
"@types/node": "^20.11.10",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.16",
"@types/webmidi": "^2.0.10",
"gh-pages": "^6.1.1",
"jest": "^29.7.0",
Expand All @@ -29,6 +29,6 @@
"deploy": "gh-pages -d docs"
},
"dependencies": {
"@hypersphere/omnibus": "^0.1.2"
"@hypersphere/omnibus": "^0.1.4"
}
}
88 changes: 88 additions & 0 deletions src/MIDIValInput.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,92 @@ describe("MIDIValInput", () => {
expect(callback).toBeCalledTimes(2);
expect(callback).toHaveBeenLastCalledWith(false, msg2);
});

it(".noteOn - once", async () => {
const { input, mock } = await createInput({
id: "1",
name: "Input",
manufacturer: "MIDIVal",
});
const allCallback = jest.fn();

input.once('noteOn', allCallback)

mock.sendMessage(
makeMessage({
channel: 1,
command: MidiCommand.NoteOn,
data1: 65,
data2: 128,
})
);

expect(allCallback).toHaveBeenCalledTimes(1);

expect(allCallback.mock.calls[0][0]).toEqual({
command: MidiCommand.NoteOn,
channel: 1,
data1: 65,
data2: 128,
note: 65,
velocity: 128,
});

mock.sendMessage(
makeMessage({
channel: 1,
command: MidiCommand.NoteOn,
data1: 65,
data2: 128,
})
);
expect(allCallback).toHaveBeenCalledTimes(1);
});

it(".noteOn - once as Promise", async () => {
const { input, mock } = await createInput({
id: "1",
name: "Input",
manufacturer: "MIDIVal",
});
const allCallback = jest.fn();

const promise = input.once('noteOn')
promise.then(allCallback)
expect(allCallback).toHaveBeenCalledTimes(0);


mock.sendMessage(
makeMessage({
channel: 1,
command: MidiCommand.NoteOn,
data1: 65,
data2: 128,
})
);

await promise

expect(allCallback).toHaveBeenCalledTimes(1);

expect(allCallback.mock.calls[0][0]).toEqual({
command: MidiCommand.NoteOn,
channel: 1,
data1: 65,
data2: 128,
note: 65,
velocity: 128,
});

mock.sendMessage(
makeMessage({
channel: 1,
command: MidiCommand.NoteOn,
data1: 65,
data2: 128,
})
);
expect(allCallback).toHaveBeenCalledTimes(1);
});

});
19 changes: 19 additions & 0 deletions src/MIDIValInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import {
} from "./utils/midiRegisteredParameters";
import { OmnibusKeys, OmnibusParams, OmnibusValue } from "./types/omnibus";

type OmnibusKeysCheck<B, T> = T extends OmnibusKeys<B> ? T : never
export type ReverseParameters<T> = T extends [infer K] ? K : T

export interface PitchBendMessage {
channel: number;
value: number;
Expand Down Expand Up @@ -146,6 +149,22 @@ export class MIDIValInput {
return ticksToBPM(this.tempoSamples);
}

once<const B extends typeof this.omnibus, const T extends OmnibusKeys<B>>(event: T): Promise<B extends Omnibus<infer X> ? T extends keyof X ? ReverseParameters<X[T]> : never : never>;
once<const B extends typeof this.omnibus, const T extends OmnibusKeys<B>, const Cb extends CallbackType<OmnibusParams<B, OmnibusKeysCheck<B, T>>>>(event: T, cb: Cb): UnregisterCallback;

/**
*
* @param key event you want to listen to
* @param cb Optional callback to be triggered. If not provided, Promise with the result is returned instead
* @returns UnregisterCallback if callback provided. Promise resolving to the event value otherwise.
*/
once<
const B extends typeof this.omnibus, const T extends OmnibusKeys<B>,
Cb extends undefined | CallbackType<OmnibusParams<B, OmnibusKeysCheck<B, T>>>
>(key: T, cb?: Cb): any {
return this.omnibus.once(key as any, cb)
}

private async registerInput(input: IMIDIInput): Promise<void> {
this.midiInput = input;
this.unregisterInput = await input.onMessage(
Expand Down
24 changes: 12 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,10 @@
dependencies:
"@hypersphere/omnibus" "^0.1.0"

"@hypersphere/omnibus@^0.1.2":
version "0.1.2"
resolved "https://registry.yarnpkg.com/@hypersphere/omnibus/-/omnibus-0.1.2.tgz#2809bce9ed9a0c2aa7d99f6c82c1f7a1cc6e0be0"
integrity sha512-6OV+snxy+w9XnJ+dIis6Nn/4GAIzthQWV1z2m4XMpEFA9G8ZBcPrebX0bpy0KOP82EmaLQ5pouG5UFevY2tnAg==
"@hypersphere/omnibus@^0.1.4":
version "0.1.4"
resolved "https://registry.yarnpkg.com/@hypersphere/omnibus/-/omnibus-0.1.4.tgz#a52b7b8889e28cae8e9c993a864d474a7d753aed"
integrity sha512-gZXWpfZshbnQgQhJl2hx3rGRk8w9USotjVRcyTDYbPJmCYRF80lENuZ5/5QQsuYzVlRlxxCXzX0rpFOjjqI8Jg==
dependencies:
"@hypersphere/omnibus" "^0.1.1"

Expand Down Expand Up @@ -910,10 +910,10 @@
dependencies:
"@types/istanbul-lib-report" "*"

"@types/jest@^29.5.11":
version "29.5.11"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-29.5.11.tgz#0c13aa0da7d0929f078ab080ae5d4ced80fa2f2c"
integrity sha512-S2mHmYIVe13vrm6q4kN6fLYYAka15ALQki/vgDC3mIukEOx8WJlv0kQPM+d4w8Gp6u0uSdKND04IlTXBv0rwnQ==
"@types/jest@^29.5.12":
version "29.5.12"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-29.5.12.tgz#7f7dc6eb4cf246d2474ed78744b05d06ce025544"
integrity sha512-eDC8bTvT/QhYdxJAulQikueigY5AsdBRH2yDKW3yveW7svY3+DzN84/2NUgkw10RTiJbWqZrTtoGVdYlvFJdLw==
dependencies:
expect "^29.0.0"
pretty-format "^29.0.0"
Expand All @@ -932,10 +932,10 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-15.14.0.tgz#74dbf254fb375551a9d2a71faf6b9dbc2178dc53"
integrity sha512-um/+/ip3QZmwLfIkWZSNtQIJNVAqrJ92OkLMeuZrjZMTAJniI7fh8N8OICyDhAJ2mzgk/fmYFo72jRr5HyZ1EQ==

"@types/node@^20.11.10":
version "20.11.10"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.11.10.tgz#6c3de8974d65c362f82ee29db6b5adf4205462f9"
integrity sha512-rZEfe/hJSGYmdfX9tvcPMYeYPW2sNl50nsw4jZmRcaG0HIAb0WYEpsB05GOb53vjqpyE9GUhlDQ4jLSoB5q9kg==
"@types/node@^20.11.16":
version "20.11.16"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.11.16.tgz#4411f79411514eb8e2926f036c86c9f0e4ec6708"
integrity sha512-gKb0enTmRCzXSSUJDq6/sPcqrfCv2mkkG6Jt/clpn5eiCbKTY+SgZUxo+p8ZKMof5dCp9vHQUAB7wOUTod22wQ==
dependencies:
undici-types "~5.26.4"

Expand Down

0 comments on commit 11fe56e

Please sign in to comment.