tauri-plugin-graphql icon indicating copy to clipboard operation
tauri-plugin-graphql copied to clipboard

Unsubscribe is not implemented

Open mbracher opened this issue 1 year ago • 0 comments

When creating an endless stream it does not get closed when (svelte) Client is unsubscribing (urql pause) or when the page is reloaded. Example of endless stream:

#[Subscription]
impl Subscription {
  async fn hello_world(&self) -> impl Stream<Item = String> {
    info!("rs2js hello_world subscribe");

      let mut value = 0;
      tokio_stream::wrappers::IntervalStream::new(tokio::time::interval(Duration::from_secs(1)))
          .map(move |_| {
              info!(?value, "rs2js hello_world produce");
              value += 1;
              format!("nr: {}", value)
          })

  }
}
<script lang="ts" >
  import { debugExchange, createClient, setContextClient, queryStore, gql, subscriptionExchange, subscriptionStore} from '@urql/svelte';
  import { invokeExchange, subscribe } from "tauri-plugin-graphql-urql";

  const gql_client = createClient({
    url: 'graphql',
    exchanges: [
      invokeExchange, 
      subscriptionExchange({
        forwardSubscription: (operation) => ({
          subscribe: (sink) => ({
            unsubscribe: subscribe(operation, sink),
          }),
        }),
      }),
    ]
  });

  const messages = subscriptionStore({
    client: gql_client,
    query: gql`
      subscription MessageSub {
        helloWorld 
      }`
  });

  function startSubscription() {
    messages.resume();
  }
  function stopSubscription() {
    messages.pause();
  }
</script>

{#if !$messages}
  <p>No new messages</p>
{:else}
  {JSON.stringify($messages.data)}
{/if}
<button on:click="{stopSubscription}">stop</button>
<button on:click="{startSubscription}">start</button>

mbracher avatar Feb 19 '23 17:02 mbracher