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

emit not working more than once #40

Closed
BearToCode opened this issue Apr 29, 2024 · 2 comments
Closed

emit not working more than once #40

BearToCode opened this issue Apr 29, 2024 · 2 comments

Comments

@BearToCode
Copy link

BearToCode commented Apr 29, 2024

I've just copied the example:

export function POST({ request }) {
  return events({
    request,
    async start({emit}) {
      while(true){
        emit('message', 'hello world')
        await delay(1000)
      }
    },
  })
}

And did the following on the client:

<script>
  const value = source('/sse/chat').select('message');

  value.subscribe((message) => {
    console.log(message);
  });
</script>

But 'hello world' is being logged only once! example

@razshare
Copy link
Owner

razshare commented Apr 29, 2024

Hello @BearToCode ,

The emit function is working correctly in this case.

The behavior you're experiencing is the default behavior of svelte's stores.
A svelte store does not forward duplicate values, it checks for equality.
If value N+1 is equal to value N, then the value of the store won't change, thus your subscribe function won't trigger.

That's just how svelte stores work.
See reference here.

This library does provide a solution.
What you're looking for is transform()

<script>
  const value = source('/sse/chat').select('message').transform((message) => {
    console.log(message);
    return message
  });
</script>

Make sure you update to the latest 0.12.1 version to get this feature working properly.


I agree the readme is a bit confusing since it's emitting a static string, so I've updated it to emit a variable string.

Let me know if this addresses your issue.

@BearToCode
Copy link
Author

My bad, now it's working. Grazie mille :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants