b0o
b0o7mo ago

Use RxJS Observable for subscription procedure

On the server, I have an RxJS Observable that I'd like to use for one of my subscription procedures. Is this possible without writing a converter function that takes an RxJS Observable and converts it to a tRPC Observable? For example, what I'd like to do looks something like this:
import { Observable } from 'rxjs'
import { authedProcedure, createTRPCRouter } from '../../api/trpc'

export const demoRouter = createTRPCRouter({
fizzBuzz: authedProcedure.subscription(() =>
new Observable<string>((subscriber) => {
let i = 1
const interval = setInterval(() => {
if (i % 15 === 0) {
subscriber.next('FizzBuzz')
} else if (i % 3 === 0) {
subscriber.next('Fizz')
} else if (i % 5 === 0) {
subscriber.next('Buzz')
} else {
subscriber.next(String(i))
}
i++
}, 1000)
return () => clearInterval(interval)
})
),
})
import { Observable } from 'rxjs'
import { authedProcedure, createTRPCRouter } from '../../api/trpc'

export const demoRouter = createTRPCRouter({
fizzBuzz: authedProcedure.subscription(() =>
new Observable<string>((subscriber) => {
let i = 1
const interval = setInterval(() => {
if (i % 15 === 0) {
subscriber.next('FizzBuzz')
} else if (i % 3 === 0) {
subscriber.next('Fizz')
} else if (i % 5 === 0) {
subscriber.next('Buzz')
} else {
subscriber.next(String(i))
}
i++
}, 1000)
return () => clearInterval(interval)
})
),
})
0 Replies
No replies yetBe the first to reply to this messageJoin