Newer
Older
navi-1 / webclient / src / composables / wsBridge.js
/**
 * The send function the in-chat cards use to answer the server.
 *
 * Cards render inside a message tree that a view mounts, so the socket
 * instance is not an ancestor they can rely on: an inject() here silently
 * returned undefined and a click on a proposal or a form was lost with a
 * misleading "no connection". The socket composable registers its send()
 * here when it mounts and clears it when it goes away, so a card asks one
 * place that cannot be forgotten when a new view starts rendering messages.
 */

let sendFn = null

export function setWsSend(fn) {
  sendFn = typeof fn === 'function' ? fn : null
}

export function getWsSend() {
  return sendFn
}

/**
 * Fire a payload at the server. Returns false when no socket is mounted, so
 * the card can say the decision is not possible instead of losing it.
 */
export function wsSend(payload) {
  if (!sendFn) return false
  sendFn(payload)
  return true
}