@sporttotal/hub

4.0.8 • Public • Published

Hub

Front end react state management. Grpc build in over websockets

import { createClient } from '@saulx/hub'

const client = createClient()

const fn = () => {
  console.log('fire!')
}

// subscribe
client.rpc(
  {
    endpoint: 'f',
    method: 'subscribe',
    args: { uuid: 'firstname' }
  },
  fn
)

client.close(
  {
    endpoint: 'f',
    method: 'subscribe',
    args: { uuid: 'firstname' }
  },
  fn
)

// one off
const result = await client.rpc('f.subscribe', { hello: true })

Use it with react

import { useRpc, useHub, Provider, createClient } from '@saulx/hub'
const client = createClient()

const Something = () => {
  // gives access to the hub context
  const hub = useHub()

  // handles unsubscribe / subscribe internally
  const myValue = useRpc('f.subscribe')
  
  // local values are on endpoint device
  const localValue = useRpc('device.value')

  return <div
    onClick={() => hub.set('device.value', Math.floor(Math.random() * 99))}
  >{myValue} {localValue}</div>
}

const App = () => {
  return (
    <Provider hub={client}>
      <Something />
    </Provider>
  )
}

Readme

Keywords

none

Package Sidebar

Install

npm i @sporttotal/hub

Weekly Downloads

2

Version

4.0.8

License

none

Unpacked Size

31.5 MB

Total Files

91

Last publish

Collaborators

  • sporttotaltv