Skip to content

Commit

Permalink
feat(useSurrealWS): handle response destr
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 committed Jun 2, 2024
1 parent d0e1d09 commit cbaab1f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 4 additions & 4 deletions playground/pages/ws-examples.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
Fetch examples
</NuxtLink>
</div>
<div>
{{ _data }}
</div>
<pre>
{{ data }}
</pre>
<button @click="userInfo">
Get user info
</button>
</div>
</template>

<script setup lang="ts">
const { _data, rpc } = useSurrealWS('staging')
const { data, rpc } = useSurrealWS('staging')
rpc({
method: 'live',
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/composables/surreal-ws.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useWebSocket } from '@vueuse/core'
import { joinURL } from 'ufo'
import { destr } from 'destr'

import type { Overrides, RpcRequestWS, RpcResponse } from '../types'
import type { MaybeRef } from '#imports'
Expand Down Expand Up @@ -57,8 +58,10 @@ export function useSurrealWS<T = any>(database?: Overrides['database'], options?
},
})

const data = computed(() => destr<RpcResponse<T> | null>(_data.value))

function rpc<T = any>(req: RpcRequestWS<T>) {
if (status.value !== 'OPEN') return
if (status.value === 'CLOSED') return
return _send(JSON.stringify({
id: idCounter.value++,
...req,
Expand All @@ -68,6 +71,7 @@ export function useSurrealWS<T = any>(database?: Overrides['database'], options?
return {
close,
_data,
data,
open,
rpc,
_send,
Expand Down

0 comments on commit cbaab1f

Please sign in to comment.