Skip to content

atelier-saulx/hub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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>
  )
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •