// Send render request for a component and associated props, and get HTML response.
// The Connect() function must be called before calling this function.
Send render request for a component and associated props, and get HTML response.
The Connect() function must be called before calling this function.
// Send render request for a component and associated props and get HTML response . // The Connect () function must be called before calling this function .
func RenderComponent(componentName string, storeName *string, props interface{}) (*RpcResponse, error) {
  mu.Lock()
  defer mu.Unlock()
  if shouldConnect {
    if err := connectToNetwork(); err != nil {
      return nil, err
    }
    shouldConnect = false
  }
  reqId += 1
  payload := map[string]interface{}{
    "id": reqId,
    "component": componentName,
  }
  if props != nil {
    payload["props"] = props
  }
  if storeName != nil {
    payload["store"] = *storeName
  }
  jsonPayload, err := json.Marshal(payload)
  if err != nil {
    return nil, err
  }
  if _, err := fmt.Fprintf(conn, string(jsonPayload) + data_end_marker); err != nil {
    shouldConnect = true
    return nil, ErrConnectionWrite
  }
  scanner := bufio.NewScanner(bufio.NewReader(conn))
  scanner.Split(split)
  scanner.Scan()
  jsonBlob := scanner.Bytes()
  if err := scanner.Err(); err != nil {
    return nil, err
  }
  var resp RpcResponse
  if err := json.Unmarshal(jsonBlob, &resp); err != nil {
    return nil, err
  }
  if storeName != nil && props != nil {
    jsonProps, _ := json.Marshal(props)
    resp.HydrateHtml = fmt.Sprintf(`
      <div
        class='preact-rpc-store'
        data-props='%s'
        data-store-name='%s'
        style='display: none'>
      </div>
    `, jsonProps, *storeName)
  }
  return &resp, nil
}
func RenderComponent ( componentName string , storeName * string , props interface { } ) ( * RpcResponse , error ) { mu . Lock ( ) defer mu . Unlock ( ) if shouldConnect { if err := connectToNetwork ( ) ; err != nil { return nil , err } shouldConnect = false } reqId += 1 payload := map [ string ] interface { } { " " : reqId , " " : componentName , } if props != nil { payload [ " " ] = props } if storeName != nil { payload [ " " ] = * storeName } jsonPayload , err := json . Marshal ( payload ) if err != nil { return nil , err } if _ , err := fmt . Fprintf ( conn , string ( jsonPayload ) + data_end_marker ) ; err != nil { shouldConnect = true return nil , ErrConnectionWrite } scanner := bufio . NewScanner ( bufio . NewReader ( conn ) ) scanner . Split ( split ) scanner . Scan ( ) jsonBlob := scanner . Bytes ( ) if err := scanner . Err ( ) ; err != nil { return nil , err } var resp RpcResponse if err := json . Unmarshal ( jsonBlob , & resp ) ; err != nil { return nil , err } if storeName != nil && props != nil { jsonProps , _ := json . Marshal ( props ) resp . HydrateHtml = fmt . Sprintf ( `
      <div
        class='preact-rpc-store'
        data-props='%s'
        data-store-name='%s'
        style='display: none'>
      </div>
    ` , jsonProps , * storeName ) } return & resp , nil }
Send render request for a component and associated props, and get HTML response.
Send render request for a component and associated props and get HTML response .
// Increment request ID // Render request to RPC server. // Add props if there are any // Add store name if provided // We'll try reconnecting in the next call // Parse JSON response. // Error reading buffer ... // Error parsing JSON // Add hydration html if store was provided