-
Notifications
You must be signed in to change notification settings - Fork 285
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
feat: implements unlisten functionality #155
Conversation
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.
I think it'd be great if we could fix subsequent listen calls not receiving the result, in this PR too. Are you up for that? It effects the way unlisten is implemented, but it's odd if only the first listener is able to unlisten.
So, the client can unlisten any of its subscription
Also Fixes #154 |
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.
Great work! And don't forget to update the types 😉
https://github.com/porsager/postgres/blob/master/types/index.d.ts#L340
(I can help with types if you want by the way)
…for the bound topic
updated types |
@porsager do you have any feedback after updates? |
I'll take a look tonight. Would you review the types @Minigugus ? |
lib/index.js
Outdated
if (channel in listeners) { | ||
listeners[channel].push(fn) | ||
return Promise.resolve(channel) | ||
return Promise.resolve(listener.result) |
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.
This will result in any listener receiving an unlisten function that only works for the initial listener.
Here's one of the test cases modified to test this.
ot('multiple listeners and unlisten one', async() => {
const listener = postgres(options)
, xs = []
await listener.listen('test', x => xs.push('1', x))
const s2 = await listener.listen('test', x => xs.push('2', x))
await listener.notify('test', 'a')
await delay(50)
await s2.unlisten()
await listener.notify('test', 'b')
await delay(50)
listener.end();
return ['1a2a1b', xs.join('')]
})
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.
Ah, right... unlisten is different for every fn
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.
fixed
I changed return type of Update: that array is always empty, so very unlikely that anybody relied on it to be an array |
Ok, it's not a bad idea since it hide implementation details and simplify the public API :) It's also a breaking change, maybe you should also update the changelog 😉
Types looks good :) |
where can I find it? |
Ok I thought there was a changelog file, my bad x) Then there is nothing to, only @porsager is responsible for what is in the changelog on the release page. |
I think it's fine that the return type is simply With regards to the changelog, add it as part of the PR description and then I'll add it to releases. |
shall I change the return type? |
I don't think there's any use case for having the result included, so if you agree let's do it :) |
@porsager looks like we can't leave t('listen reconnects', async() => {
const listener = postgres(options)
, xs = []
const { state: { pid } } = await listener.listen('test', x => xs.push(x))
await sql.notify('test', 'a')
await sql`select pg_terminate_backend(${ pid }::int)`
await delay(50)
await sql.notify('test', 'b')
await delay(50)
listener.end()
return ['ab', xs.join('')]
}) |
I actually like that it returns |
Well i guess we're ready then 😊
… On 5 Mar 2021, at 17:54, Sergii Stotskyi ***@***.***> wrote:
I actually like that it returns a pid because I planned to implement similar set of tests on my side :)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
That was a little too soon though. The pid will change if the underlying socket reconnects. Let's tackle that in another PR |
Hmm... I think a good way to handle this is to turn |
Yeah, sounds good. |
@porsager how to test unequality or comparison? I'd like to add a test that checks unequality of initial pid and a pid which I get after terminating connection: t('listen result has state getter', async() => {
const listener = postgres(options)
, xs = []
const result = await listener.listen('test', x => xs.push(x))
const initialPid = result.state.pid
await sql.notify('test', 'a')
await sql`select pg_terminate_backend(${ initialPid }::int)`
await delay(50)
listener.end()
return [result.state.pid, initialPid] // <--- looks like there is no way to check unequality
}) Let's discuss this in #160 |
Relates to #133
BREAKING CHANGE:
listen
function returns an object instead of an empty array. All properties are preserved.