-
Notifications
You must be signed in to change notification settings - Fork 18
Change NotifyOnChangeOnly to accept a list #199
base: master
Are you sure you want to change the base?
Changes from all commits
00fc4f7
f40f258
a7b0ab6
64fbd99
78395a6
403eef9
ede7812
e569016
5ddd299
9d1c463
10ce484
ccab529
625dd00
b90d9bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -9,7 +9,7 @@ export interface ParserOptions { | |||||
| errorInterval: number | ||||||
| keepListen: boolean | ||||||
| metadataInterval: number | ||||||
| notifyOnChangeOnly: boolean | ||||||
| notifyOnChangeOnly: string[] | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| url: string | ||||||
| userAgent: string | ||||||
| } | ||||||
|
|
@@ -35,7 +35,7 @@ export class Parser extends EventEmitter { | |||||
| errorInterval: 10 * 60, | ||||||
| keepListen: false, | ||||||
| metadataInterval: 5, | ||||||
| notifyOnChangeOnly: false, | ||||||
| notifyOnChangeOnly: [], | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| url: '', | ||||||
| userAgent: 'icecast-parser', | ||||||
| }; | ||||||
|
|
@@ -115,12 +115,22 @@ export class Parser extends EventEmitter { | |||||
| } | ||||||
|
|
||||||
| protected isMetadataChanged (metadata: Map<string, string>): boolean { | ||||||
| for (const [key, value] of metadata.entries()) { | ||||||
| if (this.previousMetadata.get(key) !== value) { | ||||||
| return true; | ||||||
| if (this.options.notifyOnChangeOnly.length > 0) { | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
} |
||||||
| for (const key of this.options.notifyOnChangeOnly) { | ||||||
| const data = metadata.get(key) | ||||||
| if (data) { | ||||||
| if (this.previousMetadata.get(key) !== data) { | ||||||
| return true; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } else { | ||||||
| for (const [key, value] of metadata.entries()) { | ||||||
| if (this.previousMetadata.get(key) !== value) { | ||||||
| return true; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return false; | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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' }); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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(); | ||
| }); | ||
| })); | ||
|
|
||
| it('should properly emit metadata event when notifyOnChangeOnly is used and when metadata has been updated', async () => await new Promise((resolve) => { | ||
| expect.hasAssertions() | ||
|
|
||
| const radio = new Parser({ autoUpdate: false, notifyOnChangeOnly: ['StreamTitle'], 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(); | ||
| }); | ||
| })) | ||
|
|
||
| it('should not emmit metadata event if notifyOnChangeOnly is incorrect', async () => await new Promise(async (resolve) => { | ||
| let triggered = false | ||
| const delay = (ms: number) => new Promise(res => setTimeout(res, ms)); | ||
|
|
||
| const radio = new Parser({ autoUpdate: false, notifyOnChangeOnly: ['a'], url: 'https://live.hunter.fm/80s_high' }); | ||
| radio.on('metadata', () => { | ||
| triggered = true | ||
| }); | ||
|
|
||
| await delay(1000) | ||
|
|
||
| if(!triggered) { | ||
| resolve() | ||
| } else { | ||
| throw new Error('metadata event was triggered - Check that the notifyOnChangeOnly data does not exist in the metadata') | ||
| } | ||
| })) | ||
| }); | ||
There was a problem hiding this comment.
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
falseas before for backward compatibility.