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

Change NotifyOnChangeOnly to accept a list #199

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

Beastlybear2017
Copy link

I apologise if this is really dumb, I have never attempted to contribute towards a npm package before and have no clue how to / if it is possible to test it from a local file.

Here is my vision of how this would work, although have no clue if it works in practice :)

@Beastlybear2017 Beastlybear2017 changed the title Change NotifyOnUpdateOnly to accept a list Change NotifyOnChangeOnly to accept a list Nov 8, 2023
@ghaiklor
Copy link
Owner

ghaiklor commented Nov 9, 2023

@Beastlybear2017 you can try to write a test, where to check if your solutions works.

Here, for instance, test case that checks if the notifyChange works:

it('should properly emit metadata event when metadata has been updated', async () => await new Promise((resolve) => {
expect.hasAssertions();
const radio = new Parser({ autoUpdate: false, notifyOnChangeOnly: true, url: 'https://live.hunter.fm/80s_high' });
radio.on('metadata', (metadata) => {
// @ts-expect-error I want to check that metadata was stored in the private property to later comparsion
expect(radio.previousMetadata).toStrictEqual(metadata);
resolve();
});

You can add more cases like that where you check scenarios like “what if you provided no keys at all, that way it should notify every time” or “what if I provided the keys that doesn't exist in metadata, it should notify” and so on.

@Beastlybear2017
Copy link
Author

Beastlybear2017 commented Nov 9, 2023

I have added the tests (updated yours to use the new system), fixed any issues and commited my changes. All the tests pass / fail when they should
https://github.com/Beastlybear2017/icecast-parser/blob/ccab52975b0fdf5586ff5734e9a171cdcffe779a/test/Parser.spec.ts#L68-L95

image

Fixed previous issue - Now works as intened
@Beastlybear2017
Copy link
Author

Was able to test within my project and fixed an issue that I discorvered. Now works as intened

Updates info for notifyOnChangeOnly
@ghaiklor
Copy link
Owner

Great work!

However, let's keep it backward compatible with older API. Let's leave the possibility to specify the boolean as before.

@@ -47,7 +47,7 @@ You can provide additional parameters to constructor:
- `userAgent` - by default `icecast-parser`.
- `keepListen` - by default `false`. If you set to `true`, then response from radio station will not be destroyed and you can pipe it to another streams. E.g. piping it to the `speaker` module.
- `autoUpdate` - by default `true`. If you set to `false`, then parser will not be listening for recent updates and immediately close the stream. So that, you will get a metadata only once.
- `notifyOnChangeOnly` - by default `false`. If you set both `autoUpdate` and `notifyOnChangeOnly` to `true`, it will keep listening the stream and notifying you about metadata, but it will not notify if metadata did not change from the previous time.
- `notifyOnChangeOnly` - by default `[]`. If you set both `autoUpdate` to `true` and `notifyOnChangeOnly` to include the a list of metadata keys to listen for (`['StreamTitle', 'StreamUrl']`), it will keep listening the stream and notifying you about metadata, but it will not notify if the selected metadata did not change from the previous time.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, let it be false as before for backward compatibility.

@@ -9,7 +9,7 @@ export interface ParserOptions {
errorInterval: number
keepListen: boolean
metadataInterval: number
notifyOnChangeOnly: boolean
notifyOnChangeOnly: string[]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
notifyOnChangeOnly: string[]
notifyOnChangeOnly: boolean | string[]

@@ -35,7 +35,7 @@ export class Parser extends EventEmitter {
errorInterval: 10 * 60,
keepListen: false,
metadataInterval: 5,
notifyOnChangeOnly: false,
notifyOnChangeOnly: [],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
notifyOnChangeOnly: [],
notifyOnChangeOnly: false,

for (const [key, value] of metadata.entries()) {
if (this.previousMetadata.get(key) !== value) {
return true;
if (this.options.notifyOnChangeOnly.length > 0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, here we can check the type of the option. If there is an array, we treat it by your logic but if there is a boolean, nothing changes and works as before.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (Array.isArray(this.options.notifyOnChangeOnly)) {
  // your new logic
} else if (this.options.notifyOnChangeOnly === true) {
  // old logic
}

@@ -57,11 +57,40 @@ describe('parser', () => {
it('should properly emit metadata event when metadata has been updated', async () => await new Promise((resolve) => {
expect.hasAssertions();

const radio = new Parser({ autoUpdate: false, notifyOnChangeOnly: true, url: 'https://live.hunter.fm/80s_high' });
const radio = new Parser({ autoUpdate: false, notifyOnChangeOnly: [], url: 'https://live.hunter.fm/80s_high' });
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is no longer needed then and this test will check that everything works as before

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants