Skip to content
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

Idea: Generators #135

Open
stephen-dahl opened this issue Jan 17, 2025 · 4 comments
Open

Idea: Generators #135

stephen-dahl opened this issue Jan 17, 2025 · 4 comments
Labels
enhancement ✨ New feature or request

Comments

@stephen-dahl
Copy link

stephen-dahl commented Jan 17, 2025

It would be awesome if when we returned a generator from a telefunc file it would wrap it in Server Sent Events

page.telefunc.ts

export async function* subscribe() {
  yield 1;
  yield 2;
  yield 3;
}

page.tsx

...
for await (const value of subscribe('test')) {
  console.log(value);
}
...
@brillout
Copy link
Owner

I wrote down my concerns about this here.

While I agree it would be nice to support real-time use cases, it's out-of-scope for now.

That said, feel free to elaborate what's the use case you have in mind for wanting generators.

Closing in favor #36.

@stephen-dahl
Copy link
Author

I dont see the connection with #36.
This is more like a one way web socket or streaming response.
There are many use cases from sending large amounts of data without loading it all into memory to watching for server events.

for example to send back all the rows in a database table without loading the full table into the servers memory.

export async function* bigQuery() {
  const cursor = sql`select * from bigTable`.cursor()
  
  for await (const [row] of cursor) {
    yield row;
  }
}
for await (const row of bigQuery()) {
  // do something with the row
}
console.log('all rows loaded')

I saw elysia doing this and though it was a briliant way to handle streaming responses.
https://elysiajs.com/essential/handler.html#stream

side note, I really like the framework agnostic way you integrate with servers. made hooking up to elysia easy.

@stephen-dahl
Copy link
Author

an alternative to this might be returning a stream from a normal function, but I like the generator interface better.

@brillout brillout added the enhancement ✨ New feature or request label Jan 22, 2025
@brillout
Copy link
Owner

I see, make sense and I like it as well.

@brillout brillout reopened this Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement ✨ New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants