Repository navigation
Closed
Description
Related to #1592 / the next branch.
Now that we have rxjs added, we might want to use it for our subscription support.
Examples
Before
trpc/examples/next-prisma-starter-websockets/src/server/routers/post.ts
Lines 130 to 140 in ed62629
| .subscription('onAdd', { | |
| resolve() { | |
| return new Subscription<Post>((emit) => { | |
| const onAdd = (data: Post) => emit.data(data); | |
| ee.on('add', onAdd); | |
| return () => { | |
| ee.off('add', onAdd); | |
| }; | |
| }); | |
| }, | |
| }) |
After
.subscription('onAdd', {
resolve() {
return observable<Post>((emit) => {
const onAdd = (data: Post) => emit.data(data);
ee.on('add', onAdd);
return () => {
ee.off('add', onAdd);
};
});
},
})The underlying event emitter in the example could also be observables:
| interface MyEvents { | |
| add: (data: Post) => void; | |
| isTypingUpdate: () => void; | |
| } | |
| declare interface MyEventEmitter { | |
| on<U extends keyof MyEvents>(event: U, listener: MyEvents[U]): this; | |
| once<U extends keyof MyEvents>(event: U, listener: MyEvents[U]): this; | |
| emit<U extends keyof MyEvents>( | |
| event: U, | |
| ...args: Parameters<MyEvents[U]> | |
| ): boolean; | |
| } | |
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
| class MyEventEmitter extends EventEmitter {} | |
| const ee = new MyEventEmitter(); |
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
- StatusShow more project fields✅ Done
Activity
[-]Rewrite subscriptions with RxJS[/-][+]Rewrite subscriptions with new observables[/+]john-schmitz commentedon Mar 25, 2022
Just for context; why was the issue blocked?
I'm very new to the repository and trcps internals
sachinraja commentedon Mar 25, 2022
We were evaluating whether or not to use rxjs and decided not to (see discussion in #1592 for more details).
KATT commentedon Mar 26, 2022
Yeah, what @sachinraja said.
Additionally, this would include moving the observables from
@trpc/client/observablesto servergithub-actions commentedon Oct 4, 2022
This issue has been locked because it had no new activity for 14 days. If you are running into a similar issue, please create a new issue. Thank you.