Closed moheng233 closed 2 months ago
I did think a lot about it and I came to the conclusion that a good DX would be radically different than Telefunc's current DX. So I'm thinking it's best to implement it as a separate library.
Thanks for reaching out!
For ongoing events, or large batches of data that take time to load in batches, there is a need to proactively push the data from the server to the client side.
I have initially implemented this feature at On my branch.
He uses it to look like this
```ts // hello.telefunc.ts // Environment: server import { SSEMessageEmit } from 'telefunc' export { hello } async function hello() { console.log('test') const ev = new SSEMessageEmit()
let count = 0
const interval = setInterval(async () => {
try {
const data = `hello world ${count}`
await ev.emit(data, { id: count.toString() })
console.log(data)
count += 1
if (count > 20) {
clearInterval(interval)
ev.close()
}
} catch (error) {
console.error(error)
clearInterval(interval)
ev.close()
}
}, 1000)
return ev.readable
}
```
```ts
// index.ts
// Environment: client
import { hello } from './hello.telefunc'
const gen = await hello()
const writable = gen.pipeTo(
new WritableStream({
abort(reason) {
const element = document.createElement('div')
element.textContent = `aborted: ${reason}`
document.querySelector('#view')?.appendChild(element)
},
close() {
const element = document.createElement('div')
element.textContent = 'closed'
document.querySelector('#view')?.appendChild(element)
},
start() {
const element = document.createElement('div')
element.textContent = 'started'
document.querySelector('#view')?.appendChild(element)
},
write(chunk) {
const element = document.createElement('div')
element.textContent = chunk.data
document.querySelector('#view')?.appendChild(element)
},
}),
)
```
It currently lacks detailed testing and simple documentation